聲明:本文為原創文章,如需轉載,請注明來源WAxes,謝謝!
要過年了,過年想到的就是放煙火啦。。。。於是就用canvas寫了個放煙火的效果,鼠標點擊也會產生煙火,不過不要產生太多煙火哦,一個煙火散出的粒子是30到200個之間,當頁面上的粒子數量達到一定的時候,頁面就會很卡咯,我也沒特意去優化神馬的。以后有空再說吧。
直接上DEMO吧:放煙火
原理很簡單。。。就寫一個煙火類以及碎屑類,實例化后讓它飛起來,然后到達某個點后,把這個煙火對象的dead屬性置為true,然后再實例化出一定數量的碎屑對象,並且給與碎屑對象隨機一個要到達的目標點,然后讓所有碎屑對象飛過去就行了。
【煙火】
var Boom = function(x,r,c,boomArea,shape){ //煙火對象 this.booms = []; this.x = x; this.y = (canvas.height+r); this.r = r; this.c = c; this.shape = shape || false; this.boomArea = boomArea; this.theta = 0; this.dead = false; this.ba = parseInt(getRandom(80 , 200)); } Boom.prototype = { _paint:function(){ ctx.save(); ctx.beginPath(); ctx.arc(this.x,this.y,this.r,0,2*Math.PI); ctx.fillStyle = this.c; ctx.fill(); ctx.restore(); }, _move:function(){ var dx = this.boomArea.x - this.x , dy = this.boomArea.y - this.y; this.x = this.x+dx*0.01; this.y = this.y+dy*0.01; if(Math.abs(dx)<=this.ba && Math.abs(dy)<=this.ba){ if(this.shape){ this._shapBoom(); } else this._boom(); this.dead = true; } else { this._paint(); } }, _drawLight:function(){ ctx.save(); ctx.fillStyle = "rgba(255,228,150,0.3)"; ctx.beginPath(); ctx.arc(this.x , this.y , this.r+3*Math.random()+1 , 0 , 2*Math.PI); ctx.fill(); ctx.restore(); }, _boom:function(){ //普通爆炸 var fragNum = getRandom(30 , 200); var style = getRandom(0,10)>=5? 1 : 2; var color; if(style===1){ color = { a:parseInt(getRandom(128,255)), b:parseInt(getRandom(128,255)), c:parseInt(getRandom(128,255)) } } var fanwei = parseInt(getRandom(300, 400)); for(var i=0;i<fragNum;i++){ if(style===2){ color = { a:parseInt(getRandom(128,255)), b:parseInt(getRandom(128,255)), c:parseInt(getRandom(128,255)) } } var a = getRandom(-Math.PI, Math.PI); var x = getRandom(0, fanwei) * Math.cos(a) + this.x; var y = getRandom(0, fanwei) * Math.sin(a) + this.y; var radius = getRandom(0 , 2) var frag = new Frag(this.x , this.y , radius , color , x , y ); this.booms.push(frag); } }, _shapBoom:function(){ //有形狀的爆炸 var that = this; putValue(ocas , octx , this.shape , 5, function(dots){ var dx = canvas.width/2-that.x; var dy = canvas.height/2-that.y; for(var i=0;i<dots.length;i++){ color = {a:dots[i].a,b:dots[i].b,c:dots[i].c} var x = dots[i].x; var y = dots[i].y; var radius = 1; var frag = new Frag(that.x , that.y , radius , color , x-dx , y-dy); that.booms.push(frag); } }) } }
【碎屑】
var Frag = function(centerX , centerY , radius , color ,tx , ty){ //煙火碎屑對象 this.tx = tx; this.ty = ty; this.x = centerX; this.y = centerY; this.dead = false; this.centerX = centerX; this.centerY = centerY; this.radius = radius; this.color = color; } Frag.prototype = { paint:function(){ ctx.save(); ctx.beginPath(); ctx.arc(this.x , this.y , this.radius , 0 , 2*Math.PI); ctx.fillStyle = "rgba("+this.color.a+","+this.color.b+","+this.color.c+",1)"; ctx.fill() ctx.restore(); }, moveTo:function(index){ this.ty = this.ty+0.3; var dx = this.tx - this.x , dy = this.ty - this.y; this.x = Math.abs(dx)<0.1 ? this.tx : (this.x+dx*0.1); this.y = Math.abs(dy)<0.1 ? this.ty : (this.y+dy*0.1); if(dx===0 && Math.abs(dy)<=80){ this.dead = true; } this.paint(); } }
讓碎屑產生虛影也很簡單,就是每次刷新畫布時,不是擦掉重繪,而是繪制透明度為0.1(如果想虛影更長,可以把這個值弄的更小)的背景顏色。然后虛影就可以做出來了。也就是:
ctx.save(); ctx.fillStyle = "rgba(0,5,24,0.1)"; ctx.fillRect(0,0,canvas.width,canvas.height); ctx.restore();
讓煙火形成自己想要的形狀,比如字體,圖片之類的,也很簡單,就是可以通過離屏canvas以及canvas的getImageData這個方法就可以做出來。離屏canvas,顧名思義就是離開屏幕的,也就是不可見的canvas,直接在js里面用document.createElement("canvas")就可以生成一個canvas dom對象了,只要不把這個dom對象賦給body,這個canvas對象就相當於一個離屏對象了,我們就可以獲取到這個離屏canvas的context對象,然后再用戶看不到的地方做任何我們想做的事情了。
讓煙火形成自己想要的形狀就是先把文字或者圖片畫在離屏canvas上,然后用getImageData獲取畫布上的像素數組,然后遍歷數組,獲取有顏色的像素,也就是我們想要的內容,保存起來后,再放到主canvas對象中顯示出來。
getImageData的像素處理我之前的博客上有講過,如果不會用的,請戳:隨便談談用canvas來實現文字圖片粒子化
源碼地址:https://github.com/whxaxes/canvas-test/tree/gh-pages/src/Funny-demo/shotFire