DIV CSS 佈局教程網

Canvas 百花齊放:用斜拋運動模擬煙火粒子特效
編輯:關於JavaScript     

煙火、禮花、粒子爆炸,背後其實都是同一套物理:一個初速度、一個角度、一個重力加速度,剩下的交給時間。本文用 100 個隨機粒子從畫面下方同一點噴出,做出「百花齊放」的效果,跑完一輪自動重播。

執行結果:

 <!DOCTYPE html> <html> <head> <title>demo</title> <style type="text/css"> body { margin:0; padding:0; } #canvas { border:5px solid gray; box-shadow:0 0 15px 15px #494949 inset; margin-top:50px; margin-left:200px; } </style> </head> <body > <canvas id="canvas" width="1000px" height="500px"></canvas> <script type="text/javascript"> var dyl = {}; dyl.canvas = document.getElementById("canvas"); dyl.ctx = dyl.canvas.getContext("2d"); dyl.runTime = 0; dyl.colorList = "01234567890ABCDEFabcdef".split(""); dyl.colorListLength = dyl.colorList.length; dyl.arcList = null; /** * 得到16進制隨機顏色值 */ dyl.getColor = function() { var color = "#"; for(var i=0; i<6; i++) { color += dyl.colorList[Math.floor(Math.random()*dyl.colorListLength)]; } return color; }; /** * 一個隨機角度,隨機初始速度的斜拋對象 */ var Arc = function(i) { // 設置自有屬性 this.v = Math.round(Math.random()*100)+50; this.angle = Math.round(Math.random()*145) + 45; this.startTime = +new Date(); this._angle = this.angle/180*Math.PI; this.v_x = this.v*Math.cos(this._angle); this.v_y_start = this.v*Math.sin(this._angle); this.color = dyl.getColor(); this.x = 500; this.g = 250; this.y = 490; this.index = i; var _self = this; this.run = function() { var endTime = +new Date(); var timeSpan = (endTime - _self.startTime)/1000; var v_y_now = _self.v_y_start - 1/2*_self.g*Math.pow(timeSpan, 2); _self.x = _self.x +_self.v_x * timeSpan; _self.y = _self.y - (_self.v_y_start * timeSpan - 1/2*_self.g*Math.pow(timeSpan, 2)); return this; }; return this; }; /** * 全局繪制圖像 */ dyl.draw = function() { var arcList = dyl.arcList; var ctx = dyl.ctx; dyl.runTime++; for(var i=0, length=arcList.length; i<length; i++) { var arc = arcList[i]; if(!arc) { continue; } arc.run(); ctx.save(); ctx.beginPath(); ctx.fillStyle = arc.color; ctx.arc(arc.x, arc.y, 2, 0, Math.PI*2); ctx.fill(); ctx.closePath(); ctx.restore(); } console.log(dyl.runTime); if(dyl.runTime >= 25) { setTimeout(dyl.init, 1050); } else { setTimeout(dyl.draw, 20); } }; /** * 初始化整個事件 */ dyl.init = function() { dyl.ctx.clearRect(0, 0, 1000, 500); dyl.arcList = []; dyl.runTime = 0; for(var i=0; i<100; i++) { dyl.arcList.push(new Arc(i)); } dyl.draw(); }; dyl.init(); </script> </body> </html> 

物理怎麼寫進程式碼

每個 Arc 物件在建立時就決定了自己的個性:初速 v 在 50–150 之間隨機、發射角 angle 落在 45°–190°,換算成弧度後分解成水平分量 v_x 與垂直分量 v_y_start,重力 g 固定 250,起點统一在 (500, 490) 也就是畫面下方中央。

run() 每幀做的事就是代入位移公式:水平方向等速前進 v_x × t,垂直方向則是 v_y × t − ½gt²。注意 y 軸在 Canvas 中是向下遞增的,所以垂直位移要「減」上去才會先往上升、再被重力拉回來。

動畫迴圈的控制

這裡刻意用 setTimeout(dyl.draw, 20) 而不是 setInterval。差別在於:setTimeout 會等上一輪畫完才排下一輪,不会因为某幀特別慢而堆積回呼,跑動畫時比 setInterval 穩定。畫滿 25 幀之後清空畫布、等 1050 毫秒,再重新 init 噴一次,於是形成循環。

想讓效果更真實,可以替每個粒子加上重力差異、空氣阻力衰減,或讓粒子在生命週期結束後淡出而不是一刀切掉。但作為入門示範,光是「角度 + 初速 + 重力」這三個參數,就足以撐起相當有說服力的視覺效果了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved