Canvas放置反弹效果随机图形(实例)
复制代码
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130var raf; var arror = []; var running = false; //绘制圆形 function createBall() { return { x: 0, y: 0, vx: 10-Math.random()*10, vy: 10-Math.random()*10, radius: 25, color:"red", draw: function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true); ctx.closePath(); ctx.fillStyle = this.color; ctx.fill(); } } } //绘制正方形 function createSquare() { return { x: 0, y: 0, vx: 10-Math.random()*10, vy: 10-Math.random()*10, color:"red", draw: function() { ctx.beginPath(); ctx.fillStyle = this.color; ctx.fillRect(this.x, this.y,30, 30); ctx.closePath(); } } } //绘制五角星 function createStar() { return { x: 0, y: 0, vx: 10-Math.random()*10, vy: 10-Math.random()*10, color:"red", draw: function() { ctx.font = "24px serif"; ctx.textBaseline = "hanging"; ctx.fillStyle=this.color; ctx.fillText("五角星",this.x, this.y); } } } //绘制三角形 function createTriangle() { return { x: 0, y: 0, vx: 10-Math.random()*10, vy: 10-Math.random()*10, color:"red", draw: function() { ctx.beginPath(); ctx.moveTo(this.x,this.y); ctx.lineTo(this.x+25,this.y+25); ctx.lineTo(this.x+25,this.y-25); ctx.fillStyle=this.color; ctx.fill(); } } } //清除 function clear() { ctx.fillStyle = 'rgba(255,255,255,0.3)'; ctx.fillRect(0,0,canvas.width,canvas.height); } //判断图形坐标是否超出画布范围 function draw() { clear(); arror.forEach(function(ball, i){ ball.draw(); ball.x += ball.vx; ball.y += ball.vy; if (ball.y + ball.vy > canvas.height || ball.y + ball.vy < 0) { ball.vy = -ball.vy; } if (ball.x + ball.vx > canvas.width || ball.x + ball.vx < 0) { ball.vx = -ball.vx; } }); raf = window.requestAnimationFrame(draw); } canvas.addEventListener('click',function(e){ if (!running) { raf = window.requestAnimationFrame(draw); running = true; } var colorarr=["#000000","#7F7F7F","#880015","#ED1C24","#FF7F27","#FFF200","#22B14C","#00A2E8","#3F48CC","#A349A4","#B97A57","#FFAEC9","#B5E61D"]; var Graphics = ["Round","Square","Star","Triangle"]; var typexz=Graphics[Math.floor(Math.random()*4)]; var ball; switch(typexz){ case "Round": ball = createBall(); break; case "Square": ball = createSquare(); break; case "Star": ball = createStar(); break; case "Triangle": ball = createTriangle(); break; } ball.x = e.clientX; ball.y = e.clientY; ball.color = colorarr[Math.floor(Math.random() * 10 + 3)]; arror.push(ball); }); draw(); document.addEventListener('keydown',function (e) { if(e.keyCode==32){ event.preventDefault(); window.cancelAnimationFrame(raf); running = false; } })
以上这篇Canvas放置反弹效果随机图形(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
最后
以上就是还单身钻石最近收集整理的关于Canvas放置反弹效果随机图形(实例)的全部内容,更多相关Canvas放置反弹效果随机图形(实例)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复