貝塞爾曲線是計算機圖形學中最優雅的曲線表示法,廣泛應用於矢量繪圖、動畫軌跡、字體設計、路徑規劃。Canvas 提供 quadraticCurveTo(二次)和 bezierCurveTo(三次)兩大 API。本文從數學原理到代碼實戰,全方位解析貝塞爾曲線在 Canvas 中的應用。
由起點 P0、控制點 P1、終點 P2 定義。曲線從 P0 出發朝向 P1,最終到達 P2。參數 t ∈ [0,1]:
B(t) = (1-t)²P0 + 2(1-t)tP1 + t²P2
幾何構造法(德卡斯特里奧算法):
在 P0P1 上取點 Q0,P1P2 上取點 Q1(比例 t),再在 Q0Q1 上取點 B(t)。隨 t 變化,B(t) 軌跡即為曲線。
由 P0、P1、P2、P3 定義。曲線從 P0 朝向 P1 出發,從 P2 方向到達 P3。P1、P2 不在曲線上,僅提供方向與彎曲度:
B(t) = (1-t)³P0 + 3(1-t)²tP1 + 3(1-t)t²P2 + t³P3
// 二次貝塞爾:當前點 → (cpx,cpy) 控制 → (x,y) 終點
ctx.quadraticCurveTo(cpx, cpy, x, y);
// 三次貝塞爾:當前點 → (cp1x,cp1y) → (cp2x,cp2y) → (x,y)
ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
<canvas id="canvas" width="200" height="200" style="border:1px solid #ccc"></canvas>
<script>
const ctx = document.getElementById('canvas').getContext('2d');
// 輔助線:起點-控制點-終點
ctx.beginPath();
ctx.moveTo(20, 170);
ctx.lineTo(130, 40); // 控制點
ctx.lineTo(180, 150);
ctx.strokeStyle = '#ccc';
ctx.stroke();
// 實際曲線
ctx.beginPath();
ctx.moveTo(20, 170);
ctx.quadraticCurveTo(130, 40, 180, 150);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.stroke();
</script>
<canvas id="canvas" width="200" height="200" style="border:1px solid #ccc"></canvas>
<script>
const ctx = document.getElementById('canvas').getContext('2d');
// 輔助線
ctx.beginPath();
ctx.moveTo(25, 175);
ctx.lineTo(60, 80); // 控制點1
ctx.lineTo(150, 30); // 控制點2
ctx.lineTo(170, 150);
ctx.strokeStyle = '#ccc';
ctx.stroke();
// 實際曲線
ctx.beginPath();
ctx.moveTo(25, 175);
ctx.bezierCurveTo(60, 80, 150, 30, 170, 150);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.stroke();
</script>
function smoothLine(ctx, points, tension = 0.5) {
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
for (let i = 0; i < points.length - 1; i++) {
const p0 = points[i - 1] || points[0];
const p1 = points[i];
const p2 = points[i + 1];
const p3 = points[i + 2] || p2;
// Catmull-Rom 轉三次貝塞爾控制點
const cp1x = p1.x + (p2.x - p0.x) * tension / 6;
const cp1y = p1.y + (p2.y - p0.y) * tension / 6;
const cp2x = p2.x - (p3.x - p1.x) * tension / 6;
const cp2y = p2.y - (p3.y - p1.y) * tension / 6;
ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, p2.x, p2.y);
}
ctx.stroke();
}
function drawHeart(ctx, cx, cy, size) {
ctx.beginPath();
ctx.moveTo(cx, cy + size/4);
ctx.bezierCurveTo(
cx, cy - size/2, // cp1
cx - size, cy - size/2, // cp2
cx - size, cy + size/4 // end
);
ctx.bezierCurveTo(
cx - size, cy + size, // cp1
cx, cy + size*1.2, // cp2
cx, cy + size/4 // end (閉合)
);
ctx.bezierCurveTo(
cx, cy + size*1.2, // cp1
cx + size, cy + size, // cp2
cx + size, cy + size/4 // end
);
ctx.bezierCurveTo(
cx + size, cy - size/2, // cp1
cx, cy - size/2, // cp2
cx, cy + size/4 // end
);
ctx.fillStyle = 'red';
ctx.fill();
}
// 獲取曲線上任意 t 點的坐標(三次貝塞爾)
function getCubicBezierPoint(t, p0, p1, p2, p3) {
const u = 1 - t;
const uu = u * u, uuu = uu * u;
const tt = t * t, ttt = tt * t;
return {
x: uuu * p0.x + 3 * uu * t * p1.x + 3 * u * tt * p2.x + ttt * p3.x,
y: uuu * p0.y + 3 * uu * t * p1.y + 3 * u * tt * p2.y + ttt * p3.y
};
}
// 動畫循環
let t = 0;
function animate() {
const pos = getCubicBezierPoint(t, p0, p1, p2, p3);
drawObject(pos.x, pos.y);
t += 0.01;
if (t <= 1) requestAnimationFrame(animate);
}
ctx.stroke(path),提升性能d 屬性轉為 Canvas 路徑命令quadraticCurveTo/bezierCurveTo 前必須 moveTo 設置起點貝塞爾曲線是「少數幾個改變世界的算法」之一。在 Canvas 中熟練駕馭它,能讓你的圖形從生硬的直線圓弧,進化為流暢自然的有機曲線,為遊戲特效、數據可視化、創意編程注入靈魂。