日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

JavaScript實現煙花綻放動畫效果

瀏覽:239日期:2023-10-18 18:52:41

先編寫一個煙花綻放的動畫效果。

放煙花時,一個煙花可分為兩個階段:(1)煙花上升到空中;(2)煙花炸開成碎片,炸開的碎片慢慢消散。

為此抽象出兩個對象類:Firework和Particle。其中,Firework用于表示一個煙花對象,Particle用于表示一個煙花炸開后的各碎片。

Firework對象類定義6個屬性:表示煙花上升軌跡中各點的坐標(x,y)、煙花弧狀軌跡的偏轉角度angle、上升階段水平和垂直方向的位移改變量xSpeed和ySpeed、煙花的色彩色相hue。

坐標屬性值y的初始值取畫布的高度,表示煙花從地面上升到空中,其余各屬性的初始值采用隨機數確定。具體定義如下:

function Firework() { this.x = canvas.width/4*(1+3*Math.random()); this.y = canvas.height - 15; this.angle = Math.random() * Math.PI / 4 - Math.PI / 6; this.xSpeed = Math.sin(this.angle) *(6+Math.random()*7); this.ySpeed = -Math.cos(this.angle) *(6+Math.random()*7); this.hue = Math.floor(Math.random() * 360); }

Firework對象類定義3個方法:繪制煙花上升軌跡的方法draw()、煙花上升時坐標改變方法update()和煙花炸開方法explode()。繪制煙花軌跡時,在各點(x,y)處繪制一個寬度為5、高度為15的填充小矩形表示一個軌跡點。煙花上升時,垂直方向速度ySpeed初始值為負的,每次上升時,ySpeed加上一個正值,表示上升在減速,當ySpeed的值大于0時,煙花上升到頂了(不能再上升),就炸開為70個碎片。具體方法的實現見后面的HTML文件內容。

Particle對象類定義8個屬性:表示碎片散開軌跡中各點的坐標(x,y)、碎片弧狀軌跡的偏轉角度angle、散開時水平和垂直方向的位移改變量xSpeed和ySpeed、碎片的色彩色相hue、表示碎片小圓的半徑size、碎片的亮度lightness。

function Particle(x,y,hue) { this.x = x; this.y = y; this.hue = hue; this.lightness = 50; this.size = 15 + Math.random() * 10; this.angle = Math.random() * 2 * Math.PI; this.xSpeed = Math.cos(this.angle) *(1+Math.random() * 6); this.ySpeed = Math.sin(this.angle) *(1+Math.random() * 6); }

Particle對象類定義2個方法:繪制碎片散開軌跡的方法draw()、碎片散開時坐標改變方法update()。碎片散開時逐漸變小(屬性size值減量),當size值小于1時,從碎片數組中刪除該碎片,表示碎片已消亡。

定義兩個數組var fireworks=[];和var particles=[];分別存儲煙花對象和炸開的碎片對象。

模擬動畫的函數loop中,每隔一段時間(用count計數來實現)向fireworks數組中添加一個煙花對象,煙花對象上升到頂炸開后,從fireworks數組中刪除該對象元素,然后向particles數組中添加70個碎片對象。

遍歷兩個數組的各對象,分別調用它們的draw()和update()方法。

編寫的完整HTML文件內容如下。

