svg-path-commander 2.2.1 → 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/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  /*!
2
- * SVGPathCommander v2.2.1 (http://thednp.github.io/svg-path-commander)
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.1";
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
- const halfTheta = theta / 2;
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
- let sweepAngle = angleBetween(startVector, {
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 arcLength(rx, ry, endAngle - startAngle);
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 length = arcLength(rx, ry, endAngle - startAngle);
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
- const alpha = startAngle + sweepAngle * (distance / length);
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 pP2 = arcPoint(cx, cy, rx, ry, alpha, endAngle - deltaAngle * 1e-5);
350
- const pP3 = arcPoint(cx, cy, rx, ry, alpha, endAngle - deltaAngle * .99999);
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,220 +718,6 @@ const arcTools = {
395
718
  getPointAtArcLength
396
719
  };
397
720
  //#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
721
  //#region src/math/cubicTools.ts
613
722
  /**
614
723
  * Returns a point at a given length of a CubicBezier segment.
@@ -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 currentLength = getBezierLength([
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 point = getPointAtCubicSegmentLength([
695
- x1,
696
- y1,
697
- c1x,
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 currentLength = getBezierLength([
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 point = getPointAtQuadSegmentLength([
824
- x1,
825
- y1,
826
- cx,
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
- return iterate(parsePathString(pathInput), absolutizeSegment);
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
- return iterate(parsePathString(pathInput), relativizeSegment);
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(error + ": expected string or PathArray");
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 relSegment = roundSegment(relativizeSegment(shortSegment, i, lastX, lastY), round);
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
  }
@@ -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(error + ": paths must have the same number of segments after equalization");
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++) {
@@ -4470,7 +4564,8 @@ var SVGPathCommander = class {
4470
4564
  */
4471
4565
  optimize() {
4472
4566
  const { segments } = this;
4473
- this.segments = optimizePath(segments, this.round === "off" ? 2 : this.round);
4567
+ const round = this.round === "off" ? 2 : this.round;
4568
+ this.segments = optimizePath(segments, round);
4474
4569
  return this;
4475
4570
  }
4476
4571
  /**