基于JavaScript實現(xiàn)在線網(wǎng)頁煙花效果
實現(xiàn)效果
關(guān)鍵步驟
隨機顏色
var hue = Math.random() * 360;
var hueVariance = 30;
function setupColors(p){ p.hue = Math.floor(Math.random() * ((hue + hueVariance) - (hue - hueVariance))) + (hue - hueVariance); p.brightness = Math.floor(Math.random() * 21) + 50; p.alpha = (Math.floor(Math.random() * 61) + 40) / 100; }
隨機發(fā)射槍口
for (var i = 0; i < count; i++) {
//角度
var angle = 360 / count * i;
//弧度
var radians = angle * Math.PI / 180;
var p = {}; p.x = x; p.y = y; p.radians = radians; //大小 p.size = Math.random()*2+1; //速度 p.speed = Math.random()*5+.4; //半徑 p.radius = Math.random()*81+50; p.fx = x + Math.cos(radians) * p.radius; p.fy = y + Math.sin(radians) * p.radius; setupColors(p); particles.push(p); }
每幀更新調(diào)用
//requestAnimationFrame
var lastStamp = 0;
function tick(opt=0) {
if(opt-lastStamp>2000){
lastStamp=opt;
createFireworks(Math.random()*canvas.width,Math.random()*canvas.height);
}
context.globalCompositeOperation = 'destination-out'; context.fillStyle ='rgba(0,0,0,'+10/100+')'; context.fillRect(0,0,canvas.width,canvas.height); context.globalCompositeOperation = 'lighter'; drawFireworks(); requestAnimationFrame(tick); } tick();
源碼
javascript: !(function () { var isDebug = false; var textCanvas = document.createElement("canvas"); textCanvas.width=1000; textCanvas.height=300; if(isDebug){ textCanvas.style.position="absolute"; textCanvas.style.zIndex="9999"; document.body.appendChild(textCanvas); } var textctx = textCanvas.getContext("2d"); textctx.fillStyle = "#000000"; textctx.fillRect(0,0,textCanvas.width,textCanvas.height); var canvas = document.createElement("canvas"); document.body.appendChild(canvas); canvas.style.position= "fixed"; canvas.style.left = "0"; canvas.style.top = "0"; canvas.style.zIndex = -1; var context = canvas.getContext("2d"); function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; clearCanvas(); } function clearCanvas() { context.fillStyle = "#000000"; context.fillRect(0, 0, canvas.width, canvas.height); } resizeCanvas(); window.addEventListener("resize", resizeCanvas); function mouseDownHandler(e) { var x = e.clientX; var y = e.clientY; //發(fā)射的文字 createFireworks(x, y,["1024","程序員節(jié)日快樂","去死吧 bug!"][Math.floor(Math.random()*3)]); } document.addEventListener("mousedown", mouseDownHandler); var particles = []; function createFireworks(x, y,text="") { var hue = Math.random() * 360; var hueVariance = 30; function setupColors(p){ p.hue = Math.floor(Math.random() * ((hue + hueVariance) - (hue - hueVariance))) + (hue - hueVariance); p.brightness = Math.floor(Math.random() * 21) + 50; p.alpha = (Math.floor(Math.random() * 61) + 40) / 100; } if(text!=""){ var gap = 6; var fontSize = 120; textctx.font=fontSize+"px _sans"; textctx.fillStyle = "#ffffff"; var textWidth = Math.ceil(textctx.measureText(text).width); var textHeight = Math.ceil(fontSize*1.2); textctx.fillText(text,0,fontSize); var imgData = textctx.getImageData(0,0,textWidth,textHeight); if(isDebug)context.putImageData(imgData,400,300) textctx.fillStyle = "#000000"; textctx.fillRect(0,0,textCanvas.width,textCanvas.height); for (var h = 0; h < textHeight; h+=gap) { for(var w = 0; w < textWidth; w+=gap){ var position = (textWidth * h + w) * 4; var r = imgData.data[position], g = imgData.data[position + 1], b = imgData.data[position + 2], a = imgData.data[position + 3]; if(r+g+b==0)continue; var p = {}; p.x = x; p.y = y; p.fx = x + w - textWidth/2; p.fy = y + h - textHeight/2; p.size = Math.floor(Math.random()*2)+1; p.speed = 1; setupColors(p); particles.push(p); } } }else{ var count = 100; for (var i = 0; i < count; i++) { //角度 var angle = 360 / count * i; //弧度 var radians = angle * Math.PI / 180; var p = {}; p.x = x; p.y = y; p.radians = radians; //大小 p.size = Math.random()*2+1; //速度 p.speed = Math.random()*5+.4; //半徑 p.radius = Math.random()*81+50; p.fx = x + Math.cos(radians) * p.radius; p.fy = y + Math.sin(radians) * p.radius; setupColors(p); particles.push(p); } } } function drawFireworks() { clearCanvas(); for (var i = 0; i < particles.length; i++) { var p = particles[i]; p.x += (p.fx - p.x)/10; p.y += (p.fy - p.y)/10-(p.alpha-1)*p.speed; p.alpha -= 0.006; if (p.alpha<=0) { particles.splice(i, 1); continue; } context.beginPath(); context.arc(p.x, p.y, p.size, 0, Math.PI * 2, false); context.closePath(); context.fillStyle = 'hsla('+p.hue+',100%,'+p.brightness+'%,'+p.alpha+')'; context.fill(); } } //requestAnimationFrame var lastStamp = 0; function tick(opt=0) { if(opt-lastStamp>2000){ lastStamp=opt; createFireworks(Math.random()*canvas.width,Math.random()*canvas.height); } context.globalCompositeOperation = 'destination-out'; context.fillStyle ='rgba(0,0,0,'+10/100+')'; context.fillRect(0,0,canvas.width,canvas.height); context.globalCompositeOperation = 'lighter'; drawFireworks(); requestAnimationFrame(tick); } tick(); })();
背景音樂自動播放
audio id="bgmusic" src="./bgm.mp3" autoplay="autoplay" loop="loop" style="display: block; width: 3%; height:3%;"></audio> <script type="text/javascript"> function toggleSound() { var music = document.getElementById("bgmusic");//獲取ID console.log(music); console.log(music.paused); if (music.paused) { //判讀是否播放 music.paused=false; music.play(); //沒有就播放 } } setInterval("toggleSound()",1); </script>
到此這篇關(guān)于基于JavaScript實現(xiàn)在線網(wǎng)頁煙花效果的文章就介紹到這了,更多相關(guān)JavaScript煙花內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Bootstrap Table服務器分頁與在線編輯應用總結(jié)
這篇文章主要介紹了Bootstrap Table服務器分頁與在線編輯應用總結(jié) 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08一個超簡單的JS拖拽實現(xiàn)代碼(兼容IE,Firefox)
網(wǎng)上找的一個超簡單的JS拖拽,喜歡拖拽效果的朋友可以參考下。2010-04-04JavaScript基本語法_動力節(jié)點Java學院整理
這篇文章主要介紹了JavaScript基本語法,適合剛?cè)腴T的同學,有興趣的可以了解下。2017-06-06JS獲取月份最后天數(shù)、最大天數(shù)與某日周數(shù)的方法
這篇文章主要介紹了JS獲取月份最后天數(shù)、最大天數(shù)與某日周數(shù)的方法,涉及JavaScript針對日期與實現(xiàn)的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12JavaScript實現(xiàn)點擊按鈕切換網(wǎng)頁背景色的方法
這篇文章主要介紹了JavaScript實現(xiàn)點擊按鈕切換網(wǎng)頁背景色的方法,涉及JavaScript基于鼠標事件動態(tài)操作頁面元素樣式的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10