看到 Flash 裡那種波動的炫光很順手,於是用 JavaScript 試著模擬一次。做法是把每個像素點都當成一個 DOM 元素,再用三角函數驅動上下位移。老實說鋸齒難免,因為 DOM 點陣本質上不是像素級渲染;若要真正消除鋸齒,得再加一層濾鏡,或乾脆改用 canvas 與圖片。
<script>
var lightWave = function (T, left, thick, sharp, speed, vibration, amplitude, opacity) {
this.cont = T; // 炫光容器
this.left = left; // 出生時向右的偏移量
this.thick = thick; // 粗細
this.sharp = sharp; // 尖銳度
this.speed = speed; // 波動速度
this.vibration = vibration; // 單位時間內的振動頻率
this.amplitude = amplitude; // 振幅
this.opacity = opacity; // 透明度
this.cont.style.position = 'relative';
this.move();
};
lightWave.prototype = {
// 建立一個像素點
point: function (n, l, t, c, color) {
var p = document.createElement('p');
p.innerHTML = ' ';
p.style.top = t + 'px';
p.style.left = l + 'px';
p.style.width = 1 + 'px';
p.style.height = n + 'px';
p.style.filter = 'alpha(opacity=' + this.opacity + ')';
p.style.lineHeight = 0;
p.style.position = 'absolute';
p.style.background = color;
c.appendChild(p);
return this;
},
// 隨機取色,偏向藍色系
color: function () {
var c = ['0', '3', '6', '9', 'c', 'f'];
var t = [c[Math.floor(Math.random() * 100) % 6], '0', 'f'];
t.sort(function () { return Math.random() > 0.5 ? -1 : 1; });
return '#' + t.join('');
},
// 依粗細與尖銳度畫出一束光
wave: function () {
var l = this.left, color = this.color();
var c = document.createElement('div');
c.style.top = this.amplitude + 20 + 'px';
c.style.position = 'absolute';
c.style.opacity = this.opacity / 100;
for (var i = 1; i < this.thick; i++) {
for (var j = 0; j < this.thick * this.sharp - i * i; j++, l++) {
this.point(i, l, -9999, c, color);
}
}
for (var i = this.thick; i > 0; i--) {
for (var j = this.thick * this.sharp - i * i; j > 0; j--, l++) {
this.point(i, l, -9999, c, color);
}
}
this.cont.appendChild(c);
return c;
},
// 以餘弦函數驅動上下波動
move: function () {
var wl = this.amplitude;
var vibration = this.vibration;
var w = this.wave().getElementsByTagName('p');
for (var i = 0; i < w.length; i++) { w[i].i = i; }
var m = function () {
for (var i = 0, len = w.length; i < len; i++) {
var top = (w[i].i % 180 == 90) ? 0 : wl * Math.cos(w[i].i * Math.PI / 180);
w[i].style.top = top + 'px';
if (w[i].ori == true) {
w[i].i -= vibration;
if (parseFloat(w[i].style.top) <= -wl) w[i].ori = false; // 到底就回頭
} else {
w[i].i += vibration;
if (parseFloat(w[i].style.top) >= wl) w[i].ori = true; // 到頂就回頭
}
}
};
setInterval(m, this.speed);
}
};
window.onload = function () {
var targetDom = document.body;
new lightWave(targetDom, 0, 3, 36, 120, 6, 20, 40);
new lightWave(targetDom, 50, 2, 70, 120, 10, 30, 30);
};
</script>
頁面只要一個黑底容器就能看效果:
<body style="background:#000;margin-top:100px"></body>
cont 是容器、left 控制起始水平位置、thick 決定光束粗細、sharp 影響尖端銳利程度、speed 是動畫間隔、vibration 控制每次位移的角度增量(也就是波動頻率)、amplitude 是振幅、opacity 為透明度。調整 vibration 與 amplitude 的組合,可以做出從劇烈閃動到柔緩呼吸的各種手感。
原始碼作者也記錄了一個實測心得:在 IE 下透明度濾鏡不生效,原因是「改變父容器的定位會影響子節點透明濾鏡的繼承」。這是老 IE 的 alpha filter 與 position 互動的已知怪癖,把 filter 改掛在每個點上(如本例 point 函式所為)可以繞過。
從今天的角度看,這個範例的價值主要是演算法上的:用 Math.cos 把線性位移變成正弦往返,用「到邊界就翻轉方向」的旗標控制循環,這些手法在 canvas、SVG 甚至 CSS 動畫中同樣通用。但要真的在頁面上畫光效,直接上 <canvas> 會省下成千上萬個 DOM 節點的負擔,效能差距是數量級的。