svg-path-commander 2.2.0 → 2.2.2
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 +972 -97
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +533 -432
- 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 +430 -76
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +530 -430
- package/dist/util.js.map +1 -1
- package/package.json +36 -34
- package/tsdown.config.ts +0 -75
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* SVGPathCommander v2.2.
|
|
2
|
+
* SVGPathCommander v2.2.2 (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.2";
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region src/math/midPoint.ts
|
|
11
11
|
/**
|
|
@@ -106,8 +106,325 @@ const lineTools = {
|
|
|
106
106
|
getPointAtLineLength
|
|
107
107
|
};
|
|
108
108
|
//#endregion
|
|
109
|
+
//#region src/math/bezier.ts
|
|
110
|
+
/**
|
|
111
|
+
* Tools from bezier.js by Mike 'Pomax' Kamermans
|
|
112
|
+
* @see https://github.com/Pomax/bezierjs
|
|
113
|
+
*/
|
|
114
|
+
const Tvalues = [
|
|
115
|
+
-.06405689286260563,
|
|
116
|
+
.06405689286260563,
|
|
117
|
+
-.1911188674736163,
|
|
118
|
+
.1911188674736163,
|
|
119
|
+
-.3150426796961634,
|
|
120
|
+
.3150426796961634,
|
|
121
|
+
-.4337935076260451,
|
|
122
|
+
.4337935076260451,
|
|
123
|
+
-.5454214713888396,
|
|
124
|
+
.5454214713888396,
|
|
125
|
+
-.6480936519369755,
|
|
126
|
+
.6480936519369755,
|
|
127
|
+
-.7401241915785544,
|
|
128
|
+
.7401241915785544,
|
|
129
|
+
-.820001985973903,
|
|
130
|
+
.820001985973903,
|
|
131
|
+
-.8864155270044011,
|
|
132
|
+
.8864155270044011,
|
|
133
|
+
-.9382745520027328,
|
|
134
|
+
.9382745520027328,
|
|
135
|
+
-.9747285559713095,
|
|
136
|
+
.9747285559713095,
|
|
137
|
+
-.9951872199970213,
|
|
138
|
+
.9951872199970213
|
|
139
|
+
];
|
|
140
|
+
const Cvalues = [
|
|
141
|
+
.12793819534675216,
|
|
142
|
+
.12793819534675216,
|
|
143
|
+
.1258374563468283,
|
|
144
|
+
.1258374563468283,
|
|
145
|
+
.12167047292780339,
|
|
146
|
+
.12167047292780339,
|
|
147
|
+
.1155056680537256,
|
|
148
|
+
.1155056680537256,
|
|
149
|
+
.10744427011596563,
|
|
150
|
+
.10744427011596563,
|
|
151
|
+
.09761865210411388,
|
|
152
|
+
.09761865210411388,
|
|
153
|
+
.08619016153195327,
|
|
154
|
+
.08619016153195327,
|
|
155
|
+
.0733464814110803,
|
|
156
|
+
.0733464814110803,
|
|
157
|
+
.05929858491543678,
|
|
158
|
+
.05929858491543678,
|
|
159
|
+
.04427743881741981,
|
|
160
|
+
.04427743881741981,
|
|
161
|
+
.028531388628933663,
|
|
162
|
+
.028531388628933663,
|
|
163
|
+
.0123412297999872,
|
|
164
|
+
.0123412297999872
|
|
165
|
+
];
|
|
166
|
+
/**
|
|
167
|
+
* @param points
|
|
168
|
+
* @returns
|
|
169
|
+
*/
|
|
170
|
+
const deriveBezier = (points) => {
|
|
171
|
+
const dpoints = [];
|
|
172
|
+
for (let p = points, d = p.length, c = d - 1; d > 1; d -= 1, c -= 1) {
|
|
173
|
+
const list = [];
|
|
174
|
+
for (let j = 0; j < c; j += 1) list.push({
|
|
175
|
+
x: c * (p[j + 1].x - p[j].x),
|
|
176
|
+
y: c * (p[j + 1].y - p[j].y),
|
|
177
|
+
t: 0
|
|
178
|
+
});
|
|
179
|
+
dpoints.push(list);
|
|
180
|
+
p = list;
|
|
181
|
+
}
|
|
182
|
+
return dpoints;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* @param points
|
|
186
|
+
* @param t
|
|
187
|
+
*/
|
|
188
|
+
const computeBezier = (points, t) => {
|
|
189
|
+
if (t === 0) {
|
|
190
|
+
points[0].t = 0;
|
|
191
|
+
return points[0];
|
|
192
|
+
}
|
|
193
|
+
const order = points.length - 1;
|
|
194
|
+
if (t === 1) {
|
|
195
|
+
points[order].t = 1;
|
|
196
|
+
return points[order];
|
|
197
|
+
}
|
|
198
|
+
const mt = 1 - t;
|
|
199
|
+
let p = points;
|
|
200
|
+
if (order === 0) {
|
|
201
|
+
points[0].t = t;
|
|
202
|
+
return points[0];
|
|
203
|
+
}
|
|
204
|
+
if (order === 1) return {
|
|
205
|
+
x: mt * p[0].x + t * p[1].x,
|
|
206
|
+
y: mt * p[0].y + t * p[1].y,
|
|
207
|
+
t
|
|
208
|
+
};
|
|
209
|
+
const mt2 = mt * mt;
|
|
210
|
+
const t2 = t * t;
|
|
211
|
+
let a = 0;
|
|
212
|
+
let b = 0;
|
|
213
|
+
let c = 0;
|
|
214
|
+
let d = 0;
|
|
215
|
+
if (order === 2) {
|
|
216
|
+
p = [
|
|
217
|
+
p[0],
|
|
218
|
+
p[1],
|
|
219
|
+
p[2],
|
|
220
|
+
{
|
|
221
|
+
x: 0,
|
|
222
|
+
y: 0
|
|
223
|
+
}
|
|
224
|
+
];
|
|
225
|
+
a = mt2;
|
|
226
|
+
b = mt * t * 2;
|
|
227
|
+
c = t2;
|
|
228
|
+
} else if (order === 3) {
|
|
229
|
+
a = mt2 * mt;
|
|
230
|
+
b = mt2 * t * 3;
|
|
231
|
+
c = mt * t2 * 3;
|
|
232
|
+
d = t * t2;
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
x: a * p[0].x + b * p[1].x + c * p[2].x + d * p[3].x,
|
|
236
|
+
y: a * p[0].y + b * p[1].y + c * p[2].y + d * p[3].y,
|
|
237
|
+
t
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
const calculateBezier = (derivativeFn, t) => {
|
|
241
|
+
const d = derivativeFn(t);
|
|
242
|
+
const l = d.x * d.x + d.y * d.y;
|
|
243
|
+
return Math.sqrt(l);
|
|
244
|
+
};
|
|
245
|
+
const bezierLength = (derivativeFn) => {
|
|
246
|
+
return bezierLengthAtT(derivativeFn, 1);
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* Returns the arc length of a Bezier segment from its start point
|
|
250
|
+
* up to a given `t` ratio, by integrating the curve speed `|B'(t)|`
|
|
251
|
+
* over the `[0, t]` interval with Gauss-Legendre quadrature.
|
|
252
|
+
*
|
|
253
|
+
* @param derivativeFn the curve derivative function
|
|
254
|
+
* @param t the sampled point ratio in `[0-1]`
|
|
255
|
+
* @returns the arc length up to parameter `t`
|
|
256
|
+
*/
|
|
257
|
+
const bezierLengthAtT = (derivativeFn, t) => {
|
|
258
|
+
const z = t / 2;
|
|
259
|
+
const len = Tvalues.length;
|
|
260
|
+
let sum = 0;
|
|
261
|
+
for (let i = 0, tMid; i < len; i++) {
|
|
262
|
+
tMid = z * Tvalues[i] + z;
|
|
263
|
+
sum += Cvalues[i] * calculateBezier(derivativeFn, tMid);
|
|
264
|
+
}
|
|
265
|
+
return z * sum;
|
|
266
|
+
};
|
|
267
|
+
/**
|
|
268
|
+
* Builds the Bezier points array from a curve coordinates array.
|
|
269
|
+
* @param curve cubic / quad bezier segment
|
|
270
|
+
*/
|
|
271
|
+
const getBezierPoints = (curve) => {
|
|
272
|
+
const points = [];
|
|
273
|
+
for (let idx = 0, len = curve.length, step = 2; idx < len; idx += step) points.push({
|
|
274
|
+
x: curve[idx],
|
|
275
|
+
y: curve[idx + 1]
|
|
276
|
+
});
|
|
277
|
+
return points;
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* Returns the arc length of a Bezier segment from its start point
|
|
281
|
+
* up to a given `t` ratio, for the segment given by its coordinates.
|
|
282
|
+
*
|
|
283
|
+
* @param curve cubic / quad bezier segment
|
|
284
|
+
* @param t the sampled point ratio in `[0-1]`
|
|
285
|
+
* @returns the arc length up to parameter `t`
|
|
286
|
+
*/
|
|
287
|
+
const getBezierLengthAtT = (curve, t) => {
|
|
288
|
+
const dpoints = deriveBezier(getBezierPoints(curve));
|
|
289
|
+
return bezierLengthAtT((ratio) => {
|
|
290
|
+
return computeBezier(dpoints[0], ratio);
|
|
291
|
+
}, t);
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* Returns the `t` parameter of a Bezier segment at which the cumulative
|
|
295
|
+
* arc length equals the given `distance`, found by interval bisection.
|
|
296
|
+
*
|
|
297
|
+
* @param curve cubic / quad bezier segment
|
|
298
|
+
* @param distance the distance along the segment in `[0, length]`
|
|
299
|
+
* @returns the `t` parameter in `[0-1]`
|
|
300
|
+
*/
|
|
301
|
+
const getTAtBezierLength = (curve, distance) => {
|
|
302
|
+
const dpoints = deriveBezier(getBezierPoints(curve));
|
|
303
|
+
const derivativeFn = (ratio) => {
|
|
304
|
+
return computeBezier(dpoints[0], ratio);
|
|
305
|
+
};
|
|
306
|
+
let lo = 0;
|
|
307
|
+
let hi = 1;
|
|
308
|
+
for (let i = 0; i < 25; i += 1) {
|
|
309
|
+
const mid = (lo + hi) / 2;
|
|
310
|
+
if (bezierLengthAtT(derivativeFn, mid) < distance) lo = mid;
|
|
311
|
+
else hi = mid;
|
|
312
|
+
}
|
|
313
|
+
return (lo + hi) / 2;
|
|
314
|
+
};
|
|
315
|
+
/**
|
|
316
|
+
* Returns the length of CubicBezier / Quad segment.
|
|
317
|
+
* @param curve cubic / quad bezier segment
|
|
318
|
+
*/
|
|
319
|
+
const getBezierLength = (curve) => {
|
|
320
|
+
const points = getBezierPoints(curve);
|
|
321
|
+
const dpoints = deriveBezier(points);
|
|
322
|
+
return bezierLength((t) => {
|
|
323
|
+
return computeBezier(dpoints[0], t);
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
const CBEZIER_MINMAX_EPSILON = 1e-8;
|
|
327
|
+
/**
|
|
328
|
+
* Returns the most extreme points in a Quad Bezier segment.
|
|
329
|
+
* @param A an array which consist of X/Y values
|
|
330
|
+
*/
|
|
331
|
+
const minmaxQ = ([v1, cp, v2]) => {
|
|
332
|
+
const min = Math.min(v1, v2);
|
|
333
|
+
const max = Math.max(v1, v2);
|
|
334
|
+
if (cp >= v1 ? v2 >= cp : v2 <= cp) return [min, max];
|
|
335
|
+
const E = (v1 * v2 - cp * cp) / (v1 - 2 * cp + v2);
|
|
336
|
+
return E < min ? [E, max] : [min, E];
|
|
337
|
+
};
|
|
338
|
+
/**
|
|
339
|
+
* Returns the most extreme points in a Cubic Bezier segment.
|
|
340
|
+
* @param A an array which consist of X/Y values
|
|
341
|
+
* @see https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127
|
|
342
|
+
*/
|
|
343
|
+
const minmaxC = ([v1, cp1, cp2, v2]) => {
|
|
344
|
+
const K = v1 - 3 * cp1 + 3 * cp2 - v2;
|
|
345
|
+
if (Math.abs(K) < 1e-8) {
|
|
346
|
+
if (v1 === v2 && v1 === cp1) return [v1, v2];
|
|
347
|
+
return minmaxQ([
|
|
348
|
+
v1,
|
|
349
|
+
-.5 * v1 + 1.5 * cp1,
|
|
350
|
+
v1 - 3 * cp1 + 3 * cp2
|
|
351
|
+
]);
|
|
352
|
+
}
|
|
353
|
+
const T = -v1 * cp2 + v1 * v2 - cp1 * cp2 - cp1 * v2 + cp1 * cp1 + cp2 * cp2;
|
|
354
|
+
if (T <= 0) return [Math.min(v1, v2), Math.max(v1, v2)];
|
|
355
|
+
const S = Math.sqrt(T);
|
|
356
|
+
let min = Math.min(v1, v2);
|
|
357
|
+
let max = Math.max(v1, v2);
|
|
358
|
+
const L = v1 - 2 * cp1 + cp2;
|
|
359
|
+
for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) if (R > 0 && R < 1) {
|
|
360
|
+
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;
|
|
361
|
+
if (Q < min) min = Q;
|
|
362
|
+
if (Q > max) max = Q;
|
|
363
|
+
}
|
|
364
|
+
return [min, max];
|
|
365
|
+
};
|
|
366
|
+
const bezierTools = {
|
|
367
|
+
bezierLength,
|
|
368
|
+
bezierLengthAtT,
|
|
369
|
+
calculateBezier,
|
|
370
|
+
CBEZIER_MINMAX_EPSILON,
|
|
371
|
+
computeBezier,
|
|
372
|
+
Cvalues,
|
|
373
|
+
deriveBezier,
|
|
374
|
+
getBezierLength,
|
|
375
|
+
getBezierLengthAtT,
|
|
376
|
+
getBezierPoints,
|
|
377
|
+
getTAtBezierLength,
|
|
378
|
+
minmaxC,
|
|
379
|
+
minmaxQ,
|
|
380
|
+
Tvalues
|
|
381
|
+
};
|
|
382
|
+
//#endregion
|
|
109
383
|
//#region src/math/arcTools.ts
|
|
110
384
|
/**
|
|
385
|
+
* Returns the signed arc length of an ellipse segment from angle `0` up to
|
|
386
|
+
* the given `theta` angle, by integrating the curve speed
|
|
387
|
+
* `|sqrt(rx^2 * sin^2(phi) + ry^2 * cos^2(phi))|` over the `[0, theta]`
|
|
388
|
+
* interval with Gauss-Legendre quadrature.
|
|
389
|
+
*
|
|
390
|
+
* @param rx radius along X axis
|
|
391
|
+
* @param ry radius along Y axis
|
|
392
|
+
* @param theta the angle in radians
|
|
393
|
+
* @returns the signed arc length
|
|
394
|
+
*/
|
|
395
|
+
const arcLengthAtAngle = (rx, ry, theta) => {
|
|
396
|
+
const sign = theta < 0 ? -1 : 1;
|
|
397
|
+
const absTheta = Math.abs(theta);
|
|
398
|
+
const step = Math.PI / 2;
|
|
399
|
+
let total = 0;
|
|
400
|
+
let remaining = absTheta;
|
|
401
|
+
let offset = 0;
|
|
402
|
+
while (remaining > 0) {
|
|
403
|
+
const span = Math.min(step, remaining);
|
|
404
|
+
const z = span / 2;
|
|
405
|
+
let sum = 0;
|
|
406
|
+
for (let i = 0, len = Tvalues.length; i < len; i += 1) {
|
|
407
|
+
const phi = offset + z * Tvalues[i] + z;
|
|
408
|
+
sum += Cvalues[i] * Math.sqrt((rx * Math.sin(phi)) ** 2 + (ry * Math.cos(phi)) ** 2);
|
|
409
|
+
}
|
|
410
|
+
total += z * sum;
|
|
411
|
+
offset += span;
|
|
412
|
+
remaining -= span;
|
|
413
|
+
}
|
|
414
|
+
return sign * total;
|
|
415
|
+
};
|
|
416
|
+
/**
|
|
417
|
+
* Returns the signed arc length of an ellipse segment between two angles.
|
|
418
|
+
* @param rx radius along X axis
|
|
419
|
+
* @param ry radius along Y axis
|
|
420
|
+
* @param from the start angle in radians
|
|
421
|
+
* @param to the end angle in radians
|
|
422
|
+
* @returns the signed arc length
|
|
423
|
+
*/
|
|
424
|
+
const arcLengthBetween = (rx, ry, from, to) => {
|
|
425
|
+
return arcLengthAtAngle(rx, ry, to) - arcLengthAtAngle(rx, ry, from);
|
|
426
|
+
};
|
|
427
|
+
/**
|
|
111
428
|
* Returns the Arc segment length.
|
|
112
429
|
* @param rx radius along X axis
|
|
113
430
|
* @param ry radius along Y axis
|
|
@@ -115,13 +432,7 @@ const lineTools = {
|
|
|
115
432
|
* @returns the arc length
|
|
116
433
|
*/
|
|
117
434
|
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);
|
|
435
|
+
return Math.abs(arcLengthAtAngle(rx, ry, theta));
|
|
125
436
|
};
|
|
126
437
|
/**
|
|
127
438
|
* Find point on ellipse at given angle around ellipse (theta);
|
|
@@ -224,10 +535,11 @@ const getArcProps = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
224
535
|
x: 1,
|
|
225
536
|
y: 0
|
|
226
537
|
}, startVector);
|
|
227
|
-
|
|
538
|
+
const endVector = {
|
|
228
539
|
x: (-transformedPoint.x - transformedCenter.x) / rx,
|
|
229
540
|
y: (-transformedPoint.y - transformedCenter.y) / ry
|
|
230
|
-
}
|
|
541
|
+
};
|
|
542
|
+
let sweepAngle = angleBetween(startVector, endVector);
|
|
231
543
|
if (!SF && sweepAngle > 0) sweepAngle -= 2 * PI;
|
|
232
544
|
else if (SF && sweepAngle < 0) sweepAngle += 2 * PI;
|
|
233
545
|
sweepAngle %= 2 * PI;
|
|
@@ -254,7 +566,7 @@ const getArcProps = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
254
566
|
*/
|
|
255
567
|
const getArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
256
568
|
const { rx, ry, startAngle, endAngle } = getArcProps(x1, y1, RX, RY, angle, LAF, SF, x, y);
|
|
257
|
-
return
|
|
569
|
+
return Math.abs(arcLengthBetween(rx, ry, startAngle, endAngle));
|
|
258
570
|
};
|
|
259
571
|
/**
|
|
260
572
|
* Returns a point along an Arc segment at a given distance.
|
|
@@ -278,7 +590,8 @@ const getPointAtArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y, distance) =>
|
|
|
278
590
|
};
|
|
279
591
|
const { center, rx, ry, startAngle, endAngle } = getArcProps(x1, y1, RX, RY, angle, LAF, SF, x, y);
|
|
280
592
|
if (typeof distance === "number") {
|
|
281
|
-
const
|
|
593
|
+
const sweepAngle = endAngle - startAngle;
|
|
594
|
+
const length = Math.abs(arcLengthBetween(rx, ry, startAngle, endAngle));
|
|
282
595
|
if (distance <= 0) point = {
|
|
283
596
|
x: x1,
|
|
284
597
|
y: y1
|
|
@@ -294,9 +607,15 @@ const getPointAtArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y, distance) =>
|
|
|
294
607
|
};
|
|
295
608
|
if (rx === 0 || ry === 0) return getPointAtLineLength(x1, y1, x, y, distance);
|
|
296
609
|
const { PI, cos, sin } = Math;
|
|
297
|
-
const sweepAngle = endAngle - startAngle;
|
|
298
610
|
const xRotRad = (angle % 360 + 360) % 360 * (PI / 180);
|
|
299
|
-
|
|
611
|
+
let lo = 0;
|
|
612
|
+
let hi = 1;
|
|
613
|
+
for (let i = 0; i < 25; i += 1) {
|
|
614
|
+
const mid = (lo + hi) / 2;
|
|
615
|
+
if (Math.abs(arcLengthBetween(rx, ry, startAngle, startAngle + sweepAngle * mid)) < distance) lo = mid;
|
|
616
|
+
else hi = mid;
|
|
617
|
+
}
|
|
618
|
+
const alpha = startAngle + sweepAngle * ((lo + hi) / 2);
|
|
300
619
|
const ellipseComponentX = rx * cos(alpha);
|
|
301
620
|
const ellipseComponentY = ry * sin(alpha);
|
|
302
621
|
point = {
|
|
@@ -346,8 +665,10 @@ const getArcBBox = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
346
665
|
let xMax = max(x1, x);
|
|
347
666
|
let yMin = min(y1, y);
|
|
348
667
|
let yMax = max(y1, y);
|
|
349
|
-
const
|
|
350
|
-
const
|
|
668
|
+
const angleAfterStart = endAngle - deltaAngle * 1e-5;
|
|
669
|
+
const pP2 = arcPoint(cx, cy, rx, ry, alpha, angleAfterStart);
|
|
670
|
+
const angleBeforeEnd = endAngle - deltaAngle * .99999;
|
|
671
|
+
const pP3 = arcPoint(cx, cy, rx, ry, alpha, angleBeforeEnd);
|
|
351
672
|
/**
|
|
352
673
|
* expected extremes
|
|
353
674
|
* if leaving inner bounding box
|
|
@@ -388,6 +709,8 @@ const getArcBBox = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
388
709
|
const arcTools = {
|
|
389
710
|
angleBetween,
|
|
390
711
|
arcLength,
|
|
712
|
+
arcLengthAtAngle,
|
|
713
|
+
arcLengthBetween,
|
|
391
714
|
arcPoint,
|
|
392
715
|
getArcBBox,
|
|
393
716
|
getArcLength,
|
|
@@ -395,221 +718,7 @@ const arcTools = {
|
|
|
395
718
|
getPointAtArcLength
|
|
396
719
|
};
|
|
397
720
|
//#endregion
|
|
398
|
-
//#region src/math/
|
|
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
|
-
//#region src/math/cubicTools.ts
|
|
721
|
+
//#region src/math/cubicTools.ts
|
|
613
722
|
/**
|
|
614
723
|
* Returns a point at a given length of a CubicBezier segment.
|
|
615
724
|
*
|
|
@@ -677,7 +786,7 @@ const getPointAtCubicLength = (x1, y1, c1x, c1y, c2x, c2y, x2, y2, distance) =>
|
|
|
677
786
|
y: y1
|
|
678
787
|
};
|
|
679
788
|
if (distanceIsNumber) {
|
|
680
|
-
const
|
|
789
|
+
const coords = [
|
|
681
790
|
x1,
|
|
682
791
|
y1,
|
|
683
792
|
c1x,
|
|
@@ -686,21 +795,16 @@ const getPointAtCubicLength = (x1, y1, c1x, c1y, c2x, c2y, x2, y2, distance) =>
|
|
|
686
795
|
c2y,
|
|
687
796
|
x2,
|
|
688
797
|
y2
|
|
689
|
-
]
|
|
798
|
+
];
|
|
799
|
+
const currentLength = getBezierLength(coords);
|
|
690
800
|
if (distance <= 0) {} else if (distance >= currentLength) point = {
|
|
691
801
|
x: x2,
|
|
692
802
|
y: y2
|
|
693
803
|
};
|
|
694
|
-
else
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
c1y,
|
|
699
|
-
c2x,
|
|
700
|
-
c2y,
|
|
701
|
-
x2,
|
|
702
|
-
y2
|
|
703
|
-
], distance / currentLength);
|
|
804
|
+
else {
|
|
805
|
+
const t = getTAtBezierLength(coords, distance);
|
|
806
|
+
point = getPointAtCubicSegmentLength(coords, t);
|
|
807
|
+
}
|
|
704
808
|
}
|
|
705
809
|
return point;
|
|
706
810
|
};
|
|
@@ -808,26 +912,23 @@ const getPointAtQuadLength = (x1, y1, cx, cy, x2, y2, distance) => {
|
|
|
808
912
|
y: y1
|
|
809
913
|
};
|
|
810
914
|
if (distanceIsNumber) {
|
|
811
|
-
const
|
|
915
|
+
const coords = [
|
|
812
916
|
x1,
|
|
813
917
|
y1,
|
|
814
918
|
cx,
|
|
815
919
|
cy,
|
|
816
920
|
x2,
|
|
817
921
|
y2
|
|
818
|
-
]
|
|
922
|
+
];
|
|
923
|
+
const currentLength = getBezierLength(coords);
|
|
819
924
|
if (distance <= 0) {} else if (distance >= currentLength) point = {
|
|
820
925
|
x: x2,
|
|
821
926
|
y: y2
|
|
822
927
|
};
|
|
823
|
-
else
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
cy,
|
|
828
|
-
x2,
|
|
829
|
-
y2
|
|
830
|
-
], distance / currentLength);
|
|
928
|
+
else {
|
|
929
|
+
const t = getTAtBezierLength(coords, distance);
|
|
930
|
+
point = getPointAtQuadSegmentLength(coords, t);
|
|
931
|
+
}
|
|
831
932
|
}
|
|
832
933
|
return point;
|
|
833
934
|
};
|
|
@@ -1446,7 +1547,8 @@ const iterate = (path, iterator) => {
|
|
|
1446
1547
|
* ```
|
|
1447
1548
|
*/
|
|
1448
1549
|
const pathToAbsolute = (pathInput) => {
|
|
1449
|
-
|
|
1550
|
+
const path = parsePathString(pathInput);
|
|
1551
|
+
return iterate(path, absolutizeSegment);
|
|
1450
1552
|
};
|
|
1451
1553
|
//#endregion
|
|
1452
1554
|
//#region src/process/relativizeSegment.ts
|
|
@@ -1503,7 +1605,8 @@ const relativizeSegment = (segment, index, lastX, lastY) => {
|
|
|
1503
1605
|
* ```
|
|
1504
1606
|
*/
|
|
1505
1607
|
const pathToRelative = (pathInput) => {
|
|
1506
|
-
|
|
1608
|
+
const path = parsePathString(pathInput);
|
|
1609
|
+
return iterate(path, relativizeSegment);
|
|
1507
1610
|
};
|
|
1508
1611
|
//#endregion
|
|
1509
1612
|
//#region src/process/arcToCubic.ts
|
|
@@ -2094,8 +2197,6 @@ const getPointAtLength = (pathInput, distance) => {
|
|
|
2094
2197
|
const path = normalizePath(pathInput);
|
|
2095
2198
|
let isM = false;
|
|
2096
2199
|
let data = [];
|
|
2097
|
-
let x = 0;
|
|
2098
|
-
let y = 0;
|
|
2099
2200
|
let [mx, my] = path[0].slice(1);
|
|
2100
2201
|
const distanceIsNumber = typeof distance === "number";
|
|
2101
2202
|
let point = {
|
|
@@ -2136,21 +2237,13 @@ const getPointAtLength = (pathInput, distance) => {
|
|
|
2136
2237
|
mx,
|
|
2137
2238
|
my
|
|
2138
2239
|
];
|
|
2139
|
-
point =
|
|
2140
|
-
x: mx,
|
|
2141
|
-
y: my
|
|
2142
|
-
};
|
|
2240
|
+
point = getPointAtLineLength(data[0], data[1], data[2], data[3], distance - totalLength);
|
|
2143
2241
|
length = getLineLength(data[0], data[1], data[2], data[3]);
|
|
2144
2242
|
}
|
|
2145
|
-
[x, y] = data.slice(-2);
|
|
2146
2243
|
if (totalLength < distance) POINT = point;
|
|
2147
2244
|
else return false;
|
|
2148
2245
|
totalLength += length;
|
|
2149
2246
|
});
|
|
2150
|
-
if (distance > totalLength - 1e-5) return {
|
|
2151
|
-
x,
|
|
2152
|
-
y
|
|
2153
|
-
};
|
|
2154
2247
|
return POINT;
|
|
2155
2248
|
};
|
|
2156
2249
|
//#endregion
|
|
@@ -2901,7 +2994,7 @@ const isMultiPath = (path) => {
|
|
|
2901
2994
|
}
|
|
2902
2995
|
return false;
|
|
2903
2996
|
}
|
|
2904
|
-
throw new TypeError(
|
|
2997
|
+
throw new TypeError("SVGPathCommanderError: expected string or PathArray");
|
|
2905
2998
|
};
|
|
2906
2999
|
//#endregion
|
|
2907
3000
|
//#region src/util/isPolylineArray.ts
|
|
@@ -3021,7 +3114,8 @@ const optimizePath = (pathInput, roundOption) => {
|
|
|
3021
3114
|
const shortSegment = shortenSegment(seg, normalizedSegment, optimParams, prevCommand);
|
|
3022
3115
|
const absSegment = roundSegment(shortSegment, round);
|
|
3023
3116
|
const absString = absSegment.join("");
|
|
3024
|
-
const
|
|
3117
|
+
const relativeSegment = relativizeSegment(shortSegment, i, lastX, lastY);
|
|
3118
|
+
const relSegment = roundSegment(relativeSegment, round);
|
|
3025
3119
|
const relString = relSegment.join("");
|
|
3026
3120
|
result = absString.length < relString.length ? absSegment : relSegment;
|
|
3027
3121
|
}
|
|
@@ -3423,7 +3517,7 @@ const roundPath = (path, roundOption) => {
|
|
|
3423
3517
|
//#region src/morph/fixPath.ts
|
|
3424
3518
|
/**
|
|
3425
3519
|
* Checks a `PathArray` for an unnecessary `Z` segment
|
|
3426
|
-
* and
|
|
3520
|
+
* and removes it. The `PathArray` is modified in place.
|
|
3427
3521
|
* In short, if the segment before `Z` extends to `M`,
|
|
3428
3522
|
* the `Z` segment must be removed.
|
|
3429
3523
|
*
|
|
@@ -3795,7 +3889,7 @@ function getRotations(a) {
|
|
|
3795
3889
|
*/
|
|
3796
3890
|
function getRotatedPath(pathA, pathB, computedRotations) {
|
|
3797
3891
|
const rotations = computedRotations || getRotations(pathA);
|
|
3798
|
-
if (pathA.length !== pathB.length) throw new TypeError(
|
|
3892
|
+
if (pathA.length !== pathB.length) throw new TypeError("SVGPathCommanderError: paths must have the same number of segments after equalization");
|
|
3799
3893
|
let bestIndex = 0;
|
|
3800
3894
|
let minDistanceSq = Infinity;
|
|
3801
3895
|
for (let ri = 0; ri < rotations.length; ri++) {
|
|
@@ -3893,18 +3987,7 @@ const equalizeSegments = (path1, path2, initialCfg = {}) => {
|
|
|
3893
3987
|
return [equalP1, equalP2];
|
|
3894
3988
|
};
|
|
3895
3989
|
//#endregion
|
|
3896
|
-
//#region src/
|
|
3897
|
-
const intersect = (x1, y1, x2, y2, x3, y3, x4, y4) => {
|
|
3898
|
-
if (Math.max(x1, x2) < Math.min(x3, x4) || Math.min(x1, x2) > Math.max(x3, x4) || Math.max(y1, y2) < Math.min(y3, y4) || Math.min(y1, y2) > Math.max(y3, y4)) return;
|
|
3899
|
-
const nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4), ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4), denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
|
|
3900
|
-
if (!denominator) return;
|
|
3901
|
-
const px = nx / denominator, py = ny / denominator, px2 = roundTo(px, 2), py2 = roundTo(py, 2);
|
|
3902
|
-
if (px2 < roundTo(Math.min(x1, x2), 2) || px2 > roundTo(Math.max(x1, x2), 2) || px2 < roundTo(Math.min(x3, x4), 2) || px2 > roundTo(Math.max(x3, x4), 2) || py2 < roundTo(Math.min(y1, y2), 2) || py2 > roundTo(Math.max(y1, y2), 2) || py2 < roundTo(Math.min(y3, y4), 2) || py2 > roundTo(Math.max(y3, y4), 2)) return;
|
|
3903
|
-
return {
|
|
3904
|
-
x: px,
|
|
3905
|
-
y: py
|
|
3906
|
-
};
|
|
3907
|
-
};
|
|
3990
|
+
//#region src/intersect/isPointInsideBBox.ts
|
|
3908
3991
|
/**
|
|
3909
3992
|
* Checks if a point is inside a bounding box.
|
|
3910
3993
|
*
|
|
@@ -3916,6 +3999,8 @@ const isPointInsideBBox = (bbox, [x, y]) => {
|
|
|
3916
3999
|
const [minX, minY, maxX, maxY] = bbox;
|
|
3917
4000
|
return x >= minX && x <= maxX && y >= minY && y <= maxY;
|
|
3918
4001
|
};
|
|
4002
|
+
//#endregion
|
|
4003
|
+
//#region src/intersect/boundingBoxIntersect.ts
|
|
3919
4004
|
/**
|
|
3920
4005
|
* Checks if two bounding boxes intersect.
|
|
3921
4006
|
*
|
|
@@ -3928,154 +4013,6 @@ const boundingBoxIntersect = (a, b) => {
|
|
|
3928
4013
|
const [bx1, by1, bx2, by2] = b;
|
|
3929
4014
|
return isPointInsideBBox(b, [ax1, ay1]) || isPointInsideBBox(b, [ax2, ay1]) || isPointInsideBBox(b, [ax1, ay2]) || isPointInsideBBox(b, [ax2, ay2]) || isPointInsideBBox(a, [bx1, by1]) || isPointInsideBBox(a, [bx2, by1]) || isPointInsideBBox(a, [bx1, by2]) || isPointInsideBBox(a, [bx2, by2]) || (ax1 < bx2 && ax1 > bx1 || bx1 < ax2 && bx1 > ax1) && (ay1 < by2 && ay1 > by1 || by1 < ay2 && by1 > ay1);
|
|
3930
4015
|
};
|
|
3931
|
-
const interHelper = (bez1, bez2, config) => {
|
|
3932
|
-
const bbox1 = getCubicBBox(...bez1);
|
|
3933
|
-
const bbox2 = getCubicBBox(...bez2);
|
|
3934
|
-
const { justCount, epsilon } = Object.assign({
|
|
3935
|
-
justCount: true,
|
|
3936
|
-
epsilon: DISTANCE_EPSILON
|
|
3937
|
-
}, config);
|
|
3938
|
-
if (!boundingBoxIntersect(bbox1, bbox2)) return justCount ? 0 : [];
|
|
3939
|
-
const l1 = getCubicLength(...bez1), l2 = getCubicLength(...bez2), n1 = Math.max(l1 / 5 >> 0, 1), n2 = Math.max(l2 / 5 >> 0, 1), points1 = [], points2 = [], xy = {};
|
|
3940
|
-
let res = justCount ? 0 : [];
|
|
3941
|
-
for (let i = 0; i < n1 + 1; i++) {
|
|
3942
|
-
const p = getPointAtCubicLength(...bez1, i / n1 * l1);
|
|
3943
|
-
points1.push({
|
|
3944
|
-
x: p.x,
|
|
3945
|
-
y: p.y,
|
|
3946
|
-
t: i / n1
|
|
3947
|
-
});
|
|
3948
|
-
}
|
|
3949
|
-
for (let i = 0; i < n2 + 1; i++) {
|
|
3950
|
-
const p = getPointAtCubicLength(...bez2, i / n2 * l2);
|
|
3951
|
-
points2.push({
|
|
3952
|
-
x: p.x,
|
|
3953
|
-
y: p.y,
|
|
3954
|
-
t: i / n2
|
|
3955
|
-
});
|
|
3956
|
-
}
|
|
3957
|
-
for (let i = 0; i < n1; i++) for (let j = 0; j < n2; j++) {
|
|
3958
|
-
const maxLimit = 1 + epsilon, di = points1[i], di1 = points1[i + 1], dj = points2[j], dj1 = points2[j + 1], ci = Math.abs(di1.x - di.x) < .001 ? "y" : "x", cj = Math.abs(dj1.x - dj.x) < .001 ? "y" : "x", is = intersect(di.x, di.y, di1.x, di1.y, dj.x, dj.y, dj1.x, dj1.y);
|
|
3959
|
-
if (is) {
|
|
3960
|
-
if (xy[is.x.toFixed(4)] == is.y.toFixed(4)) continue;
|
|
3961
|
-
xy[is.x.toFixed(4)] = is.y.toFixed(4);
|
|
3962
|
-
const t1 = di.t + Math.abs((is[ci] - di[ci]) / (di1[ci] - di[ci])) * (di1.t - di.t), t2 = dj.t + Math.abs((is[cj] - dj[cj]) / (dj1[cj] - dj[cj])) * (dj1.t - dj.t);
|
|
3963
|
-
if (t1 >= 0 && t1 <= maxLimit && t2 >= 0 && t2 <= maxLimit) if (justCount) res++;
|
|
3964
|
-
else res.push({
|
|
3965
|
-
x: is.x,
|
|
3966
|
-
y: is.y,
|
|
3967
|
-
t1: Math.min(t1, 1),
|
|
3968
|
-
t2: Math.min(t2, 1)
|
|
3969
|
-
});
|
|
3970
|
-
}
|
|
3971
|
-
}
|
|
3972
|
-
return res;
|
|
3973
|
-
};
|
|
3974
|
-
/**
|
|
3975
|
-
* Finds intersection points between two paths.
|
|
3976
|
-
*
|
|
3977
|
-
* @param pathInput1 - First path string or PathArray
|
|
3978
|
-
* @param pathInput2 - Second path string or PathArray
|
|
3979
|
-
* @param justCount - If true, returns the count of intersections; if false, returns the intersection points
|
|
3980
|
-
* @returns The number of intersections (when justCount is true) or an array of IntersectionPoint objects
|
|
3981
|
-
*
|
|
3982
|
-
* @example
|
|
3983
|
-
* ```ts
|
|
3984
|
-
* pathsIntersection('M0 50C0 0,100 0,100 50', 'M50 0C100 0,100 100,50 100', true)
|
|
3985
|
-
* // => 1
|
|
3986
|
-
* pathsIntersection('M0 50C0 0,100 0,100 50', 'M50 0C100 0,100 100,50 100', false)
|
|
3987
|
-
* // => [{ x: 50, y: 25, t1: 0.5, t2: 0.5 }]
|
|
3988
|
-
* ```
|
|
3989
|
-
*/
|
|
3990
|
-
const pathsIntersection = (pathInput1, pathInput2, justCount = true) => {
|
|
3991
|
-
const path1 = pathToCurve(pathInput1);
|
|
3992
|
-
const path2 = pathToCurve(pathInput2);
|
|
3993
|
-
let x1 = 0, y1 = 0, x2 = 0, y2 = 0, x1m = 0, y1m = 0, x2m = 0, y2m = 0, bez1 = [
|
|
3994
|
-
x1,
|
|
3995
|
-
y1,
|
|
3996
|
-
x1,
|
|
3997
|
-
y1,
|
|
3998
|
-
x1m,
|
|
3999
|
-
y1m,
|
|
4000
|
-
x1m,
|
|
4001
|
-
y1m
|
|
4002
|
-
], bez2 = [
|
|
4003
|
-
x2,
|
|
4004
|
-
y2,
|
|
4005
|
-
x2,
|
|
4006
|
-
y2,
|
|
4007
|
-
x2m,
|
|
4008
|
-
y2m,
|
|
4009
|
-
x2m,
|
|
4010
|
-
y2m
|
|
4011
|
-
], countResult = 0;
|
|
4012
|
-
const pointsResult = [];
|
|
4013
|
-
const pathLen1 = path1.length;
|
|
4014
|
-
const pathLen2 = path2.length;
|
|
4015
|
-
for (let i = 0; i < pathLen1; i++) {
|
|
4016
|
-
const seg1 = path1[i];
|
|
4017
|
-
if (seg1[0] == "M") {
|
|
4018
|
-
x1 = seg1[1];
|
|
4019
|
-
y1 = seg1[2];
|
|
4020
|
-
x1m = x1;
|
|
4021
|
-
y1m = y1;
|
|
4022
|
-
} else {
|
|
4023
|
-
if (seg1[0] == "C") {
|
|
4024
|
-
bez1 = [
|
|
4025
|
-
x1,
|
|
4026
|
-
y1,
|
|
4027
|
-
seg1[1],
|
|
4028
|
-
seg1[2],
|
|
4029
|
-
seg1[3],
|
|
4030
|
-
seg1[4],
|
|
4031
|
-
seg1[5],
|
|
4032
|
-
seg1[6]
|
|
4033
|
-
];
|
|
4034
|
-
x1 = bez1[6];
|
|
4035
|
-
y1 = bez1[7];
|
|
4036
|
-
} else {
|
|
4037
|
-
bez1 = [
|
|
4038
|
-
x1,
|
|
4039
|
-
y1,
|
|
4040
|
-
x1,
|
|
4041
|
-
y1,
|
|
4042
|
-
x1m,
|
|
4043
|
-
y1m,
|
|
4044
|
-
x1m,
|
|
4045
|
-
y1m
|
|
4046
|
-
];
|
|
4047
|
-
x1 = x1m;
|
|
4048
|
-
y1 = y1m;
|
|
4049
|
-
}
|
|
4050
|
-
for (let j = 0; j < pathLen2; j++) {
|
|
4051
|
-
const seg2 = path2[j];
|
|
4052
|
-
if (seg2[0] == "M") {
|
|
4053
|
-
x2 = seg2[1];
|
|
4054
|
-
y2 = seg2[2];
|
|
4055
|
-
x2m = x2;
|
|
4056
|
-
y2m = y2;
|
|
4057
|
-
} else if (seg2[0] == "C") {
|
|
4058
|
-
bez2 = [
|
|
4059
|
-
x2,
|
|
4060
|
-
y2,
|
|
4061
|
-
seg2[1],
|
|
4062
|
-
seg2[2],
|
|
4063
|
-
seg2[3],
|
|
4064
|
-
seg2[4],
|
|
4065
|
-
seg2[5],
|
|
4066
|
-
seg2[6]
|
|
4067
|
-
];
|
|
4068
|
-
x2 = bez2[6];
|
|
4069
|
-
y2 = bez2[7];
|
|
4070
|
-
}
|
|
4071
|
-
const intr = interHelper(bez1, bez2, { justCount });
|
|
4072
|
-
if (justCount) countResult += intr;
|
|
4073
|
-
else pointsResult.push(...intr);
|
|
4074
|
-
}
|
|
4075
|
-
}
|
|
4076
|
-
}
|
|
4077
|
-
return justCount ? countResult : pointsResult;
|
|
4078
|
-
};
|
|
4079
4016
|
//#endregion
|
|
4080
4017
|
//#region src/morph/createPlaceholder.ts
|
|
4081
4018
|
/**
|
|
@@ -4275,6 +4212,169 @@ const equalizePaths = (pathInput1, pathInput2, initialCfg = {}) => {
|
|
|
4275
4212
|
return [equalizedPairs.map((p) => p[0]).flat(), equalizedPairs.map((p) => p[1]).flat()];
|
|
4276
4213
|
};
|
|
4277
4214
|
//#endregion
|
|
4215
|
+
//#region src/intersect/interHelper.ts
|
|
4216
|
+
const intersect = (x1, y1, x2, y2, x3, y3, x4, y4) => {
|
|
4217
|
+
if (Math.max(x1, x2) < Math.min(x3, x4) || Math.min(x1, x2) > Math.max(x3, x4) || Math.max(y1, y2) < Math.min(y3, y4) || Math.min(y1, y2) > Math.max(y3, y4)) return;
|
|
4218
|
+
const nx = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4), ny = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4), denominator = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
|
|
4219
|
+
if (!denominator) return;
|
|
4220
|
+
const px = nx / denominator, py = ny / denominator, px2 = roundTo(px, 2), py2 = roundTo(py, 2);
|
|
4221
|
+
if (px2 < roundTo(Math.min(x1, x2), 2) || px2 > roundTo(Math.max(x1, x2), 2) || px2 < roundTo(Math.min(x3, x4), 2) || px2 > roundTo(Math.max(x3, x4), 2) || py2 < roundTo(Math.min(y1, y2), 2) || py2 > roundTo(Math.max(y1, y2), 2) || py2 < roundTo(Math.min(y3, y4), 2) || py2 > roundTo(Math.max(y3, y4), 2)) return;
|
|
4222
|
+
return {
|
|
4223
|
+
x: px,
|
|
4224
|
+
y: py
|
|
4225
|
+
};
|
|
4226
|
+
};
|
|
4227
|
+
const interHelper = (bez1, bez2, config) => {
|
|
4228
|
+
const bbox1 = getCubicBBox(...bez1);
|
|
4229
|
+
const bbox2 = getCubicBBox(...bez2);
|
|
4230
|
+
const { justCount, epsilon } = Object.assign({
|
|
4231
|
+
justCount: true,
|
|
4232
|
+
epsilon: DISTANCE_EPSILON
|
|
4233
|
+
}, config);
|
|
4234
|
+
if (!boundingBoxIntersect(bbox1, bbox2)) return justCount ? 0 : [];
|
|
4235
|
+
const l1 = getCubicLength(...bez1), l2 = getCubicLength(...bez2), n1 = Math.max(l1 / 5 >> 0, 1), n2 = Math.max(l2 / 5 >> 0, 1), points1 = [], points2 = [], xy = {};
|
|
4236
|
+
let res = justCount ? 0 : [];
|
|
4237
|
+
for (let i = 0; i < n1 + 1; i++) {
|
|
4238
|
+
const p = getPointAtCubicLength(...bez1, i / n1 * l1);
|
|
4239
|
+
points1.push({
|
|
4240
|
+
x: p.x,
|
|
4241
|
+
y: p.y,
|
|
4242
|
+
t: i / n1
|
|
4243
|
+
});
|
|
4244
|
+
}
|
|
4245
|
+
for (let i = 0; i < n2 + 1; i++) {
|
|
4246
|
+
const p = getPointAtCubicLength(...bez2, i / n2 * l2);
|
|
4247
|
+
points2.push({
|
|
4248
|
+
x: p.x,
|
|
4249
|
+
y: p.y,
|
|
4250
|
+
t: i / n2
|
|
4251
|
+
});
|
|
4252
|
+
}
|
|
4253
|
+
for (let i = 0; i < n1; i++) for (let j = 0; j < n2; j++) {
|
|
4254
|
+
const maxLimit = 1 + epsilon, di = points1[i], di1 = points1[i + 1], dj = points2[j], dj1 = points2[j + 1], ci = Math.abs(di1.x - di.x) < .001 ? "y" : "x", cj = Math.abs(dj1.x - dj.x) < .001 ? "y" : "x", is = intersect(di.x, di.y, di1.x, di1.y, dj.x, dj.y, dj1.x, dj1.y);
|
|
4255
|
+
if (is) {
|
|
4256
|
+
if (xy[is.x.toFixed(4)] == is.y.toFixed(4)) continue;
|
|
4257
|
+
xy[is.x.toFixed(4)] = is.y.toFixed(4);
|
|
4258
|
+
const t1 = di.t + Math.abs((is[ci] - di[ci]) / (di1[ci] - di[ci])) * (di1.t - di.t), t2 = dj.t + Math.abs((is[cj] - dj[cj]) / (dj1[cj] - dj[cj])) * (dj1.t - dj.t);
|
|
4259
|
+
if (t1 >= 0 && t1 <= maxLimit && t2 >= 0 && t2 <= maxLimit) if (justCount) res++;
|
|
4260
|
+
else res.push({
|
|
4261
|
+
x: is.x,
|
|
4262
|
+
y: is.y,
|
|
4263
|
+
t1: Math.min(t1, 1),
|
|
4264
|
+
t2: Math.min(t2, 1)
|
|
4265
|
+
});
|
|
4266
|
+
}
|
|
4267
|
+
}
|
|
4268
|
+
return res;
|
|
4269
|
+
};
|
|
4270
|
+
//#endregion
|
|
4271
|
+
//#region src/intersect/pathIntersection.ts
|
|
4272
|
+
/**
|
|
4273
|
+
* Finds intersection points between two paths.
|
|
4274
|
+
*
|
|
4275
|
+
* @param pathInput1 - First path string or PathArray
|
|
4276
|
+
* @param pathInput2 - Second path string or PathArray
|
|
4277
|
+
* @param justCount - If true, returns the count of intersections; if false, returns the intersection points
|
|
4278
|
+
* @returns The number of intersections (when justCount is true) or an array of IntersectionPoint objects
|
|
4279
|
+
*
|
|
4280
|
+
* @example
|
|
4281
|
+
* ```ts
|
|
4282
|
+
* pathsIntersection('M0 50C0 0,100 0,100 50', 'M50 0C100 0,100 100,50 100', true)
|
|
4283
|
+
* // => 1
|
|
4284
|
+
* pathsIntersection('M0 50C0 0,100 0,100 50', 'M50 0C100 0,100 100,50 100', false)
|
|
4285
|
+
* // => [{ x: 50, y: 25, t1: 0.5, t2: 0.5 }]
|
|
4286
|
+
* ```
|
|
4287
|
+
*/
|
|
4288
|
+
const pathsIntersection = (pathInput1, pathInput2, justCount = true) => {
|
|
4289
|
+
const path1 = pathToCurve(pathInput1);
|
|
4290
|
+
const path2 = pathToCurve(pathInput2);
|
|
4291
|
+
let x1 = 0, y1 = 0, x2 = 0, y2 = 0, x1m = 0, y1m = 0, x2m = 0, y2m = 0, bez1 = [
|
|
4292
|
+
x1,
|
|
4293
|
+
y1,
|
|
4294
|
+
x1,
|
|
4295
|
+
y1,
|
|
4296
|
+
x1m,
|
|
4297
|
+
y1m,
|
|
4298
|
+
x1m,
|
|
4299
|
+
y1m
|
|
4300
|
+
], bez2 = [
|
|
4301
|
+
x2,
|
|
4302
|
+
y2,
|
|
4303
|
+
x2,
|
|
4304
|
+
y2,
|
|
4305
|
+
x2m,
|
|
4306
|
+
y2m,
|
|
4307
|
+
x2m,
|
|
4308
|
+
y2m
|
|
4309
|
+
], countResult = 0;
|
|
4310
|
+
const pointsResult = [];
|
|
4311
|
+
const pathLen1 = path1.length;
|
|
4312
|
+
const pathLen2 = path2.length;
|
|
4313
|
+
for (let i = 0; i < pathLen1; i++) {
|
|
4314
|
+
const seg1 = path1[i];
|
|
4315
|
+
if (seg1[0] == "M") {
|
|
4316
|
+
x1 = seg1[1];
|
|
4317
|
+
y1 = seg1[2];
|
|
4318
|
+
x1m = x1;
|
|
4319
|
+
y1m = y1;
|
|
4320
|
+
} else {
|
|
4321
|
+
if (seg1[0] == "C") {
|
|
4322
|
+
bez1 = [
|
|
4323
|
+
x1,
|
|
4324
|
+
y1,
|
|
4325
|
+
seg1[1],
|
|
4326
|
+
seg1[2],
|
|
4327
|
+
seg1[3],
|
|
4328
|
+
seg1[4],
|
|
4329
|
+
seg1[5],
|
|
4330
|
+
seg1[6]
|
|
4331
|
+
];
|
|
4332
|
+
x1 = bez1[6];
|
|
4333
|
+
y1 = bez1[7];
|
|
4334
|
+
} else {
|
|
4335
|
+
bez1 = [
|
|
4336
|
+
x1,
|
|
4337
|
+
y1,
|
|
4338
|
+
x1,
|
|
4339
|
+
y1,
|
|
4340
|
+
x1m,
|
|
4341
|
+
y1m,
|
|
4342
|
+
x1m,
|
|
4343
|
+
y1m
|
|
4344
|
+
];
|
|
4345
|
+
x1 = x1m;
|
|
4346
|
+
y1 = y1m;
|
|
4347
|
+
}
|
|
4348
|
+
for (let j = 0; j < pathLen2; j++) {
|
|
4349
|
+
const seg2 = path2[j];
|
|
4350
|
+
if (seg2[0] == "M") {
|
|
4351
|
+
x2 = seg2[1];
|
|
4352
|
+
y2 = seg2[2];
|
|
4353
|
+
x2m = x2;
|
|
4354
|
+
y2m = y2;
|
|
4355
|
+
} else if (seg2[0] == "C") {
|
|
4356
|
+
bez2 = [
|
|
4357
|
+
x2,
|
|
4358
|
+
y2,
|
|
4359
|
+
seg2[1],
|
|
4360
|
+
seg2[2],
|
|
4361
|
+
seg2[3],
|
|
4362
|
+
seg2[4],
|
|
4363
|
+
seg2[5],
|
|
4364
|
+
seg2[6]
|
|
4365
|
+
];
|
|
4366
|
+
x2 = bez2[6];
|
|
4367
|
+
y2 = bez2[7];
|
|
4368
|
+
}
|
|
4369
|
+
const intr = interHelper(bez1, bez2, { justCount });
|
|
4370
|
+
if (justCount) countResult += intr;
|
|
4371
|
+
else pointsResult.push(...intr);
|
|
4372
|
+
}
|
|
4373
|
+
}
|
|
4374
|
+
}
|
|
4375
|
+
return justCount ? countResult : pointsResult;
|
|
4376
|
+
};
|
|
4377
|
+
//#endregion
|
|
4278
4378
|
//#region src/main.ts
|
|
4279
4379
|
/**
|
|
4280
4380
|
* Creates a new SVGPathCommander instance with the following properties:
|
|
@@ -4464,7 +4564,8 @@ var SVGPathCommander = class {
|
|
|
4464
4564
|
*/
|
|
4465
4565
|
optimize() {
|
|
4466
4566
|
const { segments } = this;
|
|
4467
|
-
|
|
4567
|
+
const round = this.round === "off" ? 2 : this.round;
|
|
4568
|
+
this.segments = optimizePath(segments, round);
|
|
4468
4569
|
return this;
|
|
4469
4570
|
}
|
|
4470
4571
|
/**
|