-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-演示-随机颜色绘制绘制圆.html
69 lines (54 loc) · 1.55 KB
/
06-演示-随机颜色绘制绘制圆.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<canvas id="cv"></canvas>
<script>
var cv = document.getElementById('cv');
cv.height = 400;
cv.width = 600;
cv.style.border = '1px solid red';
var context = cv.getContext('2d');
var toRadian = function (angle) {
return angle / 180 * Math.PI;
};
// 动画绘制圆
var startAngle = -90,
x = cv.width / 2,
y = cv.height / 2,
r = 100,
step = 3,
timerId = null;
// 设置颜色(目的:观察有没有重复绘制)
context.fillStyle = 'rgba(255, 0, 0, .5)';
// 获取随机数从 0-255
function getRandom() {
return Math.floor( Math.random() * 256 );
}
// 获取随机颜色
function getColor() {
return [getRandom(), getRandom(), getRandom()];
}
timerId = setInterval(function () {
// 判断是否达到目标值:
if (startAngle >= 270) {
clearInterval(timerId);
return;
}
// 方式一:开启新路径(每次只绘制3度)
// context.beginPath();
context.clearRect(0, 0, cv.width, cv.height);
context.fillStyle = 'rgb(' + getColor().join(',') + ')'
context.moveTo(x, y);
context.arc(x, y, r, toRadian(startAngle), toRadian(startAngle + step))
context.fill();
startAngle += step;
}, 50);
</script>
</body>
</html>