每到歲末年初,總會聯想到綻放在夜空裡的絢爛煙火。既然年節將至,那就不如動手用 Canvas 做一場屬於自己的煙火秀吧!這篇文章將帶你一步步實作出可互動的煙火特效,就連用滑鼠點擊畫面,也能瞬間引爆一朵絢麗的火花。
先說個要注意的地方:每一朵煙火炸開後,會散出大約 30 到 200 個不等的碎屑粒子。如果同時引爆太多煙火,畫面上的粒子總數就會暴增,頁面自然會變得卡頓。由於這只是個好玩的小實驗,我並沒有特別針對效能做過度優化,想玩得盡興的話記得節制一下囉。
整個效果的核心概念其實很單純,可以拆成兩個部分來理解:
首先我們設計一個「煙火類別」與一個「碎屑類別」。煙火實體化之後會朝設定的目標點飛升,一旦抵達指定位置,就把它的 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」搭配 getImageData 這兩個利器。
所謂離屏 canvas,顧名思義就是一個不顯示在畫面上的隱形畫布。只要在 JavaScript 裡用 document.createElement("canvas") 建立一個 canvas DOM 物件,並且不要把它掛到 body 上,它就成為了一塊看不見的畫布。我們可以取得它的 context,在觀眾看不到的地方從容地進行各種運算。
具體流程是:先把文字或圖片畫到離屏 canvas 上,再用 getImageData 取出畫布的像素陣列,接著遍歷這個陣列,把帶有顏色的像素(也就是我們真正想要的輪廓)篩選並保存起來,最後再將這些座標套用到主 canvas 上呈現出來即可。
原始碼可參考:https://github.com/whxaxes/canvas-test/tree/gh-pages/src/Funny-demo/shotFire