用程式碼模擬煙花,是很多人學習 JavaScript 動畫的第一個目標。這個範例用 150 個粒子組成一朵煙花,每個粒子有自己的速度向量,加上重力加速度與隨機風向的影響,就能得到相當自然的爆炸與散落效果。
每個煙花粒子是一個 Fire 物件,記錄位置、三軸速度、質量與外觀。原型上掛了四個方法:append 掛到 DOM、setSize 調整大小、setPosition 更新座標、setVisible 控制顯示。
動畫迴圈每 30 毫秒執行一次,對每個粒子施加重力與風力,更新座標後重繪。粒子超出畫布範圍就重置回發射點重新發射,形成連續煙火。風向本身也會每隔一段時間隨機改變,讓畫面更有變化。
<!DOCTYPE html>
<html>
<head>
<title>超炫網頁煙花效果</title>
<style type="text/css">
.fire { display: block; overflow: hidden; font-size: 12px; position: absolute; }
body, html { overflow: hidden; background: #000; }
</style>
</head>
<body>
<script type="text/javascript">
var Fire = function (r, color) {
this.radius = r || 12;
this.color = color;
this.xpos = 0;
this.ypos = 0;
this.zpos = 0;
this.vx = 0;
this.vy = 0;
this.vz = 0;
this.mass = 1;
this.x = 0;
this.y = 0;
this.p = document.createElement("span");
this.p.className = "fire";
this.p.innerHTML = "*";
this.p.style.fontSize = this.radius + "px";
this.p.style.color = "#" + this.color;
};
Fire.prototype = {
append: function (parent) {
parent.appendChild(this.p);
},
setSize: function (scale) {
this.p.style.fontSize = this.radius * scale + "px";
},
setPosition: function (x, y) {
this.p.style.left = x + "px";
this.p.style.top = y + "px";
},
setVisible: function (b) {
this.p.style.display = b ? "block" : "none";
}
};
var fireworks = function () {
var fires = new Array();
var count = 150; // 粒子數量
var gravity = .5; // 重力加速度
var timer;
// 隨機風向與風力
var wind = ((Math.floor(Math.random() * 3) + 3) / 10)
* (Math.random() * 2 - 1 > 0 ? 1 : -1) * .25;
var wpos = 0;
var wcount;
return {
init: function () {
wcount = 50 + Math.floor(Math.random() * 100);
for (var i = 0; i < count; i++) {
// 隨機產生顏色,並補零湊足六位
var color = (Math.random() * 0xFFFFFF).toString(16).split(".")[0];
while (color.length < 6) { color = "0" + color; }
var fire = new Fire(12, color);
fires.push(fire);
fire.ypos = -100;
fire.vz = Math.random() * 6 - 3;
fire.vx = (Math.random() * 2 - 1) * 2;
fire.vy = Math.random() * -15 - 15;
fire.x = 500;
fire.y = 600;
fire.append(document.body);
}
var that = this;
timer = setInterval(function () {
// 週期性改變風向
wpos++;
if (wpos >= wcount) {
wpos = 0;
wcount = 50 + Math.floor(Math.random() * 100);
wind = ((Math.floor(Math.random() * 3) + 3) / 10)
* (Math.random() * 2 - 1 > 0 ? 1 : -1) * .25;
}
for (var i = 0; i < count; i++) {
that.move(fires[i]);
}
}, 30);
},
// 物理更新:施加重力與風力後重繪
move: function (fire) {
fire.vy += gravity;
fire.x += fire.vx;
fire.y += fire.vy;
fire.vx += wind;
fire.setPosition(fire.x, fire.y);
// 超出畫布則重置
if (fire.x < 0 || fire.x > 1000 || fire.y < 0 || fire.y > 600) {
fire.vx = (Math.random() * 2 - 1) * 2;
fire.vy = Math.random() * -15 - 15;
fire.x = 500;
fire.y = 600;
fire.setPosition(fire.x, fire.y);
}
}
};
};
fireworks().init();
</script>
</body>
</html>
用 150 個 span 元素搭配 setInterval 逐帧改 left / top,每次更新都會觸發瀏覽器的重排(reflow),在一般電腦上尚可,但手機或低階裝置會明顯掉幀。若要正式使用,建議這樣改:
1. 改用 Canvas 2D 或 WebGL:直接在 canvas 上 fillRect 繪製粒子,不必建立上百個 DOM 節點,效能差距可達數十倍;
2. 用 requestAnimationFrame 取代 setInterval:讓動畫與螢幕刷新同步,並在分頁隱藏時自動暫停,不會浪費資源;
3. 時間差積分:把速度與位置的更新乘上實際經過的時間,而不是固定值,這樣在不同刷新率的裝置上就不會出現速度快慢不一;
4. 尊重 prefers-reduced-motion:強烈的閃光與快速移動對部分使用者不友善,偵測到該偏好時應直接跳過動畫;
5. 座標不要寫死:範例中的 500、600、1000 是硬編碼的畫布尺寸,實作時應改用 window.innerWidth 與 innerHeight,並在視窗縮放時重新計算。
希望本文所述對大家的 JavaScript 程式設計有所幫助。