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