实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码
程序员文章站
2023-12-04 10:29:22
这是一个很酷的HTML5 Canvas动画,它将模拟的是我们现实生活中烟花绽放的动画特效,效果非常逼真,下面我们来简单分析一下实现这款HTML5烟花特效的过程及代码,主要由HTML代码、CSS代... 14-11-05...
这是一个很酷的html5 canvas动画,它将模拟的是我们现实生活中烟花绽放的动画特效,效果非常逼真,但是毕竟是电脑模拟,带女朋友看就算了,效果还是差了点,呵呵。这个html5 canvas动画有一点比较出色,就是其性能,chrome上基本没有卡的感觉,就算你放出很多烟花也一样。
下面我们来简单分析一下实现这款html5烟花特效的过程及代码,主要由html代码、css代码以及javascript代码组成,当然javascript代码是最重要的。
html代码:
xml/html code复制内容到剪贴板
- <div id=”gui”></div>
- <div id=”canvas-container”> <div id=”mountains2′></div>
- <div id=”mountains1′></div><div id=”skyline”></div> </div>
html的结构非常简单,即构造了一个canvas容器,我们会利用js在这个容器中生成一个canvas对象。看最后的js代码你就会知道了。
css代码:
css code复制内容到剪贴板
- #canvas-container { background: #000 url(bg.jpg); height: 400px; left: 50%; margin: -200px 0 0 -300px; position: absolute; top: 50%; width: 600px; z-index: 2;
- } canvas { cursor: crosshair; display: block; position: relative; z-index: 3;
- } canvas:active { cursor: crosshair;
- } #skyline { background: url(skyline.png) repeat-x 50% 0; bottombottom: 0; height: 135px; left: 0; position: absolute; width: 100%; z-index: 1;
- } #mountains1 { background: url(mountains1.png) repeat-x 40% 0; bottombottom: 0; height: 200px; left: 0; position: absolute; width: 100%; z-index: 1;
- } #mountains2 { background: url(mountains2.png) repeat-x 30% 0; bottombottom: 0; height: 250px; left: 0; position: absolute; width: 100%; z-index: 1;
- } #gui { rightright: 0; position: fixed; top: 0; z-index: 3;
- }
css代码没什么特别,主要也就定义一下背景色和边框之类的。
接下来是最重要的javascript代码。
javascript代码:
javascript code复制内容到剪贴板
- self.init = function(){
- self.dt = 0;
- self.oldtime = date.now();
- self.canvas = document.createelement('canvas');
- self.canvascontainer = $('#canvas-container'); var canvascontainerdisabled = document.getelementbyid('canvas-container');
- self.canvas.onselectstart = function() { return false;
- };
- self.canvas.width = self.cw = 600;
- self.canvas.height = self.ch = 400;
- self.particles = [];
- self.partcount = 30;
- self.fireworks = [];
- self.mx = self.cw/2;
- self.my = self.ch/2;
- self.currenthue = 170;
- self.partspeed = 5;
- self.partspeedvariance = 10;
- self.partwind = 50;
- self.partfriction = 5;
- self.partgravity = 1;
- self.huemin = 150;
- self.huemax = 200;
- self.fworkspeed = 2;
- self.fworkaccel = 4;
- self.huevariance = 30;
- self.flickerdensity = 20;
- self.showshockwave = false;
- self.showtarget = true;
- self.clearalpha = 25;
- self.canvascontainer.append(self.canvas);
- self.ctx = self.canvas.getcontext('2d');
- self.ctx.linecap = 'round';
- self.ctx.linejoin = 'round';
- self.linewidth = 1;
- self.bindevents();
- self.canvasloop();
- self.canvas.onselectstart = function() { return false;
- };
- };
这段js代码主要是往canvas容器中构造一个canvas对象,并且对这个canvas对象的外观以及动画属性作了初始化。
javascript code复制内容到剪贴板
- var particle = function(x, y, hue){ this.x = x; this.y = y; this.coordlast = [
- {x: x, y: y},
- {x: x, y: y},
- {x: x, y: y}
- ]; this.angle = rand(0, 360); this.speed = rand(((self.partspeed - self.partspeedvariance) <= 0) ? 1 : self.partspeed - self.partspeedvariance, (self.partspeed + self.partspeedvariance)); this.friction = 1 - self.partfriction/100; this.gravity = self.partgravity/2; this.hue = rand(hue-self.huevariance, hue+self.huevariance); this.brightness = rand(50, 80); this.alpha = rand(40,100)/100; this.decay = rand(10, 50)/1000; this.wind = (rand(0, self.partwind) - (self.partwind/2))/25; this.linewidth = self.linewidth;
- };
- particle.prototype.update = function(index){ var radians = this.angle * math.pi / 180; var vx = math.cos(radians) * this.speed; var vy = math.sin(radians) * this.speed + this.gravity; this.speed *= this.friction; this.coordlast[2].x = this.coordlast[1].x; this.coordlast[2].y = this.coordlast[1].y; this.coordlast[1].x = this.coordlast[0].x; this.coordlast[1].y = this.coordlast[0].y; this.coordlast[0].x = this.x; this.coordlast[0].y = this.y; this.x += vx * self.dt; this.y += vy * self.dt; this.angle += this.wind; this.alpha -= this.decay; if(!hittest(0,0,self.cw,self.ch,this.x-this.radius, this.y-this.radius, this.radius*2, this.radius*2) || this.alpha < .05){
- self.particles.splice(index, 1);
- }
- };
- particle.prototype.draw = function(){ var coordrand = (rand(1,3)-1);
- self.ctx.beginpath();
- self.ctx.moveto(math.round(this.coordlast[coordrand].x), math.round(this.coordlast[coordrand].y));
- self.ctx.lineto(math.round(this.x), math.round(this.y));
- self.ctx.closepath();
- self.ctx.strokestyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+this.alpha+')';
- self.ctx.stroke(); if(self.flickerdensity > 0){ var inversedensity = 50 - self.flickerdensity; if(rand(0, inversedensity) === inversedensity){
- self.ctx.beginpath();
- self.ctx.arc(math.round(this.x), math.round(this.y), rand(this.linewidth,this.linewidth+3)/2, 0, math.pi*2, false) self.ctx.closepath(); var randalpha = rand(50,100)/100;
- self.ctx.fillstyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+randalpha+')';
- self.ctx.fill();
- }
- }
- };
这段js代码的功能是实现烟花爆炸后的小颗粒的绘制,从draw方法中可以看出,创建几个随机点,烟花颗粒即可在这个范围的随机点中散落。
javascript code复制内容到剪贴板
- var firework = function(startx, starty, targetx, targety){ this.x = startx; this.y = starty; this.startx = startx; this.starty = starty; this.hitx = false; this.hity = false; this.coordlast = [
- {x: startx, y: starty},
- {x: startx, y: starty},
- {x: startx, y: starty}
- ]; this.targetx = targetx; this.targety = targety; this.speed = self.fworkspeed; this.angle = math.atan2(targety - starty, targetx - startx); this.shockwaveangle = math.atan2(targety - starty, targetx - startx)+(90*(math.pi/180)); this.acceleration = self.fworkaccel/100; this.hue = self.currenthue; this.brightness = rand(50, 80); this.alpha = rand(50,100)/100; this.linewidth = self.linewidth; this.targetradius = 1;
- };
- firework.prototype.update = function(index){
- self.ctx.linewidth = this.linewidth;
- vx = math.cos(this.angle) * this.speed,
- vy = math.sin(this.angle) * this.speed; this.speed *= 1 + this.acceleration; this.coordlast[2].x = this.coordlast[1].x; this.coordlast[2].y = this.coordlast[1].y; this.coordlast[1].x = this.coordlast[0].x; this.coordlast[1].y = this.coordlast[0].y; this.coordlast[0].x = this.x; this.coordlast[0].y = this.y; if(self.showtarget){ if(this.targetradius < 8){ this.targetradius += .25 * self.dt;
- } else { this.targetradius = 1 * self.dt;
- }
- } if(this.startx >= this.targetx){ if(this.x + vx <= this.targetx){ this.x = this.targetx; this.hitx = true;
- } else { this.x += vx * self.dt;
- }
- } else { if(this.x + vx >= this.targetx){ this.x = this.targetx; this.hitx = true;
- } else { this.x += vx * self.dt;
- }
- } if(this.starty >= this.targety){ if(this.y + vy <= this.targety){ this.y = this.targety; this.hity = true;
- } else { this.y += vy * self.dt;
- }
- } else { if(this.y + vy >= this.targety){ this.y = this.targety; this.hity = true;
- } else { this.y += vy * self.dt;
- }
- } if(this.hitx && this.hity){ var randexplosion = rand(0, 9);
- self.createparticles(this.targetx, this.targety, this.hue);
- self.fireworks.splice(index, 1);
- }
- };
- firework.prototype.draw = function(){
- self.ctx.linewidth = this.linewidth; var coordrand = (rand(1,3)-1);
- self.ctx.beginpath();
- self.ctx.moveto(math.round(this.coordlast[coordrand].x), math.round(this.coordlast[coordrand].y));
- self.ctx.lineto(math.round(this.x), math.round(this.y));
- self.ctx.closepath();
- self.ctx.strokestyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+this.alpha+')';
- self.ctx.stroke(); if(self.showtarget){
- self.ctx.save();
- self.ctx.beginpath();
- self.ctx.arc(math.round(this.targetx), math.round(this.targety), this.targetradius, 0, math.pi*2, false)
- self.ctx.closepath();
- self.ctx.linewidth = 1;
- self.ctx.stroke();
- self.ctx.restore();
- } if(self.showshockwave){
- self.ctx.save();
- self.ctx.translate(math.round(this.x), math.round(this.y));
- self.ctx.rotate(this.shockwaveangle);
- self.ctx.beginpath();
- self.ctx.arc(0, 0, 1*(this.speed/5), 0, math.pi, true);
- self.ctx.strokestyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+rand(25, 60)/100+')';
- self.ctx.linewidth = this.linewidth;
- self.ctx.stroke();
- self.ctx.restore();
- }
- };
这段js代码是创建烟花实例的,我们也可以从draw方法中看出,当我们鼠标点击画布中的某点时,烟花发射的目的地就在那个点上。
这款html5 canvas烟花效果的核心代码就是这样,谢谢阅读,希望能帮到大家,请继续关注,我们会努力分享更多优秀的文章。