怎么用HTML5制作煙火效果

這篇文章主要介紹“怎么用HTML5制作煙火效果”,在日常操作中,相信很多人在怎么用HTML5制作煙火效果問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用HTML5制作煙火效果”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

創(chuàng)新互聯(lián)建站擁有網(wǎng)站維護(hù)技術(shù)和項(xiàng)目管理團(tuán)隊(duì),建立的售前、實(shí)施和售后服務(wù)體系,為客戶提供定制化的成都做網(wǎng)站、成都網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、電信內(nèi)江機(jī)房解決方案。為客戶網(wǎng)站安全和日常運(yùn)維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護(hù)服務(wù)覆蓋集團(tuán)企業(yè)、上市公司、外企網(wǎng)站、商城網(wǎng)站開(kāi)發(fā)、政府網(wǎng)站等各類型客戶群體,為全球成百上千家企業(yè)提供全方位網(wǎng)站維護(hù)、服務(wù)器維護(hù)解決方案。

原理很簡(jiǎn)單。。。就寫(xiě)一個(gè)煙火類以及碎屑類,實(shí)例化后讓它飛起來(lái),然后到達(dá)某個(gè)點(diǎn)后,把這個(gè)煙火對(duì)象的dead屬性置為true,然后再實(shí)例化出一定數(shù)量的碎屑對(duì)象,并且給與碎屑對(duì)象隨機(jī)一個(gè)要到達(dá)的目標(biāo)點(diǎn),然后讓所有碎屑對(duì)象飛過(guò)去就行了。

【煙火】

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. var Boom = function(x,r,c,boomArea,shape){  //煙火對(duì)象   

  2.             this.booms = [];   

  3.             this.x = x;   

  4.             this.y = (canvas.height+r);   

  5.             this.r = r;   

  6.             this.c = c;   

  7.             this.shape = shape || false;   

  8.             this.boomArea = boomArea;   

  9.             this.theta = 0;   

  10.             this.dead = false;   

  11.             this.ba = parseInt(getRandom(80 , 200));   

  12.         }   

  13.         Boom.prototype = {   

  14.             _paint:function(){     

  15.                 ctx.save();   

  16.                 ctx.beginPath();   

  17.                 ctx.arc(this.x,this.y,this.r,0,2*Math.PI);   

  18.                 ctx.fillStyle = this.c;   

  19.                 ctx.fill();   

  20.                 ctx.restore();   

  21.             },   

  22.             _move:function(){   

  23.                 var dx = this.boomArea.x - this.x , dy = this.boomArea.y - this.y;   

  24.                 thisthis.x = this.x+dx*0.01;   

  25.                 thisthis.y = this.y+dy*0.01;   

  26.   

  27.                 if(Math.abs(dx)<=this.ba && Math.abs(dy)<=this.ba){   

  28.                     if(this.shape){   

  29.                         this._shapBoom();   

  30.                     }   

  31.                     else this._boom();   

  32.                     this.dead = true;   

  33.                 }   

  34.                 else {   

  35.                     this._paint();   

  36.                 }   

  37.             },   

  38.             _drawLight:function(){   

  39.                 ctx.save();   

  40.                 ctx.fillStyle = "rgba(255,228,150,0.3)";   

  41.                 ctx.beginPath();   

  42.                 ctx.arc(this.x , this.y , this.r+3*Math.random()+1 , 0 , 2*Math.PI);   

  43.                 ctx.fill();   

  44.                 ctx.restore();   

  45.             },   

  46.             _boom:function(){    //普通爆炸   

  47.                 var fragNum = getRandom(30 , 200);   

  48.                 var style = getRandom(0,10)>=5? 1 : 2;   

  49.                 var color;   

  50.                 if(style===1){   

  51.                     color = {   

  52.                         a:parseInt(getRandom(128,255)),   

  53.                         b:parseInt(getRandom(128,255)),   

  54.                         c:parseInt(getRandom(128,255))   

  55.                     }   

  56.                 }   

  57.   

  58.                 var fanwei = parseInt(getRandom(300, 400));   

  59.                 for(var i=0;i<fragNum;i++){   

  60.                     if(style===2){   

  61.                         color = {   

  62.                             a:parseInt(getRandom(128,255)),   

  63.                             b:parseInt(getRandom(128,255)),   

  64.                             c:parseInt(getRandom(128,255))   

  65.                         }   

  66.                     }   

  67.                     var a = getRandom(-Math.PI, Math.PI);   

  68.                     var x = getRandom(0, fanwei) * Math.cos(a) + this.x;   

  69.                     var y = getRandom(0, fanwei) * Math.sin(a) + this.y;    

  70.                     var radius = getRandom(0 , 2)   

  71.                     var frag = new Frag(this.x , this.y , radius , color , x , y );   

  72.                     this.booms.push(frag);   

  73.                 }   

  74.             },   

  75.             _shapBoom:function(){    //有形狀的爆炸   

  76.                 var that = this;   

  77.                 putValue(ocas , octx , this.shape , 5, function(dots){   

  78.                     var dx = canvas.width/2-that.x;   

  79.                     var dy = canvas.height/2-that.y;   

  80.                     for(var i=0;i<dots.length;i++){   

  81.                         color = {a:dots[i].a,b:dots[i].b,c:dots[i].c}   

  82.                         var x = dots[i].x;   

  83.                         var y = dots[i].y;   

  84.                         var radius = 1;   

  85.                         var frag = new Frag(that.x , that.y , radius , color , x-dx , y-dy);   

  86.                         that.booms.push(frag);   

  87.                     }   

  88.                 })   

  89.             }   

  90.         }   


