我对贝塞尔曲线的数学以及它们在 SVG 中的表示方式进行了一些研究。
我的路径使用m 命令表示路径的起点,使用c 命令表示具有相对控制点和终点的贝塞尔曲线。这条曲线有多个段。
一个示例路径
m 1,2 c 3,4 5,6 7,8 9,10 11,12 13,14
意思是:
- 路径的起点是 (1,2)
- 然后是一条有 2 段的曲线
- 第一段从 (1,2) 开始,它的第一个控制点在 (1+3,2+4)=(4,6),第二个控制点在 (1+5,2+6)= (6,8) 结束于 (1+7,2+8)=(8,10)
- 第二段从最后一段的终点 (8,10) 开始。它的第一个控制点是(8+9,10+10)=(18,19),第二个控制点是(8+11,10+13)=(18,24),终点是(8+13) ,10+14)=(18,27)
使用贝塞尔曲线的数学,我编写了这段代码来返回具有多段的贝塞尔曲线上的点。请注意,它只处理路径以m 开头然后以c 命令继续的情况。
const POINTS_IN_SEGMENT = 6;
class Point {
x;
y;
constructor(xStr, yStr) {
this.x = parseInt(xStr, 10);
this.y = parseInt(yStr, 10);
}
add(other) {
return new Point(this.x + other.x, this.y + other.y);
}
}
class BezierCurveSegment {
startPoint;
controlPoint1;
controlPoint2;
endPoint;
constructor(startPoint, controlPoint1, controlPoint2, endPoint) {
this.startPoint = startPoint;
this.controlPoint1 = controlPoint1;
this.controlPoint2 = controlPoint2;
this.endPoint = endPoint;
}
getPointsOnSegment(accuracy) {
const points = [];
for (let i = 0; i < 1; i += accuracy) {
const p = bezier(i, this.startPoint, this.controlPoint1, this.controlPoint2, this.endPoint);
points.push(new Point(p.x, p.y));
}
return points;
}
}
class BezierCurve {
startPoint;
segments;
constructor(startPoint, segments) {
this.startPoint = startPoint;
this.segments = segments;
}
getPointsOnCurve(pointCount) {
const accuracy = 1 / pointCount * this.segments.length;
let points = [];
for (let segment of this.segments) {
let pointsOnSegment = segment.getPointsOnSegment(accuracy);
points = points.concat(pointsOnSegment);
}
return points;
}
}
// https://stackoverflow.com/questions/16227300/
function bezier(t, p0, p1, p2, p3) {
// console.log("bezier", t, p0, p1, p2, p3)
var cX = 3 * (p1.x - p0.x),
bX = 3 * (p2.x - p1.x) - cX,
aX = p3.x - p0.x - cX - bX;
var cY = 3 * (p1.y - p0.y),
bY = 3 * (p2.y - p1.y) - cY,
aY = p3.y - p0.y - cY - bY;
var x = (aX * Math.pow(t, 3)) + (bX * Math.pow(t, 2)) + (cX * t) + p0.x;
var y = (aY * Math.pow(t, 3)) + (bY * Math.pow(t, 2)) + (cY * t) + p0.y;
return {x: x, y: y};
}
function createCurveFromSVGPath(pathStr) {
pathStr = pathStr.replaceAll(',', ' ');
let items = pathStr.split(' ');
if (items[0] !== "m") {
throw "First item in the path is not 'm' command (only relative is supported)";
}
if (items[3] !== "c") {
throw "Third item in the path is not 'c' command (only relative is supported)";
}
let startPoint = new Point(items[1], items[2]);
// done with "m x,y c" items
items = items.slice(4);
// divide by the number of points each segment has
let segmentCount = items.length / POINTS_IN_SEGMENT;
let segments = [];
for (let i = 0; i < segmentCount; i++) {
let lastSegmentEndPoint;
if (i === 0) {
lastSegmentEndPoint = startPoint;
} else {
lastSegmentEndPoint = segments[i - 1].endPoint;
}
// noinspection PointlessArithmeticExpressionJS
const segment = new BezierCurveSegment(
// all these numbers are relative to previous segment's end point
// see https://stackoverflow.com/questions/26675960/
lastSegmentEndPoint,
lastSegmentEndPoint.add(new Point(items[i * POINTS_IN_SEGMENT + 0], items[i * POINTS_IN_SEGMENT + 1])),
lastSegmentEndPoint.add(new Point(items[i * POINTS_IN_SEGMENT + 2], items[i * POINTS_IN_SEGMENT + 3])),
lastSegmentEndPoint.add(new Point(items[i * POINTS_IN_SEGMENT + 4], items[i * POINTS_IN_SEGMENT + 5])),
);
segments.push(segment);
}
return new BezierCurve(startPoint, segments);
}
为了验证它是否正常工作,我在画布上绘制点,并使用 Path2D 绘制相同的路径。
function drawPoints(ctx, points) {
ctx.moveTo(points[0].x, points[0].y);
for (let point of points) {
ctx.lineTo(point.x, point.y);
ctx.moveTo(point.x, point.y);
}
ctx.stroke();
}
let pathStr = 'm 776.65415,2226.6574 c 0,0 258.61385,-173.7593 289.34025,-325.8576 57.158,-282.9367 15.5277,-622.2212 50.8732,-933.13714 12.8345,-112.89946 104.2775,-278.6582 22.2568,-340.66923 -50.5144,-38.19103 -158.97817,99.97904 -158.97817,99.97904';
const curve = createCurveFromSVGPath(pathStr);
let pointsOnCurve = curve.getPointsOnCurve(100000);
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
drawPoints(ctx, pointsOnCurve);
ctx.strokeStyle = "#FF0000";
ctx.stroke(new Path2D(pathStr););