本文展示一個純 JavaScript 實作的「炫光波動」視覺特效:以大量 1px × 1px 的 <p> 元素模擬像素點,透過正弦波公式計算垂直位移,構建出類似 Flash 光波流動的動態效果。雖然 DOM 模擬像素會有鋸齒與效能限制,但作為 Canvas/WebGL 之前的歷史技術探索極具參考價值。
thick × thick × sharp 數量的 <p> 元素,每個代表 1 像素高的垂直線段y = amplitude × cos(angle) 正弦波公式,配合 vibration 控制頻率setInterval 週期性更新每個像素點的 style.topvar 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; // 動畫間隔 ms
this.vibration = vibration; // 振動頻率
this.amplitude = amplitude; // 振幅
this.opacity = opacity; // 透明度 0-100
this.cont.style.position = 'relative';
this.move();
};
LightWave.prototype = {
// 建立單一像素點
point: function(n, l, t, container, color) {
var p = document.createElement('p');
p.innerHTML = ' ';
p.style.top = t + 'px';
p.style.left = l + 'px';
p.style.width = '1px';
p.style.height = n + 'px';
p.style.filter = 'alpha(opacity=' + this.opacity + ')'; // IE 濾鏡
p.style.opacity = this.opacity / 100; // 標準 opacity
p.style.lineHeight = 0;
p.style.position = 'absolute';
p.style.background = color;
container.appendChild(p);
return this;
},
// 隨機生成高亮色系
color: function() {
var c = ['0','3','6','9','c','f'];
var t = [c[Math.floor(Math.random()*6)], '0', 'f'];
t.sort(() => 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 vib = this.vibration;
var waveDiv = this.wave();
var points = waveDiv.getElementsByTagName('p');
// 初始化每個點的相位與方向
for (var i = 0; i < points.length; i++) {
points[i].i = i;
points[i].ori = true; // true=向上, false=向下
}
var self = this;
var animate = function() {
for (var i = 0; i < points.length; i++) {
var p = points[i];
if (p.ori) {
p.i -= vib;
var top = (p.i % 180 === 90) ? 0 : wl * Math.cos(p.i * Math.PI / 180);
p.style.top = top + 'px';
if (parseFloat(p.style.top) <= -wl) p.ori = false;
} else {
p.i += vib;
var top = (p.i % 180 === 90) ? 0 : wl * Math.cos(p.i * Math.PI / 180);
p.style.top = top + 'px';
if (parseFloat(p.style.top) >= wl) p.ori = true;
}
}
};
setInterval(animate, this.speed);
}
};
// 初始化兩組不同參數的光波
window.onload = function() {
new LightWave(document.body, 0, 3, 36, 120, 6, 20, 40);
new LightWave(document.body, 50, 2, 70, 120, 10, 30, 30);
};
| 參數 | 意義 | 範例值 |
|---|---|---|
| T | 掛載容器 | document.body |
| left | 初始水平偏移 | 0 / 50 |
| thick | 光波粗細(像素) | 2~3 |
| sharp | 尖銳度(三角形底寬係數) | 36~70 |
| speed | 動畫間隔 ms | 120 |
| vibration | 單位時間相位變化量 | 6~10 |
| amplitude | 振幅(垂直位移範圍) | 20~30 |
| opacity | 透明度 0-100 | 30~40 |
作者發現:container.style.position = 'relative' 會導致子元素 filter: alpha(opacity=...) 失效。這是 IE 專有的渲染 Bug,現代瀏覽器已不相關。
<canvas id="waveCanvas" width="800" height="200" style="background:#000"></canvas>
<script>
const ctx = document.getElementById('waveCanvas').getContext('2d');
const waves = [
{ left: 0, thick: 3, sharp: 36, speed: 120, vib: 6, amp: 20, opacity: 0.4 },
{ left: 50, thick: 2, sharp: 70, speed: 120, vib: 10, amp: 30, opacity: 0.3 }
];
function randomColor() {
const c = ['0','3','6','9','c','f'];
const t = [c[Math.floor(Math.random()*6)], '0', 'f'];
t.sort(() => Math.random() > 0.5 ? -1 : 1);
return '#' + t.join('');
}
function drawWave(w, phase) {
ctx.beginPath();
ctx.fillStyle = w.color;
ctx.globalAlpha = w.opacity;
for (let x = 0; x < w.thick * w.sharp; x++) {
// 計算三角形剖面高度
let h;
if (x < w.thick * w.sharp / 2) h = x / w.sharp;
else h = (w.thick * w.sharp - x) / w.sharp;
const y = w.amp * Math.cos((x + phase) * Math.PI / 180) + 100;
ctx.fillRect(w.left + x, y, 1, h);
}
}
let phase = 0;
function animate() {
ctx.clearRect(0, 0, 800, 200);
waves.forEach(w => drawWave(w, phase));
phase += 2;
requestAnimationFrame(animate);
}
waves.forEach(w => w.color = randomColor());
animate();
</script>
DOM 像素模擬屬於「遠古前端技術考古」,理解其原理有助於掌握光柵渲染基礎。實際專案請直接使用 Canvas 2D、WebGL 或 CSS/SVG 動畫,效能與視覺品質皆遠超 DOM 方案。