【碎屑】

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. var Frag = function(centerX , centerY , radius , color ,tx , ty){   //煙火碎屑對(duì)象   

  2.             this.tx = tx;   

  3.             this.ty = ty;   

  4.             this.x = centerX;   

  5.             this.y = centerY;   

  6.             this.dead = false;   

  7.             this.centerX = centerX;   

  8.             this.centerY = centerY;   

  9.             this.radius = radius;   

  10.             this.color = color;   

  11.         }   

  12.   

  13.         Frag.prototype = {   

  14.             paint:function(){   

  15.                 ctx.save();   

  16.                 ctx.beginPath();   

  17.                 ctx.arc(this.x , this.y , this.radius , 0 , 2*Math.PI);   

  18.                 ctx.fillStyle = "rgba("+this.color.a+","+this.color.b+","+this.color.c+",1)";   

  19.                 ctx.fill()   

  20.                 ctx.restore();   

  21.             },   

  22.             moveTo:function(index){   

  23.                 thisthis.ty = this.ty+0.3;   

  24.                 var dx = this.tx - this.x , dy = this.ty - this.y;   

  25.                 this.x = Math.abs(dx)<0.1 ? this.tx : (this.x+dx*0.1);   

  26.                 this.y = Math.abs(dy)<0.1 ? this.ty : (this.y+dy*0.1);   

  27.                 if(dx===0 && Math.abs(dy)<=80){   

  28.                     this.dead = true;   

  29.                 }   

  30.                 this.paint();   

  31.             }   

  32.         }  


讓碎屑產(chǎn)生虛影也很簡(jiǎn)單,就是每次刷新畫(huà)布時(shí),不是擦掉重繪,而是繪制透明度為0.1(如果想虛影更長(zhǎng),可以把這個(gè)值弄的更?。┑谋尘邦伾?。然后虛影就可以做出來(lái)了。也就是:

XML/HTML Code復(fù)制內(nèi)容到剪貼板

  1. ctx.save();   

  2.             ctx.fillStyle = "rgba(0,5,24,0.1)";   

  3.             ctx.fillRect(0,0,canvas.width,canvas.height);   

  4.             ctx.restore();  

讓煙火形成自己想要的形狀,比如字體,圖片之類的,也很簡(jiǎn)單,就是可以通過(guò)離屏canvas以及canvas的getImageData這個(gè)方法就可以做出來(lái)。離屏canvas,顧名思義就是離開(kāi)屏幕的,也就是不可見(jiàn)的canvas,直接在js里面用document.createElement("canvas")就可以生成一個(gè)canvas dom對(duì)象了,只要不把這個(gè)dom對(duì)象賦給body,這個(gè)canvas對(duì)象就相當(dāng)于一個(gè)離屏對(duì)象了,我們就可以獲取到這個(gè)離屏canvas的context對(duì)象,然后再用戶看不到的地方做任何我們想做的事情了。

讓煙火形成自己想要的形狀就是先把文字或者圖片畫(huà)在離屏canvas上,然后用getImageData獲取畫(huà)布上的像素?cái)?shù)組,然后遍歷數(shù)組,獲取有顏色的像素,也就是我們想要的內(nèi)容,保存起來(lái)后,再放到主canvas對(duì)象中顯示出來(lái)。

getImageData的像素處理我之前的博客上有講過(guò),如果不會(huì)用的,請(qǐng)戳:隨便談?wù)動(dòng)胏anvas來(lái)實(shí)現(xiàn)文字圖片粒子化

源碼地址:https://github.com/whxaxes/canvas-test/tree/gh-pages/src/Funny-demo/shotFire

到此,關(guān)于“怎么用HTML5制作煙火效果”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

網(wǎng)頁(yè)標(biāo)題:怎么用HTML5制作煙火效果
鏈接地址:http://muchs.cn/article22/ieppjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、手機(jī)網(wǎng)站建設(shè)品牌網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化、網(wǎng)站內(nèi)鏈網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)