<html> <head> <title>煙花綻放</title> </head><body><canvas style='border:3px double #996633;background:black;'></canvas><script type='text/javascript'> var canvas=document.getElementById(’myCanvas’); ctx= canvas.getContext(’2d’); var fireworks=[]; var particles=[]; var counter = 0; function Firework() { this.x = canvas.width/4*(1+3*Math.random()); this.y = canvas.height - 15; this.angle = Math.random() * Math.PI / 4 - Math.PI / 6; this.xSpeed = Math.sin(this.angle) *(6+Math.random()*7); this.ySpeed = -Math.cos(this.angle) *(6+Math.random()*7); this.hue = Math.floor(Math.random() * 360); } Firework.prototype.draw= function() { ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(Math.atan2(this.ySpeed, this.xSpeed) + Math.PI / 2); ctx.fillStyle =`hsl(${this.hue}, 100%, 50%)`; ctx.fillRect(0, 0, 5, 15); ctx.restore(); } Firework.prototype.update= function() { this.x = this.x + this.xSpeed; this.y = this.y + this.ySpeed; this.ySpeed += 0.1; } Firework.prototype.explode= function() { for (var i = 0; i < 70; i++) { particles.push(new Particle(this.x, this.y, this.hue)); } } function Particle(x,y,hue) { this.x = x; this.y = y; this.hue = hue; this.lightness = 50; this.size = 15 + Math.random() * 10; this.angle = Math.random() * 2 * Math.PI; this.xSpeed = Math.cos(this.angle) *(1+Math.random() * 6); this.ySpeed = Math.sin(this.angle) *(1+Math.random() * 6); } Particle.prototype.draw= function() { ctx.fillStyle = `hsl(${this.hue}, 100%, ${this.lightness}%)`; ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI); ctx.closePath(); ctx.fill(); } Particle.prototype.update= function(index) { this.ySpeed += 0.05; this.size = this.size*0.95; this.x = this.x + this.xSpeed; this.y = this.y + this.ySpeed; if (this.size<1) { particles.splice(index,1); } } function loop() { ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'; ctx.fillRect(0,0,canvas.width,canvas.height); counter++; if (counter==15) { fireworks.push(new Firework()); counter=0; } var i=fireworks.length; while (i--) { fireworks[i].draw(); fireworks[i].update(); if (fireworks[i].ySpeed > 0) { fireworks[i].explode(); fireworks.splice(i, 1); } } var i=particles.length; while (i--) {particles[i].draw(); particles[i].update(i); } requestAnimationFrame(loop); } loop();</script></body> </html>

在瀏覽器中打開包含這段HTML代碼的html文件,可以看到在瀏覽器窗口中呈現出如圖所示的煙花綻放動畫效果。

JavaScript實現煙花綻放動畫效果

實現了煙花綻放的效果,我們還可以繼續讓一定區域內的綻放的煙花碎片拼成“Happy New Year”粒子文本。

編寫如下的HTML代碼。

<html> <head> <title>迎新年煙花綻放</title> <style> body { margin: 0; background: black; } canvas { position: absolute; }</style></head><body><canvas id='myCanvas1'></canvas><canvas id='myCanvas2'></canvas><canvas id='myCanvas3'></canvas><script type='text/javascript'> function Particle(x, y, hue) { this.x = x; this.y = y; this.hue = hue; this.lightness = 50; this.size = 15 + Math.random() * 10; this.angle = Math.random() * 2 * Math.PI; this.xSpeed = Math.cos(this.angle) * (1 + Math.random() * 6); this.ySpeed = Math.sin(this.angle) * (1 + Math.random() * 6); this.target = getTarget(); this.timer = 0; } Particle.prototype.draw= function() { ctx2.fillStyle =`hsl(${this.hue}, 100%, ${this.lightness}%)`; ctx2.beginPath(); ctx2.arc(this.x, this.y, this.size, 0, 2 * Math.PI); ctx2.closePath(); ctx2.fill(); } Particle.prototype.update= function(idx) { if (this.target) { var dx = this.target.x - this.x; var dy = this.target.y - this.y; var dist = Math.sqrt(dx * dx + dy * dy); var a = Math.atan2(dy, dx); var tx = Math.cos(a) * 5; var ty = Math.sin(a) * 5; this.size = lerp(this.size, 1.5, 0.05); if (dist < 5) { this.lightness = lerp(this.lightness, 100, 0.01); this.xSpeed = this.ySpeed = 0; this.x = lerp(this.x, this.target.x + fidelity / 2, 0.05); this.y = lerp(this.y, this.target.y + fidelity / 2, 0.05); this.timer += 1; } else if (dist < 10) { this.lightness = lerp(this.lightness, 100, 0.01); this.xSpeed = lerp(this.xSpeed, tx, 0.1); this.ySpeed = lerp(this.ySpeed, ty, 0.1); this.timer += 1; } else { this.xSpeed = lerp(this.xSpeed, tx, 0.02); this.ySpeed = lerp(this.ySpeed, ty, 0.02); } } else { this.ySpeed += 0.05; this.size = this.size*0.95; if (this.size<1) { particles.splice(idx,1); } } this.x = this.x + this.xSpeed; this.y = this.y + this.ySpeed; } function Firework() { this.x = canvas2.width*(1+ 3*Math.random())/4; this.y = canvas2.height - 15; this.angle = Math.random() * Math.PI / 4 - Math.PI / 6; this.xSpeed = Math.sin(this.angle) * (6 + Math.random() * 7); this.ySpeed = -Math.cos(this.angle) * (6 + Math.random() * 7); this.hue = Math.floor(Math.random() * 360); } Firework.prototype.draw= function() { ctx2.save(); ctx2.translate(this.x, this.y); ctx2.rotate(Math.atan2(this.ySpeed, this.xSpeed) + Math.PI / 2); ctx2.fillStyle = `hsl(${this.hue}, 100%, 50%)`; ctx2.fillRect(0, 0, 5, 15); ctx2.restore(); } Firework.prototype.update= function() { this.x = this.x + this.xSpeed; this.y = this.y + this.ySpeed; this.ySpeed += 0.1; } Firework.prototype.explode= function() { for (var i = 0; i < 70; i++) { particles.push(new Particle(this.x, this.y, this.hue)); } } function lerp(a, b, t) { return Math.abs(b - a)> 0.1 ? a + t * (b - a) : b; } function getTarget() { if (targets.length > 0) { var idx = Math.floor(Math.random() * targets.length); var { x, y } = targets[idx]; targets.splice(idx, 1); x += canvas2.width / 2 - textWidth / 2; y += canvas2.height / 2 - fontSize / 2; return { x, y }; } } var canvas1=document.getElementById(’myCanvas1’); ctx1= canvas1.getContext(’2d’); var canvas2=document.getElementById(’myCanvas2’); ctx2= canvas2.getContext(’2d’); var canvas3=document.getElementById(’myCanvas3’); ctx3= canvas3.getContext(’2d’); var fontSize = 200; var fireworks = []; var particles = []; var targets = []; var fidelity = 3; var counter = 0; canvas2.width = canvas3.width = window.innerWidth; canvas2.height = canvas3.height = window.innerHeight; ctx1.fillStyle = ’#000’; var text = ’Happy New Year’; var textWidth = 999999; while (textWidth > window.innerWidth) { ctx1.font = `900 ${fontSize--}px Arial`; textWidth = ctx1.measureText(text).width; } canvas1.width = textWidth; canvas1.height = fontSize * 1.5; ctx1.font = `900 ${fontSize}px Arial`; ctx1.fillText(text, 0, fontSize); var imgData = ctx1.getImageData(0, 0, canvas1.width, canvas1.height); for (var i = 0, max = imgData.data.length; i < max; i += 4) { var alpha = imgData.data[i + 3]; var x = Math.floor(i / 4) % imgData.width; var y = Math.floor(i / 4 / imgData.width); if (alpha && x % fidelity === 0 && y % fidelity === 0) { targets.push({ x, y }); } } ctx3.fillStyle = ’#FFF’; ctx3.shadowColor = ’#FFF’; ctx3.shadowBlur = 25; function loop() { ctx2.fillStyle = 'rgba(0, 0, 0, .1)'; ctx2.fillRect(0, 0, canvas2.width, canvas2.height); counter += 1; if (counter==15) { fireworks.push(new Firework()); counter=0; } var i=fireworks.length; while (i--) { fireworks[i].draw(); fireworks[i].update(); if (fireworks[i].ySpeed > 0) { fireworks[i].explode(); fireworks.splice(i, 1); } } var i=particles.length; while (i--) {particles[i].draw(); particles[i].update(i); if (particles[i].timer >= 100 || particles[i].lightness >= 99) { ctx3.fillRect(particles[i].target.x, particles[i].target.y, fidelity + 1, fidelity + 1); particles.splice(i, 1); } } requestAnimationFrame(loop); } loop();</script></body> </html>

在瀏覽器中打開包含這段HTML代碼的html文件,可以看到在瀏覽器窗口中呈現出如圖所示的煙花綻放迎新年動畫效果。圖2中為了控制圖片的大小,刪除了大量的中間幀,因此和實際運行的效果有所不同。

JavaScript實現煙花綻放動畫效果

以上就是JavaScript實現煙花綻放動畫效果的詳細內容,更多關于JavaScript動畫效果的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
福利一区二区| 中文字幕在线高清| 国产精品av久久久久久麻豆网| 欧美交a欧美精品喷水| 国产精品久久久久av蜜臀| 欧美日韩xxxx| 国产精品日韩精品中文字幕| 91福利精品在线观看| 欧美精品三级在线| 日本va欧美va精品发布| 日本在线视频一区二区| 国产亚洲欧美日韩精品一区二区三区| 国产日韩一区二区三区在线播放| 国产精品伦一区二区| 精品国产一区二区三区性色av| 久久精品一区| 久久都是精品| 日韩精品视频网| 国产精品亚洲综合色区韩国| 久久99性xxx老妇胖精品| 久久久久久久欧美精品| 国产欧美啪啪| 精品国产不卡一区二区| 亚洲成人va| 夜夜精品视频| 日本亚州欧洲精品不卡| 国产精品一区二区三区av麻| 成人午夜在线| 国产国产精品| 日本不卡不码高清免费观看| 久久99高清| 欧美日韩国产一区二区三区不卡| 在线成人直播| 91在线成人| 97精品在线| 亚洲在线免费| 国产精品毛片视频| 久久亚洲国产| 亚洲精品一级| av最新在线| 国产一卡不卡| 激情久久久久久| 青草综合视频| 欧美不卡高清一区二区三区| 水蜜桃久久夜色精品一区的特点| 国产精品欧美日韩一区| 国产精品亚洲一区二区在线观看 | 欧美aa国产视频| 亚洲精品三级| 日本在线高清| 色8久久久久| 热三久草你在线| 免费观看日韩电影| 欧美国产极品| 91久久久精品国产| 久久国产麻豆精品| 亚洲国内精品| 国产精品一区毛片| 国产视频一区欧美| 国产日韩免费| 欧美日韩激情在线一区二区三区| 日本天堂一区| 狠狠久久婷婷| 精品国产美女a久久9999| 免费人成在线不卡| 日韩一区电影| 日韩一区二区三区精品| 日韩欧美精品一区| 日韩不卡一二三区| 欧美理论视频| 国产一区二区三区国产精品| 日韩在线一二三区| 久久天堂av| 久久的色偷偷| 在线观看一区| 99精品综合| 韩国一区二区三区视频| 日本综合精品一区| 99综合视频| 精品日韩视频| 国产一区二区精品久| 91精品麻豆| 久久99伊人| 午夜久久99| 欧洲av一区二区| 精品免费av| 国产精品亚洲成在人线| 亚洲午夜免费| 99在线精品免费视频九九视 | 国产成人免费视频网站视频社区| 亚洲v天堂v手机在线| 欧美久久精品一级c片| 欧美成人基地 | 蜜桃一区二区三区在线观看| 国产一区二区三区四区五区传媒| 亚州av一区| 亚洲一区国产一区| 亚洲福利专区| 成人久久一区| аⅴ资源天堂资源库在线| 日韩av在线播放中文字幕| 免费一级片91| 丝袜亚洲另类欧美| 日韩一区二区免费看| 婷婷亚洲综合| 999国产精品视频| 成人小电影网站| 欧美韩日一区| 精品视频黄色| 精品日韩一区| 日韩.com| 国产黄大片在线观看| 电影91久久久| 国产精品13p| 超碰99在线| 蜜桃av.网站在线观看| 日韩久久精品网| 日韩在线精品| 日本欧美不卡| 欧美福利专区| 欧美日韩精品一本二本三本| 亚洲婷婷在线| 中文一区在线| 亚洲一区二区三区四区五区午夜| 香蕉精品视频在线观看| 免费视频国产一区| 黄色亚洲在线| 久久高清免费观看| 最新亚洲国产| 欧美一区激情| 久久不见久久见中文字幕免费| 麻豆成人综合网| 国产成人精选| 精品亚洲美女网站| 欧洲激情综合| 蜜臀久久久久久久| 日韩国产精品久久久| 久久国产麻豆精品| 国产精品2023| av资源新版天堂在线| 欧美日中文字幕| 夜久久久久久| 欧美亚洲一区二区三区| 精品三级国产| 久久国产中文字幕| 亚洲永久字幕| 欧美在线看片| 97欧美在线视频| 999在线观看精品免费不卡网站| 香蕉久久国产| 欧美精品影院| av资源新版天堂在线| 2023国产精品久久久精品双| 免费在线观看不卡| 久久99精品久久久野外观看| av中文资源在线资源免费观看| 极品日韩av| 婷婷成人av| 国产成人免费精品| 欧美在线资源| 国产欧美亚洲精品a| 日韩欧美一区二区三区在线视频| 9国产精品视频| 国产精品美女午夜爽爽| 99精品视频在线| 日本va欧美va精品发布| 精品国产乱码久久久久久樱花| 成人羞羞视频播放网站| 一本色道久久精品| 久久99久久久精品欧美| 久久国产成人午夜av影院宅| 欧美专区一区二区三区| 麻豆精品在线观看| 九色精品91| 国产日产高清欧美一区二区三区| 日韩在线综合| 日韩av一二三| 日本免费一区二区三区四区| 蜜桃久久久久久| 国产成人精品福利| 亚洲综合精品四区| 精品视频在线你懂得| 欧美资源在线| 成人午夜在线| 亚洲男人在线| 成人va天堂| 日本午夜免费一区二区| 欧美中文字幕一区二区| 国产精品天堂蜜av在线播放| 欧美/亚洲一区| 国产精品17p| 亚洲一区成人| 精品国产免费人成网站| 亚洲专区视频| 久久免费国产| 国产精品久久久亚洲一区| 午夜久久美女| 超级白嫩亚洲国产第一| 午夜久久av| 免费av一区二区三区四区|