-
Notifications
You must be signed in to change notification settings - Fork 0
/
绘制一个圆分成几分.html
42 lines (41 loc) · 963 Bytes
/
绘制一个圆分成几分.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绘制扇形</title>
<style>
canvas{
border: 1px solid black;
}
</style>
</head>
<body>
<canvas width="600" height="600">
</canvas>
<script>
var randomColor = function(){
var R = Math.floor(Math.random() * 256);
var G = Math.floor(Math.random() * 256);
var B = Math.floor(Math.random() * 256);
var RGB = "rgb("+R+","+G+","+B+")";
return RGB;
}
var mycanvas = document.querySelector("canvas");
var ctx = mycanvas.getContext("2d");
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var num = 6;
var angle = Math.PI * 2 / num;
for(var i=0; i<num; i++){
var startAngle = i * angle;
var endAngle = (i+1) * angle;
ctx.beginPath();
ctx.moveTo(width/2,height/2);
ctx.arc(width/2,height/2,150,startAngle,endAngle);
console.log(randomColor());
ctx.fillStyle = randomColor();
ctx.fill();
}
</script>
</body>
</html>