forked from Fzw-com/animate_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-circle2.html
67 lines (58 loc) · 2.13 KB
/
canvas-circle2.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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>canvas-circle2绘制随机运动的圆</title>
<style>
body{background: #000000;}
#c1{background: #ffffff;}
</style>
<script>
window.onload=function(){
var oC=document.getElementById('c1');
var oGC=oC.getContext('2d');
var setArr=[];
setInterval(function(){//进行运动操作
oGC.clearRect(0,0,oC.width,oC.height);
for(var i=0;i<setArr.length;i++){
setArr[i].r+=1;//半径每次增大1
setArr[i].c4-=0.01;
if(setArr[i].c4<=0){//删除背景为透明的,避免元素过大
setArr.splice(i,1);
}
}
for(var i=0;i<setArr.length;i++){
oGC.beginPath();
oGC.fillStyle='rgba('+setArr[i].c1+','+setArr[i].c2+','+setArr[i].c3+','+setArr[i].c4+')';
oGC.moveTo(setArr[i].x,setArr[i].y);
oGC.arc(setArr[i].x,setArr[i].y,setArr[i].r,0,360*Math.PI/180,false);
oGC.closePath();
oGC.fill();
}
},1000/60);
setInterval(function(){//添加数据
var x=Math.floor(Math.random()*oC.width);//随机产生圆的x值
var y=Math.floor(Math.random()*oC.height);//随机产生圆的y值
var r=4;//圆的半径
//随机产生圆的颜色rgb
var c1=Math.floor(Math.random()*255);
var c2=Math.floor(Math.random()*255);
var c3=Math.floor(Math.random()*255);
var c4=1;//设置透明度
setArr.push({
x : x,
y : y,
r : r,
c1 : c1,
c2 : c2,
c3 : c3,
c4 : c4
});
},500);//往数组中放元素
}
</script>
</head>
<body>
<canvas id="c1" width="400" height="400"></canvas>
</body>
</html>