傳統進度條枯燥乏味?利用 Canvas 粒子系統打造極具科技感的動態載入效果:進度條推進伴隨彩色粒子噴發、重力下落、漸變消失。本文完整拆解這個視覺效果的核心架構與實現細節。
requestAnimationFrame 驅動 60fps 流暢動畫class LightLoader {
constructor(canvas, width, height) {
this.ctx = canvas.getContext('2d');
this.cw = width; this.ch = height;
// 進度條配置
this.loaded = 0;
this.loaderSpeed = 0.6; // 進度增量/幀
this.loaderWidth = 310;
this.loaderHeight = 10;
this.loader = { x: (width-310)/2, y: (height-10)/2 };
// 粒子配置
this.particles = [];
this.particleLift = 180; // 初始向上速度範圍
this.hueStart = 0; this.hueEnd = 120; // 色相範圍
this.gravity = 0.15;
this.particleRate = 4; // 每幀生成粒子數
}
// 主循環
loop() {
const tick = () => {
requestAnimationFrame(tick);
this.clearCanvas();
this.createParticles();
this.updateLoader();
this.updateParticles();
this.renderLoader();
this.renderParticles();
};
tick();
}
// ... 具體方法見下文
}
updateLoader() {
this.loaded = (this.loaded + this.loaderSpeed) % 100; // 循環
}
renderLoader() {
const {ctx, loader, loaderWidth, loaderHeight, loaded, hueStart, hueEnd} = this;
// 背景槽
ctx.fillStyle = '#000';
ctx.fillRect(loader.x, loader.y, loaderWidth, loaderHeight);
// 進度填充(漸變色相)
const hue = hueStart + (loaded/100) * (hueEnd - hueStart);
const w = (loaded/100) * loaderWidth;
ctx.fillStyle = `hsla(${hue}, 100%, 40%, 1)`;
ctx.fillRect(loader.x, loader.y, w, loaderHeight);
// 高光
ctx.fillStyle = '#222';
ctx.fillRect(loader.x, loader.y, w, loaderHeight/2);
}
createParticles() {
for (let i = 0; i < this.particleRate; i++) {
this.particles.push(new this.Particle());
}
}
Particle() {
// 生成位置:進度條前端附近
this.x = this.loader.x + (this.loaded/100)*this.loaderWidth + rand(-1,1);
this.y = this.ch/2 + rand(0, this.loaderHeight) - this.loaderHeight/2;
// 隨機速度
this.vx = (rand(0,4)-2)/100;
this.vy = (rand(0, this.particleLift) - this.particleLift*2)/100;
this.width = this.height = rand(1,4)/2;
this.hue = this.hue; // 當前進度色相
}
update(i) {
this.vx += (rand(0,6)-3)/100; // 水平擴散
this.vy += this.gravity; // 重力
this.x += this.vx;
this.y += this.vy;
if (this.y > this.ch) this.particles.splice(i, 1); // 超出移除
}
render() {
ctx.fillStyle = `hsla(${this.hue}, 100%, ${rand(50,70)}%, ${rand(20,100)/100})`;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
clearCanvas() {
this.ctx.globalCompositeOperation = 'source-over';
this.ctx.clearRect(0, 0, this.cw, this.ch);
this.ctx.globalCompositeOperation = 'lighter'; // 疊加發光效果
}
lighter 模式讓粒子重疊處顏色相加,營造發光感。
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame']
|| window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = (cb) => {
var curr = Date.now();
var delay = Math.max(0, 16 - (curr - lastTime));
lastTime = curr + delay;
return setTimeout(() => cb(curr + delay), delay);
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = (id) => clearTimeout(id);
}
function isCanvasSupported() {
var elem = document.createElement('canvas');
return !!(elem.getContext && elem.getContext('2d'));
}
if (isCanvasSupported()) {
var c = document.createElement('canvas');
c.width = 400; c.height = 100;
document.body.appendChild(c);
var loader = new LightLoader(c, c.width, c.height);
setupRAF(); // 兼容性封裝
loader.loop();
}
xhr.onprogress、fetch + ReadableStream)arc)、圖片、文字、自定義 Path2DglobalCompositeOperation 狀態變更最小化這個粒子進度條展示了 Canvas 程序化動畫的迷人魅力:簡單的物理規則 + 色彩理論 + 高頻循環 = 豐富的涌現視覺效果。掌握此模式,可快速構建載入動畫、數據可視化過渡、遊戲特效等場景。