濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 實(shí)例教程 HTML5 Canvas 超炫酷煙花綻放動(dòng)畫實(shí)現(xiàn)代碼

實(shí)例教程 HTML5 Canvas 超炫酷煙花綻放動(dòng)畫實(shí)現(xiàn)代碼

熱門標(biāo)簽:申請(qǐng)400電話流程簡(jiǎn)介 呼和浩特外呼電銷系統(tǒng)排名 阜陽企業(yè)外呼系統(tǒng) 外呼線穩(wěn)定線路 地圖標(biāo)注位置能賺錢嗎 pageadm實(shí)現(xiàn)地圖標(biāo)注 南通數(shù)據(jù)外呼系統(tǒng)推廣 外呼系統(tǒng)電話怎么投訴 邢臺(tái)縣地圖標(biāo)注app

  這是一個(gè)很酷的HTML5 Canvas動(dòng)畫,它將模擬的是我們現(xiàn)實(shí)生活中煙花綻放的動(dòng)畫特效,效果非常逼真,但是畢竟是電腦模擬,帶女朋友看就算了,效果還是差了點(diǎn),呵呵。這個(gè)HTML5 Canvas動(dòng)畫有一點(diǎn)比較出色,就是其性能,Chrome上基本沒有卡的感覺,就算你放出很多煙花也一樣。

  下面我們來簡(jiǎn)單分析一下實(shí)現(xiàn)這款HTML5煙花特效的過程及代碼,主要由HTML代碼、CSS代碼以及Javascript代碼組成,當(dāng)然javascript代碼是最重要的。

  HTML代碼:

XML/HTML Code復(fù)制內(nèi)容到剪貼板
  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的結(jié)構(gòu)非常簡(jiǎn)單,即構(gòu)造了一個(gè)canvas容器,我們會(huì)利用JS在這個(gè)容器中生成一個(gè)Canvas對(duì)象。看最后的JS代碼你就會(huì)知道了。

  CSS代碼:

CSS Code復(fù)制內(nèi)容到剪貼板
  1. #canvas-container { background#000 url(bg.jpg); height400pxleft: 50%; margin: -200px 0 0 -300pxpositionabsolutetop: 50%; width600pxz-index: 2;   
  2. } canvas { cursorcrosshairdisplayblockpositionrelativez-index: 3;   
  3. } canvas:active { cursorcrosshair;   
  4. #skyline { backgroundurl(skyline.png) repeat-x 50% 0; bottombottom: 0; height135pxleft: 0; positionabsolutewidth: 100%; z-index: 1;       
  5. #mountains1 { backgroundurl(mountains1.png) repeat-x 40% 0; bottombottom: 0; height200pxleft: 0; positionabsolutewidth: 100%; z-index: 1;       
  6. #mountains2 { backgroundurl(mountains2.png) repeat-x 30% 0; bottombottom: 0; height250pxleft: 0; positionabsolutewidth: 100%; z-index: 1;       
  7. #gui { rightright: 0; positionfixedtop: 0; z-index: 3;   
  8. }  

  CSS代碼沒什么特別,主要也就定義一下背景色和邊框之類的。

  接下來是最重要的Javascript代碼。

  Javascript代碼:

JavaScript Code復(fù)制內(nèi)容到剪貼板
  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.   
  9.         self.canvas.width = self.cw = 600;   
  10.         self.canvas.height = self.ch = 400;       
  11.   
  12.         self.particles = [];       
  13.         self.partCount = 30;   
  14.         self.fireworks = [];       
  15.         self.mx = self.cw/2;   
  16.         self.my = self.ch/2;   
  17.         self.currentHue = 170;   
  18.         self.partSpeed = 5;   
  19.         self.partSpeedVariance = 10;   
  20.         self.partWind = 50;   
  21.         self.partFriction = 5;   
  22.         self.partGravity = 1;   
  23.         self.hueMin = 150;   
  24.         self.hueMax = 200;   
  25.         self.fworkSpeed = 2;   
  26.         self.fworkAccel = 4;   
  27.         self.hueVariance = 30;   
  28.         self.flickerDensity = 20;   
  29.         self.showShockwave = false;   
  30.         self.showTarget = true;   
  31.         self.clearAlpha = 25;   
  32.   
  33.         self.canvasContainer.append(self.canvas);   
  34.         self.ctx = self.canvas.getContext('2d');   
  35.         self.ctx.lineCap = 'round';   
  36.         self.ctx.lineJoin = 'round';   
  37.         self.lineWidth = 1;   
  38.         self.bindEvents();               
  39.         self.canvasLoop();   
  40.   
  41.         self.canvas.onselectstart = function() { return false;   
  42.         };   
  43.   
  44.     };  

  這段JS代碼主要是往canvas容器中構(gòu)造一個(gè)Canvas對(duì)象,并且對(duì)這個(gè)canvas對(duì)象的外觀以及動(dòng)畫屬性作了初始化。

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

  這段JS代碼的功能是實(shí)現(xiàn)煙花爆炸后的小顆粒的繪制,從draw方法中可以看出,創(chuàng)建幾個(gè)隨機(jī)點(diǎn),煙花顆粒即可在這個(gè)范圍的隨機(jī)點(diǎn)中散落。

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

  這段JS代碼是創(chuàng)建煙花實(shí)例的,我們也可以從draw方法中看出,當(dāng)我們鼠標(biāo)點(diǎn)擊畫布中的某點(diǎn)時(shí),煙花發(fā)射的目的地就在那個(gè)點(diǎn)上。

  這款HTML5 Canvas煙花效果的核心代碼就是這樣,謝謝閱讀,希望能幫到大家,請(qǐng)繼續(xù)關(guān)注腳本之家,我們會(huì)努力分享更多優(yōu)秀的文章。

標(biāo)簽:辛集 楊凌 黃山 撫順 蚌埠 內(nèi)蒙古 鶴崗 德州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《實(shí)例教程 HTML5 Canvas 超炫酷煙花綻放動(dòng)畫實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  實(shí)例,教程,HTML5,Canvas,超炫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《實(shí)例教程 HTML5 Canvas 超炫酷煙花綻放動(dòng)畫實(shí)現(xiàn)代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于實(shí)例教程 HTML5 Canvas 超炫酷煙花綻放動(dòng)畫實(shí)現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    丰原市| 营口市| 沿河| 尤溪县| 格尔木市| 江北区| 凯里市| 罗平县| 嘉义县| 凤城市| 永靖县| 平塘县| 筠连县| 玉环县| 西乌| 蒙山县| 霍州市| 金平| 南乐县| 保德县| 罗田县| 本溪| 新晃| 天气| 浙江省| 澳门| 友谊县| 上思县| 金寨县| 开封县| 兰溪市| 勃利县| 宁晋县| 咸阳市| 洛川县| 垣曲县| 永仁县| 吴川市| 桃园县| 海伦市| 德兴市|