想把自己的 HTML5 練習作品集合到一個網頁上,但單純把 DEMO 一條條排下去實在不好看。既然學了 Canvas,不如拿它做個開場動畫——用粒子。
之所以挑粒子,是因為它好玩。之前寫過一篇「文字圖片粒子化」的筆記,那時只做直線運動加一點 3D 透視,公式簡單到不行。這次想做得更有動感一些。
結果發現,真正用到的知識就是國中(或高中)物理的加速與減速公式。小小一條公式,卻能撐起很不錯的視覺效果——這大概是折騰這類東西最迷人的地方,就算工作上用不到,對編程思維的訓練也很實在。
每個粒子要嘛在自由落體,要嘛被一股吸力拉向指定座標。核心的 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;
}
this.vy = vy;
}
},
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;
},
先給每個粒子一個目的地,再定義一個加速度常數 za(這裡取 20,實際數值多試幾次就有感覺了)。za 代表沿著「粒子→目的地」連線方向的加速度大小,因此只要用最簡單的三角函數,就能把它分解成水平與垂直分量:
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;
乘上 0.97 是為了模擬能量損耗,讓粒子逐漸減速收斂,否則它會在目的地附近一直振盪。time 是這一幀的時間差。最後更新位置就完成一幀:
this.x += this.vx * time;
this.y += this.vy * time;
要注意的是,粒子飛行途中與目的地的連線方向不斷改變,所以每一幀都得重算一次 ax 與 ay。
做法是在一張離屏 canvas 上把文字畫出來,再用 getImageData 取出像素,掃描找出有被繪製的位置。imageData 的 data 是一個 RGBA 陣列,只要 alpha(最後一位)大於 128,就代表那個點被畫過。取到座標後,把它變成粒子的目的地。
為了避免粒子數量爆炸導致掉幀,取樣時 x 與 y 每次遞增 2,粒子數立刻降到四分之一:
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);
dots = [];
for (var x = 0; x < this.osCanvas.width; x += 2) {
for (var y = 0; y < this.osCanvas.height; y += 2) {
var i = (y * bigImageData.width + x) * 4;
if (bigImageData.data[i + 3] > 128) {
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);
}
}
}
粒子的起始位置刻意設在畫布外(左右兩側或上方隨機),初始速度為 0,顏色直接沿用原圖該點的 RGB。動畫一啟動,它們就會被吸向各自的目的地,慢慢拼出那兩行字。