-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-circle1.html
71 lines (61 loc) · 2.21 KB
/
canvas-circle1.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
70
71
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>canvas绘制上下移动的圆</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=[];
oC.width=document.documentElement.clientWidth;
setInterval(function(){//进行运动操作
oGC.clearRect(0,0,oC.width,oC.height);
for(var i=0;i<setArr.length;i++){
setArr[i].Y+=0.02;//使圆做抛物线运动
setArr[i].x+=setArr[i].X;
setArr[i].y+=setArr[i].Y;
}
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=oC.width/2;//随机产生圆的x值
var y=oC.height-20;//随机产生圆的y值
var r=8;//圆的半径
//随机产生圆的颜色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;//设置透明度
var X=Math.random()*6-3;//产生-3到3之间的数
var Y=-(Math.random()*3+1);//1到4之间的数
setArr.push({
x : x,
y : y,
r : r,
c1 : c1,
c2 : c2,
c3 : c3,
c4 : c4,
X : X,
Y : Y
});
},500);//往数组中放元素
}
</script>
</head>
<body>
<canvas id="c1" width="400" height="400"></canvas>
</body>
</html>