本文為大家帶來了一款,免費又安全環保的HTML5 Canvas實現的放煙火特效。
效果如下:
程式碼如下:
XML/HTML Code複製內容到剪貼簿
- HTML>
-
html>
-
頭>
-
標題>Canvas 🎜>標題>
元 字符集=字符集>
元 -UA-相容" 內容="IE=edge"> 🎜>
元 -
名稱="視口" 內容=「寬度=設備寬度,高度=設備高度,初始比例=1.0,最大比例=1.0 ,user-scalable =no">
樣式
- 類型>
html,body{高度:100%;邊距:0;填色:0}
ul,li{text-indent:0;text-decoration:none;margin:0;padding:0}
img{border:0}
- 正文{背景顏色:#000;顏色:#999;字體:100%/18px helvetica、 arial、 sans-serif}
- 畫布{遊標:十字準線;顯示:塊;左:0;位置:絕對;頂部:0;z-index:20}
- #header img{width:100%; 高度:20%;}
- #bg img{寬度:100%; 高度:80%;}
- #header,#bg{position:fixed;left:0;right:0;z-index:10}
- #header{top:0}
- #bg{position:fixed;z-index:1;bottom:0}
- 音訊{position:fixed;display:none;bottom:0;left:0;right:0;width:100%;z-index:5}
-
樣式>
-
頭>
-
身體>
-
div id=id=id
=- 🎜>>
img id id = 🎜>
-
src="http://img.ivsky.com/img/tupian/pre/201508/02/yuzhou_xingkong jpg">
-
div>
腳本 src=
src-
= /cdn.bootcss.com/jquery/2.2.0/jquery.min.js">腳本 >
- 腳本 >
>- 腳本 > >腳本 > >腳本 >
-
腳本>-
- $(function(){
var 煙火 = 函數
- (){
var
- 自我 =
- 這個; 這個;
//產生煙火隨機數
-
var rand = 函數(rMi,
//按位取反練習
return ~~((Math.random()*(rMa-rMi 1)) rMi);
},hitTest = 函數(x1, h2){
返回! (x1 w1
- x2 || x2 🎜>x1 || y1 h1 y1);
};
- //請求動畫影格
-
window.requestAnimFrame=函數(){ >
返回 window.requestAnimationFrame -
||window.webkitRequestAnimationFrame -
||window.mozRequestAnimationFrame -
||window.oRequestAnimationFrame -
||window.msRequestAnimationFrame -
||函數(回呼){ -
window.setTimeout(callback,1000/60); -
} -
}();
-
self.init = 函數(){>函數
- (){
self.canvas = 文件..
//canvas 全螢幕 -
-
selfself.canvas.width = self
self
self.canvas.height-
= ).innerHeight();
self.articles = [];
-
self.partCount = 150
- self.fireworks = [];
self
- self.mx = self.cw/2;
self
- self.my = self.ch/2; = self.ch/2;
- self.currentHue = 30;
- self.partSpeed = 5; 🎜>;
self.partSpeedVariance
- = 10
self.partWind
- = 50;
self.partFriction =
- 5;
self.partGravity = 1
- ;
self.hueMin = 0;
- self.hueMax = 360;
- self.fworkSpeed = 4;
self.fworkAccel
- = 10;
self.hueVariance =
- 30;
self.flickerDensity = 25
-
self.showShockwave = true
;
-
self.showTarget = false self.clearAlpha = 25;
- $(document.body).append(self.canvas);
-
selfself.ctx = self.canvas.getContext('2d');
-
self.ctx.lineCap = '圓形';
-
self.ctx.lineJoin = '圓形';
-
self.lineWidth = 1;
-
self.bindEvents();
-
self.canvasLoop();
-
self.canvas.onselectstart = 函數
回傳 false; -
}; -
}; -
// 建立包裝藥 -
-
self.createParticles = 函數(x,p
var
倒數-
= self.partCount;
while(倒數--){
- var
newParticle-
= {
x: x,
- y: y,
- coordLast: [
- {x: x, y: y},
- {x: x, y: y},
- {x: x, y: y}
- ],
- 角度:rand(0, 360),
- 速度: rand(((self.partSpeed - self.partSpeedVariance)
摩擦力:1 - self.partFriction/100,
- 重力:self.partGravity/2,
- 色調:rand(hue-self.hueVariance, 色調 self.hueVariance),
- 亮度:rand(50, 80),
- alpha:蘭特(40,100)/100,
- 衰變:rand(10, 50)/1000,
- 風:(rand(0, self.partWind) - (self.partWind/2))/25,
- lineWidth: self.lineWidth
- };
- self.articles.push(newParticle);
- }
- };
- // 更新彈藥
-
self.updateParticles = 函數(
var -
i = self.self.
while(i--){ -
var -
p = self var 弧度
- = p.
var vx =
數學 var vy = 數學
p.速 *= p.摩擦力; -
p.coordLast[2].x = p<.>
- p.coordLast[2].y
- = p
p.coordLast[1].x =
p<.>-
p.coordLast[1].y = p<.>
p.coordLast[0]-
.x = p
p.coordLast[0]
- .y = p
p.x = vx;
-
p.y = vy;
p.y = p.gravity;
p.angle = p.wind;
-
p.alpha -= p if(!hitTest(0,0,self.cw,self.ch,p.x-p.radius, p.y-p.radius, p.radius*2, p.radius*2) .alpha
-
- .05){
-
self.articles.splice(i, 1); -
} -
};
};
- // 均勻劑量
-
self.drawParticles = 函數(
var -
i = self.self
. -
while(i--){ -
var p = self
.articles -
var coordRand
= (rand(1,3)-1); -
self.ctx.beginPath(); -
self.ctx.moveTo(Math.round(p.coordLast[coordRand].x), Math.round(p.coordLast[coordRand].y)); -
self.ctx.lineTo(Math.round(p.x), Math.round(p.y)); -
self.ctx.closePath(); -
self.ctx.strokeStyle = 'hsla('
-
self.ctx.中風();
-
if(self.flickerDensity > 0){
-
var inverseDensity = 50 =
- if(rand(0, inverseDensity) === inverseDensity){
- self.ctx.beginPath();
- self.ctx.arc(Math.round(p.x), Math.round(p.y), rand(p.lineWidth,p.lineWidth 3)/2, 0,205)
- self.ctx.closePath();
-
var randrandAlpha = rand(50,1000)/1000;
-
self.ctx.fillStyle = ? .亮度'%,'randAlpha')';
self.ctx.fill(); -
} -
} -
}; -
};
- // 創建煙火
-
self.createFireworks = 函數
var
- newFirework = {
x: startX,
-
y:開始Y,
-
startX: startX,
-
startY: startY,
-
hitX:假,
-
hitY:假,
-
coordLast: [
-
{x: startX, y: startY},
-
{x: startX, y: startY},
-
{x: startX, y: startY}
-
],
-
targetX: targetX,
-
targetY: targetY,
-
速度:self.fworkSpeed,
-
角度:Math.atan2(targetY - startY, targetX - startX),
-
shockwaveAngle: Math.atan2(targetY - startY, targetX - startX) (90*(Math.PI/180)),
-
加速度:self.fworkAccel/100, -
色調:self.currentHue, -
亮度:rand(50, 80), -
alpha:蘭特(50,100)/100, -
lineWidth: self.lineWidth -
}; -
self.fireworks.push(newFirework); -
};
- //更新煙火
-
self.updateFireworks = 函數(
var -
i = self .
while(i--){ -
var -
f = self.
- self.ctx.lineWidth = f.lineWidth;
- vx = 數學
vy
= -
.sin(.>.sin(f..angle);
f.speed *= 1 f.acceleration;
-
f.coordLast[2]
- .x = ff
-
f.coordLast[2].y = f
-
f.coordLast[1].x = f
f-
f.coordLast[1].y =
f-
f.coordLast[0].x =
f-
f.coordLast[0].y = f
-
if(f.startX >
= f.targetX){ -
= f.targetX){ = f.targetX){
if(f.x vx - = f.targetX){ = f.targetX){ = f.targetX){
= f.targetX){ -
ff.x = f.targetX;
-
f.hitX- =
true- ;
- } 其他 {
-
f.x = vx;
}
-
} 其他 {
if(f.x vx >
= f.targetX){ -
= f.targetX){ = f.targetX){ = f.targetX){
f- f.x
= f.targetX; -
- f.hitX = true;
} 其他 {
f.x = vx;
}
- }
-
if(f.startY >= f.targetY){
= f.targetY){ -
= f.targetY){
if(f.y vy
= f.targetY){
ff.y = f.targetY; -
f.hitY = true
;
- } 其他 {
- f.y = vy;
- }
- } 其他 {
-
if(f.y vy >= f.targetY){ >
-
ff.y = f.targetY;
-
f.hitY = true;
- } 其他 {
- f.y = vy;
- }
- }
- if(f.hitX && f.hitY){
- self.createParticles(f.targetX, f.targetY, f.hue);
- self.fireworks.splice(i, 1);
- }
- };
- };
- // 均勻煙火
-
self.drawFireworks = 函數(){
var -
i = self .
-
self.ctx.globalCompositeOperation = '更輕' = '更輕';
-
while(i--){
-
var f = self.
-
self.ctx.lineWidth = f.lineWidth;
-
var coordRand = (rand(1,3)-1);
- self.ctx.beginPath();
- self.ctx.moveTo(Math.round(f.coordLast[coordRand].x), Math.round(f.coordLast[coordRand].y));
- self.ctx.lineTo(Math.round(f.x), Math.round(f.y));
- self.ctx.closePath();
-
self.ctx.strokeStyle = 'hsla(' =
- self.ctx.中風();
- if(self.showTarget){
- self.ctx.save();
- self.ctx.beginPath();
-
self.ctx.arc(Math.round(f.targetX), Math.round(f.targetY), rand
- self.ctx.closePath();
- self.ctx.lineWidth = 1;
- self.ctx.中風();
- self.ctx.restore();
- }
- if(self.showShockwave){
- self.ctx.save();
- self.ctx.translate(Math.round(f.x), Math.round(f.y));
- self.ctx.rotate(f.shockwaveAngle);
- self.ctx.beginPath();
- self.ctx.arc(0, 0, 1*(f.speed/5), 0, Math.PI, true);
-
self.ctx.strokeStyle = 'hsla(' = 'hsla('
= - 'hsla('
self.ctx.lineWidth- = f.lineWidth;
- self.ctx.中風();
- self.ctx.restore();
- }
- };
- };
-
// 綁定事件
self.bindEvents =
函數- ()
- $(window).on('resize', function(){
-
clearTimeout(self.timeout);
self.timeout =
setTimeout-
setTimeout
selfself.canvas.width
=o
selfself.canvas.height self ).innerHeight(); -
self.ctx.lineCap = '圓形' =
'圓形'-
;
self.ctx.lineJoin =
'圓形'- = '圓形'
- ;
}, 100);
});
- $(self.canvas).on('mousedown', function(e){
-
self.mx = e.pageX - self.canvas.offsetLeft;
-
self.my = e.pageY - self.canvas.offsetTop;
-
self.currentHue = rand(self.hu>rand
- (self.hu
- self.createFireworks(self.cw/2, self.ch, self.mx, self.my);
- $(self.canvas).on('mousemove.fireworks', function(e){
self.mx
- = e.pageX - self.canvas.offsetLeft;
self.my
- = e.pageY - self.canvas.offsetTop;
self.currentHue = rand
-
rand
-
- self.createFireworks(self.cw/2, self.ch, self.mx, self.my);
- });
- });
- $(self.canvas).on('mouseup', function(e){
- $(self.canvas).off('mousemove.fireworks');
- });
};
self.clear
- = 函數(){🎜>函數
(){ >
-
self.articles = [];
-
self.fireworks- = [];
-
self.ctx.clearRect(0, 0, self.cw, self.ch);
};
self.canvasLoop- =
函數-
()
requestAnimFrame(self.canvasLoop, self.canvas);
self.ctx.globalCompositeOperation-
= 'destination-out
- self.ctx.fillStyle =
- 'rgba(0,0,0,00,0,0,000 100 ')';
- self.ctx.fillRect(0,0,self.cw,self.ch);
- self.updateFireworks();
- self.updateParticles();
- self.drawFireworks();
- self.drawParticles();
- };
self.init();
}
-
var fworks = new
$('#info-toggle').on('click', function(e){ -
$('#info-inner').stop(false, true).slideToggle(100); -
e.preventDefault(); -
}); -
}); -
-
腳本>
- 畫布 寬度=寬度= 🎜> 身高="449">> >
-
身體>
-
html>
不是被HTML5強大的效果驚呆了,一飽眼福了吧。