萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML5 Canvas 玻璃碎片破碎特效:Delaunay 三角剖分 + GSAP 動畫序列實戰

將圖片切割為不規則三角形碎片(Delaunay 三角剖分),再以 GSAP (TweenMax) 編排碎片「從天而降 → 落地彈跳 → 3D 翻轉淡出」的電影級動畫序列。本文完整解析三角剖分邏輯、碎片建立流程、時間軸編排技巧,並提供可直接套用的模組化代碼架構。

核心技術棧

  • Delaunay 三角剖分:delaunay.js 將規則網格點加入隨機抖動後剖分,生成自然裂紋感的三角形拼圖。
  • Canvas 裁切:每個 Fragment 建立獨立 <canvas>,用 ctx.clip() 裁切出三角形區域並 drawImage 繪製對應圖片部分。
  • GSAP TimelineMax:聲明式編排複雜動畫序列(to()、insert()、repeat:-1 循環),自動處理緩動、回調、並行/串行。
  • CSS 3D Transform:rotationX、rotationY、z 讓碎片在 3D 空間翻轉消失,需容器 perspective:500px。

關鍵實作步驟

1. 三角剖分生成頂點與索引

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, ...]
}

2. 計算每個三角形的外接矩形

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});
  }
}

3. 建立碎片 Canvas 與動畫時間軸

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);
  }
}

4. Fragment 類別:裁切繪製核心

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);
}

HTML 最小掛載結構

<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 合成層建立速度。
  • 行動裝置建議降低網格密度(如 6×6)並簡化動畫序列。
  • 記得在頁面卸載時 tl0.kill() 釋放 GSAP 時間軸資源。

玻璃碎片破碎特效動畫

copyright © 萬盛學電腦網 all rights reserved