將圖片切割為不規則三角形碎片(Delaunay 三角剖分),再以 GSAP (TweenMax) 編排碎片「從天而降 → 落地彈跳 → 3D 翻轉淡出」的電影級動畫序列。本文完整解析三角剖分邏輯、碎片建立流程、時間軸編排技巧,並提供可直接套用的模組化代碼架構。
delaunay.js 將規則網格點加入隨機抖動後剖分,生成自然裂紋感的三角形拼圖。Fragment 建立獨立 <canvas>,用 ctx.clip() 裁切出三角形區域並 drawImage 繪製對應圖片部分。to()、insert()、repeat:-1 循環),自動處理緩動、回調、並行/串行。rotationX、rotationY、z 讓碎片在 3D 空間翻轉消失,需容器 perspective:500px。function triangulate() {
const dx = imageWidth / 8, dy = imageHeight / 8, offset = 0.5;
for (let i = 0; i <= imageWidth; i += dx) {
for (let j = 0; j <= imageHeight; j += dy) {
const x = (i && i !== imageWidth) ? i + randomRange(-dx*offset, dx*offset) : i;
const y = (j && j !== imageHeight) ? j + randomRange(-dy*offset, dy*offset) : j;
vertices.push([x, y]);
}
}
indices = Delaunay.triangulate(vertices); // 返回扁平索引陣列 [i0,i1,i2, i3,i4,i5, ...]
}
function makeBoxes() {
for (let i = 0; i < indices.length; i += 3) {
const p0 = vertices[indices[i]], p1 = vertices[indices[i+1]], p2 = vertices[indices[i+2]];
const xMin = Math.min(p0[0],p1[0],p2[0]), xMax = Math.max(p0[0],p1[0],p2[0]);
const yMin = Math.min(p0[1],p1[1],p2[1]), yMax = Math.max(p0[1],p1[1],p2[1]);
boxes.push({x:xMin, y:yMin, w:xMax-xMin, h:yMax-yMin});
}
}
function makeFragments() {
TweenMax.set(container, {perspective:500});
const tl0 = new TimelineMax({repeat:-1}); // 總時間軸循環
for (let i = 0; i < indices.length; i += 3) {
const p0 = vertices[indices[i]], p1 = vertices[indices[i+1]], p2 = vertices[indices[i+2]];
const box = boxes[i/3];
const fragment = new Fragment(p0, p1, p2, box);
// 隨機旋轉方向與角度
const rx = randomRange(30,60) * (i%2 ? 1 : -1);
const ry = randomRange(30,60) * (i%2 ? -1 : 1);
const tl1 = new TimelineMax();
TweenMax.set(fragment.canvas, {y: box.y - 1000}); // 起始位置:畫面上方外
tl1.to(fragment.canvas, randomRange(0.9,1.1), {y: box.y, ease: Back.easeOut}); // 落下彈跳
tl1.to(fragment.canvas, 0.5, {z: -100, ease: Cubic.easeIn, delay: 0.4}); // 向內凹陷
tl1.to(fragment.canvas, randomRange(1,1.2), {rotationX:rx, rotationY:ry, z:250, alpha:0, ease: Cubic.easeOut}); // 3D 翻轉淡出
tl0.insert(tl1, i * 0.01); // 錯開啟動時間,形成波浪感
fragments.push(fragment);
container.appendChild(fragment.canvas);
}
}
function Fragment(v0, v1, v2, box) {
this.v0=v0; this.v1=v1; this.v2=v2; this.box=box;
this.canvas = document.createElement('canvas');
this.canvas.width = box.w; this.canvas.height = box.h;
this.canvas.style.width = box.w+'px'; this.canvas.style.height = box.h+'px';
this.ctx = this.canvas.getContext('2d');
TweenMax.set(this.canvas, {x:box.x, y:box.y});
this.ctx.translate(-box.x, -box.y);
this.ctx.beginPath();
this.ctx.moveTo(v0[0],v0[1]); this.ctx.lineTo(v1[0],v1[1]); this.ctx.lineTo(v2[0],v2[1]);
this.ctx.closePath(); this.ctx.clip(); this.ctx.drawImage(image, 0, 0);
}
<img src="city_copy.jpg" id="src_img" style="display:none">
<div id="container" style="perspective:500px; position:relative; width:768px; height:485px; margin:0 auto;"></div>
<script src="delaunay.js"></script>
<script src="TweenMax.min.js"></script>
<script src="main.js"></script>
(8+1)×(8+1) ≈ 81 個三角形 × 2 = 162 顆碎片,每顆一個 Canvas,DOM 節點較多。若需更高效能,可改用單一 Canvas 重繪所有碎片(需自行管理 3D 變換矩陣)。will-change: transform, opacity 加在碎片 <canvas> 上可提升 GPU 合成層建立速度。tl0.kill() 釋放 GSAP 時間軸資源。