svg-path-interpolator 0.2.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,41 @@
1
+ import type { Point } from './calculators.js';
2
+ /**
3
+ * A mutable 2D affine transform using SVG's six matrix values.
4
+ *
5
+ * Points are mapped with `x' = ax + cy + e` and `y' = bx + dy + f`.
6
+ * Each transform method appends its operation to the current matrix.
7
+ */
8
+ export declare class SVGTransform {
9
+ protected m_transform: number[];
10
+ constructor(a?: number, b?: number, c?: number, d?: number, e?: number, f?: number);
11
+ makeIdentity(): void;
12
+ setMatrix(a?: number, b?: number, c?: number, d?: number, e?: number, f?: number): void;
13
+ matrix(a?: number, b?: number, c?: number, d?: number, e?: number, f?: number): void;
14
+ isIdentity(): boolean;
15
+ static det(transform: number[]): number;
16
+ get xScale(): number;
17
+ get yScale(): number;
18
+ get isInvertible(): boolean;
19
+ inverse(): SVGTransform;
20
+ isIdentityOrTranslation(): boolean;
21
+ /** Append another transform to this matrix. */
22
+ multiply(other: SVGTransform): this;
23
+ rotate(degrees: number, x?: number, y?: number): this;
24
+ scale(sx: number, sy?: number): this;
25
+ translate(tx: number, ty?: number): this;
26
+ rotateFromVector(x: number, y: number): this;
27
+ flipX(): this;
28
+ flipY(): this;
29
+ shear(sx: number, sy: number): this;
30
+ skew(angleX: number, angleY: number): this;
31
+ skewX(angle: number): this;
32
+ skewY(angle: number): this;
33
+ map(x: number, y: number): {
34
+ x: number;
35
+ y: number;
36
+ };
37
+ mapPoint(point: Point): {
38
+ x: number;
39
+ y: number;
40
+ };
41
+ }
@@ -0,0 +1,117 @@
1
+ import { degToRads, radToDeg } from './utils.js';
2
+ /**
3
+ * A mutable 2D affine transform using SVG's six matrix values.
4
+ *
5
+ * Points are mapped with `x' = ax + cy + e` and `y' = bx + dy + f`.
6
+ * Each transform method appends its operation to the current matrix.
7
+ */
8
+ export class SVGTransform {
9
+ m_transform;
10
+ constructor(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
11
+ this.m_transform = [Number(a), Number(b), Number(c), Number(d), Number(e), Number(f)];
12
+ }
13
+ makeIdentity() {
14
+ this.setMatrix();
15
+ }
16
+ setMatrix(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
17
+ this.m_transform[0] = Number(a);
18
+ this.m_transform[1] = Number(b);
19
+ this.m_transform[2] = Number(c);
20
+ this.m_transform[3] = Number(d);
21
+ this.m_transform[4] = Number(e);
22
+ this.m_transform[5] = Number(f);
23
+ }
24
+ matrix(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
25
+ this.setMatrix(a, b, c, d, e, f);
26
+ }
27
+ isIdentity() {
28
+ const [a, b, c, d, e, f] = this.m_transform;
29
+ return a === 1 && b === 0 && c === 0 && d === 1 && e === 0 && f === 0;
30
+ }
31
+ static det(transform) {
32
+ return transform[0] * transform[3] - transform[1] * transform[2];
33
+ }
34
+ get xScale() {
35
+ const [a, b] = this.m_transform;
36
+ return Math.hypot(a, b);
37
+ }
38
+ get yScale() {
39
+ const [, , c, d] = this.m_transform;
40
+ return Math.hypot(c, d);
41
+ }
42
+ get isInvertible() {
43
+ const determinant = SVGTransform.det(this.m_transform);
44
+ return determinant !== 0 && Number.isFinite(determinant);
45
+ }
46
+ inverse() {
47
+ const [a, b, c, d, e, f] = this.m_transform;
48
+ const determinant = SVGTransform.det(this.m_transform);
49
+ if (this.isIdentityOrTranslation()) {
50
+ return new SVGTransform(1, 0, 0, 1, -e, -f);
51
+ }
52
+ return new SVGTransform(d / determinant, -b / determinant, -c / determinant, a / determinant, (c * f - d * e) / determinant, (b * e - a * f) / determinant);
53
+ }
54
+ isIdentityOrTranslation() {
55
+ const [a, b, c, d] = this.m_transform;
56
+ return a === 1 && b === 0 && c === 0 && d === 1;
57
+ }
58
+ /** Append another transform to this matrix. */
59
+ multiply(other) {
60
+ const [a, b, c, d, e, f] = this.m_transform;
61
+ const [oa, ob, oc, od, oe, of] = other.m_transform;
62
+ this.m_transform = [
63
+ a * oa + c * ob,
64
+ b * oa + d * ob,
65
+ a * oc + c * od,
66
+ b * oc + d * od,
67
+ a * oe + c * of + e,
68
+ b * oe + d * of + f
69
+ ];
70
+ return this;
71
+ }
72
+ rotate(degrees, x, y) {
73
+ if (x !== undefined && y !== undefined) {
74
+ this.translate(x, y);
75
+ }
76
+ const radians = degToRads(degrees);
77
+ this.multiply(new SVGTransform(Math.cos(radians), Math.sin(radians), -Math.sin(radians), Math.cos(radians)));
78
+ if (x !== undefined && y !== undefined) {
79
+ this.translate(-x, -y);
80
+ }
81
+ return this;
82
+ }
83
+ scale(sx, sy = sx) {
84
+ return this.multiply(new SVGTransform(sx, 0, 0, sy));
85
+ }
86
+ translate(tx, ty = 0) {
87
+ return this.multiply(new SVGTransform(1, 0, 0, 1, tx, ty));
88
+ }
89
+ rotateFromVector(x, y) {
90
+ return this.rotate(radToDeg(Math.atan2(y, x)), x, y);
91
+ }
92
+ flipX() {
93
+ return this.scale(-1, 1);
94
+ }
95
+ flipY() {
96
+ return this.scale(1, -1);
97
+ }
98
+ shear(sx, sy) {
99
+ return this.multiply(new SVGTransform(1, sy, sx, 1));
100
+ }
101
+ skew(angleX, angleY) {
102
+ return this.shear(Math.tan(degToRads(angleX)), Math.tan(degToRads(angleY)));
103
+ }
104
+ skewX(angle) {
105
+ return this.shear(Math.tan(degToRads(angle)), 0);
106
+ }
107
+ skewY(angle) {
108
+ return this.shear(0, Math.tan(degToRads(angle)));
109
+ }
110
+ map(x, y) {
111
+ const [a, b, c, d, e, f] = this.m_transform;
112
+ return { x: a * x + c * y + e, y: b * x + d * y + f };
113
+ }
114
+ mapPoint(point) {
115
+ return this.map(point.x, point.y);
116
+ }
117
+ }
@@ -0,0 +1,54 @@
1
+ export type Point = {
2
+ x: number;
3
+ y: number;
4
+ };
5
+ /**
6
+ * Counts the work done while sampling one path or whole SVG document.
7
+ *
8
+ * The same budget object moves through every calculator, so limits still work
9
+ * when an arc is split into several curves or a document has several paths.
10
+ */
11
+ export interface SamplingBudget {
12
+ /** Stop before trying more than this many sample positions. */
13
+ maxSamples?: number;
14
+ /** Stop before returning more than this many x/y point pairs. */
15
+ maxOutputPoints?: number;
16
+ /** Number of sample positions tried so far. */
17
+ samples: number;
18
+ /** Number of x/y point pairs returned so far. */
19
+ outputPoints: number;
20
+ }
21
+ /** Count one sampling attempt and stop before it would exceed the configured limit. */
22
+ export declare function consumeSample(budget?: SamplingBudget): void;
23
+ /** Sample one or more straight line segments. */
24
+ declare function calculateCoordinatesLinear(points: number[], minDistance: number, roundToNearest: number, sampleFrequency: number, budget?: SamplingBudget): number[];
25
+ /** Sample one or more quadratic Bézier curve segments. */
26
+ declare function calculateCoordinatesQuad(points: number[], minDistance: number, roundToNearest: number, sampleFrequency: number, budget?: SamplingBudget): number[];
27
+ /** Sample one or more cubic Bézier curve segments. */
28
+ declare function calculateCoordinatesCubic(points: number[], minDistance: number, roundToNearest: number, sampleFrequency: number, budget?: SamplingBudget): number[];
29
+ /**
30
+ * Sample smooth cubic curves by reflecting the previous control point.
31
+ *
32
+ * SVG uses this rule for its `S` and `s` path commands.
33
+ */
34
+ declare function calculateCoordinatesSmoothCubic(points: number[], minDistance: number, roundToNearest: number, sampleFrequency: number, budget?: SamplingBudget): number[];
35
+ /**
36
+ * Sample SVG elliptical arcs.
37
+ *
38
+ * Each arc is changed into one or more cubic curves first, then sampled with
39
+ * the same rules and shared budget as any other curve.
40
+ */
41
+ declare function calculateCoordinatesArc(points: number[], minDistance: number, roundToNearest: number, sampleFrequency: number, budget?: SamplingBudget): number[];
42
+ /** Map SVG command letters to the sampler that understands their point layout. */
43
+ export declare const calculators: {
44
+ a: typeof calculateCoordinatesArc;
45
+ c: typeof calculateCoordinatesCubic;
46
+ h: typeof calculateCoordinatesLinear;
47
+ l: typeof calculateCoordinatesLinear;
48
+ m: typeof calculateCoordinatesLinear;
49
+ q: typeof calculateCoordinatesQuad;
50
+ s: typeof calculateCoordinatesSmoothCubic;
51
+ v: typeof calculateCoordinatesLinear;
52
+ z: typeof calculateCoordinatesLinear;
53
+ };
54
+ export {};
@@ -0,0 +1,331 @@
1
+ import { SVGTransform } from './SVGTransform.js';
2
+ import { degToRads, isNullOrUndefined } from './utils.js';
3
+ /** Count one sampling attempt and stop before it would exceed the configured limit. */
4
+ export function consumeSample(budget) {
5
+ if (!budget) {
6
+ return;
7
+ }
8
+ if (budget.maxSamples !== undefined && budget.samples >= budget.maxSamples) {
9
+ throw new RangeError(`maxSamples of ${budget.maxSamples} would be exceeded`);
10
+ }
11
+ budget.samples++;
12
+ }
13
+ /** Round and save one point, while enforcing the shared output limit. */
14
+ function addPoint(points, x, y, roundToNearest, budget) {
15
+ if (budget?.maxOutputPoints !== undefined && budget.outputPoints >= budget.maxOutputPoints) {
16
+ throw new RangeError(`maxOutputPoints of ${budget.maxOutputPoints} would be exceeded`);
17
+ }
18
+ points.push(x - (x % roundToNearest), y - (y % roundToNearest));
19
+ if (budget) {
20
+ budget.outputPoints++;
21
+ }
22
+ }
23
+ /** Find one value between two values at position `t`, where 0 is the start and 1 is the end. */
24
+ function calculateLinear(t, p1, p2) {
25
+ return p1 + t * (p2 - p1);
26
+ }
27
+ /** Find one coordinate on a quadratic Bézier curve at position `t`. */
28
+ function calculatePointQuadratic(t, p1, p2, p3) {
29
+ const oneMinusT = 1 - t;
30
+ return (oneMinusT * oneMinusT) * p1 + 2 * oneMinusT * t * p2 + (t * t) * p3;
31
+ }
32
+ /** Find one coordinate on a cubic Bézier curve at position `t`. */
33
+ function calculatePointCubic(t, p1, p2, p3, p4) {
34
+ const t2 = t * t;
35
+ const t3 = t2 * t;
36
+ const oneMinusT = 1 - t;
37
+ // Calculate each Bernstein weight before multiplying by its coordinate.
38
+ // This avoids overflowing a finite control point solely because it is first
39
+ // multiplied by 3.
40
+ return p1 * Math.pow(oneMinusT, 3)
41
+ + p2 * (3 * oneMinusT * oneMinusT * t)
42
+ + p3 * (3 * oneMinusT * t2)
43
+ + p4 * t3;
44
+ }
45
+ /** Sample one or more straight line segments. */
46
+ function calculateCoordinatesLinear(points, minDistance, roundToNearest, sampleFrequency, budget) {
47
+ const pts = [];
48
+ let [startX, startY] = points.splice(0, 2);
49
+ for (let i = 0; i < points.length; i += 2) {
50
+ let endX = points[i];
51
+ let endY = points[i + 1];
52
+ let t = 0;
53
+ let lastX = startX;
54
+ let lastY = startY;
55
+ while (t <= 1.0000000000000007) {
56
+ consumeSample(budget);
57
+ const x = calculateLinear(t, startX, endX);
58
+ const y = calculateLinear(t, startY, endY);
59
+ const deltaX = x - lastX;
60
+ const deltaY = y - lastY;
61
+ const dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
62
+ if (Math.abs(dist) > minDistance) {
63
+ addPoint(pts, x, y, roundToNearest, budget);
64
+ lastX = x;
65
+ lastY = y;
66
+ }
67
+ t += sampleFrequency;
68
+ }
69
+ startX = endX;
70
+ startY = endY;
71
+ }
72
+ return pts;
73
+ }
74
+ /** Sample one or more quadratic Bézier curve segments. */
75
+ function calculateCoordinatesQuad(points, minDistance, roundToNearest, sampleFrequency, budget) {
76
+ const pts = [];
77
+ let [startX, startY] = points.splice(0, 2);
78
+ for (let i = 0; i < points.length; i += 4) {
79
+ let ctrl1x = points[i];
80
+ let ctrl1y = points[i + 1];
81
+ let endX = points[i + 2];
82
+ let endY = points[i + 3];
83
+ let t = 0;
84
+ let lastX = startX;
85
+ let lastY = startY;
86
+ while (t <= 1.0000000000000007) {
87
+ consumeSample(budget);
88
+ const x = calculatePointQuadratic(t, startX, ctrl1x, endX);
89
+ const y = calculatePointQuadratic(t, startY, ctrl1y, endY);
90
+ const deltaX = x - lastX;
91
+ const deltaY = y - lastY;
92
+ const dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
93
+ if (Math.abs(dist) > minDistance) {
94
+ addPoint(pts, x, y, roundToNearest, budget);
95
+ lastX = x;
96
+ lastY = y;
97
+ }
98
+ t += sampleFrequency;
99
+ }
100
+ startX = endX;
101
+ startY = endY;
102
+ }
103
+ return pts;
104
+ }
105
+ /** Sample one or more cubic Bézier curve segments. */
106
+ function calculateCoordinatesCubic(points, minDistance, roundToNearest, sampleFrequency, budget) {
107
+ const pts = [];
108
+ let [startX, startY] = points.splice(0, 2);
109
+ for (let i = 0; i < points.length; i += 6) {
110
+ let ctrl1x = points[i];
111
+ let ctrl1y = points[i + 1];
112
+ let ctrl2x = points[i + 2];
113
+ let ctrl2y = points[i + 3];
114
+ let endX = points[i + 4];
115
+ let endY = points[i + 5];
116
+ let t = 0;
117
+ let lastX = startX;
118
+ let lastY = startY;
119
+ while (t <= 1.0000000000000007) {
120
+ consumeSample(budget);
121
+ const x = calculatePointCubic(t, startX, ctrl1x, ctrl2x, endX);
122
+ const y = calculatePointCubic(t, startY, ctrl1y, ctrl2y, endY);
123
+ const deltaX = x - lastX;
124
+ const deltaY = y - lastY;
125
+ const dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
126
+ if (Math.abs(dist) > minDistance) {
127
+ addPoint(pts, x, y, roundToNearest, budget);
128
+ lastX = x;
129
+ lastY = y;
130
+ }
131
+ t += sampleFrequency;
132
+ }
133
+ startX = endX;
134
+ startY = endY;
135
+ }
136
+ return pts;
137
+ }
138
+ /**
139
+ * Sample smooth cubic curves by reflecting the previous control point.
140
+ *
141
+ * SVG uses this rule for its `S` and `s` path commands.
142
+ */
143
+ function calculateCoordinatesSmoothCubic(points, minDistance, roundToNearest, sampleFrequency, budget) {
144
+ const pts = [];
145
+ let [startX, startY, previousCtrl2x, previousCtrl2y] = points.splice(0, 4);
146
+ for (let i = 0; i < points.length; i += 4) {
147
+ const ctrl2x = points[i];
148
+ const ctrl2y = points[i + 1];
149
+ const endX = points[i + 2];
150
+ const endY = points[i + 3];
151
+ if (isNullOrUndefined(previousCtrl2x) || isNullOrUndefined(previousCtrl2y)) {
152
+ previousCtrl2x = startX;
153
+ previousCtrl2y = startY;
154
+ }
155
+ let svgTransform = new SVGTransform(1, 0, 0, 1, previousCtrl2x - startX, previousCtrl2y - startY).inverse();
156
+ let { x: ctrl1x, y: ctrl1y } = svgTransform.map(startX, startY);
157
+ const interpolatedPts = calculateCoordinatesCubic([startX, startY, ctrl1x, ctrl1y, ctrl2x, ctrl2y, endX, endY], minDistance, roundToNearest, sampleFrequency, budget);
158
+ pts.push(...interpolatedPts);
159
+ startX = endX;
160
+ startY = endY;
161
+ previousCtrl2x = ctrl2x;
162
+ previousCtrl2y = ctrl2y;
163
+ }
164
+ return pts;
165
+ }
166
+ /**
167
+ * Sample SVG elliptical arcs.
168
+ *
169
+ * Each arc is changed into one or more cubic curves first, then sampled with
170
+ * the same rules and shared budget as any other curve.
171
+ */
172
+ function calculateCoordinatesArc(points, minDistance, roundToNearest, sampleFrequency, budget) {
173
+ const pts = [];
174
+ let [startX, startY] = points.splice(0, 2);
175
+ for (let i = 0; i < points.length; i += 7) {
176
+ let rx = points[i];
177
+ let ry = points[i + 1];
178
+ let angle = points[i + 2];
179
+ let largeArc = points[i + 3];
180
+ let sweep = points[i + 4];
181
+ let endX = points[i + 5];
182
+ let endY = points[i + 6];
183
+ // If the endpoints (x1, y1) and (x2, y2) are identical, then
184
+ // this is equivalent to omitting the elliptical arc segment entirely.
185
+ if (startX === endX && startY === endY) {
186
+ continue;
187
+ }
188
+ // If rx = 0 or ry = 0 then this arc is treated as a straight
189
+ // line segment (a "lineto") joining the endpoints.
190
+ if (rx === 0 || ry === 0) {
191
+ pts.push(...calculateCoordinatesLinear([startX, startY, endX, endY], minDistance, roundToNearest, sampleFrequency, budget));
192
+ startX = endX;
193
+ startY = endY;
194
+ continue;
195
+ }
196
+ // If rx or ry have negative signs, these are dropped;
197
+ // the absolute value is used instead.
198
+ if (rx < 0) {
199
+ rx *= -1;
200
+ }
201
+ if (ry < 0) {
202
+ ry *= -1;
203
+ }
204
+ const beziers = decomposeArcToCubic({ x: startX, y: startY }, angle, rx, ry, largeArc, sweep, { x: endX, y: endY });
205
+ // Triplet points - start of new bezier is end of last
206
+ for (let i = 0; i < beziers.length; i += 3) {
207
+ const ctrlPt1 = beziers[i];
208
+ const ctrlPt2 = beziers[i + 1];
209
+ const endPoint = beziers[i + 2];
210
+ const points = [startX, startY, ctrlPt1.x, ctrlPt1.y, ctrlPt2.x, ctrlPt2.y, endPoint.x, endPoint.y];
211
+ const interpolatedPoints = calculateCoordinatesCubic(points, minDistance, roundToNearest, sampleFrequency, budget);
212
+ pts.push(...interpolatedPoints);
213
+ startX = endPoint.x;
214
+ startY = endPoint.y;
215
+ }
216
+ startX = endX;
217
+ startY = endY;
218
+ }
219
+ return pts;
220
+ }
221
+ /**
222
+ * Change an SVG arc into cubic Bézier control points.
223
+ *
224
+ * A cubic curve is easier for the rest of this module to sample consistently.
225
+ */
226
+ function decomposeArcToCubic(point1, rotationInDegrees, rx, ry, largeArcFlag, sweepFlag, point2) {
227
+ // This follows SVG's endpoint-to-center conversion, then approximates each
228
+ // quarter-or-smaller ellipse section with a cubic Bézier curve.
229
+ const phi = degToRads(rotationInDegrees % 360);
230
+ const cosPhi = Math.cos(phi);
231
+ const sinPhi = Math.sin(phi);
232
+ // Divide before subtracting so opposite finite coordinates near the limits
233
+ // of a JavaScript number do not overflow while finding their midpoint delta.
234
+ const halfDx = point1.x / 2 - point2.x / 2;
235
+ const halfDy = point1.y / 2 - point2.y / 2;
236
+ const x1p = cosPhi * halfDx + sinPhi * halfDy;
237
+ const y1p = -sinPhi * halfDx + cosPhi * halfDy;
238
+ const coordinateScale = Math.max(Math.abs(x1p), Math.abs(y1p));
239
+ if (coordinateScale === 0) {
240
+ return [];
241
+ }
242
+ // Keep the correction calculation close to one. Directly squaring very large
243
+ // or very small radii can overflow or underflow even when the final ellipse
244
+ // is representable.
245
+ const normalizedX = x1p / coordinateScale;
246
+ const normalizedY = y1p / coordinateScale;
247
+ const radiusScale = Math.max(rx, ry);
248
+ const normalizedRx = rx / radiusScale;
249
+ const normalizedRy = ry / radiusScale;
250
+ const radiusDistance = Math.hypot(normalizedX / normalizedRx, normalizedY / normalizedRy);
251
+ if (radiusDistance > radiusScale / coordinateScale) {
252
+ const correctedRx = coordinateScale * Math.hypot(normalizedX, normalizedY * (normalizedRx / normalizedRy));
253
+ const correctedRy = coordinateScale * Math.hypot(normalizedX * (normalizedRy / normalizedRx), normalizedY);
254
+ if (!Number.isFinite(correctedRx) || !Number.isFinite(correctedRy)) {
255
+ throw new RangeError('SVG arc cannot be represented with finite radii');
256
+ }
257
+ rx = correctedRx;
258
+ ry = correctedRy;
259
+ }
260
+ const endpointDistance = Math.hypot(x1p / rx, y1p / ry);
261
+ if (!Number.isFinite(endpointDistance) || endpointDistance === 0) {
262
+ return [];
263
+ }
264
+ let centerFactor = Math.sqrt(Math.max(0, 1 - endpointDistance * endpointDistance)) / endpointDistance;
265
+ if (largeArcFlag === sweepFlag) {
266
+ centerFactor = -centerFactor;
267
+ }
268
+ const centerXp = centerFactor * ((rx / ry) * y1p);
269
+ const centerYp = centerFactor * (-(ry / rx) * x1p);
270
+ const startUnitX = (x1p - centerXp) / rx;
271
+ const startUnitY = (y1p - centerYp) / ry;
272
+ const endUnitX = (-x1p - centerXp) / rx;
273
+ const endUnitY = (-y1p - centerYp) / ry;
274
+ if (![centerXp, centerYp, startUnitX, startUnitY, endUnitX, endUnitY].every(Number.isFinite)) {
275
+ throw new RangeError('SVG arc cannot be represented with finite coordinates');
276
+ }
277
+ let arcAngle = Math.atan2(startUnitX * endUnitY - startUnitY * endUnitX, startUnitX * endUnitX + startUnitY * endUnitY);
278
+ if (sweepFlag && arcAngle < 0) {
279
+ arcAngle += Math.PI * 2;
280
+ }
281
+ else if (!sweepFlag && arcAngle > 0) {
282
+ arcAngle -= Math.PI * 2;
283
+ }
284
+ const segmentCount = Math.ceil(Math.abs(arcAngle) / (Math.PI / 2));
285
+ if (!Number.isFinite(segmentCount) || segmentCount === 0) {
286
+ return [];
287
+ }
288
+ const outputScale = Math.max(Math.abs(point1.x), Math.abs(point1.y), Math.abs(point2.x), Math.abs(point2.y), rx, ry);
289
+ const mapToEllipse = (x, y) => ({
290
+ // Do the addition in a common scale. This avoids an intermediate overflow
291
+ // when a finite endpoint is the difference of two very large values.
292
+ x: outputScale * (point1.x / outputScale
293
+ + (rx / outputScale) * cosPhi * (x - startUnitX)
294
+ - (ry / outputScale) * sinPhi * (y - startUnitY)),
295
+ y: outputScale * (point1.y / outputScale
296
+ + (rx / outputScale) * sinPhi * (x - startUnitX)
297
+ + (ry / outputScale) * cosPhi * (y - startUnitY))
298
+ });
299
+ const cubicBeziers = [];
300
+ const segmentAngle = arcAngle / segmentCount;
301
+ for (let index = 0; index < segmentCount; index++) {
302
+ const startOffset = index * segmentAngle;
303
+ const endOffset = startOffset + segmentAngle;
304
+ const startCos = Math.cos(startOffset);
305
+ const startSin = Math.sin(startOffset);
306
+ const endCos = Math.cos(endOffset);
307
+ const endSin = Math.sin(endOffset);
308
+ const startX = startUnitX * startCos - startUnitY * startSin;
309
+ const startY = startUnitX * startSin + startUnitY * startCos;
310
+ const endX = startUnitX * endCos - startUnitY * endSin;
311
+ const endY = startUnitX * endSin + startUnitY * endCos;
312
+ const alpha = (4 / 3) * Math.tan(segmentAngle / 4);
313
+ const targetPoint = index === segmentCount - 1
314
+ ? point2
315
+ : mapToEllipse(endX, endY);
316
+ cubicBeziers.push(mapToEllipse(startX - alpha * startY, startY + alpha * startX), mapToEllipse(endX + alpha * endY, endY - alpha * endX), targetPoint);
317
+ }
318
+ return cubicBeziers;
319
+ }
320
+ /** Map SVG command letters to the sampler that understands their point layout. */
321
+ export const calculators = {
322
+ a: calculateCoordinatesArc,
323
+ c: calculateCoordinatesCubic,
324
+ h: calculateCoordinatesLinear,
325
+ l: calculateCoordinatesLinear,
326
+ m: calculateCoordinatesLinear,
327
+ q: calculateCoordinatesQuad,
328
+ s: calculateCoordinatesSmoothCubic,
329
+ v: calculateCoordinatesLinear,
330
+ z: calculateCoordinatesLinear
331
+ };
@@ -0,0 +1,19 @@
1
+ import type { Point } from './calculators.js';
2
+ export declare function degToRads(deg: number): number;
3
+ export declare function radToDeg(rad: number): number;
4
+ export declare function isNullOrUndefined(value: number): boolean;
5
+ /**
6
+ * Rotates a point around the given origin
7
+ * by the specified radians and returns the
8
+ * rotated point.
9
+ *
10
+ * @param originX The x coordinate of the point to rotate around.
11
+ * @param originY The y coordinate of the point to rotate around.
12
+ * @param x The x coordinate of the point to be rotated.
13
+ * @param y The y coordinate of the point to be rotated.
14
+ * @param radiansX Radians to rotate along the x axis.
15
+ * @param radiansY Radians to rotate along the y axis.
16
+ *
17
+ * @returns {Object} The point with the rotated coordinates.
18
+ */
19
+ export declare function rotatePoint(originX: number, originY: number, x: number, y: number, radiansX: number, radiansY: number): Point;
@@ -1,40 +1,29 @@
1
- function degToRads(deg){
2
- return Math.PI * deg / 180;
3
- }
4
-
5
- function radToDeg(rad){
6
- return rad * 180 / Math.PI;
7
- }
8
-
9
- function isNullOrUndefined(value){
10
- return Boolean(value === undefined || value === null);
11
- }
12
-
13
- /**
14
- * Rotates a point around the given origin
15
- * by the specified radians and returns the
16
- * rotated point.
17
- *
18
- * @param originX The x coordinate of the point to rotate around.
19
- * @param originY The y coordinate of the point to rotate around.
20
- * @param x The x coordinate of the point to be rotated.
21
- * @param y The y coordinate of the point to be rotated.
22
- * @param radiansX Radians to rotate along the x axis.
23
- * @param radiansY Radians to rotate along the y axis.
24
- *
25
- * @returns {Object} The point with the rotated coordinates.
26
- */
27
- function rotatePoint(originX, originY, x, y, radiansX, radiansY) {
28
- const v = {x: x - originX, y: y - originY};
29
- const vx = (v.x * Math.cos(radiansX)) - (v.y * Math.sin(radiansX));
30
- const vy = (v.x * Math.sin(radiansY)) + (v.y * Math.cos(radiansY));
31
- return {x: vx + originX, y: vy + originY};
32
- }
33
-
34
-
35
- module.exports = {
36
- degToRads,
37
- isNullOrUndefined,
38
- radToDeg,
39
- rotatePoint
40
- };
1
+ export function degToRads(deg) {
2
+ return Math.PI * deg / 180;
3
+ }
4
+ export function radToDeg(rad) {
5
+ return rad * 180 / Math.PI;
6
+ }
7
+ export function isNullOrUndefined(value) {
8
+ return Boolean(value === undefined || value === null);
9
+ }
10
+ /**
11
+ * Rotates a point around the given origin
12
+ * by the specified radians and returns the
13
+ * rotated point.
14
+ *
15
+ * @param originX The x coordinate of the point to rotate around.
16
+ * @param originY The y coordinate of the point to rotate around.
17
+ * @param x The x coordinate of the point to be rotated.
18
+ * @param y The y coordinate of the point to be rotated.
19
+ * @param radiansX Radians to rotate along the x axis.
20
+ * @param radiansY Radians to rotate along the y axis.
21
+ *
22
+ * @returns {Object} The point with the rotated coordinates.
23
+ */
24
+ export function rotatePoint(originX, originY, x, y, radiansX, radiansY) {
25
+ const v = { x: x - originX, y: y - originY };
26
+ const vx = (v.x * Math.cos(radiansX)) - (v.y * Math.sin(radiansX));
27
+ const vy = (v.x * Math.sin(radiansY)) + (v.y * Math.cos(radiansY));
28
+ return { x: vx + originX, y: vy + originY };
29
+ }