JavaScript实现烟花绽放动画效果
先编写一个烟花绽放的动画效果。
放烟花时,一个烟花可分为两个阶段:(1)烟花上升到空中;(2)烟花炸开成碎片,炸开的碎片慢慢消散。
为此抽象出两个对象类:firework和particle。其中,firework用于表示一个烟花对象,particle用于表示一个烟花炸开后的各碎片。
firework对象类定义6个属性:表示烟花上升轨迹中各点的坐标(x,y)、烟花弧状轨迹的偏转角度angle、上升阶段水平和垂直方向的位移改变量xspeed和yspeed、烟花的色彩色相hue。
坐标属性值y的初始值取画布的高度,表示烟花从地面上升到空中,其余各属性的初始值采用随机数确定。具体定义如下:
function firework() { this.x = canvas.width/4*(1+3*math.random()); this.y = canvas.height - 15; this.angle = math.random() * math.pi / 4 - math.pi / 6; this.xspeed = math.sin(this.angle) *(6+math.random()*7); this.yspeed = -math.cos(this.angle) *(6+math.random()*7); this.hue = math.floor(math.random() * 360); }
firework对象类定义3个方法:绘制烟花上升轨迹的方法draw()、烟花上升时坐标改变方法update()和烟花炸开方法explode()。绘制烟花轨迹时,在各点(x,y)处绘制一个宽度为5、高度为15的填充小矩形表示一个轨迹点。烟花上升时,垂直方向速度yspeed初始值为负的,每次上升时,yspeed加上一个正值,表示上升在减速,当yspeed的值大于0时,烟花上升到顶了(不能再上升),就炸开为70个碎片。具体方法的实现见后面的html文件内容。
particle对象类定义8个属性:表示碎片散开轨迹中各点的坐标(x,y)、碎片弧状轨迹的偏转角度angle、散开时水平和垂直方向的位移改变量xspeed和yspeed、碎片的色彩色相hue、表示碎片小圆的半径size、碎片的亮度lightness。
function particle(x,y,hue) { this.x = x; this.y = y; this.hue = hue; this.lightness = 50; this.size = 15 + math.random() * 10; this.angle = math.random() * 2 * math.pi; this.xspeed = math.cos(this.angle) *(1+math.random() * 6); this.yspeed = math.sin(this.angle) *(1+math.random() * 6); }
particle对象类定义2个方法:绘制碎片散开轨迹的方法draw()、碎片散开时坐标改变方法update()。碎片散开时逐渐变小(属性size值减量),当size值小于1时,从碎片数组中删除该碎片,表示碎片已消亡。
定义两个数组var fireworks=[];和var particles=[];分别存储烟花对象和炸开的碎片对象。
模拟动画的函数loop中,每隔一段时间(用count计数来实现)向fireworks数组中添加一个烟花对象,烟花对象上升到顶炸开后,从fireworks数组中删除该对象元素,然后向particles数组中添加70个碎片对象。
遍历两个数组的各对象,分别调用它们的draw()和update()方法。
编写的完整html文件内容如下。
<html> <head> <title>烟花绽放</title> </head> <body> <canvas id="mycanvas" width="800" height="600" style="border:3px double #996633;background:black;"> </canvas> <script type="text/javascript"> var canvas=document.getelementbyid('mycanvas'); ctx= canvas.getcontext('2d'); var fireworks=[]; var particles=[]; var counter = 0; function firework() { this.x = canvas.width/4*(1+3*math.random()); this.y = canvas.height - 15; this.angle = math.random() * math.pi / 4 - math.pi / 6; this.xspeed = math.sin(this.angle) *(6+math.random()*7); this.yspeed = -math.cos(this.angle) *(6+math.random()*7); this.hue = math.floor(math.random() * 360); } firework.prototype.draw= function() { ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(math.atan2(this.yspeed, this.xspeed) + math.pi / 2); ctx.fillstyle =`hsl(${this.hue}, 100%, 50%)`; ctx.fillrect(0, 0, 5, 15); ctx.restore(); } firework.prototype.update= function() { this.x = this.x + this.xspeed; this.y = this.y + this.yspeed; this.yspeed += 0.1; } firework.prototype.explode= function() { for (var i = 0; i < 70; i++) { particles.push(new particle(this.x, this.y, this.hue)); } } function particle(x,y,hue) { this.x = x; this.y = y; this.hue = hue; this.lightness = 50; this.size = 15 + math.random() * 10; this.angle = math.random() * 2 * math.pi; this.xspeed = math.cos(this.angle) *(1+math.random() * 6); this.yspeed = math.sin(this.angle) *(1+math.random() * 6); } particle.prototype.draw= function() { ctx.fillstyle = `hsl(${this.hue}, 100%, ${this.lightness}%)`; ctx.beginpath(); ctx.arc(this.x, this.y, this.size, 0, 2 * math.pi); ctx.closepath(); ctx.fill(); } particle.prototype.update= function(index) { this.yspeed += 0.05; this.size = this.size*0.95; this.x = this.x + this.xspeed; this.y = this.y + this.yspeed; if (this.size<1) { particles.splice(index,1); } } function loop() { ctx.fillstyle = "rgba(0, 0, 0, 0.1)"; ctx.fillrect(0,0,canvas.width,canvas.height); counter++; if (counter==15) { fireworks.push(new firework()); counter=0; } var i=fireworks.length; while (i--) { fireworks[i].draw(); fireworks[i].update(); if (fireworks[i].yspeed > 0) { fireworks[i].explode(); fireworks.splice(i, 1); } } var i=particles.length; while (i--) { particles[i].draw(); particles[i].update(i); } requestanimationframe(loop); } loop(); </script> </body> </html>
在浏览器中打开包含这段html代码的html文件,可以看到在浏览器窗口中呈现出如图所示的烟花绽放动画效果。
实现了烟花绽放的效果,我们还可以继续让一定区域内的绽放的烟花碎片拼成“happy new year”粒子文本。
编写如下的html代码。
<html> <head> <title>迎新年烟花绽放</title> <style> body { margin: 0; background: black; } canvas { position: absolute; } </style> </head> <body> <canvas id="mycanvas1"></canvas> <canvas id="mycanvas2"></canvas> <canvas id="mycanvas3"></canvas> <script type="text/javascript"> function particle(x, y, hue) { this.x = x; this.y = y; this.hue = hue; this.lightness = 50; this.size = 15 + math.random() * 10; this.angle = math.random() * 2 * math.pi; this.xspeed = math.cos(this.angle) * (1 + math.random() * 6); this.yspeed = math.sin(this.angle) * (1 + math.random() * 6); this.target = gettarget(); this.timer = 0; } particle.prototype.draw= function() { ctx2.fillstyle =`hsl(${this.hue}, 100%, ${this.lightness}%)`; ctx2.beginpath(); ctx2.arc(this.x, this.y, this.size, 0, 2 * math.pi); ctx2.closepath(); ctx2.fill(); } particle.prototype.update= function(idx) { if (this.target) { var dx = this.target.x - this.x; var dy = this.target.y - this.y; var dist = math.sqrt(dx * dx + dy * dy); var a = math.atan2(dy, dx); var tx = math.cos(a) * 5; var ty = math.sin(a) * 5; this.size = lerp(this.size, 1.5, 0.05); if (dist < 5) { this.lightness = lerp(this.lightness, 100, 0.01); this.xspeed = this.yspeed = 0; this.x = lerp(this.x, this.target.x + fidelity / 2, 0.05); this.y = lerp(this.y, this.target.y + fidelity / 2, 0.05); this.timer += 1; } else if (dist < 10) { this.lightness = lerp(this.lightness, 100, 0.01); this.xspeed = lerp(this.xspeed, tx, 0.1); this.yspeed = lerp(this.yspeed, ty, 0.1); this.timer += 1; } else { this.xspeed = lerp(this.xspeed, tx, 0.02); this.yspeed = lerp(this.yspeed, ty, 0.02); } } else { this.yspeed += 0.05; this.size = this.size*0.95; if (this.size<1) { particles.splice(idx,1); } } this.x = this.x + this.xspeed; this.y = this.y + this.yspeed; } function firework() { this.x = canvas2.width*(1+ 3*math.random())/4; this.y = canvas2.height - 15; this.angle = math.random() * math.pi / 4 - math.pi / 6; this.xspeed = math.sin(this.angle) * (6 + math.random() * 7); this.yspeed = -math.cos(this.angle) * (6 + math.random() * 7); this.hue = math.floor(math.random() * 360); } firework.prototype.draw= function() { ctx2.save(); ctx2.translate(this.x, this.y); ctx2.rotate(math.atan2(this.yspeed, this.xspeed) + math.pi / 2); ctx2.fillstyle = `hsl(${this.hue}, 100%, 50%)`; ctx2.fillrect(0, 0, 5, 15); ctx2.restore(); } firework.prototype.update= function() { this.x = this.x + this.xspeed; this.y = this.y + this.yspeed; this.yspeed += 0.1; } firework.prototype.explode= function() { for (var i = 0; i < 70; i++) { particles.push(new particle(this.x, this.y, this.hue)); } } function lerp(a, b, t) { return math.abs(b - a)> 0.1 ? a + t * (b - a) : b; } function gettarget() { if (targets.length > 0) { var idx = math.floor(math.random() * targets.length); var { x, y } = targets[idx]; targets.splice(idx, 1); x += canvas2.width / 2 - textwidth / 2; y += canvas2.height / 2 - fontsize / 2; return { x, y }; } } var canvas1=document.getelementbyid('mycanvas1'); ctx1= canvas1.getcontext('2d'); var canvas2=document.getelementbyid('mycanvas2'); ctx2= canvas2.getcontext('2d'); var canvas3=document.getelementbyid('mycanvas3'); ctx3= canvas3.getcontext('2d'); var fontsize = 200; var fireworks = []; var particles = []; var targets = []; var fidelity = 3; var counter = 0; canvas2.width = canvas3.width = window.innerwidth; canvas2.height = canvas3.height = window.innerheight; ctx1.fillstyle = '#000'; var text = 'happy new year'; var textwidth = 999999; while (textwidth > window.innerwidth) { ctx1.font = `900 ${fontsize--}px arial`; textwidth = ctx1.measuretext(text).width; } canvas1.width = textwidth; canvas1.height = fontsize * 1.5; ctx1.font = `900 ${fontsize}px arial`; ctx1.filltext(text, 0, fontsize); var imgdata = ctx1.getimagedata(0, 0, canvas1.width, canvas1.height); for (var i = 0, max = imgdata.data.length; i < max; i += 4) { var alpha = imgdata.data[i + 3]; var x = math.floor(i / 4) % imgdata.width; var y = math.floor(i / 4 / imgdata.width); if (alpha && x % fidelity === 0 && y % fidelity === 0) { targets.push({ x, y }); } } ctx3.fillstyle = '#fff'; ctx3.shadowcolor = '#fff'; ctx3.shadowblur = 25; function loop() { ctx2.fillstyle = "rgba(0, 0, 0, .1)"; ctx2.fillrect(0, 0, canvas2.width, canvas2.height); counter += 1; if (counter==15) { fireworks.push(new firework()); counter=0; } var i=fireworks.length; while (i--) { fireworks[i].draw(); fireworks[i].update(); if (fireworks[i].yspeed > 0) { fireworks[i].explode(); fireworks.splice(i, 1); } } var i=particles.length; while (i--) { particles[i].draw(); particles[i].update(i); if (particles[i].timer >= 100 || particles[i].lightness >= 99) { ctx3.fillrect(particles[i].target.x, particles[i].target.y, fidelity + 1, fidelity + 1); particles.splice(i, 1); } } requestanimationframe(loop); } loop(); </script> </body> </html>
在浏览器中打开包含这段html代码的html文件,可以看到在浏览器窗口中呈现出如图所示的烟花绽放迎新年动画效果。图2中为了控制图片的大小,删除了大量的中间帧,因此和实际运行的效果有所不同。
以上就是javascript实现烟花绽放动画效果的详细内容,更多关于javascript动画效果的资料请关注其它相关文章!
上一篇: Spring IOC底层原理