欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码_html5教程技巧

程序员文章站 2022-03-11 21:21:36
...
  这是一个很酷的HTML5 Canvas动画,它将模拟的是我们现实生活中烟花绽放的动画特效,效果非常逼真,但是毕竟是电脑模拟,带女朋友看就算了,效果还是差了点,呵呵。这个HTML5 Canvas动画有一点比较出色,就是其性能,Chrome上基本没有卡的感觉,就算你放出很多烟花也一样。

实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码_html5教程技巧

  下面我们来简单分析一下实现这款HTML5烟花特效的过程及代码,主要由HTML代码、CSS代码以及Javascript代码组成,当然javascript代码是最重要的。

  HTML代码:

XML/HTML Code复制内容到剪贴板
  1. div id=”gui”>div>
  2. div id=”canvas-container”> div id=”mountains2″>div>
  3. div id=”mountains1″>div>div id=”skyline”>div> div>

  HTML的结构非常简单,即构造了一个canvas容器,我们会利用JS在这个容器中生成一个Canvas对象。看最后的JS代码你就会知道了。

  CSS代码:

CSS Code复制内容到剪贴板
  1. #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;
  2. } canvas { cursor: crosshair; display: block; position: relative; z-index: 3;
  3. } canvas:active { cursor: crosshair;
  4. } #skyline { background: url(skyline.png) repeat-x 50% 0; bottombottom: 0; height: 135px; left: 0; position: absolute; width: 100%; z-index: 1;
  5. } #mountains1 { background: url(mountains1.png) repeat-x 40% 0; bottombottom: 0; height: 200px; left: 0; position: absolute; width: 100%; z-index: 1;
  6. } #mountains2 { background: url(mountains2.png) repeat-x 30% 0; bottombottom: 0; height: 250px; left: 0; position: absolute; width: 100%; z-index: 1;
  7. } #gui { rightright: 0; position: fixed; top: 0; z-index: 3;
  8. }

  CSS代码没什么特别,主要也就定义一下背景色和边框之类的。

  接下来是最重要的Javascript代码。

  Javascript代码:

JavaScript Code复制内容到剪贴板
  1. self.init = function(){
  2. self.dt = 0;
  3. self.oldTime = Date.now();
  4. self.canvas = document.createElement('canvas');
  5. self.canvasContainer = $('#canvas-container'); var canvasContainerDisabled = document.getElementById('canvas-container');
  6. self.canvas.onselectstart = function() { return false;
  7. };
  8. self.canvas.width = self.cw = 600;
  9. self.canvas.height = self.ch = 400;
  10. self.particles = [];
  11. self.partCount = 30;
  12. self.fireworks = [];
  13. self.mx = self.cw/2;
  14. self.my = self.ch/2;
  15. self.currentHue = 170;
  16. self.partSpeed = 5;
  17. self.partSpeedVariance = 10;
  18. self.partWind = 50;
  19. self.partFriction = 5;
  20. self.partGravity = 1;
  21. self.hueMin = 150;
  22. self.hueMax = 200;
  23. self.fworkSpeed = 2;
  24. self.fworkAccel = 4;
  25. self.hueVariance = 30;
  26. self.flickerDensity = 20;
  27. self.showShockwave = false;
  28. self.showTarget = true;
  29. self.clearAlpha = 25;
  30. self.canvasContainer.append(self.canvas);
  31. self.ctx = self.canvas.getContext('2d');
  32. self.ctx.lineCap = 'round';
  33. self.ctx.lineJoin = 'round';
  34. self.lineWidth = 1;
  35. self.bindEvents();
  36. self.canvasLoop();
  37. self.canvas.onselectstart = function() { return false;
  38. };
  39. };

  这段JS代码主要是往canvas容器中构造一个Canvas对象,并且对这个canvas对象的外观以及动画属性作了初始化。

JavaScript Code复制内容到剪贴板
  1. var Particle = function(x, y, hue){ this.x = x; this.y = y; this.coordLast = [
  2. {x: x, y: y},
  3. {x: x, y: y},
  4. {x: x, y: y}
  5. ]; this.angle = rand(0, 360); this.speed = rand(((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;
  6. };
  7. 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
  8. self.particles.splice(index, 1);
  9. }
  10. };
  11. Particle.prototype.draw = function(){ var coordRand = (rand(1,3)-1);
  12. self.ctx.beginPath();
  13. self.ctx.moveTo(Math.round(this.coordLast[coordRand].x), Math.round(this.coordLast[coordRand].y));
  14. self.ctx.lineTo(Math.round(this.x), Math.round(this.y));
  15. self.ctx.closePath();
  16. self.ctx.strokeStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+this.alpha+')';
  17. self.ctx.stroke(); if(self.flickerDensity > 0){ var inverseDensity = 50 - self.flickerDensity; if(rand(0, inverseDensity) === inverseDensity){
  18. self.ctx.beginPath();
  19. 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;
  20. self.ctx.fillStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+randAlpha+')';
  21. self.ctx.fill();
  22. }
  23. }
  24. };

  这段JS代码的功能是实现烟花爆炸后的小颗粒的绘制,从draw方法中可以看出,创建几个随机点,烟花颗粒即可在这个范围的随机点中散落。

JavaScript Code复制内容到剪贴板
  1. 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 = [
  2. {x: startX, y: startY},
  3. {x: startX, y: startY},
  4. {x: startX, y: startY}
  5. ]; 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;
  6. };
  7. Firework.prototype.update = function(index){
  8. self.ctx.lineWidth = this.lineWidth;
  9. vx = Math.cos(this.angle) * this.speed,
  10. 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 this.targetRadius += .25 * self.dt;
  11. } else { this.targetRadius = 1 * self.dt;
  12. }
  13. } if(this.startX >= this.targetX){ if(this.x + vx this.targetX){ this.x = this.targetX; this.hitX = true;
  14. } else { this.x += vx * self.dt;
  15. }
  16. } else { if(this.x + vx >= this.targetX){ this.x = this.targetX; this.hitX = true;
  17. } else { this.x += vx * self.dt;
  18. }
  19. } if(this.startY >= this.targetY){ if(this.y + vy this.targetY){ this.y = this.targetY; this.hitY = true;
  20. } else { this.y += vy * self.dt;
  21. }
  22. } else { if(this.y + vy >= this.targetY){ this.y = this.targetY; this.hitY = true;
  23. } else { this.y += vy * self.dt;
  24. }
  25. } if(this.hitX && this.hitY){ var randExplosion = rand(0, 9);
  26. self.createParticles(this.targetX, this.targetY, this.hue);
  27. self.fireworks.splice(index, 1);
  28. }
  29. };
  30. Firework.prototype.draw = function(){
  31. self.ctx.lineWidth = this.lineWidth; var coordRand = (rand(1,3)-1);
  32. self.ctx.beginPath();
  33. self.ctx.moveTo(Math.round(this.coordLast[coordRand].x), Math.round(this.coordLast[coordRand].y));
  34. self.ctx.lineTo(Math.round(this.x), Math.round(this.y));
  35. self.ctx.closePath();
  36. self.ctx.strokeStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+this.alpha+')';
  37. self.ctx.stroke(); if(self.showTarget){
  38. self.ctx.save();
  39. self.ctx.beginPath();
  40. self.ctx.arc(Math.round(this.targetX), Math.round(this.targetY), this.targetRadius, 0, Math.PI*2, false)
  41. self.ctx.closePath();
  42. self.ctx.lineWidth = 1;
  43. self.ctx.stroke();
  44. self.ctx.restore();
  45. } if(self.showShockwave){
  46. self.ctx.save();
  47. self.ctx.translate(Math.round(this.x), Math.round(this.y));
  48. self.ctx.rotate(this.shockwaveAngle);
  49. self.ctx.beginPath();
  50. self.ctx.arc(0, 0, 1*(this.speed/5), 0, Math.PI, true);
  51. self.ctx.strokeStyle = 'hsla('+this.hue+', 100%, '+this.brightness+'%, '+rand(25, 60)/100+')';
  52. self.ctx.lineWidth = this.lineWidth;
  53. self.ctx.stroke();
  54. self.ctx.restore();
  55. }
  56. };

  这段JS代码是创建烟花实例的,我们也可以从draw方法中看出,当我们鼠标点击画布中的某点时,烟花发射的目的地就在那个点上。

  这款HTML5 Canvas烟花效果的核心代码就是这样,谢谢阅读,希望能帮到大家,请继续关注脚本之家,我们会努力分享更多优秀的文章。