HTML5 Canvas实现烟花绽放特效
程序员文章站
2023-11-28 22:29:04
这是一个款绚丽的HTML5 Canvas动画,它将模拟的是我们生活中烟花绽放的动画特效,效果非常逼真,下面我们来简单分析一下实现这款HTML5烟花特效的过程及代码,感兴趣的小伙伴们可以参... 16-03-02...
本文为大家带来了一款,免费而又安全环保的html5 canvas实现的放烟花特效。
效果如下:
代码如下:
xml/html code复制内容到剪贴板
- <!doctype html>
- <html>
- <head>
- <title>canvas 实现放烟花特效</title>
- <meta charset="utf-8">
- <meta http-equiv="x-ua-compatible" content="ie=edge">
- <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no">
- <style type="text/css">
- html,body{height:100%;margin:0;padding:0}
- ul,li{text-indent:0;text-decoration:none;margin:0;padding:0}
- img{border:0}
- body{background-color:#000;color:#999;font:100%/18px helvetica, arial, sans-serif}
- canvas{cursor:crosshair;display:block;left:0;position:absolute;top:0;z-index:20}
- #header img{width:100%; height:20%;}
- #bg img{width:100%; height:80%;}
- #header,#bg{position:fixed;left:0;right:0;z-index:10}
- #header{top:0}
- #bg{position:fixed;z-index:1;bottom:0}
- audio{position:fixed;display:none;bottom:0;left:0;right:0;width:100%;z-index:5}
- </style>
- </head>
- <body>
- <div id="bg">
- <img id="bgimg" src="http://img.ivsky.com/img/tupian/pre/201508/02/yuzhou_xingkong_yu_yueliang-006.jpg">
- </div>
- <script src="http://cdn.bootcss.com/jquery/2.2.0/jquery.min.js"></script>
- <script>
- $(function(){
- var fireworks = function(){
- var self = this;
- // 产生烟花随机数
- var rand = function(rmi, rma){
- //按位取反运算符
- return ~~((math.random()*(rma-rmi+1))+rmi);
- },hittest = function(x1, y1, w1, h1, x2, y2, w2, h2){
- return !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1);
- };
- //请求动画帧
- window.requestanimframe=function(){
- return window.requestanimationframe
- ||window.webkitrequestanimationframe
- ||window.mozrequestanimationframe
- ||window.orequestanimationframe
- ||window.msrequestanimationframe
- ||function(callback){
- window.settimeout(callback,1000/60);
- }
- }();
- self.init = function(){
- self.canvas = document.createelement('canvas');
- //canvas 全屏
- selfself.canvas.width = self.cw = $(window).innerwidth();
- selfself.canvas.height = self.ch = $(window).innerheight();
- self.particles = [];
- self.partcount = 150;
- self.fireworks = [];
- selfself.mx = self.cw/2;
- selfself.my = self.ch/2;
- self.currenthue = 30;
- self.partspeed = 5;
- self.partspeedvariance = 10;
- self.partwind = 50;
- self.partfriction = 5;
- self.partgravity = 1;
- self.huemin = 0;
- self.huemax = 360;
- self.fworkspeed = 4;
- self.fworkaccel = 10;
- self.huevariance = 30;
- self.flickerdensity = 25;
- self.showshockwave = true;
- self.showtarget = false;
- self.clearalpha = 25;
- $(document.body).append(self.canvas);
- selfself.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;
- };
- };
- // 创建粒子
- self.createparticles = function(x,y, hue){
- var countdown = self.partcount;
- while(countdown--){
- var newparticle = {
- x: x,
- y: y,
- coordlast: [
- {x: x, y: y},
- {x: x, y: y},
- {x: x, y: y}
- ],
- angle: rand(0, 360),
- speed: rand(((self.partspeed - self.partspeedvariance) <= 0) ? 1 : self.partspeed - self.partspeedvariance, (self.partspeed + self.partspeedvariance)),
- friction: 1 - self.partfriction/100,
- gravity: self.partgravity/2,
- hue: rand(hue-self.huevariance, hue+self.huevariance),
- brightness: rand(50, 80),
- alpha: rand(40,100)/100,
- decay: rand(10, 50)/1000,
- wind: (rand(0, self.partwind) - (self.partwind/2))/25,
- linewidth: self.linewidth
- };
- self.particles.push(newparticle);
- }
- };
- // 更新粒子
- self.updateparticles = function(){
- var i = self.particles.length;
- while(i--){
- var p = self.particles[i];
- var radians = p.angle * math.pi / 180;
- var vx = math.cos(radians) * p.speed;
- var vy = math.sin(radians) * p.speed;
- p.speed *= p.friction;
- p.coordlast[2].x = p.coordlast[1].x;
- p.coordlast[2].y = p.coordlast[1].y;
- p.coordlast[1].x = p.coordlast[0].x;
- p.coordlast[1].y = p.coordlast[0].y;
- p.coordlast[0].x = p.x;
- p.coordlast[0].y = p.y;
- p.x += vx;
- p.y += vy;
- p.y += p.gravity;
- p.angle += p.wind;
- p.alpha -= p.decay;
- if(!hittest(0,0,self.cw,self.ch,p.x-p.radius, p.y-p.radius, p.radius*2, p.radius*2) || p.alpha < .05){
- self.particles.splice(i, 1);
- }
- };
- };
- // 绘制粒子
- self.drawparticles = function(){
- var i = self.particles.length;
- while(i--){
- var p = self.particles[i];
- var coordrand = (rand(1,3)-1);
- self.ctx.beginpath();
- self.ctx.moveto(math.round(p.coordlast[coordrand].x), math.round(p.coordlast[coordrand].y));
- self.ctx.lineto(math.round(p.x), math.round(p.y));
- self.ctx.closepath();
- self.ctx.strokestyle = 'hsla('+p.hue+', 100%, '+p.brightness+'%, '+p.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(p.x), math.round(p.y), rand(p.linewidth,p.linewidth+3)/2, 0, math.pi*2, false)
- self.ctx.closepath();
- var randrandalpha = rand(50,100)/100;
- self.ctx.fillstyle = 'hsla('+p.hue+', 100%, '+p.brightness+'%, '+randalpha+')';
- self.ctx.fill();
- }
- }
- };
- };
- // 创建烟花
- self.createfireworks = function(startx, starty, targetx, targety){
- var newfirework = {
- x: startx,
- y: starty,
- startx: startx,
- starty: starty,
- hitx: false,
- hity: false,
- coordlast: [
- {x: startx, y: starty},
- {x: startx, y: starty},
- {x: startx, y: starty}
- ],
- targetx: targetx,
- targety: targety,
- speed: self.fworkspeed,
- angle: math.atan2(targety - starty, targetx - startx),
- shockwaveangle: math.atan2(targety - starty, targetx - startx)+(90*(math.pi/180)),
- acceleration: self.fworkaccel/100,
- hue: self.currenthue,
- brightness: rand(50, 80),
- alpha: rand(50,100)/100,
- linewidth: self.linewidth
- };
- self.fireworks.push(newfirework);
- };
- // 更新烟花
- self.updatefireworks = function(){
- var i = self.fireworks.length;
- while(i--){
- var f = self.fireworks[i];
- self.ctx.linewidth = f.linewidth;
- vx = math.cos(f.angle) * f.speed,
- vy = math.sin(f.angle) * f.speed;
- f.speed *= 1 + f.acceleration;
- f.coordlast[2].x = f.coordlast[1].x;
- f.coordlast[2].y = f.coordlast[1].y;
- f.coordlast[1].x = f.coordlast[0].x;
- f.coordlast[1].y = f.coordlast[0].y;
- f.coordlast[0].x = f.x;
- f.coordlast[0].y = f.y;
- if(f.startx >= f.targetx){
- if(f.x + vx <= f.targetx){
- ff.x = f.targetx;
- f.hitx = true;
- } else {
- f.x += vx;
- }
- } else {
- if(f.x + vx >= f.targetx){
- ff.x = f.targetx;
- f.hitx = true;
- } else {
- f.x += vx;
- }
- }
- if(f.starty >= f.targety){
- if(f.y + vy <= f.targety){
- ff.y = f.targety;
- f.hity = true;
- } else {
- f.y += vy;
- }
- } else {
- if(f.y + vy >= f.targety){
- ff.y = f.targety;
- f.hity = true;
- } else {
- f.y += vy;
- }
- }
- if(f.hitx && f.hity){
- self.createparticles(f.targetx, f.targety, f.hue);
- self.fireworks.splice(i, 1);
- }
- };
- };
- // 绘制烟花
- self.drawfireworks = function(){
- var i = self.fireworks.length;
- self.ctx.globalcompositeoperation = 'lighter';
- while(i--){
- var f = self.fireworks[i];
- self.ctx.linewidth = f.linewidth;
- var coordrand = (rand(1,3)-1);
- self.ctx.beginpath();
- self.ctx.moveto(math.round(f.coordlast[coordrand].x), math.round(f.coordlast[coordrand].y));
- self.ctx.lineto(math.round(f.x), math.round(f.y));
- self.ctx.closepath();
- self.ctx.strokestyle = 'hsla('+f.hue+', 100%, '+f.brightness+'%, '+f.alpha+')';
- self.ctx.stroke();
- if(self.showtarget){
- self.ctx.save();
- self.ctx.beginpath();
- self.ctx.arc(math.round(f.targetx), math.round(f.targety), rand(1,8), 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(f.x), math.round(f.y));
- self.ctx.rotate(f.shockwaveangle);
- self.ctx.beginpath();
- self.ctx.arc(0, 0, 1*(f.speed/5), 0, math.pi, true);
- self.ctx.strokestyle = 'hsla('+f.hue+', 100%, '+f.brightness+'%, '+rand(25, 60)/100+')';
- self.ctx.linewidth = f.linewidth;
- self.ctx.stroke();
- self.ctx.restore();
- }
- };
- };
- // 绑定事件
- self.bindevents = function(){
- $(window).on('resize', function(){
- cleartimeout(self.timeout);
- self.timeout = settimeout(function() {
- selfself.canvas.width = self.cw = $(window).innerwidth();
- selfself.canvas.height = self.ch = $(window).innerheight();
- self.ctx.linecap = 'round';
- self.ctx.linejoin = 'round';
- }, 100);
- });
- $(self.canvas).on('mousedown', function(e){
- self.mx = e.pagex - self.canvas.offsetleft;
- self.my = e.pagey - self.canvas.offsettop;
- self.currenthue = rand(self.huemin, self.huemax);
- self.createfireworks(self.cw/2, self.ch, self.mx, self.my);
- $(self.canvas).on('mousemove.fireworks', function(e){
- self.mx = e.pagex - self.canvas.offsetleft;
- self.my = e.pagey - self.canvas.offsettop;
- self.currenthue = rand(self.huemin, self.huemax);
- self.createfireworks(self.cw/2, self.ch, self.mx, self.my);
- });
- });
- $(self.canvas).on('mouseup', function(e){
- $(self.canvas).off('mousemove.fireworks');
- });
- };
- self.clear = function(){
- self.particles = [];
- self.fireworks = [];
- self.ctx.clearrect(0, 0, self.cw, self.ch);
- };
- self.canvasloop = function(){
- requestanimframe(self.canvasloop, self.canvas);
- self.ctx.globalcompositeoperation = 'destination-out';
- self.ctx.fillstyle = 'rgba(0,0,0,'+self.clearalpha/100+')';
- self.ctx.fillrect(0,0,self.cw,self.ch);
- self.updatefireworks();
- self.updateparticles();
- self.drawfireworks();
- self.drawparticles();
- };
- self.init();
- }
- var fworks = new fireworks();
- $('#info-toggle').on('click', function(e){
- $('#info-inner').stop(false, true).slidetoggle(100);
- e.preventdefault();
- });
- });
- </script>
- <canvas width="1400" height="449"></canvas>
- </body>
- </html>
是不是被html5强大的效果惊呆了,一饱眼福了吧。
上一篇: HTML5实现多张图片上传功能