想把自己的 HTML5 練習作品整理成一頁作品集,但單純把 DEMO 一條條排下去實在不好看。既然學了 canvas,不如讓它派上用場——做一段開場動畫。而動畫的主角,選粒子。
粒子有趣的地方在於,你只需要一點點中學程度的物理知識,就能撐起相當有戲劇性的效果:加速、減速、自由落體、阻尼。公式很小,樂趣很大;順帶還能練一下把物理直覺翻成程式碼的能力。
整段動畫裡,每個粒子要嘛被「吸」向目標點,要嘛進入自由落體。先來看粒子的屬性:
var Dot = function(x, y, vx, vy, tox, toy, color) {
this.x = x; // 目前位置
this.y = y;
this.vx = vx; // 水平速度
this.vy = vy; // 垂直速度
this.nextox = tox; // 暫存用
this.nextoy = toy;
this.color = color;
this.visible = true;
this.globleDown = false;
this.setEnd(tox, toy);
}
setEnd: function(tox, toy) {
this.tox = tox; // 目標座標
this.toy = toy;
var yc = this.toy - this.y;
var xc = this.tox - this.x;
}
x、y 是位置,vx、vy 是速度,tox、toy 是這一顆粒子要飛去的地方。nextox 這類欄位只是暫存,知道用途就好。
先把加速度定下來。這裡用一個常數 za 當作「吸引力強度」,數值需要自己試,範例用 20 手感大致剛好。由於粒子與目標點的連線方向一直在變,所以水平與垂直加速度必須每一幀重新算一次,用最基本的三角關係做向量分解即可:
var yc = this.toy - this.y;
var xc = this.tox - this.x;
this.jl = Math.sqrt(xc * xc + yc * yc); // 兩點距離
var za = 20;
var ax = za * (xc / this.jl),
ay = za * (yc / this.jl);
有了 ax、ay,接著更新速度。這裡乘上 0.97 是為了模擬能量損耗,讓粒子逐漸減速、不會永遠彈跳:
vx = (this.vx + ax * time) * 0.97, vy = (this.vy + ay * time) * 0.97;
time 是這一幀與上一幀的時間差。用時間差而不是固定值,是為了讓不同刷新率的裝置跑出一致的手感。最後更新位置:
this.x += this.vx * time; this.y += this.vy * time;
把這幾段組進 update 方法,就得到完整的運動邏輯:
update: function(time) {
this.x += this.vx * time;
this.y += this.vy * time;
if (!this.globleDown && this.y > 0) {
var yc = this.toy - this.y;
var xc = this.tox - this.x;
this.jl = Math.sqrt(xc * xc + yc * yc);
var za = 20;
var ax = za * (xc / this.jl),
ay = za * (yc / this.jl),
vx = (this.vx + ax * time) * 0.97,
vy = (this.vy + ay * time) * 0.97;
this.vx = vx;
this.vy = vy;
} else {
// 進入自由落體
var gravity = 9.8;
var vy = this.vy + gravity * time;
if (this.y > canvas.height) {
vy = -vy * 0.7; // 落地反彈,係數 0.7 讓每次彈得比上次低
}
this.vy = vy;
}
}
globleDown 是個全域開關:一旦切換,所有粒子就從「被吸引」進入「受重力自由落體」,落到畫布底端再逐次衰減地反彈。整段動畫的敘事感,就是靠這個開關切出來的。
粒子的目的地座標從哪來?答案是:先在一張看不見的離屏 canvas 上把文字或圖畫好,再把像素掃一遍。
this.osCanvas = document.createElement("canvas");
var osCtx = this.osCanvas.getContext("2d");
this.osCanvas.width = 1000;
this.osCanvas.height = 150;
osCtx.textAlign = "center";
osCtx.textBaseline = "middle";
osCtx.font = "70px 微軟雅黑, 黑體 bold";
osCtx.fillStyle = "#1D181F";
osCtx.fillText("WelCome", this.osCanvas.width / 2, this.osCanvas.height / 2 - 40);
osCtx.fillText("To wAxes' HOME", this.osCanvas.width / 2, this.osCanvas.height / 2 + 40);
var bigImageData = osCtx.getImageData(0, 0, this.osCanvas.width, this.osCanvas.height);
getImageData 拿到的 data 是一串 RGBA 的平坦陣列,每四個值代表一個像素。因此只要檢查每個像素的 alpha 值(索引 +3)是否足夠不透明,就能判斷那個位置有沒有被畫到。同時,R、G、B 三個值也正好可以拿來當粒子的顏色,讓粒子拼回去之後顏色與原圖一致。
這裡有個實務上的細節:如果每個像素都生一顆粒子,數量動輒上萬,頁面很快就會卡住。範例的做法是把 x 與 y 的掃描步長設為 2,粒子數量立刻降到四分之一,視覺上幾乎看不出差別。
for (var x = 0; x < bigImageData.width; x += 2) {
for (var y = 0; y < bigImageData.height; y += 2) {
var i = (y * bigImageData.width + x) * 4;
if (bigImageData.data[i + 3] > 128) { // alpha 足夠,代表這裡有內容
var dot = new Dot(
Math.random() > 0.5
? Math.random() * 20 + 10
: Math.random() * 20 + canvas.width - 40, // 從左右兩側外飛入
-Math.random() * canvas.height * 2, // 從上方外飛入
0, 0,
x + (canvas.width / 2 - this.osCanvas.width / 2), // 換算成主畫布座標
y + (canvas.height / 2 - this.osCanvas.height / 2),
"rgba(" + bigImageData.data[i] + "," + bigImageData.data[i + 1] + "," + bigImageData.data[i + 2] + ",1)"
);
dot.setEnd(canvas.width / 2, canvas.height / 2);
dots.push(dot);
}
}
}
初始位置刻意設在畫布外,讓粒子從四面八方飛進來;目的地則是離屏 canvas 上該像素對應到主畫布的座標。全部就位後啟動動畫迴圈,文字就會一點一點被粒子「長」出來。
整套效果的核心其實只有三件事:向量分解求加速度、乘以阻尼係數模擬損耗、用時間差更新位置。剩下的都是包裝。想再往下加料,可以試試讓粒子之間互相排斥、滑鼠靠近時產生斥力、或者把 za 與阻尼係數做成可調參數即時調校——手感這種東西,最終還是要靠眼睛和手指去校準。