DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5特效代碼
HTML5 Canvas 火焰特效:跟著滑鼠跑的粒子火焰
編輯:HTML5特效代碼     

點擊這裡查看效果:https:///keleyi/phtml/html5/16.htm

這個範例用 JavaScript 驅動 Canvas,在螢幕上畫出一團持續跳動的火焰。它由上百個粒子組成,每個粒子從中心向外飄散、半徑與亮度隨生命週期遞減,全部疊在一起就形成了火焰的形狀。最有趣的地方是火焰會跟著滑鼠游標移動。

完整程式碼

把以下程式碼存成 HTML 檔開啟即可看到效果:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>HTML5 Canvas 實驗:一個很酷的火焰效果</title>
<style type="text/css">
body { margin: 0; padding: 0; background: #000; }
#canvas-keleyi-com { display: block; }
</style>
</head>
<body>
<canvas id="canvas-keleyi-com"></canvas>
<script type="text/javascript">
window.onload = function () {
var keleyi_canvas = document.getElementById("canvas-kel" + "eyi-com");
var ctx = keleyi_canvas.getContext("2d");
var W = window.innerWidth, H = window.innerHeight;
keleyi_canvas.width = W;
keleyi_canvas.height = H;

var particles = [];
var mouse = {};

// 先產生一批粒子
var particle_count = 100;
for (var i = 0; i < particle_count; i++) {
particles.push(new particle());
}

keleyi_canvas.addEventListener('mousemove', track_mouse, false);

function track_mouse(e) {
mouse.x = e.pageX;
mouse.y = e.pageY;
}

function particle() {
// 速度:x 方向左右飄散,y 方向一律向上
this.speed = { x: -2.5 + Math.random() * 5, y: -15 + Math.random() * 10 };

// 起始位置跟著滑鼠;沒有滑鼠座標時落在畫面中央
if (mouse.x && mouse.y) {
this.location = { x: mouse.x, y: mouse.y };
} else {
this.location = { x: W / 2, y: H / 2 };
}

this.radius = 10 + Math.random() * 20; // 半徑 10~30
this.life = 20 + Math.random() * 10; // 生命週期 20~30 格
this.remaining_life = this.life;

// 隨機顏色
this.r = Math.round(Math.random() * 255);
this.g = Math.round(Math.random() * 255);
this.b = Math.round(Math.random() * 255);
}

function draw() {
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "black";
ctx.fillRect(0, 0, W, H);

// lighter 讓粒子堆疊處更亮,這是火焰感的關鍵
ctx.globalCompositeOperation = "lighter";

for (var i = 0; i < particles.length; i++) {
var p = particles[i];
ctx.beginPath();

// 透明度隨剩餘生命線性衰減
p.opacity = Math.round(p.remaining_life / p.life * 100) / 100;

var gradient = ctx.createRadialGradient(
p.location.x, p.location.y, 0,
p.location.x, p.location.y, p.radius);
gradient.addColorStop(0, "rgba(" + p.r + ", " + p.g + ", " + p.b + ", " + p.opacity + ")");
gradient.addColorStop(0.5, "rgba(" + p.r + ", " + p.g + ", " + p.b + ", " + p.opacity + ")");
gradient.addColorStop(1, "rgba(" + p.r + ", " + p.g + ", " + p.b + ", 0)");

ctx.fillStyle = gradient;
ctx.arc(p.location.x, p.location.y, p.radius, Math.PI * 2, false);
ctx.fill();

// 更新狀態
p.remaining_life--;
p.radius--;
p.location.x += p.speed.x;
p.location.y += p.speed.y;

// 生命耗盡就重新生成一顆,形成源源不絕的效果
if (p.remaining_life < 0 || p.radius < 0) {
particles[i] = new particle();
}
}
}

setInterval(draw, 86);
};
</script>
</body>
</html>

火焰是怎麼做出來的

關鍵有三個。第一是 globalCompositeOperation = "lighter",這種加法混合模式讓重疊的粒子亮度相加,中心自然會燒到接近白色,外圍則是暗紅,正是真實火焰的明暗分佈。

第二是每顆粒子都用 createRadialGradient 畫出由實心到透明的放射漸層,邊緣才不會出現生硬的圓形輪廓。

第三是「死亡即重生」的設計:粒子的 remaining_life 與 radius 每幀遞減,歸零就立刻換成一顆全新的粒子。因為初始半徑、速度與顏色都是隨機的,整體看起來就不會有機械式的規律感。

可以怎麼調整

想要火燒得更旺,就提高 particle_count;想要火焰竄得更高,把 speed.y 的負值加大;想改成偏暖的紅橙色,就不要用完全隨機的 RGB,而是在色相 0–40 之間取值、提高飽和度。

效能方面,setInterval(draw, 86) 換成 requestAnimationFrame 會更順;粒子數量破千時,建議改用較小的 canvas 或降低更新率,行動裝置尤其需要留意這點。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved