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/util.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
|
*/
|
|
@@ -103,8 +103,325 @@ const lineTools = {
|
|
|
103
103
|
getPointAtLineLength
|
|
104
104
|
};
|
|
105
105
|
//#endregion
|
|
106
|
+
//#region src/math/bezier.ts
|
|
107
|
+
/**
|
|
108
|
+
* Tools from bezier.js by Mike 'Pomax' Kamermans
|
|
109
|
+
* @see https://github.com/Pomax/bezierjs
|
|
110
|
+
*/
|
|
111
|
+
const Tvalues = [
|
|
112
|
+
-.06405689286260563,
|
|
113
|
+
.06405689286260563,
|
|
114
|
+
-.1911188674736163,
|
|
115
|
+
.1911188674736163,
|
|
116
|
+
-.3150426796961634,
|
|
117
|
+
.3150426796961634,
|
|
118
|
+
-.4337935076260451,
|
|
119
|
+
.4337935076260451,
|
|
120
|
+
-.5454214713888396,
|
|
121
|
+
.5454214713888396,
|
|
122
|
+
-.6480936519369755,
|
|
123
|
+
.6480936519369755,
|
|
124
|
+
-.7401241915785544,
|
|
125
|
+
.7401241915785544,
|
|
126
|
+
-.820001985973903,
|
|
127
|
+
.820001985973903,
|
|
128
|
+
-.8864155270044011,
|
|
129
|
+
.8864155270044011,
|
|
130
|
+
-.9382745520027328,
|
|
131
|
+
.9382745520027328,
|
|
132
|
+
-.9747285559713095,
|
|
133
|
+
.9747285559713095,
|
|
134
|
+
-.9951872199970213,
|
|
135
|
+
.9951872199970213
|
|
136
|
+
];
|
|
137
|
+
const Cvalues = [
|
|
138
|
+
.12793819534675216,
|
|
139
|
+
.12793819534675216,
|
|
140
|
+
.1258374563468283,
|
|
141
|
+
.1258374563468283,
|
|
142
|
+
.12167047292780339,
|
|
143
|
+
.12167047292780339,
|
|
144
|
+
.1155056680537256,
|
|
145
|
+
.1155056680537256,
|
|
146
|
+
.10744427011596563,
|
|
147
|
+
.10744427011596563,
|
|
148
|
+
.09761865210411388,
|
|
149
|
+
.09761865210411388,
|
|
150
|
+
.08619016153195327,
|
|
151
|
+
.08619016153195327,
|
|
152
|
+
.0733464814110803,
|
|
153
|
+
.0733464814110803,
|
|
154
|
+
.05929858491543678,
|
|
155
|
+
.05929858491543678,
|
|
156
|
+
.04427743881741981,
|
|
157
|
+
.04427743881741981,
|
|
158
|
+
.028531388628933663,
|
|
159
|
+
.028531388628933663,
|
|
160
|
+
.0123412297999872,
|
|
161
|
+
.0123412297999872
|
|
162
|
+
];
|
|
163
|
+
/**
|
|
164
|
+
* @param points
|
|
165
|
+
* @returns
|
|
166
|
+
*/
|
|
167
|
+
const deriveBezier = (points) => {
|
|
168
|
+
const dpoints = [];
|
|
169
|
+
for (let p = points, d = p.length, c = d - 1; d > 1; d -= 1, c -= 1) {
|
|
170
|
+
const list = [];
|
|
171
|
+
for (let j = 0; j < c; j += 1) list.push({
|
|
172
|
+
x: c * (p[j + 1].x - p[j].x),
|
|
173
|
+
y: c * (p[j + 1].y - p[j].y),
|
|
174
|
+
t: 0
|
|
175
|
+
});
|
|
176
|
+
dpoints.push(list);
|
|
177
|
+
p = list;
|
|
178
|
+
}
|
|
179
|
+
return dpoints;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* @param points
|
|
183
|
+
* @param t
|
|
184
|
+
*/
|
|
185
|
+
const computeBezier = (points, t) => {
|
|
186
|
+
if (t === 0) {
|
|
187
|
+
points[0].t = 0;
|
|
188
|
+
return points[0];
|
|
189
|
+
}
|
|
190
|
+
const order = points.length - 1;
|
|
191
|
+
if (t === 1) {
|
|
192
|
+
points[order].t = 1;
|
|
193
|
+
return points[order];
|
|
194
|
+
}
|
|
195
|
+
const mt = 1 - t;
|
|
196
|
+
let p = points;
|
|
197
|
+
if (order === 0) {
|
|
198
|
+
points[0].t = t;
|
|
199
|
+
return points[0];
|
|
200
|
+
}
|
|
201
|
+
if (order === 1) return {
|
|
202
|
+
x: mt * p[0].x + t * p[1].x,
|
|
203
|
+
y: mt * p[0].y + t * p[1].y,
|
|
204
|
+
t
|
|
205
|
+
};
|
|
206
|
+
const mt2 = mt * mt;
|
|
207
|
+
const t2 = t * t;
|
|
208
|
+
let a = 0;
|
|
209
|
+
let b = 0;
|
|
210
|
+
let c = 0;
|
|
211
|
+
let d = 0;
|
|
212
|
+
if (order === 2) {
|
|
213
|
+
p = [
|
|
214
|
+
p[0],
|
|
215
|
+
p[1],
|
|
216
|
+
p[2],
|
|
217
|
+
{
|
|
218
|
+
x: 0,
|
|
219
|
+
y: 0
|
|
220
|
+
}
|
|
221
|
+
];
|
|
222
|
+
a = mt2;
|
|
223
|
+
b = mt * t * 2;
|
|
224
|
+
c = t2;
|
|
225
|
+
} else if (order === 3) {
|
|
226
|
+
a = mt2 * mt;
|
|
227
|
+
b = mt2 * t * 3;
|
|
228
|
+
c = mt * t2 * 3;
|
|
229
|
+
d = t * t2;
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
x: a * p[0].x + b * p[1].x + c * p[2].x + d * p[3].x,
|
|
233
|
+
y: a * p[0].y + b * p[1].y + c * p[2].y + d * p[3].y,
|
|
234
|
+
t
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
const calculateBezier = (derivativeFn, t) => {
|
|
238
|
+
const d = derivativeFn(t);
|
|
239
|
+
const l = d.x * d.x + d.y * d.y;
|
|
240
|
+
return Math.sqrt(l);
|
|
241
|
+
};
|
|
242
|
+
const bezierLength = (derivativeFn) => {
|
|
243
|
+
return bezierLengthAtT(derivativeFn, 1);
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* Returns the arc length of a Bezier segment from its start point
|
|
247
|
+
* up to a given `t` ratio, by integrating the curve speed `|B'(t)|`
|
|
248
|
+
* over the `[0, t]` interval with Gauss-Legendre quadrature.
|
|
249
|
+
*
|
|
250
|
+
* @param derivativeFn the curve derivative function
|
|
251
|
+
* @param t the sampled point ratio in `[0-1]`
|
|
252
|
+
* @returns the arc length up to parameter `t`
|
|
253
|
+
*/
|
|
254
|
+
const bezierLengthAtT = (derivativeFn, t) => {
|
|
255
|
+
const z = t / 2;
|
|
256
|
+
const len = Tvalues.length;
|
|
257
|
+
let sum = 0;
|
|
258
|
+
for (let i = 0, tMid; i < len; i++) {
|
|
259
|
+
tMid = z * Tvalues[i] + z;
|
|
260
|
+
sum += Cvalues[i] * calculateBezier(derivativeFn, tMid);
|
|
261
|
+
}
|
|
262
|
+
return z * sum;
|
|
263
|
+
};
|
|
264
|
+
/**
|
|
265
|
+
* Builds the Bezier points array from a curve coordinates array.
|
|
266
|
+
* @param curve cubic / quad bezier segment
|
|
267
|
+
*/
|
|
268
|
+
const getBezierPoints = (curve) => {
|
|
269
|
+
const points = [];
|
|
270
|
+
for (let idx = 0, len = curve.length, step = 2; idx < len; idx += step) points.push({
|
|
271
|
+
x: curve[idx],
|
|
272
|
+
y: curve[idx + 1]
|
|
273
|
+
});
|
|
274
|
+
return points;
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* Returns the arc length of a Bezier segment from its start point
|
|
278
|
+
* up to a given `t` ratio, for the segment given by its coordinates.
|
|
279
|
+
*
|
|
280
|
+
* @param curve cubic / quad bezier segment
|
|
281
|
+
* @param t the sampled point ratio in `[0-1]`
|
|
282
|
+
* @returns the arc length up to parameter `t`
|
|
283
|
+
*/
|
|
284
|
+
const getBezierLengthAtT = (curve, t) => {
|
|
285
|
+
const dpoints = deriveBezier(getBezierPoints(curve));
|
|
286
|
+
return bezierLengthAtT((ratio) => {
|
|
287
|
+
return computeBezier(dpoints[0], ratio);
|
|
288
|
+
}, t);
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* Returns the `t` parameter of a Bezier segment at which the cumulative
|
|
292
|
+
* arc length equals the given `distance`, found by interval bisection.
|
|
293
|
+
*
|
|
294
|
+
* @param curve cubic / quad bezier segment
|
|
295
|
+
* @param distance the distance along the segment in `[0, length]`
|
|
296
|
+
* @returns the `t` parameter in `[0-1]`
|
|
297
|
+
*/
|
|
298
|
+
const getTAtBezierLength = (curve, distance) => {
|
|
299
|
+
const dpoints = deriveBezier(getBezierPoints(curve));
|
|
300
|
+
const derivativeFn = (ratio) => {
|
|
301
|
+
return computeBezier(dpoints[0], ratio);
|
|
302
|
+
};
|
|
303
|
+
let lo = 0;
|
|
304
|
+
let hi = 1;
|
|
305
|
+
for (let i = 0; i < 25; i += 1) {
|
|
306
|
+
const mid = (lo + hi) / 2;
|
|
307
|
+
if (bezierLengthAtT(derivativeFn, mid) < distance) lo = mid;
|
|
308
|
+
else hi = mid;
|
|
309
|
+
}
|
|
310
|
+
return (lo + hi) / 2;
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* Returns the length of CubicBezier / Quad segment.
|
|
314
|
+
* @param curve cubic / quad bezier segment
|
|
315
|
+
*/
|
|
316
|
+
const getBezierLength = (curve) => {
|
|
317
|
+
const points = getBezierPoints(curve);
|
|
318
|
+
const dpoints = deriveBezier(points);
|
|
319
|
+
return bezierLength((t) => {
|
|
320
|
+
return computeBezier(dpoints[0], t);
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
const CBEZIER_MINMAX_EPSILON = 1e-8;
|
|
324
|
+
/**
|
|
325
|
+
* Returns the most extreme points in a Quad Bezier segment.
|
|
326
|
+
* @param A an array which consist of X/Y values
|
|
327
|
+
*/
|
|
328
|
+
const minmaxQ = ([v1, cp, v2]) => {
|
|
329
|
+
const min = Math.min(v1, v2);
|
|
330
|
+
const max = Math.max(v1, v2);
|
|
331
|
+
if (cp >= v1 ? v2 >= cp : v2 <= cp) return [min, max];
|
|
332
|
+
const E = (v1 * v2 - cp * cp) / (v1 - 2 * cp + v2);
|
|
333
|
+
return E < min ? [E, max] : [min, E];
|
|
334
|
+
};
|
|
335
|
+
/**
|
|
336
|
+
* Returns the most extreme points in a Cubic Bezier segment.
|
|
337
|
+
* @param A an array which consist of X/Y values
|
|
338
|
+
* @see https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127
|
|
339
|
+
*/
|
|
340
|
+
const minmaxC = ([v1, cp1, cp2, v2]) => {
|
|
341
|
+
const K = v1 - 3 * cp1 + 3 * cp2 - v2;
|
|
342
|
+
if (Math.abs(K) < 1e-8) {
|
|
343
|
+
if (v1 === v2 && v1 === cp1) return [v1, v2];
|
|
344
|
+
return minmaxQ([
|
|
345
|
+
v1,
|
|
346
|
+
-.5 * v1 + 1.5 * cp1,
|
|
347
|
+
v1 - 3 * cp1 + 3 * cp2
|
|
348
|
+
]);
|
|
349
|
+
}
|
|
350
|
+
const T = -v1 * cp2 + v1 * v2 - cp1 * cp2 - cp1 * v2 + cp1 * cp1 + cp2 * cp2;
|
|
351
|
+
if (T <= 0) return [Math.min(v1, v2), Math.max(v1, v2)];
|
|
352
|
+
const S = Math.sqrt(T);
|
|
353
|
+
let min = Math.min(v1, v2);
|
|
354
|
+
let max = Math.max(v1, v2);
|
|
355
|
+
const L = v1 - 2 * cp1 + cp2;
|
|
356
|
+
for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) if (R > 0 && R < 1) {
|
|
357
|
+
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;
|
|
358
|
+
if (Q < min) min = Q;
|
|
359
|
+
if (Q > max) max = Q;
|
|
360
|
+
}
|
|
361
|
+
return [min, max];
|
|
362
|
+
};
|
|
363
|
+
const bezierTools = {
|
|
364
|
+
bezierLength,
|
|
365
|
+
bezierLengthAtT,
|
|
366
|
+
calculateBezier,
|
|
367
|
+
CBEZIER_MINMAX_EPSILON,
|
|
368
|
+
computeBezier,
|
|
369
|
+
Cvalues,
|
|
370
|
+
deriveBezier,
|
|
371
|
+
getBezierLength,
|
|
372
|
+
getBezierLengthAtT,
|
|
373
|
+
getBezierPoints,
|
|
374
|
+
getTAtBezierLength,
|
|
375
|
+
minmaxC,
|
|
376
|
+
minmaxQ,
|
|
377
|
+
Tvalues
|
|
378
|
+
};
|
|
379
|
+
//#endregion
|
|
106
380
|
//#region src/math/arcTools.ts
|
|
107
381
|
/**
|
|
382
|
+
* Returns the signed arc length of an ellipse segment from angle `0` up to
|
|
383
|
+
* the given `theta` angle, by integrating the curve speed
|
|
384
|
+
* `|sqrt(rx^2 * sin^2(phi) + ry^2 * cos^2(phi))|` over the `[0, theta]`
|
|
385
|
+
* interval with Gauss-Legendre quadrature.
|
|
386
|
+
*
|
|
387
|
+
* @param rx radius along X axis
|
|
388
|
+
* @param ry radius along Y axis
|
|
389
|
+
* @param theta the angle in radians
|
|
390
|
+
* @returns the signed arc length
|
|
391
|
+
*/
|
|
392
|
+
const arcLengthAtAngle = (rx, ry, theta) => {
|
|
393
|
+
const sign = theta < 0 ? -1 : 1;
|
|
394
|
+
const absTheta = Math.abs(theta);
|
|
395
|
+
const step = Math.PI / 2;
|
|
396
|
+
let total = 0;
|
|
397
|
+
let remaining = absTheta;
|
|
398
|
+
let offset = 0;
|
|
399
|
+
while (remaining > 0) {
|
|
400
|
+
const span = Math.min(step, remaining);
|
|
401
|
+
const z = span / 2;
|
|
402
|
+
let sum = 0;
|
|
403
|
+
for (let i = 0, len = Tvalues.length; i < len; i += 1) {
|
|
404
|
+
const phi = offset + z * Tvalues[i] + z;
|
|
405
|
+
sum += Cvalues[i] * Math.sqrt((rx * Math.sin(phi)) ** 2 + (ry * Math.cos(phi)) ** 2);
|
|
406
|
+
}
|
|
407
|
+
total += z * sum;
|
|
408
|
+
offset += span;
|
|
409
|
+
remaining -= span;
|
|
410
|
+
}
|
|
411
|
+
return sign * total;
|
|
412
|
+
};
|
|
413
|
+
/**
|
|
414
|
+
* Returns the signed arc length of an ellipse segment between two angles.
|
|
415
|
+
* @param rx radius along X axis
|
|
416
|
+
* @param ry radius along Y axis
|
|
417
|
+
* @param from the start angle in radians
|
|
418
|
+
* @param to the end angle in radians
|
|
419
|
+
* @returns the signed arc length
|
|
420
|
+
*/
|
|
421
|
+
const arcLengthBetween = (rx, ry, from, to) => {
|
|
422
|
+
return arcLengthAtAngle(rx, ry, to) - arcLengthAtAngle(rx, ry, from);
|
|
423
|
+
};
|
|
424
|
+
/**
|
|
108
425
|
* Returns the Arc segment length.
|
|
109
426
|
* @param rx radius along X axis
|
|
110
427
|
* @param ry radius along Y axis
|
|
@@ -112,13 +429,7 @@ const lineTools = {
|
|
|
112
429
|
* @returns the arc length
|
|
113
430
|
*/
|
|
114
431
|
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);
|
|
432
|
+
return Math.abs(arcLengthAtAngle(rx, ry, theta));
|
|
122
433
|
};
|
|
123
434
|
/**
|
|
124
435
|
* Find point on ellipse at given angle around ellipse (theta);
|
|
@@ -221,10 +532,11 @@ const getArcProps = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
221
532
|
x: 1,
|
|
222
533
|
y: 0
|
|
223
534
|
}, startVector);
|
|
224
|
-
|
|
535
|
+
const endVector = {
|
|
225
536
|
x: (-transformedPoint.x - transformedCenter.x) / rx,
|
|
226
537
|
y: (-transformedPoint.y - transformedCenter.y) / ry
|
|
227
|
-
}
|
|
538
|
+
};
|
|
539
|
+
let sweepAngle = angleBetween(startVector, endVector);
|
|
228
540
|
if (!SF && sweepAngle > 0) sweepAngle -= 2 * PI;
|
|
229
541
|
else if (SF && sweepAngle < 0) sweepAngle += 2 * PI;
|
|
230
542
|
sweepAngle %= 2 * PI;
|
|
@@ -251,7 +563,7 @@ const getArcProps = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
251
563
|
*/
|
|
252
564
|
const getArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
253
565
|
const { rx, ry, startAngle, endAngle } = getArcProps(x1, y1, RX, RY, angle, LAF, SF, x, y);
|
|
254
|
-
return
|
|
566
|
+
return Math.abs(arcLengthBetween(rx, ry, startAngle, endAngle));
|
|
255
567
|
};
|
|
256
568
|
/**
|
|
257
569
|
* Returns a point along an Arc segment at a given distance.
|
|
@@ -275,7 +587,8 @@ const getPointAtArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y, distance) =>
|
|
|
275
587
|
};
|
|
276
588
|
const { center, rx, ry, startAngle, endAngle } = getArcProps(x1, y1, RX, RY, angle, LAF, SF, x, y);
|
|
277
589
|
if (typeof distance === "number") {
|
|
278
|
-
const
|
|
590
|
+
const sweepAngle = endAngle - startAngle;
|
|
591
|
+
const length = Math.abs(arcLengthBetween(rx, ry, startAngle, endAngle));
|
|
279
592
|
if (distance <= 0) point = {
|
|
280
593
|
x: x1,
|
|
281
594
|
y: y1
|
|
@@ -291,9 +604,15 @@ const getPointAtArcLength = (x1, y1, RX, RY, angle, LAF, SF, x, y, distance) =>
|
|
|
291
604
|
};
|
|
292
605
|
if (rx === 0 || ry === 0) return getPointAtLineLength(x1, y1, x, y, distance);
|
|
293
606
|
const { PI, cos, sin } = Math;
|
|
294
|
-
const sweepAngle = endAngle - startAngle;
|
|
295
607
|
const xRotRad = (angle % 360 + 360) % 360 * (PI / 180);
|
|
296
|
-
|
|
608
|
+
let lo = 0;
|
|
609
|
+
let hi = 1;
|
|
610
|
+
for (let i = 0; i < 25; i += 1) {
|
|
611
|
+
const mid = (lo + hi) / 2;
|
|
612
|
+
if (Math.abs(arcLengthBetween(rx, ry, startAngle, startAngle + sweepAngle * mid)) < distance) lo = mid;
|
|
613
|
+
else hi = mid;
|
|
614
|
+
}
|
|
615
|
+
const alpha = startAngle + sweepAngle * ((lo + hi) / 2);
|
|
297
616
|
const ellipseComponentX = rx * cos(alpha);
|
|
298
617
|
const ellipseComponentY = ry * sin(alpha);
|
|
299
618
|
point = {
|
|
@@ -343,8 +662,10 @@ const getArcBBox = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
343
662
|
let xMax = max(x1, x);
|
|
344
663
|
let yMin = min(y1, y);
|
|
345
664
|
let yMax = max(y1, y);
|
|
346
|
-
const
|
|
347
|
-
const
|
|
665
|
+
const angleAfterStart = endAngle - deltaAngle * 1e-5;
|
|
666
|
+
const pP2 = arcPoint(cx, cy, rx, ry, alpha, angleAfterStart);
|
|
667
|
+
const angleBeforeEnd = endAngle - deltaAngle * .99999;
|
|
668
|
+
const pP3 = arcPoint(cx, cy, rx, ry, alpha, angleBeforeEnd);
|
|
348
669
|
/**
|
|
349
670
|
* expected extremes
|
|
350
671
|
* if leaving inner bounding box
|
|
@@ -385,6 +706,8 @@ const getArcBBox = (x1, y1, RX, RY, angle, LAF, SF, x, y) => {
|
|
|
385
706
|
const arcTools = {
|
|
386
707
|
angleBetween,
|
|
387
708
|
arcLength,
|
|
709
|
+
arcLengthAtAngle,
|
|
710
|
+
arcLengthBetween,
|
|
388
711
|
arcPoint,
|
|
389
712
|
getArcBBox,
|
|
390
713
|
getArcLength,
|
|
@@ -392,221 +715,7 @@ const arcTools = {
|
|
|
392
715
|
getPointAtArcLength
|
|
393
716
|
};
|
|
394
717
|
//#endregion
|
|
395
|
-
//#region src/math/
|
|
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
|
-
//#region src/math/cubicTools.ts
|
|
718
|
+
//#region src/math/cubicTools.ts
|
|
610
719
|
/**
|
|
611
720
|
* Returns a point at a given length of a CubicBezier segment.
|
|
612
721
|
*
|
|
@@ -674,7 +783,7 @@ const getPointAtCubicLength = (x1, y1, c1x, c1y, c2x, c2y, x2, y2, distance) =>
|
|
|
674
783
|
y: y1
|
|
675
784
|
};
|
|
676
785
|
if (distanceIsNumber) {
|
|
677
|
-
const
|
|
786
|
+
const coords = [
|
|
678
787
|
x1,
|
|
679
788
|
y1,
|
|
680
789
|
c1x,
|
|
@@ -683,21 +792,16 @@ const getPointAtCubicLength = (x1, y1, c1x, c1y, c2x, c2y, x2, y2, distance) =>
|
|
|
683
792
|
c2y,
|
|
684
793
|
x2,
|
|
685
794
|
y2
|
|
686
|
-
]
|
|
795
|
+
];
|
|
796
|
+
const currentLength = getBezierLength(coords);
|
|
687
797
|
if (distance <= 0) {} else if (distance >= currentLength) point = {
|
|
688
798
|
x: x2,
|
|
689
799
|
y: y2
|
|
690
800
|
};
|
|
691
|
-
else
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
c1y,
|
|
696
|
-
c2x,
|
|
697
|
-
c2y,
|
|
698
|
-
x2,
|
|
699
|
-
y2
|
|
700
|
-
], distance / currentLength);
|
|
801
|
+
else {
|
|
802
|
+
const t = getTAtBezierLength(coords, distance);
|
|
803
|
+
point = getPointAtCubicSegmentLength(coords, t);
|
|
804
|
+
}
|
|
701
805
|
}
|
|
702
806
|
return point;
|
|
703
807
|
};
|
|
@@ -805,26 +909,23 @@ const getPointAtQuadLength = (x1, y1, cx, cy, x2, y2, distance) => {
|
|
|
805
909
|
y: y1
|
|
806
910
|
};
|
|
807
911
|
if (distanceIsNumber) {
|
|
808
|
-
const
|
|
912
|
+
const coords = [
|
|
809
913
|
x1,
|
|
810
914
|
y1,
|
|
811
915
|
cx,
|
|
812
916
|
cy,
|
|
813
917
|
x2,
|
|
814
918
|
y2
|
|
815
|
-
]
|
|
919
|
+
];
|
|
920
|
+
const currentLength = getBezierLength(coords);
|
|
816
921
|
if (distance <= 0) {} else if (distance >= currentLength) point = {
|
|
817
922
|
x: x2,
|
|
818
923
|
y: y2
|
|
819
924
|
};
|
|
820
|
-
else
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
cy,
|
|
825
|
-
x2,
|
|
826
|
-
y2
|
|
827
|
-
], distance / currentLength);
|
|
925
|
+
else {
|
|
926
|
+
const t = getTAtBezierLength(coords, distance);
|
|
927
|
+
point = getPointAtQuadSegmentLength(coords, t);
|
|
928
|
+
}
|
|
828
929
|
}
|
|
829
930
|
return point;
|
|
830
931
|
};
|
|
@@ -1432,7 +1533,8 @@ const iterate = (path, iterator) => {
|
|
|
1432
1533
|
* ```
|
|
1433
1534
|
*/
|
|
1434
1535
|
const pathToAbsolute = (pathInput) => {
|
|
1435
|
-
|
|
1536
|
+
const path = parsePathString(pathInput);
|
|
1537
|
+
return iterate(path, absolutizeSegment);
|
|
1436
1538
|
};
|
|
1437
1539
|
//#endregion
|
|
1438
1540
|
//#region src/process/relativizeSegment.ts
|
|
@@ -1489,7 +1591,8 @@ const relativizeSegment = (segment, index, lastX, lastY) => {
|
|
|
1489
1591
|
* ```
|
|
1490
1592
|
*/
|
|
1491
1593
|
const pathToRelative = (pathInput) => {
|
|
1492
|
-
|
|
1594
|
+
const path = parsePathString(pathInput);
|
|
1595
|
+
return iterate(path, relativizeSegment);
|
|
1493
1596
|
};
|
|
1494
1597
|
//#endregion
|
|
1495
1598
|
//#region src/process/arcToCubic.ts
|
|
@@ -2091,8 +2194,6 @@ const getPointAtLength = (pathInput, distance) => {
|
|
|
2091
2194
|
const path = normalizePath(pathInput);
|
|
2092
2195
|
let isM = false;
|
|
2093
2196
|
let data = [];
|
|
2094
|
-
let x = 0;
|
|
2095
|
-
let y = 0;
|
|
2096
2197
|
let [mx, my] = path[0].slice(1);
|
|
2097
2198
|
const distanceIsNumber = typeof distance === "number";
|
|
2098
2199
|
let point = {
|
|
@@ -2133,21 +2234,13 @@ const getPointAtLength = (pathInput, distance) => {
|
|
|
2133
2234
|
mx,
|
|
2134
2235
|
my
|
|
2135
2236
|
];
|
|
2136
|
-
point =
|
|
2137
|
-
x: mx,
|
|
2138
|
-
y: my
|
|
2139
|
-
};
|
|
2237
|
+
point = getPointAtLineLength(data[0], data[1], data[2], data[3], distance - totalLength);
|
|
2140
2238
|
length = getLineLength(data[0], data[1], data[2], data[3]);
|
|
2141
2239
|
}
|
|
2142
|
-
[x, y] = data.slice(-2);
|
|
2143
2240
|
if (totalLength < distance) POINT = point;
|
|
2144
2241
|
else return false;
|
|
2145
2242
|
totalLength += length;
|
|
2146
2243
|
});
|
|
2147
|
-
if (distance > totalLength - 1e-5) return {
|
|
2148
|
-
x,
|
|
2149
|
-
y
|
|
2150
|
-
};
|
|
2151
2244
|
return POINT;
|
|
2152
2245
|
};
|
|
2153
2246
|
//#endregion
|
|
@@ -2507,7 +2600,7 @@ const isMultiPath = (path) => {
|
|
|
2507
2600
|
}
|
|
2508
2601
|
return false;
|
|
2509
2602
|
}
|
|
2510
|
-
throw new TypeError(
|
|
2603
|
+
throw new TypeError("SVGPathCommanderError: expected string or PathArray");
|
|
2511
2604
|
};
|
|
2512
2605
|
//#endregion
|
|
2513
2606
|
//#region src/util/isClosedPath.ts
|
|
@@ -2991,7 +3084,8 @@ const optimizePath = (pathInput, roundOption) => {
|
|
|
2991
3084
|
const shortSegment = shortenSegment(seg, normalizedSegment, optimParams, prevCommand);
|
|
2992
3085
|
const absSegment = roundSegment(shortSegment, round);
|
|
2993
3086
|
const absString = absSegment.join("");
|
|
2994
|
-
const
|
|
3087
|
+
const relativeSegment = relativizeSegment(shortSegment, i, lastX, lastY);
|
|
3088
|
+
const relSegment = roundSegment(relativeSegment, round);
|
|
2995
3089
|
const relString = relSegment.join("");
|
|
2996
3090
|
result = absString.length < relString.length ? absSegment : relSegment;
|
|
2997
3091
|
}
|
|
@@ -3393,7 +3487,7 @@ const roundPath = (path, roundOption) => {
|
|
|
3393
3487
|
//#region src/morph/fixPath.ts
|
|
3394
3488
|
/**
|
|
3395
3489
|
* Checks a `PathArray` for an unnecessary `Z` segment
|
|
3396
|
-
* and
|
|
3490
|
+
* and removes it. The `PathArray` is modified in place.
|
|
3397
3491
|
* In short, if the segment before `Z` extends to `M`,
|
|
3398
3492
|
* the `Z` segment must be removed.
|
|
3399
3493
|
*
|
|
@@ -3792,7 +3886,7 @@ function getRotations(a) {
|
|
|
3792
3886
|
*/
|
|
3793
3887
|
function getRotatedPath(pathA, pathB, computedRotations) {
|
|
3794
3888
|
const rotations = computedRotations || getRotations(pathA);
|
|
3795
|
-
if (pathA.length !== pathB.length) throw new TypeError(
|
|
3889
|
+
if (pathA.length !== pathB.length) throw new TypeError("SVGPathCommanderError: paths must have the same number of segments after equalization");
|
|
3796
3890
|
let bestIndex = 0;
|
|
3797
3891
|
let minDistanceSq = Infinity;
|
|
3798
3892
|
for (let ri = 0; ri < rotations.length; ri++) {
|
|
@@ -3890,18 +3984,7 @@ const equalizeSegments = (path1, path2, initialCfg = {}) => {
|
|
|
3890
3984
|
return [equalP1, equalP2];
|
|
3891
3985
|
};
|
|
3892
3986
|
//#endregion
|
|
3893
|
-
//#region src/
|
|
3894
|
-
const intersect = (x1, y1, x2, y2, x3, y3, x4, y4) => {
|
|
3895
|
-
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;
|
|
3896
|
-
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);
|
|
3897
|
-
if (!denominator) return;
|
|
3898
|
-
const px = nx / denominator, py = ny / denominator, px2 = roundTo(px, 2), py2 = roundTo(py, 2);
|
|
3899
|
-
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;
|
|
3900
|
-
return {
|
|
3901
|
-
x: px,
|
|
3902
|
-
y: py
|
|
3903
|
-
};
|
|
3904
|
-
};
|
|
3987
|
+
//#region src/intersect/isPointInsideBBox.ts
|
|
3905
3988
|
/**
|
|
3906
3989
|
* Checks if a point is inside a bounding box.
|
|
3907
3990
|
*
|
|
@@ -3913,6 +3996,8 @@ const isPointInsideBBox = (bbox, [x, y]) => {
|
|
|
3913
3996
|
const [minX, minY, maxX, maxY] = bbox;
|
|
3914
3997
|
return x >= minX && x <= maxX && y >= minY && y <= maxY;
|
|
3915
3998
|
};
|
|
3999
|
+
//#endregion
|
|
4000
|
+
//#region src/intersect/boundingBoxIntersect.ts
|
|
3916
4001
|
/**
|
|
3917
4002
|
* Checks if two bounding boxes intersect.
|
|
3918
4003
|
*
|
|
@@ -3925,154 +4010,6 @@ const boundingBoxIntersect = (a, b) => {
|
|
|
3925
4010
|
const [bx1, by1, bx2, by2] = b;
|
|
3926
4011
|
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);
|
|
3927
4012
|
};
|
|
3928
|
-
const interHelper = (bez1, bez2, config) => {
|
|
3929
|
-
const bbox1 = getCubicBBox(...bez1);
|
|
3930
|
-
const bbox2 = getCubicBBox(...bez2);
|
|
3931
|
-
const { justCount, epsilon } = Object.assign({
|
|
3932
|
-
justCount: true,
|
|
3933
|
-
epsilon: DISTANCE_EPSILON
|
|
3934
|
-
}, config);
|
|
3935
|
-
if (!boundingBoxIntersect(bbox1, bbox2)) return justCount ? 0 : [];
|
|
3936
|
-
const l1 = getCubicLength(...bez1), l2 = getCubicLength(...bez2), n1 = Math.max(l1 / 5 >> 0, 1), n2 = Math.max(l2 / 5 >> 0, 1), points1 = [], points2 = [], xy = {};
|
|
3937
|
-
let res = justCount ? 0 : [];
|
|
3938
|
-
for (let i = 0; i < n1 + 1; i++) {
|
|
3939
|
-
const p = getPointAtCubicLength(...bez1, i / n1 * l1);
|
|
3940
|
-
points1.push({
|
|
3941
|
-
x: p.x,
|
|
3942
|
-
y: p.y,
|
|
3943
|
-
t: i / n1
|
|
3944
|
-
});
|
|
3945
|
-
}
|
|
3946
|
-
for (let i = 0; i < n2 + 1; i++) {
|
|
3947
|
-
const p = getPointAtCubicLength(...bez2, i / n2 * l2);
|
|
3948
|
-
points2.push({
|
|
3949
|
-
x: p.x,
|
|
3950
|
-
y: p.y,
|
|
3951
|
-
t: i / n2
|
|
3952
|
-
});
|
|
3953
|
-
}
|
|
3954
|
-
for (let i = 0; i < n1; i++) for (let j = 0; j < n2; j++) {
|
|
3955
|
-
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);
|
|
3956
|
-
if (is) {
|
|
3957
|
-
if (xy[is.x.toFixed(4)] == is.y.toFixed(4)) continue;
|
|
3958
|
-
xy[is.x.toFixed(4)] = is.y.toFixed(4);
|
|
3959
|
-
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);
|
|
3960
|
-
if (t1 >= 0 && t1 <= maxLimit && t2 >= 0 && t2 <= maxLimit) if (justCount) res++;
|
|
3961
|
-
else res.push({
|
|
3962
|
-
x: is.x,
|
|
3963
|
-
y: is.y,
|
|
3964
|
-
t1: Math.min(t1, 1),
|
|
3965
|
-
t2: Math.min(t2, 1)
|
|
3966
|
-
});
|
|
3967
|
-
}
|
|
3968
|
-
}
|
|
3969
|
-
return res;
|
|
3970
|
-
};
|
|
3971
|
-
/**
|
|
3972
|
-
* Finds intersection points between two paths.
|
|
3973
|
-
*
|
|
3974
|
-
* @param pathInput1 - First path string or PathArray
|
|
3975
|
-
* @param pathInput2 - Second path string or PathArray
|
|
3976
|
-
* @param justCount - If true, returns the count of intersections; if false, returns the intersection points
|
|
3977
|
-
* @returns The number of intersections (when justCount is true) or an array of IntersectionPoint objects
|
|
3978
|
-
*
|
|
3979
|
-
* @example
|
|
3980
|
-
* ```ts
|
|
3981
|
-
* pathsIntersection('M0 50C0 0,100 0,100 50', 'M50 0C100 0,100 100,50 100', true)
|
|
3982
|
-
* // => 1
|
|
3983
|
-
* pathsIntersection('M0 50C0 0,100 0,100 50', 'M50 0C100 0,100 100,50 100', false)
|
|
3984
|
-
* // => [{ x: 50, y: 25, t1: 0.5, t2: 0.5 }]
|
|
3985
|
-
* ```
|
|
3986
|
-
*/
|
|
3987
|
-
const pathsIntersection = (pathInput1, pathInput2, justCount = true) => {
|
|
3988
|
-
const path1 = pathToCurve(pathInput1);
|
|
3989
|
-
const path2 = pathToCurve(pathInput2);
|
|
3990
|
-
let x1 = 0, y1 = 0, x2 = 0, y2 = 0, x1m = 0, y1m = 0, x2m = 0, y2m = 0, bez1 = [
|
|
3991
|
-
x1,
|
|
3992
|
-
y1,
|
|
3993
|
-
x1,
|
|
3994
|
-
y1,
|
|
3995
|
-
x1m,
|
|
3996
|
-
y1m,
|
|
3997
|
-
x1m,
|
|
3998
|
-
y1m
|
|
3999
|
-
], bez2 = [
|
|
4000
|
-
x2,
|
|
4001
|
-
y2,
|
|
4002
|
-
x2,
|
|
4003
|
-
y2,
|
|
4004
|
-
x2m,
|
|
4005
|
-
y2m,
|
|
4006
|
-
x2m,
|
|
4007
|
-
y2m
|
|
4008
|
-
], countResult = 0;
|
|
4009
|
-
const pointsResult = [];
|
|
4010
|
-
const pathLen1 = path1.length;
|
|
4011
|
-
const pathLen2 = path2.length;
|
|
4012
|
-
for (let i = 0; i < pathLen1; i++) {
|
|
4013
|
-
const seg1 = path1[i];
|
|
4014
|
-
if (seg1[0] == "M") {
|
|
4015
|
-
x1 = seg1[1];
|
|
4016
|
-
y1 = seg1[2];
|
|
4017
|
-
x1m = x1;
|
|
4018
|
-
y1m = y1;
|
|
4019
|
-
} else {
|
|
4020
|
-
if (seg1[0] == "C") {
|
|
4021
|
-
bez1 = [
|
|
4022
|
-
x1,
|
|
4023
|
-
y1,
|
|
4024
|
-
seg1[1],
|
|
4025
|
-
seg1[2],
|
|
4026
|
-
seg1[3],
|
|
4027
|
-
seg1[4],
|
|
4028
|
-
seg1[5],
|
|
4029
|
-
seg1[6]
|
|
4030
|
-
];
|
|
4031
|
-
x1 = bez1[6];
|
|
4032
|
-
y1 = bez1[7];
|
|
4033
|
-
} else {
|
|
4034
|
-
bez1 = [
|
|
4035
|
-
x1,
|
|
4036
|
-
y1,
|
|
4037
|
-
x1,
|
|
4038
|
-
y1,
|
|
4039
|
-
x1m,
|
|
4040
|
-
y1m,
|
|
4041
|
-
x1m,
|
|
4042
|
-
y1m
|
|
4043
|
-
];
|
|
4044
|
-
x1 = x1m;
|
|
4045
|
-
y1 = y1m;
|
|
4046
|
-
}
|
|
4047
|
-
for (let j = 0; j < pathLen2; j++) {
|
|
4048
|
-
const seg2 = path2[j];
|
|
4049
|
-
if (seg2[0] == "M") {
|
|
4050
|
-
x2 = seg2[1];
|
|
4051
|
-
y2 = seg2[2];
|
|
4052
|
-
x2m = x2;
|
|
4053
|
-
y2m = y2;
|
|
4054
|
-
} else if (seg2[0] == "C") {
|
|
4055
|
-
bez2 = [
|
|
4056
|
-
x2,
|
|
4057
|
-
y2,
|
|
4058
|
-
seg2[1],
|
|
4059
|
-
seg2[2],
|
|
4060
|
-
seg2[3],
|
|
4061
|
-
seg2[4],
|
|
4062
|
-
seg2[5],
|
|
4063
|
-
seg2[6]
|
|
4064
|
-
];
|
|
4065
|
-
x2 = bez2[6];
|
|
4066
|
-
y2 = bez2[7];
|
|
4067
|
-
}
|
|
4068
|
-
const intr = interHelper(bez1, bez2, { justCount });
|
|
4069
|
-
if (justCount) countResult += intr;
|
|
4070
|
-
else pointsResult.push(...intr);
|
|
4071
|
-
}
|
|
4072
|
-
}
|
|
4073
|
-
}
|
|
4074
|
-
return justCount ? countResult : pointsResult;
|
|
4075
|
-
};
|
|
4076
4013
|
//#endregion
|
|
4077
4014
|
//#region src/morph/createPlaceholder.ts
|
|
4078
4015
|
/**
|
|
@@ -4272,6 +4209,169 @@ const equalizePaths = (pathInput1, pathInput2, initialCfg = {}) => {
|
|
|
4272
4209
|
return [equalizedPairs.map((p) => p[0]).flat(), equalizedPairs.map((p) => p[1]).flat()];
|
|
4273
4210
|
};
|
|
4274
4211
|
//#endregion
|
|
4212
|
+
//#region src/intersect/interHelper.ts
|
|
4213
|
+
const intersect = (x1, y1, x2, y2, x3, y3, x4, y4) => {
|
|
4214
|
+
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;
|
|
4215
|
+
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);
|
|
4216
|
+
if (!denominator) return;
|
|
4217
|
+
const px = nx / denominator, py = ny / denominator, px2 = roundTo(px, 2), py2 = roundTo(py, 2);
|
|
4218
|
+
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;
|
|
4219
|
+
return {
|
|
4220
|
+
x: px,
|
|
4221
|
+
y: py
|
|
4222
|
+
};
|
|
4223
|
+
};
|
|
4224
|
+
const interHelper = (bez1, bez2, config) => {
|
|
4225
|
+
const bbox1 = getCubicBBox(...bez1);
|
|
4226
|
+
const bbox2 = getCubicBBox(...bez2);
|
|
4227
|
+
const { justCount, epsilon } = Object.assign({
|
|
4228
|
+
justCount: true,
|
|
4229
|
+
epsilon: DISTANCE_EPSILON
|
|
4230
|
+
}, config);
|
|
4231
|
+
if (!boundingBoxIntersect(bbox1, bbox2)) return justCount ? 0 : [];
|
|
4232
|
+
const l1 = getCubicLength(...bez1), l2 = getCubicLength(...bez2), n1 = Math.max(l1 / 5 >> 0, 1), n2 = Math.max(l2 / 5 >> 0, 1), points1 = [], points2 = [], xy = {};
|
|
4233
|
+
let res = justCount ? 0 : [];
|
|
4234
|
+
for (let i = 0; i < n1 + 1; i++) {
|
|
4235
|
+
const p = getPointAtCubicLength(...bez1, i / n1 * l1);
|
|
4236
|
+
points1.push({
|
|
4237
|
+
x: p.x,
|
|
4238
|
+
y: p.y,
|
|
4239
|
+
t: i / n1
|
|
4240
|
+
});
|
|
4241
|
+
}
|
|
4242
|
+
for (let i = 0; i < n2 + 1; i++) {
|
|
4243
|
+
const p = getPointAtCubicLength(...bez2, i / n2 * l2);
|
|
4244
|
+
points2.push({
|
|
4245
|
+
x: p.x,
|
|
4246
|
+
y: p.y,
|
|
4247
|
+
t: i / n2
|
|
4248
|
+
});
|
|
4249
|
+
}
|
|
4250
|
+
for (let i = 0; i < n1; i++) for (let j = 0; j < n2; j++) {
|
|
4251
|
+
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);
|
|
4252
|
+
if (is) {
|
|
4253
|
+
if (xy[is.x.toFixed(4)] == is.y.toFixed(4)) continue;
|
|
4254
|
+
xy[is.x.toFixed(4)] = is.y.toFixed(4);
|
|
4255
|
+
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);
|
|
4256
|
+
if (t1 >= 0 && t1 <= maxLimit && t2 >= 0 && t2 <= maxLimit) if (justCount) res++;
|
|
4257
|
+
else res.push({
|
|
4258
|
+
x: is.x,
|
|
4259
|
+
y: is.y,
|
|
4260
|
+
t1: Math.min(t1, 1),
|
|
4261
|
+
t2: Math.min(t2, 1)
|
|
4262
|
+
});
|
|
4263
|
+
}
|
|
4264
|
+
}
|
|
4265
|
+
return res;
|
|
4266
|
+
};
|
|
4267
|
+
//#endregion
|
|
4268
|
+
//#region src/intersect/pathIntersection.ts
|
|
4269
|
+
/**
|
|
4270
|
+
* Finds intersection points between two paths.
|
|
4271
|
+
*
|
|
4272
|
+
* @param pathInput1 - First path string or PathArray
|
|
4273
|
+
* @param pathInput2 - Second path string or PathArray
|
|
4274
|
+
* @param justCount - If true, returns the count of intersections; if false, returns the intersection points
|
|
4275
|
+
* @returns The number of intersections (when justCount is true) or an array of IntersectionPoint objects
|
|
4276
|
+
*
|
|
4277
|
+
* @example
|
|
4278
|
+
* ```ts
|
|
4279
|
+
* pathsIntersection('M0 50C0 0,100 0,100 50', 'M50 0C100 0,100 100,50 100', true)
|
|
4280
|
+
* // => 1
|
|
4281
|
+
* pathsIntersection('M0 50C0 0,100 0,100 50', 'M50 0C100 0,100 100,50 100', false)
|
|
4282
|
+
* // => [{ x: 50, y: 25, t1: 0.5, t2: 0.5 }]
|
|
4283
|
+
* ```
|
|
4284
|
+
*/
|
|
4285
|
+
const pathsIntersection = (pathInput1, pathInput2, justCount = true) => {
|
|
4286
|
+
const path1 = pathToCurve(pathInput1);
|
|
4287
|
+
const path2 = pathToCurve(pathInput2);
|
|
4288
|
+
let x1 = 0, y1 = 0, x2 = 0, y2 = 0, x1m = 0, y1m = 0, x2m = 0, y2m = 0, bez1 = [
|
|
4289
|
+
x1,
|
|
4290
|
+
y1,
|
|
4291
|
+
x1,
|
|
4292
|
+
y1,
|
|
4293
|
+
x1m,
|
|
4294
|
+
y1m,
|
|
4295
|
+
x1m,
|
|
4296
|
+
y1m
|
|
4297
|
+
], bez2 = [
|
|
4298
|
+
x2,
|
|
4299
|
+
y2,
|
|
4300
|
+
x2,
|
|
4301
|
+
y2,
|
|
4302
|
+
x2m,
|
|
4303
|
+
y2m,
|
|
4304
|
+
x2m,
|
|
4305
|
+
y2m
|
|
4306
|
+
], countResult = 0;
|
|
4307
|
+
const pointsResult = [];
|
|
4308
|
+
const pathLen1 = path1.length;
|
|
4309
|
+
const pathLen2 = path2.length;
|
|
4310
|
+
for (let i = 0; i < pathLen1; i++) {
|
|
4311
|
+
const seg1 = path1[i];
|
|
4312
|
+
if (seg1[0] == "M") {
|
|
4313
|
+
x1 = seg1[1];
|
|
4314
|
+
y1 = seg1[2];
|
|
4315
|
+
x1m = x1;
|
|
4316
|
+
y1m = y1;
|
|
4317
|
+
} else {
|
|
4318
|
+
if (seg1[0] == "C") {
|
|
4319
|
+
bez1 = [
|
|
4320
|
+
x1,
|
|
4321
|
+
y1,
|
|
4322
|
+
seg1[1],
|
|
4323
|
+
seg1[2],
|
|
4324
|
+
seg1[3],
|
|
4325
|
+
seg1[4],
|
|
4326
|
+
seg1[5],
|
|
4327
|
+
seg1[6]
|
|
4328
|
+
];
|
|
4329
|
+
x1 = bez1[6];
|
|
4330
|
+
y1 = bez1[7];
|
|
4331
|
+
} else {
|
|
4332
|
+
bez1 = [
|
|
4333
|
+
x1,
|
|
4334
|
+
y1,
|
|
4335
|
+
x1,
|
|
4336
|
+
y1,
|
|
4337
|
+
x1m,
|
|
4338
|
+
y1m,
|
|
4339
|
+
x1m,
|
|
4340
|
+
y1m
|
|
4341
|
+
];
|
|
4342
|
+
x1 = x1m;
|
|
4343
|
+
y1 = y1m;
|
|
4344
|
+
}
|
|
4345
|
+
for (let j = 0; j < pathLen2; j++) {
|
|
4346
|
+
const seg2 = path2[j];
|
|
4347
|
+
if (seg2[0] == "M") {
|
|
4348
|
+
x2 = seg2[1];
|
|
4349
|
+
y2 = seg2[2];
|
|
4350
|
+
x2m = x2;
|
|
4351
|
+
y2m = y2;
|
|
4352
|
+
} else if (seg2[0] == "C") {
|
|
4353
|
+
bez2 = [
|
|
4354
|
+
x2,
|
|
4355
|
+
y2,
|
|
4356
|
+
seg2[1],
|
|
4357
|
+
seg2[2],
|
|
4358
|
+
seg2[3],
|
|
4359
|
+
seg2[4],
|
|
4360
|
+
seg2[5],
|
|
4361
|
+
seg2[6]
|
|
4362
|
+
];
|
|
4363
|
+
x2 = bez2[6];
|
|
4364
|
+
y2 = bez2[7];
|
|
4365
|
+
}
|
|
4366
|
+
const intr = interHelper(bez1, bez2, { justCount });
|
|
4367
|
+
if (justCount) countResult += intr;
|
|
4368
|
+
else pointsResult.push(...intr);
|
|
4369
|
+
}
|
|
4370
|
+
}
|
|
4371
|
+
}
|
|
4372
|
+
return justCount ? countResult : pointsResult;
|
|
4373
|
+
};
|
|
4374
|
+
//#endregion
|
|
4275
4375
|
export { PathParser, absolutizeSegment, arcToCubic, arcTools, bezierTools, boundingBoxIntersect, cubicTools, DISTANCE_EPSILON as distanceEpsilon, distanceSquareRoot, equalizePaths, equalizeSegments, finalizeSegment, fixPath, getClosestPoint, getDrawDirection, getPathArea, getPathBBox, getPointAtLength, getPropertiesAtLength, getPropertiesAtPoint, getSVGMatrix, getSegmentAtLength, getSegmentOfPoint, getTotalLength, invalidPathValue, isAbsoluteArray, isArcCommand, isClosedPath, isCurveArray, isDigit, isDigitStart, isMoveCommand, isMultiPath, isNormalizedArray, isPathArray, isPathCommand, isPointInStroke, isPointInsideBBox, isPolygonArray, isPolylineArray, isRelativeArray, isSpace, isValidPath, iterate, lineToCubic, lineTools, midPoint, normalizePath, normalizeSegment, optimizePath, paramsCounts, paramsParser, parsePathString, pathToAbsolute, pathToCurve, pathToRelative, pathToString, pathsIntersection, polygonTools, projection2d, quadToCubic, quadTools, relativizeSegment, reverseCurve, reversePath, rotateVector, roundPath, roundSegment, roundTo, scanFlag, scanParam, scanSegment, segmentToCubic, shapeParams, shapeToPath, shapeToPathArray, shortenSegment, skipSpaces, splitCubicSegment, splitPath, transformPath };
|
|
4276
4376
|
|
|
4277
4377
|
//# sourceMappingURL=util.js.map
|