Canvas 路徑 API 提供 quadraticCurveTo(二次貝塞爾)和 bezierCurveTo(三次貝塞爾)兩大曲線繪製方法。本文詳細解析參數含義、數學原理、可視化輔助線、實戰應用模式與常見誤區。
| 方法 | 參數 | 含義 |
|---|---|---|
quadraticCurveTo | cpx, cpy | 控制點座標(1個) |
| x, y | 終點座標 | |
bezierCurveTo | cp1x, cp1y | 第一控制點座標 |
| cp2x, cp2y | 第二控制點座標 | |
| x, y | 終點座標 |
起點由當前路徑位置決定(moveTo 設置)。
P0 (起點) ---- 拉向 ---- P1 (控制點)
\ /
\ /
\ /
\ /
\ /
\ /
\ /
\ /
\ /
\ /
\ /
\ /
P2 (終點)
曲線切線方向:起點指向控制點,終點來自控制點方向。
P0 ---- P1 (控制點1:出發切線)
\ \
\ \
\ \
\ \
\ \
\ \
P3 (終點) ← P2 (控制點2:到達切線)
P1 決定起始切線方向與彎曲幅度,P2 決定到達切線方向。經典「S 形」曲線:P1 在起點上方,P2 在終點下方(或反之)。
<canvas id="c" width="200" height="200" style="border:1px solid #ccc"></canvas>
<script>
const ctx = document.getElementById('c').getContext('2d');
// 輔助線(灰色虛線)
ctx.setLineDash([4,4]);
ctx.strokeStyle = '#ccc';
ctx.beginPath();
ctx.moveTo(20, 170);
ctx.lineTo(130, 40); // 控制點
ctx.lineTo(180, 150); // 終點
ctx.stroke();
ctx.setLineDash([]);
// 實際曲線(紅色)
ctx.beginPath();
ctx.moveTo(20, 170);
ctx.quadraticCurveTo(130, 40, 180, 150);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.stroke();
// 標註點
ctx.fillStyle = 'black';
[ [20,170], [130,40], [180,150] ].forEach(([x,y],i) => {
ctx.beginPath(); ctx.arc(x,y,3,0,Math.PI*2); ctx.fill();
ctx.fillText(['起點','控制點','終點'][i], x+5, y-5);
});
</script>
ctx.setLineDash([4,4]);
ctx.strokeStyle = '#ccc';
ctx.beginPath();
ctx.moveTo(25, 175);
ctx.lineTo(60, 80); // cp1
ctx.lineTo(150, 30); // cp2
ctx.lineTo(170, 150);
ctx.stroke();
ctx.setLineDash([]);
ctx.beginPath();
ctx.moveTo(25, 175);
ctx.bezierCurveTo(60, 80, 150, 30, 170, 150);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.stroke();
function drawSpline(ctx, points, tension = 0.5) {
if (points.length < 2) return;
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 roundPolygon(ctx, points, radius) {
ctx.beginPath();
const n = points.length;
for (let i = 0; i < n; i++) {
const p0 = points[(i-1+n)%n];
const p1 = points[i];
const p2 = points[(i+1)%n];
// 向量
const v1 = {x: p0.x-p1.x, y: p0.y-p1.y};
const v2 = {x: p2.x-p1.x, y: p2.y-p1.y};
const len1 = Math.hypot(v1.x, v1.y), len2 = Math.hypot(v2.x, v2.y);
const u1 = {x: v1.x/len1, y: v1.y/len1};
const u2 = {x: v2.x/len2, y: v2.y/len2};
// 切點
const t1 = {x: p1.x + u1.x*radius, y: p1.y + u1.y*radius};
const t2 = {x: p1.x + u2.x*radius, y: p1.y + u2.y*radius};
if (i === 0) ctx.moveTo(t2.x, t2.y);
else ctx.lineTo(t1.x, t1.y);
ctx.quadraticCurveTo(p1.x, p1.y, t2.x, t2.y);
}
ctx.closePath();
}
// 獲取三次貝塞爾曲線上 t 處的點
function getCubicPoint(t, p0, p1, p2, p3) {
const u = 1-t;
return {
x: u*u*u*p0.x + 3*u*u*t*p1.x + 3*u*t*t*p2.x + t*t*t*p3.x,
y: u*u*u*p0.y + 3*u*u*t*p1.y + 3*u*t*t*p2.y + t*t*t*p3.y
};
}
// 獲取切線角度
function getCubicTangent(t, p0, p1, p2, p3) {
const u = 1-t;
const tx = 3*u*u*(p1.x-p0.x) + 6*u*t*(p2.x-p1.x) + 3*t*t*(p3.x-p2.x);
const ty = 3*u*u*(p1.y-p0.y) + 6*u*t*(p2.y-p1.y) + 3*t*t*(p3.y-p2.y);
return Math.atan2(ty, tx);
}
// 動畫
let t = 0;
function animate() {
const pos = getCubicPoint(t, p0, p1, p2, p3);
const angle = getCubicTangent(t, p0, p1, p2, p3);
drawCar(pos.x, pos.y, angle); // 繪製跟隨切線方向的物體
t = (t + 0.005) % 1;
requestAnimationFrame(animate);
}
const heartPath = new Path2D();
heartPath.moveTo(75, 40);
heartPath.bezierCurveTo(75, 37, 70, 25, 50, 25);
heartPath.bezierCurveTo(20, 25, 20, 62.5, 20, 62.5);
heartPath.bezierCurveTo(20, 80, 40, 102, 75, 120);
heartPath.bezierCurveTo(110, 102, 130, 80, 130, 62.5);
heartPath.bezierCurveTo(130, 62.5, 130, 25, 100, 25);
heartPath.bezierCurveTo(85, 25, 75, 37, 75, 40);
// 多次復用
ctx.fillStyle = 'red'; ctx.fill(heartPath);
ctx.strokeStyle = 'darkred'; ctx.lineWidth=3; ctx.stroke(heartPath);
// 縮放繪製
ctx.save(); ctx.scale(0.5, 0.5); ctx.fill(heartPath); ctx.restore();
moveTo 設置起點 → 曲線從上一路徑終點開始,產生意外連線Path2D + OffscreenCanvas + Worker貝塞爾曲線是矢量圖形的靈魂。在 Canvas 中掌握 quadraticCurveTo/bezierCurveTo,配合輔助線可視化調試、Path2D 復用、參數方程動畫,能讓你繪製任意複雜的有機曲線,為遊戲軌跡、UI 動效、數據可視化、創意編程提供核心繪圖能力。