时间:2021-05-28
Canvas放置反弹效果随机图形(实例)
var 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放置反弹效果随机图形(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了利用canvas绘制图形验证码的具体代码,供大家参考,具体内容如下思路:通过随机获取指定字符串的指定数的字符,通过canvas绘制出来返回指
本文实例为大家分享了javascript实现点击产生随机图形的具体代码,供大家参考,具体内容如下点击产生随机图形效果如下:用javascript来实现主要用ca
本文实例讲述了Python模拟随机游走图形效果。分享给大家供大家参考,具体如下:在python中,可以利用数组操作来模拟随机游走。下面是一个单一的200步随机游
本文实例为大家分享了js+canvas实现下雪效果的具体代码,供大家参考,具体内容如下效果展示:源码展示:canvas下雪效果(原生js)*{margin:0;
本文实例讲述了Android编程开发之在Canvas中利用Path绘制基本图形的方法。分享给大家供大家参考,具体如下:在Android中绘制基本的集合图形,本程