svg-path-commander 2.2.1 → 2.2.3
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.
- package/AGENTS.md +18 -4
- package/CHANGELOG.md +34 -0
- package/README.md +15 -3
- package/dist/index.d.ts +1085 -97
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +394 -270
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +1 -1
- package/dist/util.d.ts +525 -58
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +391 -268
- package/dist/util.js.map +1 -1
- package/package.json +14 -19
package/dist/util.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* SVGPathCommander v2.2.
|
|
2
|
+
* SVGPathCommander v2.2.3 (http://thednp.github.io/svg-path-commander)
|
|
3
3
|
* Copyright 2026 © thednp
|
|
4
4
|
* Licensed under MIT (https://github.com/thednp/svg-path-commander/blob/master/LICENSE)
|
|
5
5
|
*/
|
|
@@ -97,14 +97,348 @@ const getLineBBox = (x1, y1, x2, y2) => {
|
|
|
97
97
|
max(y1, y2)
|
|
98
98
|
];
|
|
99
99
|
};
|
|
100
|
+
/** The tools for line segment computation. */
|
|
100
101
|
const lineTools = {
|
|
101
102
|
getLineBBox,
|
|
102
103
|
getLineLength,
|
|
103
104
|
getPointAtLineLength
|
|
104
105
|
};
|
|
105
106
|
//#endregion
|
|
107
|
+
//#region src/math/bezier.ts
|
|
108
|
+
/**
|
|
109
|
+
* Tools from bezier.js by Mike 'Pomax' Kamermans
|
|
110
|
+
* @see https://github.com/Pomax/bezierjs
|
|
111
|
+
*/
|
|
112
|
+
const Tvalues = [
|
|
113
|
+
-.06405689286260563,
|
|
114
|
+
.06405689286260563,
|
|
115
|
+
-.1911188674736163,
|
|
116
|
+
.1911188674736163,
|
|
117
|
+
-.3150426796961634,
|
|
118
|
+
.3150426796961634,
|
|
119
|
+
-.4337935076260451,
|
|
120
|
+
.4337935076260451,
|
|
121
|
+
-.5454214713888396,
|
|
122
|
+
.5454214713888396,
|
|
123
|
+
-.6480936519369755,
|
|
124
|
+
.6480936519369755,
|
|
125
|
+
-.7401241915785544,
|
|
126
|
+
.7401241915785544,
|
|
127
|
+
-.820001985973903,
|
|
128
|
+
.820001985973903,
|
|
129
|
+
-.8864155270044011,
|
|
130
|
+
.8864155270044011,
|
|
131
|
+
-.9382745520027328,
|
|
132
|
+
.9382745520027328,
|
|
133
|
+
-.9747285559713095,
|
|
134
|
+
.9747285559713095,
|
|
135
|
+
-.9951872199970213,
|
|
136
|
+
.9951872199970213
|
|
137
|
+
];
|
|
138
|
+
/** The weights for the 32-point Gauss-Legendre quadrature. */
|
|
139
|
+
const Cvalues = [
|
|
140
|
+
.12793819534675216,
|
|
141
|
+
.12793819534675216,
|
|
142
|
+
.1258374563468283,
|
|
143
|
+
.1258374563468283,
|
|
144
|
+
.12167047292780339,
|
|
145
|
+
.12167047292780339,
|
|
146
|
+
.1155056680537256,
|
|
147
|
+
.1155056680537256,
|
|
148
|
+
.10744427011596563,
|
|
149
|
+
.10744427011596563,
|
|
150
|
+
.09761865210411388,
|
|
151
|
+
.09761865210411388,
|
|
152
|
+
.08619016153195327,
|
|
153
|
+
.08619016153195327,
|
|
154
|
+
.0733464814110803,
|
|
155
|
+
.0733464814110803,
|
|
156
|
+
.05929858491543678,
|
|
157
|
+
.05929858491543678,
|
|
158
|
+
.04427743881741981,
|
|
159
|
+
.04427743881741981,
|
|
160
|
+
.028531388628933663,
|
|
161
|
+
.028531388628933663,
|
|
162
|
+
.0123412297999872,
|
|
163
|
+
.0123412297999872
|
|
164
|
+
];
|
|
165
|
+
/**
|
|
166
|
+
* @param points
|
|
167
|
+
* @returns
|
|
168
|
+
*/
|
|
169
|
+
const deriveBezier = (points) => {
|
|
170
|
+
const dpoints = [];
|
|
171
|
+
for (let p = points, d = p.length, c = d - 1; d > 1; d -= 1, c -= 1) {
|
|
172
|
+
const list = [];
|
|
173
|
+
for (let j = 0; j < c; j += 1) list.push({
|
|
174
|
+
x: c * (p[j + 1].x - p[j].x),
|
|
175
|
+
y: c * (p[j + 1].y - p[j].y),
|
|
176
|
+
t: 0
|
|
177
|
+
});
|
|
178
|
+
dpoints.push(list);
|
|
179
|
+
p = list;
|
|
180
|
+
}
|
|
181
|
+
return dpoints;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* @param points
|
|
185
|
+
* @param t
|
|
186
|
+
*/
|
|
187
|
+
const computeBezier = (points, t) => {
|
|
188
|
+
if (t === 0) {
|
|
189
|
+
points[0].t = 0;
|
|
190
|
+
return points[0];
|
|
191
|
+
}
|
|
192
|
+
const order = points.length - 1;
|
|
193
|
+
if (t === 1) {
|
|
194
|
+
points[order].t = 1;
|
|
195
|
+
return points[order];
|
|
196
|
+
}
|
|
197
|
+
const mt = 1 - t;
|
|
198
|
+
let p = points;
|
|
199
|
+
if (order === 0) {
|
|
200
|
+
points[0].t = t;
|
|
201
|
+
return points[0];
|
|
202
|
+
}
|
|
203
|
+
if (order === 1) return {
|
|
204
|
+
x: mt * p[0].x + t * p[1].x,
|
|
205
|
+
y: mt * p[0].y + t * p[1].y,
|
|
206
|
+
t
|
|
207
|
+
};
|
|
208
|
+
const mt2 = mt * mt;
|
|
209
|
+
const t2 = t * t;
|
|
210
|
+
let a = 0;
|
|
211
|
+
let b = 0;
|
|
212
|
+
let c = 0;
|
|
213
|
+
let d = 0;
|
|
214
|
+
if (order === 2) {
|
|
215
|
+
p = [
|
|
216
|
+
p[0],
|
|
217
|
+
p[1],
|
|
218
|
+
p[2],
|
|
219
|
+
{
|
|
220
|
+
x: 0,
|
|
221
|
+
y: 0
|
|
222
|
+
}
|
|
223
|
+
];
|
|
224
|
+
a = mt2;
|
|
225
|
+
b = mt * t * 2;
|
|
226
|
+
c = t2;
|
|
227
|
+
} else if (order === 3) {
|
|
228
|
+
a = mt2 * mt;
|
|
229
|
+
b = mt2 * t * 3;
|
|
230
|
+
c = mt * t2 * 3;
|
|
231
|
+
d = t * t2;
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
x: a * p[0].x + b * p[1].x + c * p[2].x + d * p[3].x,
|
|
235
|
+
y: a * p[0].y + b * p[1].y + c * p[2].y + d * p[3].y,
|
|
236
|
+
t
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* Calculates the speed (derivative magnitude) of a Bezier curve at parameter `t`.
|
|
241
|
+
*
|
|
242
|
+
* @param derivativeFn the curve derivative function
|
|
243
|
+
* @param t the sampled point ratio in `[0-1]`
|
|
244
|
+
* @returns the curve speed at parameter `t`
|
|
245
|
+
*/
|
|
246
|
+
const calculateBezier = (derivativeFn, t) => {
|
|
247
|
+
const d = derivativeFn(t);
|
|
248
|
+
const l = d.x * d.x + d.y * d.y;
|
|
249
|
+
return Math.sqrt(l);
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Returns the total arc length of a Bezier segment.
|
|
253
|
+
*
|
|
254
|
+
* @param derivativeFn the curve derivative function
|
|
255
|
+
* @returns the total arc length
|
|
256
|
+
*/
|
|
257
|
+
const bezierLength = (derivativeFn) => {
|
|
258
|
+
return bezierLengthAtT(derivativeFn, 1);
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* Returns the arc length of a Bezier segment from its start point
|
|
262
|
+
* up to a given `t` ratio, by integrating the curve speed `|B'(t)|`
|
|
263
|
+
* over the `[0, t]` interval with Gauss-Legendre quadrature.
|
|
264
|
+
*
|
|
265
|
+
* @param derivativeFn the curve derivative function
|
|
266
|
+
* @param t the sampled point ratio in `[0-1]`
|
|
267
|
+
* @returns the arc length up to parameter `t`
|
|
268
|
+
*/
|
|
269
|
+
const bezierLengthAtT = (derivativeFn, t) => {
|
|
270
|
+
const z = t / 2;
|
|
271
|
+
const len = Tvalues.length;
|
|
272
|
+
let sum = 0;
|
|
273
|
+
for (let i = 0, tMid; i < len; i++) {
|
|
274
|
+
tMid = z * Tvalues[i] + z;
|
|
275
|
+
sum += Cvalues[i] * calculateBezier(derivativeFn, tMid);
|
|
276
|
+
}
|
|
277
|
+
return z * sum;
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* Builds the Bezier points array from a curve coordinates array.
|
|
281
|
+
* @param curve cubic / quad bezier segment
|
|
282
|
+
*/
|
|
283
|
+
const getBezierPoints = (curve) => {
|
|
284
|
+
const points = [];
|
|
285
|
+
for (let idx = 0, len = curve.length, step = 2; idx < len; idx += step) points.push({
|
|
286
|
+
x: curve[idx],
|
|
287
|
+
y: curve[idx + 1]
|
|
288
|
+
});
|
|
289
|
+
return points;
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Returns the arc length of a Bezier segment from its start point
|
|
293
|
+
* up to a given `t` ratio, for the segment given by its coordinates.
|
|
294
|
+
*
|
|
295
|
+
* @param curve cubic / quad bezier segment
|
|
296
|
+
* @param t the sampled point ratio in `[0-1]`
|
|
297
|
+
* @returns the arc length up to parameter `t`
|
|
298
|
+
*/
|
|
299
|
+
const getBezierLengthAtT = (curve, t) => {
|
|
300
|
+
const dpoints = deriveBezier(getBezierPoints(curve));
|
|
301
|
+
return bezierLengthAtT((ratio) => {
|
|
302
|
+
return computeBezier(dpoints[0], ratio);
|
|
303
|
+
}, t);
|
|
304
|
+
};
|
|
305
|
+
/**
|
|
306
|
+
* Returns the `t` parameter of a Bezier segment at which the cumulative
|
|
307
|
+
* arc length equals the given `distance`, found by interval bisection.
|
|
308
|
+
*
|
|
309
|
+
* @param curve cubic / quad bezier segment
|
|
310
|
+
* @param distance the distance along the segment in `[0, length]`
|
|
311
|
+
* @returns the `t` parameter in `[0-1]`
|
|
312
|
+
*/
|
|
313
|
+
const getTAtBezierLength = (curve, distance) => {
|
|
314
|
+
const dpoints = deriveBezier(getBezierPoints(curve));
|
|
315
|
+
const derivativeFn = (ratio) => {
|
|
316
|
+
return computeBezier(dpoints[0], ratio);
|
|
317
|
+
};
|
|
318
|
+
let lo = 0;
|
|
319
|
+
let hi = 1;
|
|
320
|
+
for (let i = 0; i < 25; i += 1) {
|
|
321
|
+
const mid = (lo + hi) / 2;
|
|
322
|
+
if (bezierLengthAtT(derivativeFn, mid) < distance) lo = mid;
|
|
323
|
+
else hi = mid;
|
|
324
|
+
}
|
|
325
|
+
return (lo + hi) / 2;
|
|
326
|
+
};
|
|
327
|
+
/**
|
|
328
|
+
* Returns the length of CubicBezier / Quad segment.
|
|
329
|
+
* @param curve cubic / quad bezier segment
|
|
330
|
+
*/
|
|
331
|
+
const getBezierLength = (curve) => {
|
|
332
|
+
const points = getBezierPoints(curve);
|
|
333
|
+
const dpoints = deriveBezier(points);
|
|
334
|
+
return bezierLength((t) => {
|
|
335
|
+
return computeBezier(dpoints[0], t);
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
/** The precision threshold to consider a cubic polynomial as quadratic. */
|
|
339
|
+
const CBEZIER_MINMAX_EPSILON = 1e-8;
|
|
340
|
+
/**
|
|
341
|
+
* Returns the most extreme points in a Quad Bezier segment.
|
|
342
|
+
* @param A an array which consist of X/Y values
|
|
343
|
+
*/
|
|
344
|
+
const minmaxQ = ([v1, cp, v2]) => {
|
|
345
|
+
const min = Math.min(v1, v2);
|
|
346
|
+
const max = Math.max(v1, v2);
|
|
347
|
+
if (cp >= v1 ? v2 >= cp : v2 <= cp) return [min, max];
|
|
348
|
+
const E = (v1 * v2 - cp * cp) / (v1 - 2 * cp + v2);
|
|
349
|
+
return E < min ? [E, max] : [min, E];
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* Returns the most extreme points in a Cubic Bezier segment.
|
|
353
|
+
* @param A an array which consist of X/Y values
|
|
354
|
+
* @see https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127
|
|
355
|
+
*/
|
|
356
|
+
const minmaxC = ([v1, cp1, cp2, v2]) => {
|
|
357
|
+
const K = v1 - 3 * cp1 + 3 * cp2 - v2;
|
|
358
|
+
if (Math.abs(K) < 1e-8) {
|
|
359
|
+
if (v1 === v2 && v1 === cp1) return [v1, v2];
|
|
360
|
+
return minmaxQ([
|
|
361
|
+
v1,
|
|
362
|
+
-.5 * v1 + 1.5 * cp1,
|
|
363
|
+
v1 - 3 * cp1 + 3 * cp2
|
|
364
|
+
]);
|
|
365
|
+
}
|
|
366
|
+
const T = -v1 * cp2 + v1 * v2 - cp1 * cp2 - cp1 * v2 + cp1 * cp1 + cp2 * cp2;
|
|
367
|
+
if (T <= 0) return [Math.min(v1, v2), Math.max(v1, v2)];
|
|
368
|
+
const S = Math.sqrt(T);
|
|
369
|
+
let min = Math.min(v1, v2);
|
|
370
|
+
let max = Math.max(v1, v2);
|
|
371
|
+
const L = v1 - 2 * cp1 + cp2;
|
|
372
|
+
for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) if (R > 0 && R < 1) {
|
|
373
|
+
const Q = v1 * (1 - R) * (1 - R) * (1 - R) + cp1 * 3 * (1 - R) * (1 - R) * R + cp2 * 3 * (1 - R) * R * R + v2 * R * R * R;
|
|
374
|
+
if (Q < min) min = Q;
|
|
375
|
+
if (Q > max) max = Q;
|
|
376
|
+
}
|
|
377
|
+
return [min, max];
|
|
378
|
+
};
|
|
379
|
+
/** The tools for Bezier curve computation. */
|
|
380
|
+
const bezierTools = {
|
|
381
|
+
bezierLength,
|
|
382
|
+
bezierLengthAtT,
|
|
383
|
+
calculateBezier,
|
|
384
|
+
CBEZIER_MINMAX_EPSILON,
|
|
385
|
+
computeBezier,
|
|
386
|
+
Cvalues,
|
|
387
|
+
deriveBezier,
|
|
388
|
+
getBezierLength,
|
|
389
|
+
getBezierLengthAtT,
|
|
390
|
+
getBezierPoints,
|
|
391
|
+
getTAtBezierLength,
|
|
392
|
+
minmaxC,
|
|
393
|
+
minmaxQ,
|
|
394
|
+
Tvalues
|
|
395
|
+
};
|
|
396
|
+
//#endregion
|
|
106
397
|
//#region src/math/arcTools.ts
|
|
107
398
|
/**
|
|
399
|
+
* Returns the signed arc length of an ellipse segment from angle `0` up to
|
|
400
|
+
* the given `theta` angle, by integrating the curve speed
|
|
401
|
+
* `|sqrt(rx^2 * sin^2(phi) + ry^2 * cos^2(phi))|` over the `[0, theta]`
|
|
402
|
+
* interval with Gauss-Legendre quadrature.
|
|
403
|
+
*
|
|
404
|
+
* @param rx radius along X axis
|
|
405
|
+
* @param ry radius along Y axis
|
|
406
|
+
* @param theta the angle in radians
|
|
407
|
+
* @returns the signed arc length
|
|
408
|
+
*/
|
|
409
|
+
const arcLengthAtAngle = (rx, ry, theta) => {
|
|
410
|
+
const sign = theta < 0 ? -1 : 1;
|
|
411
|
+
const absTheta = Math.abs(theta);
|
|
412
|
+
const step = Math.PI / 2;
|
|
413
|
+
let total = 0;
|
|
414
|
+
let remaining = absTheta;
|
|
415
|
+
let offset = 0;
|
|
416
|
+
while (remaining > 0) {
|
|
417
|
+
const span = Math.min(step, remaining);
|
|
418
|
+
const z = span / 2;
|
|
419
|
+
let sum = 0;
|
|
420
|
+
for (let i = 0, len = Tvalues.length; i < len; i += 1) {
|
|
421
|
+
const phi = offset + z * Tvalues[i] + z;
|
|
422
|
+
sum += Cvalues[i] * Math.sqrt((rx * Math.sin(phi)) ** 2 + (ry * Math.cos(phi)) ** 2);
|
|
423
|
+
}
|
|
424
|
+
total += z * sum;
|
|
425
|
+
offset += span;
|
|
426
|
+
remaining -= span;
|
|
427
|
+
}
|
|
428
|
+
return sign * total;
|
|
429
|
+
};
|
|
430
|
+
/**
|
|
431
|
+
* Returns the signed arc length of an ellipse segment between two angles.
|
|
432
|
+
* @param rx radius along X axis
|
|
433
|
+
* @param ry radius along Y axis
|
|
434
|
+
* @param from the start angle in radians
|
|
435
|
+
* @param to the end angle in radians
|
|
436
|
+
* @returns the signed arc length
|
|
437
|
+
*/
|
|
438
|
+
const arcLengthBetween = (rx, ry, from, to) => {
|
|
439
|
+
return arcLengthAtAngle(rx, ry, to) - arcLengthAtAngle(rx, ry, from);
|
|
440
|
+
};
|
|
441
|
+
/**
|
|
108
442
|
* Returns the Arc segment length.
|
|
109
443
|
* @param rx radius along X axis
|
|
110
444
|
* @param ry radius along Y axis
|
|
@@ -112,13 +446,7 @@ const lineTools = {
|
|
|
112
446
|
* @returns the arc length
|
|
113
447
|
*/
|
|
114
448
|
const arcLength = (rx, ry, theta) => {
|
|
115
|
-
|
|
116
|
-
const sinHalfTheta = Math.sin(halfTheta);
|
|
117
|
-
const cosHalfTheta = Math.cos(halfTheta);
|
|
118
|
-
const term1 = rx ** 2 * sinHalfTheta ** 2;
|
|
119
|
-
const term2 = ry ** 2 * cosHalfTheta ** 2;
|
|
120
|
-
const length = Math.sqrt(term1 + term2) * theta;
|
|
121
|
-
return Math.abs(length);
|
|
449
|
+
return Math.abs(arcLengthAtAngle(rx, ry, theta));
|
|
122
450
|
};
|
|
123
451
|
/**
|
|
124
452
|
* Find point on ellipse at given angle around ellipse (theta);
|
|
@@ -221,10 +549,11 @@ const getArcProps = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
221
549
|
x: 1,
|
|
222
550
|
y: 0
|
|
223
551
|
}, startVector);
|
|
224
|
-
|
|
552
|
+
const endVector = {
|
|
225
553
|
x: (-transformedPoint.x - transformedCenter.x) / rx,
|
|
226
554
|
y: (-transformedPoint.y - transformedCenter.y) / ry
|
|
227
|
-
}
|
|
555
|
+
};
|
|
556
|
+
let sweepAngle = angleBetween(startVector, endVector);
|
|
228
557
|
if (!SF && sweepAngle > 0) sweepAngle -= 2 * PI;
|
|
229
558
|
else if (SF && sweepAngle < 0) sweepAngle += 2 * PI;
|
|
230
559
|
sweepAngle %= 2 * PI;
|
|
@@ -251,7 +580,7 @@ const getArcProps = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
251
580
|
*/
|
|
252
581
|
const getArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
253
582
|
const { rx, ry, startAngle, endAngle } = getArcProps(x1, y1, RX, RY, angle, LAF, SF, x, y);
|
|
254
|
-
return
|
|
583
|
+
return Math.abs(arcLengthBetween(rx, ry, startAngle, endAngle));
|
|
255
584
|
};
|
|
256
585
|
/**
|
|
257
586
|
* Returns a point along an Arc segment at a given distance.
|
|
@@ -275,7 +604,8 @@ const getPointAtArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y, distance) =>
|
|
|
275
604
|
};
|
|
276
605
|
const { center, rx, ry, startAngle, endAngle } = getArcProps(x1, y1, RX, RY, angle, LAF, SF, x, y);
|
|
277
606
|
if (typeof distance === "number") {
|
|
278
|
-
const
|
|
607
|
+
const sweepAngle = endAngle - startAngle;
|
|
608
|
+
const length = Math.abs(arcLengthBetween(rx, ry, startAngle, endAngle));
|
|
279
609
|
if (distance <= 0) point = {
|
|
280
610
|
x: x1,
|
|
281
611
|
y: y1
|
|
@@ -291,9 +621,15 @@ const getPointAtArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y, distance) =>
|
|
|
291
621
|
};
|
|
292
622
|
if (rx === 0 || ry === 0) return getPointAtLineLength(x1, y1, x, y, distance);
|
|
293
623
|
const { PI, cos, sin } = Math;
|
|
294
|
-
const sweepAngle = endAngle - startAngle;
|
|
295
624
|
const xRotRad = (angle % 360 + 360) % 360 * (PI / 180);
|
|
296
|
-
|
|
625
|
+
let lo = 0;
|
|
626
|
+
let hi = 1;
|
|
627
|
+
for (let i = 0; i < 25; i += 1) {
|
|
628
|
+
const mid = (lo + hi) / 2;
|
|
629
|
+
if (Math.abs(arcLengthBetween(rx, ry, startAngle, startAngle + sweepAngle * mid)) < distance) lo = mid;
|
|
630
|
+
else hi = mid;
|
|
631
|
+
}
|
|
632
|
+
const alpha = startAngle + sweepAngle * ((lo + hi) / 2);
|
|
297
633
|
const ellipseComponentX = rx * cos(alpha);
|
|
298
634
|
const ellipseComponentY = ry * sin(alpha);
|
|
299
635
|
point = {
|
|
@@ -343,8 +679,10 @@ const getArcBBox = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
343
679
|
let xMax = max(x1, x);
|
|
344
680
|
let yMin = min(y1, y);
|
|
345
681
|
let yMax = max(y1, y);
|
|
346
|
-
const
|
|
347
|
-
const
|
|
682
|
+
const angleAfterStart = endAngle - deltaAngle * 1e-5;
|
|
683
|
+
const pP2 = arcPoint(cx, cy, rx, ry, alpha, angleAfterStart);
|
|
684
|
+
const angleBeforeEnd = endAngle - deltaAngle * .99999;
|
|
685
|
+
const pP3 = arcPoint(cx, cy, rx, ry, alpha, angleBeforeEnd);
|
|
348
686
|
/**
|
|
349
687
|
* expected extremes
|
|
350
688
|
* if leaving inner bounding box
|
|
@@ -382,9 +720,12 @@ const getArcBBox = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
382
720
|
yMax
|
|
383
721
|
];
|
|
384
722
|
};
|
|
723
|
+
/** The tools for elliptical arc computation. */
|
|
385
724
|
const arcTools = {
|
|
386
725
|
angleBetween,
|
|
387
726
|
arcLength,
|
|
727
|
+
arcLengthAtAngle,
|
|
728
|
+
arcLengthBetween,
|
|
388
729
|
arcPoint,
|
|
389
730
|
getArcBBox,
|
|
390
731
|
getArcLength,
|
|
@@ -392,220 +733,6 @@ const arcTools = {
|
|
|
392
733
|
getPointAtArcLength
|
|
393
734
|
};
|
|
394
735
|
//#endregion
|
|
395
|
-
//#region src/math/bezier.ts
|
|
396
|
-
/**
|
|
397
|
-
* Tools from bezier.js by Mike 'Pomax' Kamermans
|
|
398
|
-
* @see https://github.com/Pomax/bezierjs
|
|
399
|
-
*/
|
|
400
|
-
const Tvalues = [
|
|
401
|
-
-.06405689286260563,
|
|
402
|
-
.06405689286260563,
|
|
403
|
-
-.1911188674736163,
|
|
404
|
-
.1911188674736163,
|
|
405
|
-
-.3150426796961634,
|
|
406
|
-
.3150426796961634,
|
|
407
|
-
-.4337935076260451,
|
|
408
|
-
.4337935076260451,
|
|
409
|
-
-.5454214713888396,
|
|
410
|
-
.5454214713888396,
|
|
411
|
-
-.6480936519369755,
|
|
412
|
-
.6480936519369755,
|
|
413
|
-
-.7401241915785544,
|
|
414
|
-
.7401241915785544,
|
|
415
|
-
-.820001985973903,
|
|
416
|
-
.820001985973903,
|
|
417
|
-
-.8864155270044011,
|
|
418
|
-
.8864155270044011,
|
|
419
|
-
-.9382745520027328,
|
|
420
|
-
.9382745520027328,
|
|
421
|
-
-.9747285559713095,
|
|
422
|
-
.9747285559713095,
|
|
423
|
-
-.9951872199970213,
|
|
424
|
-
.9951872199970213
|
|
425
|
-
];
|
|
426
|
-
const Cvalues = [
|
|
427
|
-
.12793819534675216,
|
|
428
|
-
.12793819534675216,
|
|
429
|
-
.1258374563468283,
|
|
430
|
-
.1258374563468283,
|
|
431
|
-
.12167047292780339,
|
|
432
|
-
.12167047292780339,
|
|
433
|
-
.1155056680537256,
|
|
434
|
-
.1155056680537256,
|
|
435
|
-
.10744427011596563,
|
|
436
|
-
.10744427011596563,
|
|
437
|
-
.09761865210411388,
|
|
438
|
-
.09761865210411388,
|
|
439
|
-
.08619016153195327,
|
|
440
|
-
.08619016153195327,
|
|
441
|
-
.0733464814110803,
|
|
442
|
-
.0733464814110803,
|
|
443
|
-
.05929858491543678,
|
|
444
|
-
.05929858491543678,
|
|
445
|
-
.04427743881741981,
|
|
446
|
-
.04427743881741981,
|
|
447
|
-
.028531388628933663,
|
|
448
|
-
.028531388628933663,
|
|
449
|
-
.0123412297999872,
|
|
450
|
-
.0123412297999872
|
|
451
|
-
];
|
|
452
|
-
/**
|
|
453
|
-
* @param points
|
|
454
|
-
* @returns
|
|
455
|
-
*/
|
|
456
|
-
const deriveBezier = (points) => {
|
|
457
|
-
const dpoints = [];
|
|
458
|
-
for (let p = points, d = p.length, c = d - 1; d > 1; d -= 1, c -= 1) {
|
|
459
|
-
const list = [];
|
|
460
|
-
for (let j = 0; j < c; j += 1) list.push({
|
|
461
|
-
x: c * (p[j + 1].x - p[j].x),
|
|
462
|
-
y: c * (p[j + 1].y - p[j].y),
|
|
463
|
-
t: 0
|
|
464
|
-
});
|
|
465
|
-
dpoints.push(list);
|
|
466
|
-
p = list;
|
|
467
|
-
}
|
|
468
|
-
return dpoints;
|
|
469
|
-
};
|
|
470
|
-
/**
|
|
471
|
-
* @param points
|
|
472
|
-
* @param t
|
|
473
|
-
*/
|
|
474
|
-
const computeBezier = (points, t) => {
|
|
475
|
-
if (t === 0) {
|
|
476
|
-
points[0].t = 0;
|
|
477
|
-
return points[0];
|
|
478
|
-
}
|
|
479
|
-
const order = points.length - 1;
|
|
480
|
-
if (t === 1) {
|
|
481
|
-
points[order].t = 1;
|
|
482
|
-
return points[order];
|
|
483
|
-
}
|
|
484
|
-
const mt = 1 - t;
|
|
485
|
-
let p = points;
|
|
486
|
-
if (order === 0) {
|
|
487
|
-
points[0].t = t;
|
|
488
|
-
return points[0];
|
|
489
|
-
}
|
|
490
|
-
if (order === 1) return {
|
|
491
|
-
x: mt * p[0].x + t * p[1].x,
|
|
492
|
-
y: mt * p[0].y + t * p[1].y,
|
|
493
|
-
t
|
|
494
|
-
};
|
|
495
|
-
const mt2 = mt * mt;
|
|
496
|
-
const t2 = t * t;
|
|
497
|
-
let a = 0;
|
|
498
|
-
let b = 0;
|
|
499
|
-
let c = 0;
|
|
500
|
-
let d = 0;
|
|
501
|
-
if (order === 2) {
|
|
502
|
-
p = [
|
|
503
|
-
p[0],
|
|
504
|
-
p[1],
|
|
505
|
-
p[2],
|
|
506
|
-
{
|
|
507
|
-
x: 0,
|
|
508
|
-
y: 0
|
|
509
|
-
}
|
|
510
|
-
];
|
|
511
|
-
a = mt2;
|
|
512
|
-
b = mt * t * 2;
|
|
513
|
-
c = t2;
|
|
514
|
-
} else if (order === 3) {
|
|
515
|
-
a = mt2 * mt;
|
|
516
|
-
b = mt2 * t * 3;
|
|
517
|
-
c = mt * t2 * 3;
|
|
518
|
-
d = t * t2;
|
|
519
|
-
}
|
|
520
|
-
return {
|
|
521
|
-
x: a * p[0].x + b * p[1].x + c * p[2].x + d * p[3].x,
|
|
522
|
-
y: a * p[0].y + b * p[1].y + c * p[2].y + d * p[3].y,
|
|
523
|
-
t
|
|
524
|
-
};
|
|
525
|
-
};
|
|
526
|
-
const calculateBezier = (derivativeFn, t) => {
|
|
527
|
-
const d = derivativeFn(t);
|
|
528
|
-
const l = d.x * d.x + d.y * d.y;
|
|
529
|
-
return Math.sqrt(l);
|
|
530
|
-
};
|
|
531
|
-
const bezierLength = (derivativeFn) => {
|
|
532
|
-
const z = .5;
|
|
533
|
-
const len = Tvalues.length;
|
|
534
|
-
let sum = 0;
|
|
535
|
-
for (let i = 0, t; i < len; i++) {
|
|
536
|
-
t = z * Tvalues[i] + z;
|
|
537
|
-
sum += Cvalues[i] * calculateBezier(derivativeFn, t);
|
|
538
|
-
}
|
|
539
|
-
return z * sum;
|
|
540
|
-
};
|
|
541
|
-
/**
|
|
542
|
-
* Returns the length of CubicBezier / Quad segment.
|
|
543
|
-
* @param curve cubic / quad bezier segment
|
|
544
|
-
*/
|
|
545
|
-
const getBezierLength = (curve) => {
|
|
546
|
-
const points = [];
|
|
547
|
-
for (let idx = 0, len = curve.length, step = 2; idx < len; idx += step) points.push({
|
|
548
|
-
x: curve[idx],
|
|
549
|
-
y: curve[idx + 1]
|
|
550
|
-
});
|
|
551
|
-
const dpoints = deriveBezier(points);
|
|
552
|
-
return bezierLength((t) => {
|
|
553
|
-
return computeBezier(dpoints[0], t);
|
|
554
|
-
});
|
|
555
|
-
};
|
|
556
|
-
const CBEZIER_MINMAX_EPSILON = 1e-8;
|
|
557
|
-
/**
|
|
558
|
-
* Returns the most extreme points in a Quad Bezier segment.
|
|
559
|
-
* @param A an array which consist of X/Y values
|
|
560
|
-
*/
|
|
561
|
-
const minmaxQ = ([v1, cp, v2]) => {
|
|
562
|
-
const min = Math.min(v1, v2);
|
|
563
|
-
const max = Math.max(v1, v2);
|
|
564
|
-
if (cp >= v1 ? v2 >= cp : v2 <= cp) return [min, max];
|
|
565
|
-
const E = (v1 * v2 - cp * cp) / (v1 - 2 * cp + v2);
|
|
566
|
-
return E < min ? [E, max] : [min, E];
|
|
567
|
-
};
|
|
568
|
-
/**
|
|
569
|
-
* Returns the most extreme points in a Cubic Bezier segment.
|
|
570
|
-
* @param A an array which consist of X/Y values
|
|
571
|
-
* @see https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127
|
|
572
|
-
*/
|
|
573
|
-
const minmaxC = ([v1, cp1, cp2, v2]) => {
|
|
574
|
-
const K = v1 - 3 * cp1 + 3 * cp2 - v2;
|
|
575
|
-
if (Math.abs(K) < 1e-8) {
|
|
576
|
-
if (v1 === v2 && v1 === cp1) return [v1, v2];
|
|
577
|
-
return minmaxQ([
|
|
578
|
-
v1,
|
|
579
|
-
-.5 * v1 + 1.5 * cp1,
|
|
580
|
-
v1 - 3 * cp1 + 3 * cp2
|
|
581
|
-
]);
|
|
582
|
-
}
|
|
583
|
-
const T = -v1 * cp2 + v1 * v2 - cp1 * cp2 - cp1 * v2 + cp1 * cp1 + cp2 * cp2;
|
|
584
|
-
if (T <= 0) return [Math.min(v1, v2), Math.max(v1, v2)];
|
|
585
|
-
const S = Math.sqrt(T);
|
|
586
|
-
let min = Math.min(v1, v2);
|
|
587
|
-
let max = Math.max(v1, v2);
|
|
588
|
-
const L = v1 - 2 * cp1 + cp2;
|
|
589
|
-
for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) if (R > 0 && R < 1) {
|
|
590
|
-
const Q = v1 * (1 - R) * (1 - R) * (1 - R) + cp1 * 3 * (1 - R) * (1 - R) * R + cp2 * 3 * (1 - R) * R * R + v2 * R * R * R;
|
|
591
|
-
if (Q < min) min = Q;
|
|
592
|
-
if (Q > max) max = Q;
|
|
593
|
-
}
|
|
594
|
-
return [min, max];
|
|
595
|
-
};
|
|
596
|
-
const bezierTools = {
|
|
597
|
-
bezierLength,
|
|
598
|
-
calculateBezier,
|
|
599
|
-
CBEZIER_MINMAX_EPSILON,
|
|
600
|
-
computeBezier,
|
|
601
|
-
Cvalues,
|
|
602
|
-
deriveBezier,
|
|
603
|
-
getBezierLength,
|
|
604
|
-
minmaxC,
|
|
605
|
-
minmaxQ,
|
|
606
|
-
Tvalues
|
|
607
|
-
};
|
|
608
|
-
//#endregion
|
|
609
736
|
//#region src/math/cubicTools.ts
|
|
610
737
|
/**
|
|
611
738
|
* Returns a point at a given length of a CubicBezier segment.
|
|
@@ -674,7 +801,7 @@ const getPointAtCubicLength = (x1, y1, c1x, c1y, c2x, c2y, x2, y2, distance) =>
|
|
|
674
801
|
y: y1
|
|
675
802
|
};
|
|
676
803
|
if (distanceIsNumber) {
|
|
677
|
-
const
|
|
804
|
+
const coords = [
|
|
678
805
|
x1,
|
|
679
806
|
y1,
|
|
680
807
|
c1x,
|
|
@@ -683,21 +810,16 @@ const getPointAtCubicLength = (x1, y1, c1x, c1y, c2x, c2y, x2, y2, distance) =>
|
|
|
683
810
|
c2y,
|
|
684
811
|
x2,
|
|
685
812
|
y2
|
|
686
|
-
]
|
|
813
|
+
];
|
|
814
|
+
const currentLength = getBezierLength(coords);
|
|
687
815
|
if (distance <= 0) {} else if (distance >= currentLength) point = {
|
|
688
816
|
x: x2,
|
|
689
817
|
y: y2
|
|
690
818
|
};
|
|
691
|
-
else
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
c1y,
|
|
696
|
-
c2x,
|
|
697
|
-
c2y,
|
|
698
|
-
x2,
|
|
699
|
-
y2
|
|
700
|
-
], distance / currentLength);
|
|
819
|
+
else {
|
|
820
|
+
const t = getTAtBezierLength(coords, distance);
|
|
821
|
+
point = getPointAtCubicSegmentLength(coords, t);
|
|
822
|
+
}
|
|
701
823
|
}
|
|
702
824
|
return point;
|
|
703
825
|
};
|
|
@@ -735,6 +857,7 @@ const getCubicBBox = (x1, y1, c1x, c1y, c2x, c2y, x2, y2) => {
|
|
|
735
857
|
cyMinMax[1]
|
|
736
858
|
];
|
|
737
859
|
};
|
|
860
|
+
/** The tools for cubic Bezier computation. */
|
|
738
861
|
const cubicTools = {
|
|
739
862
|
getCubicBBox,
|
|
740
863
|
getCubicLength,
|
|
@@ -805,26 +928,23 @@ const getPointAtQuadLength = (x1, y1, cx, cy, x2, y2, distance) => {
|
|
|
805
928
|
y: y1
|
|
806
929
|
};
|
|
807
930
|
if (distanceIsNumber) {
|
|
808
|
-
const
|
|
931
|
+
const coords = [
|
|
809
932
|
x1,
|
|
810
933
|
y1,
|
|
811
934
|
cx,
|
|
812
935
|
cy,
|
|
813
936
|
x2,
|
|
814
937
|
y2
|
|
815
|
-
]
|
|
938
|
+
];
|
|
939
|
+
const currentLength = getBezierLength(coords);
|
|
816
940
|
if (distance <= 0) {} else if (distance >= currentLength) point = {
|
|
817
941
|
x: x2,
|
|
818
942
|
y: y2
|
|
819
943
|
};
|
|
820
|
-
else
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
cy,
|
|
825
|
-
x2,
|
|
826
|
-
y2
|
|
827
|
-
], distance / currentLength);
|
|
944
|
+
else {
|
|
945
|
+
const t = getTAtBezierLength(coords, distance);
|
|
946
|
+
point = getPointAtQuadSegmentLength(coords, t);
|
|
947
|
+
}
|
|
828
948
|
}
|
|
829
949
|
return point;
|
|
830
950
|
};
|
|
@@ -858,6 +978,7 @@ const getQuadBBox = (x1, y1, cx, cy, x2, y2) => {
|
|
|
858
978
|
cyMinMax[1]
|
|
859
979
|
];
|
|
860
980
|
};
|
|
981
|
+
/** The tools for quadratic Bezier computation. */
|
|
861
982
|
const quadTools = {
|
|
862
983
|
getPointAtQuadLength,
|
|
863
984
|
getPointAtQuadSegmentLength,
|
|
@@ -921,6 +1042,7 @@ const polygonCentroid = (polygon) => {
|
|
|
921
1042
|
const count = polygon.length;
|
|
922
1043
|
return [sumX / count, sumY / count];
|
|
923
1044
|
};
|
|
1045
|
+
/** The tools for polygon computation. */
|
|
924
1046
|
const polygonTools = {
|
|
925
1047
|
polygonArea,
|
|
926
1048
|
polygonLength,
|
|
@@ -1432,7 +1554,8 @@ const iterate = (path, iterator) => {
|
|
|
1432
1554
|
* ```
|
|
1433
1555
|
*/
|
|
1434
1556
|
const pathToAbsolute = (pathInput) => {
|
|
1435
|
-
|
|
1557
|
+
const path = parsePathString(pathInput);
|
|
1558
|
+
return iterate(path, absolutizeSegment);
|
|
1436
1559
|
};
|
|
1437
1560
|
//#endregion
|
|
1438
1561
|
//#region src/process/relativizeSegment.ts
|
|
@@ -1489,7 +1612,8 @@ const relativizeSegment = (segment, index, lastX, lastY) => {
|
|
|
1489
1612
|
* ```
|
|
1490
1613
|
*/
|
|
1491
1614
|
const pathToRelative = (pathInput) => {
|
|
1492
|
-
|
|
1615
|
+
const path = parsePathString(pathInput);
|
|
1616
|
+
return iterate(path, relativizeSegment);
|
|
1493
1617
|
};
|
|
1494
1618
|
//#endregion
|
|
1495
1619
|
//#region src/process/arcToCubic.ts
|
|
@@ -2091,8 +2215,6 @@ const getPointAtLength = (pathInput, distance) => {
|
|
|
2091
2215
|
const path = normalizePath(pathInput);
|
|
2092
2216
|
let isM = false;
|
|
2093
2217
|
let data = [];
|
|
2094
|
-
let x = 0;
|
|
2095
|
-
let y = 0;
|
|
2096
2218
|
let [mx, my] = path[0].slice(1);
|
|
2097
2219
|
const distanceIsNumber = typeof distance === "number";
|
|
2098
2220
|
let point = {
|
|
@@ -2133,21 +2255,13 @@ const getPointAtLength = (pathInput, distance) => {
|
|
|
2133
2255
|
mx,
|
|
2134
2256
|
my
|
|
2135
2257
|
];
|
|
2136
|
-
point =
|
|
2137
|
-
x: mx,
|
|
2138
|
-
y: my
|
|
2139
|
-
};
|
|
2258
|
+
point = getPointAtLineLength(data[0], data[1], data[2], data[3], distance - totalLength);
|
|
2140
2259
|
length = getLineLength(data[0], data[1], data[2], data[3]);
|
|
2141
2260
|
}
|
|
2142
|
-
[x, y] = data.slice(-2);
|
|
2143
2261
|
if (totalLength < distance) POINT = point;
|
|
2144
2262
|
else return false;
|
|
2145
2263
|
totalLength += length;
|
|
2146
2264
|
});
|
|
2147
|
-
if (distance > totalLength - 1e-5) return {
|
|
2148
|
-
x,
|
|
2149
|
-
y
|
|
2150
|
-
};
|
|
2151
2265
|
return POINT;
|
|
2152
2266
|
};
|
|
2153
2267
|
//#endregion
|
|
@@ -2507,7 +2621,7 @@ const isMultiPath = (path) => {
|
|
|
2507
2621
|
}
|
|
2508
2622
|
return false;
|
|
2509
2623
|
}
|
|
2510
|
-
throw new TypeError(
|
|
2624
|
+
throw new TypeError("SVGPathCommanderError: expected string or PathArray");
|
|
2511
2625
|
};
|
|
2512
2626
|
//#endregion
|
|
2513
2627
|
//#region src/util/isClosedPath.ts
|
|
@@ -2991,7 +3105,8 @@ const optimizePath = (pathInput, roundOption) => {
|
|
|
2991
3105
|
const shortSegment = shortenSegment(seg, normalizedSegment, optimParams, prevCommand);
|
|
2992
3106
|
const absSegment = roundSegment(shortSegment, round);
|
|
2993
3107
|
const absString = absSegment.join("");
|
|
2994
|
-
const
|
|
3108
|
+
const relativeSegment = relativizeSegment(shortSegment, i, lastX, lastY);
|
|
3109
|
+
const relSegment = roundSegment(relativeSegment, round);
|
|
2995
3110
|
const relString = relSegment.join("");
|
|
2996
3111
|
result = absString.length < relString.length ? absSegment : relSegment;
|
|
2997
3112
|
}
|
|
@@ -3792,7 +3907,7 @@ function getRotations(a) {
|
|
|
3792
3907
|
*/
|
|
3793
3908
|
function getRotatedPath(pathA, pathB, computedRotations) {
|
|
3794
3909
|
const rotations = computedRotations || getRotations(pathA);
|
|
3795
|
-
if (pathA.length !== pathB.length) throw new TypeError(
|
|
3910
|
+
if (pathA.length !== pathB.length) throw new TypeError("SVGPathCommanderError: paths must have the same number of segments after equalization");
|
|
3796
3911
|
let bestIndex = 0;
|
|
3797
3912
|
let minDistanceSq = Infinity;
|
|
3798
3913
|
for (let ri = 0; ri < rotations.length; ri++) {
|
|
@@ -4127,6 +4242,14 @@ const intersect = (x1, y1, x2, y2, x3, y3, x4, y4) => {
|
|
|
4127
4242
|
y: py
|
|
4128
4243
|
};
|
|
4129
4244
|
};
|
|
4245
|
+
/**
|
|
4246
|
+
* Returns the intersection point(s) of two cubic Bezier curves.
|
|
4247
|
+
*
|
|
4248
|
+
* @param bez1 the coordinates of the first cubic Bezier segment
|
|
4249
|
+
* @param bez2 the coordinates of the second cubic Bezier segment
|
|
4250
|
+
* @param config optional intersection options
|
|
4251
|
+
* @returns the intersection points, or `undefined` if the curves do not intersect
|
|
4252
|
+
*/
|
|
4130
4253
|
const interHelper = (bez1, bez2, config) => {
|
|
4131
4254
|
const bbox1 = getCubicBBox(...bez1);
|
|
4132
4255
|
const bbox2 = getCubicBBox(...bez2);
|