toosoon-utils 4.2.3 → 4.3.1

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.
Files changed (43) hide show
  1. package/README.md +501 -603
  2. package/lib/colors.d.ts +147 -66
  3. package/lib/colors.js +149 -63
  4. package/lib/constants.js +1 -1
  5. package/lib/dom.d.ts +1 -1
  6. package/lib/dom.js +1 -1
  7. package/lib/extras/colors/Color.d.ts +406 -0
  8. package/lib/extras/colors/Color.js +546 -0
  9. package/lib/extras/colors/ColorPalette.d.ts +105 -0
  10. package/lib/extras/colors/ColorPalette.js +124 -0
  11. package/lib/extras/colors/ColorScale.d.ts +257 -0
  12. package/lib/extras/colors/ColorScale.js +347 -0
  13. package/lib/extras/colors/_ColorScale.d.ts +62 -0
  14. package/lib/extras/colors/_ColorScale.js +156 -0
  15. package/lib/extras/colors/index.d.ts +3 -0
  16. package/lib/extras/colors/index.js +3 -0
  17. package/lib/extras/frame-rate/FrameRate.d.ts +12 -9
  18. package/lib/extras/frame-rate/FrameRate.js +10 -7
  19. package/lib/extras/geometry/Vector.d.ts +1 -1
  20. package/lib/extras/geometry/Vector2.d.ts +17 -11
  21. package/lib/extras/geometry/Vector2.js +29 -23
  22. package/lib/extras/geometry/Vector3.d.ts +5 -5
  23. package/lib/extras/geometry/Vector3.js +10 -10
  24. package/lib/extras/paths/Path.d.ts +3 -3
  25. package/lib/extras/paths/Path.js +10 -10
  26. package/lib/extras/paths/PathContext.d.ts +7 -10
  27. package/lib/extras/paths/PathContext.js +79 -102
  28. package/lib/extras/paths/PathSVG.d.ts +31 -25
  29. package/lib/extras/paths/PathSVG.js +36 -39
  30. package/lib/extras/paths/index.d.ts +1 -1
  31. package/lib/geometry.d.ts +7 -7
  32. package/lib/geometry.js +13 -13
  33. package/lib/maths.d.ts +19 -13
  34. package/lib/maths.js +23 -17
  35. package/lib/prng.d.ts +4 -4
  36. package/lib/prng.js +4 -4
  37. package/lib/random.d.ts +4 -4
  38. package/lib/random.js +4 -4
  39. package/lib/strings.d.ts +14 -8
  40. package/lib/strings.js +14 -8
  41. package/lib/tsconfig.tsbuildinfo +1 -1
  42. package/lib/types.d.ts +15 -8
  43. package/package.json +14 -14
@@ -14,8 +14,12 @@ import Path from './Path';
14
14
  export default class PathContext extends Path {
15
15
  _currentPosition = new Vector2(NaN, NaN);
16
16
  _currentTransform = new DOMMatrix();
17
- _positionTransform = new DOMMatrix();
18
17
  _transformStack = [];
18
+ autoClose;
19
+ constructor() {
20
+ super({ autoClose: false });
21
+ this.autoClose = false;
22
+ }
19
23
  /**
20
24
  * Create a path from a given list of points
21
25
  *
@@ -45,8 +49,8 @@ export default class PathContext extends Path {
45
49
  * @return {this}
46
50
  */
47
51
  closePath() {
48
- const startPoint = this.subpaths[0]?.getPoint(0);
49
- const endPoint = this.subpaths[this.subpaths.length - 1]?.getPoint(1);
52
+ const startPoint = this.curves[0]?.getPoint(0);
53
+ const endPoint = this.curves[this.curves.length - 1]?.getPoint(1);
50
54
  if (!startPoint.equals(endPoint)) {
51
55
  const curve = new LineCurve(endPoint.x, endPoint.y, startPoint.x, startPoint.y);
52
56
  this.add(curve);
@@ -61,8 +65,8 @@ export default class PathContext extends Path {
61
65
  * @return {this}
62
66
  */
63
67
  moveTo(x, y) {
64
- const [tX, tY] = this._transformPoint([x, y]);
65
- this._setCurrentPosition(tX, tY);
68
+ [x, y] = this._transformPoint([x, y]);
69
+ this._setCurrentPosition(x, y);
66
70
  return this;
67
71
  }
68
72
  /**
@@ -74,12 +78,12 @@ export default class PathContext extends Path {
74
78
  * @return {this}
75
79
  */
76
80
  lineTo(x, y) {
77
- const [tX, tY] = this._transformPoint([x, y]);
81
+ [x, y] = this._transformPoint([x, y]);
78
82
  if (!this._hasCurrentPosition())
79
- return this._setCurrentPosition(tX, tY);
80
- const curve = new LineCurve(this._currentPosition.x, this._currentPosition.y, tX, tY);
83
+ return this._setCurrentPosition(x, y);
84
+ const curve = new LineCurve(this._currentPosition.x, this._currentPosition.y, x, y);
81
85
  this.add(curve);
82
- this._setCurrentPosition(tX, tY);
86
+ this._setCurrentPosition(x, y);
83
87
  return this;
84
88
  }
85
89
  /**
@@ -90,12 +94,12 @@ export default class PathContext extends Path {
90
94
  * @returns {this}
91
95
  */
92
96
  polylineTo(points) {
93
- const tPoints = this._transformPoints(points);
97
+ points = this._transformPoints(points);
94
98
  if (!this._hasCurrentPosition())
95
- this._setCurrentPosition(...tPoints[0]);
96
- const curve = new PolylineCurve([this._currentPosition.toArray()].concat(tPoints));
99
+ this._setCurrentPosition(...points[0]);
100
+ const curve = new PolylineCurve([this._currentPosition.toArray()].concat(points));
97
101
  this.add(curve);
98
- this._setCurrentPosition(...tPoints[tPoints.length - 1]);
102
+ this._setCurrentPosition(...points[points.length - 1]);
99
103
  return this;
100
104
  }
101
105
  /**
@@ -109,13 +113,13 @@ export default class PathContext extends Path {
109
113
  * @return {this}
110
114
  */
111
115
  quadraticCurveTo(cpx, cpy, x2, y2) {
112
- const [tCpx, tCpy] = this._transformPoint([cpx, cpy]);
113
- const [tX2, tY2] = this._transformPoint([x2, y2]);
116
+ [cpx, cpy] = this._transformPoint([cpx, cpy]);
117
+ [x2, y2] = this._transformPoint([x2, y2]);
114
118
  if (!this._hasCurrentPosition())
115
- this._setCurrentPosition(tCpx, tCpy);
116
- const curve = new QuadraticBezierCurve(this._currentPosition.x, this._currentPosition.y, tCpx, tCpy, tX2, tY2);
119
+ this._setCurrentPosition(cpx, cpy);
120
+ const curve = new QuadraticBezierCurve(this._currentPosition.x, this._currentPosition.y, cpx, cpy, x2, y2);
117
121
  this.add(curve);
118
- this._setCurrentPosition(tX2, tY2);
122
+ this._setCurrentPosition(x2, y2);
119
123
  return this;
120
124
  }
121
125
  /**
@@ -131,14 +135,14 @@ export default class PathContext extends Path {
131
135
  * @return {this}
132
136
  */
133
137
  bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x2, y2) {
134
- const [tCp1x, tCp1y] = this._transformPoint([cp1x, cp1y]);
135
- const [tCp2x, tCp2y] = this._transformPoint([cp2x, cp2y]);
136
- const [tX2, tY2] = this._transformPoint([x2, y2]);
138
+ [cp1x, cp1y] = this._transformPoint([cp1x, cp1y]);
139
+ [cp2x, cp2y] = this._transformPoint([cp2x, cp2y]);
140
+ [x2, y2] = this._transformPoint([x2, y2]);
137
141
  if (!this._hasCurrentPosition())
138
- this._setCurrentPosition(tCp1x, tCp1y);
139
- const curve = new CubicBezierCurve(this._currentPosition.x, this._currentPosition.y, tCp1x, tCp1y, tCp2x, tCp2y, tX2, tY2);
142
+ this._setCurrentPosition(cp1x, cp1y);
143
+ const curve = new CubicBezierCurve(this._currentPosition.x, this._currentPosition.y, cp1x, cp1y, cp2x, cp2y, x2, y2);
140
144
  this.add(curve);
141
- this._setCurrentPosition(tX2, tY2);
145
+ this._setCurrentPosition(x2, y2);
142
146
  return this;
143
147
  }
144
148
  /**
@@ -154,14 +158,14 @@ export default class PathContext extends Path {
154
158
  * @return {this}
155
159
  */
156
160
  catmullRomCurveTo(cp1x, cp1y, cp2x, cp2y, x2, y2) {
157
- const [tCp1x, tCp1y] = this._transformPoint([cp1x, cp1y]);
158
- const [tCp2x, tCp2y] = this._transformPoint([cp2x, cp2y]);
159
- const [tX2, tY2] = this._transformPoint([x2, y2]);
161
+ [cp1x, cp1y] = this._transformPoint([cp1x, cp1y]);
162
+ [cp2x, cp2y] = this._transformPoint([cp2x, cp2y]);
163
+ [x2, y2] = this._transformPoint([x2, y2]);
160
164
  if (!this._hasCurrentPosition())
161
- this._setCurrentPosition(tCp1x, tCp1y);
162
- const curve = new CatmullRomCurve(this._currentPosition.x, this._currentPosition.y, tCp1x, tCp1y, tCp2x, tCp2y, tX2, tY2);
165
+ this._setCurrentPosition(cp1x, cp1y);
166
+ const curve = new CatmullRomCurve(this._currentPosition.x, this._currentPosition.y, cp1x, cp1y, cp2x, cp2y, x2, y2);
163
167
  this.add(curve);
164
- this._setCurrentPosition(tX2, tY2);
168
+ this._setCurrentPosition(x2, y2);
165
169
  return this;
166
170
  }
167
171
  /**
@@ -172,12 +176,12 @@ export default class PathContext extends Path {
172
176
  * @return {this}
173
177
  */
174
178
  splineTo(points) {
175
- const tPoints = this._transformPoints(points);
179
+ points = this._transformPoints(points);
176
180
  if (!this._hasCurrentPosition())
177
- this._setCurrentPosition(...tPoints[0]);
178
- const curve = new SplineCurve([this._currentPosition.toArray()].concat(tPoints));
181
+ this._setCurrentPosition(...points[0]);
182
+ const curve = new SplineCurve([this._currentPosition.toArray()].concat(points));
179
183
  this.add(curve);
180
- this._setCurrentPosition(...tPoints[tPoints.length - 1]);
184
+ this._setCurrentPosition(...points[points.length - 1]);
181
185
  return this;
182
186
  }
183
187
  /**
@@ -195,18 +199,16 @@ export default class PathContext extends Path {
195
199
  * @return {this}
196
200
  */
197
201
  ellipse(cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise) {
198
- const [tCx, tCy, tRx, tRy, tRotation] = this._transformEllipse(cx, cy, rx, ry, rotation);
199
- const start = EllipseCurve.interpolate(0, tCx, tCy, tRx, tRy, tRotation, startAngle, endAngle, counterclockwise);
200
- const end = EllipseCurve.interpolate(1, tCx, tCy, tRx, tRy, tRotation, startAngle, endAngle, counterclockwise);
201
- if (!this._hasCurrentPosition())
202
- this._setCurrentPosition(...start);
203
- else if (!this._currentPosition.equals(start)) {
204
- const curve = new LineCurve(this._currentPosition.x, this._currentPosition.y, ...start);
205
- this.add(curve);
202
+ [cx, cy, rx, ry, rotation] = this._transformEllipse(cx, cy, rx, ry, rotation);
203
+ const start = EllipseCurve.interpolate(0, cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise);
204
+ const end = EllipseCurve.interpolate(1, cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise);
205
+ if (this._hasCurrentPosition() && !this._currentPosition.equals(start)) {
206
+ const line = new LineCurve(this._currentPosition.x, this._currentPosition.y, ...start);
207
+ this.add(line);
206
208
  }
207
- if (tRx <= EPSILON && tRy <= EPSILON)
209
+ if (rx <= EPSILON && ry <= EPSILON)
208
210
  return this;
209
- const curve = new EllipseCurve(tCx, tCy, tRx, tRy, tRotation, startAngle, endAngle, counterclockwise);
211
+ const curve = new EllipseCurve(cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise);
210
212
  this.add(curve);
211
213
  this._setCurrentPosition(...end);
212
214
  return this;
@@ -227,25 +229,23 @@ export default class PathContext extends Path {
227
229
  if (!this._isUniform || this._isRotated) {
228
230
  return this.ellipse(cx, cy, radius, radius, 0, startAngle, endAngle, counterclockwise);
229
231
  }
230
- const [tCx, tCy, tRadius] = this._transformEllipse(cx, cy, radius, radius, 0);
231
- const start = EllipseCurve.interpolate(0, tCx, tCy, tRadius, tRadius, 0, startAngle, endAngle, counterclockwise);
232
- const end = EllipseCurve.interpolate(1, tCx, tCy, tRadius, tRadius, 0, startAngle, endAngle, counterclockwise);
233
- if (!this._hasCurrentPosition())
234
- this._setCurrentPosition(...start);
235
- else if (!this._currentPosition.equals(start)) {
236
- const curve = new LineCurve(this._currentPosition.x, this._currentPosition.y, ...start);
237
- this.add(curve);
232
+ [cx, cy, radius] = this._transformEllipse(cx, cy, radius, radius, 0);
233
+ const start = EllipseCurve.interpolate(0, cx, cy, radius, radius, 0, startAngle, endAngle, counterclockwise);
234
+ const end = EllipseCurve.interpolate(1, cx, cy, radius, radius, 0, startAngle, endAngle, counterclockwise);
235
+ if (this._hasCurrentPosition() && !this._currentPosition.equals(start)) {
236
+ const line = new LineCurve(this._currentPosition.x, this._currentPosition.y, ...start);
237
+ this.add(line);
238
238
  }
239
- if (tRadius <= EPSILON)
239
+ if (radius <= EPSILON)
240
240
  return this;
241
- const curve = new ArcCurve(tCx, tCy, tRadius, startAngle, endAngle, counterclockwise);
241
+ const curve = new ArcCurve(cx, cy, radius, startAngle, endAngle, counterclockwise);
242
242
  this.add(curve);
243
243
  this._setCurrentPosition(...end);
244
244
  return this;
245
245
  }
246
246
  /**
247
247
  * Draw an Arc curve from the current position, tangential to the 2 segments created by both control points
248
- * Add an instance of {@link ArcCurve} to this path
248
+ * Add an instance of {@link EllipseCurve} to this path
249
249
  *
250
250
  * @param {number} x1 X-axis coordinate of the first control point
251
251
  * @param {number} y1 Y-axis coordinate of the first control point
@@ -285,8 +285,7 @@ export default class PathContext extends Path {
285
285
  const c = t1.clone().add(n1.clone().multiplyScalar(normalLength));
286
286
  const startAngle = Math.atan2(t1.y - c.y, t1.x - c.x);
287
287
  const endAngle = Math.atan2(t2.y - c.y, t2.x - c.x);
288
- const deltaAngle = endAngle - startAngle;
289
- const counterclockwise = deltaAngle < 0;
288
+ const counterclockwise = (p0.y - p1.y) * (p2.x - p0.x) <= (p0.x - p1.x) * (p2.y - p0.y);
290
289
  t1.applyMatrix(this._currentTransform);
291
290
  t2.applyMatrix(this._currentTransform);
292
291
  c.applyMatrix(this._currentTransform);
@@ -343,9 +342,8 @@ export default class PathContext extends Path {
343
342
  topRightRadius = Math.min(topRightRadius, maxRadius);
344
343
  bottomRightRadius = Math.min(bottomRightRadius, maxRadius);
345
344
  bottomLeftRadius = Math.min(bottomLeftRadius, maxRadius);
346
- const curve = new PathContext({ autoClose: true });
345
+ const curve = new PathContext();
347
346
  curve.setTransform(this.getTransform());
348
- this.add(curve);
349
347
  // Top-Right corner
350
348
  if (topRightRadius > 0) {
351
349
  curve.lineTo(x + width - topRightRadius, y);
@@ -378,6 +376,9 @@ export default class PathContext extends Path {
378
376
  else {
379
377
  curve.lineTo(x, y);
380
378
  }
379
+ curve.closePath();
380
+ this.add(curve);
381
+ this.moveTo(x, y);
381
382
  return this;
382
383
  }
383
384
  setTransform(a, b, c, d, e, f) {
@@ -426,43 +427,39 @@ export default class PathContext extends Path {
426
427
  }
427
428
  _setCurrentPosition(x, y) {
428
429
  this._currentPosition.set(x, y);
429
- this._positionTransform = this.getTransform();
430
430
  return this;
431
431
  }
432
432
  // ****************************
433
433
  // Matrix transformations
434
434
  // ****************************
435
- _transformPoint(point, matrix = this._currentTransform) {
436
- if (matrix.isIdentity)
435
+ _transformPoint(point) {
436
+ if (this._isIdentity)
437
437
  return point;
438
- const { x, y } = matrix.transformPoint({ x: point[0], y: point[1] });
438
+ const { x, y } = this._currentTransform.transformPoint({ x: point[0], y: point[1] });
439
439
  return [x, y];
440
440
  }
441
- _transformPoints(points, matrix = this._currentTransform) {
442
- if (matrix.isIdentity)
441
+ _transformPoints(points) {
442
+ if (this._isIdentity)
443
443
  return points;
444
- return points.map((point) => this._transformPoint(point, matrix));
444
+ return points.map((point) => this._transformPoint(point));
445
445
  }
446
- _transformVector(vector, matrix = this._currentTransform) {
447
- if (matrix.isIdentity)
446
+ _transformVector(vector) {
447
+ if (this._isIdentity)
448
448
  return vector;
449
- const [x0, y0] = this._transformPoint([0, 0], matrix);
450
- const [vx, vy] = this._transformPoint(vector, matrix);
449
+ const [x0, y0] = this._transformPoint([0, 0]);
450
+ const [vx, vy] = this._transformPoint(vector);
451
451
  return [vx - x0, vy - y0];
452
452
  }
453
- _transformEllipse(cx, cy, rx, ry, rotation, matrix = this._currentTransform) {
454
- if (matrix.isIdentity)
453
+ _transformEllipse(cx, cy, rx, ry, rotation) {
454
+ if (this._isIdentity)
455
455
  return [cx, cy, rx, ry, rotation];
456
- const [tCx, tCy] = this._transformPoint([cx, cy], matrix);
457
- const [ux1, uy1] = this._transformVector([Math.cos(rotation) * rx, Math.sin(rotation) * rx], matrix);
458
- const [ux2, uy2] = this._transformVector([-Math.sin(rotation) * ry, Math.cos(rotation) * ry], matrix);
459
- const tRx = Math.hypot(ux1, uy1);
460
- const tRy = Math.hypot(ux2, uy2);
461
- const tRotation = Math.atan2(uy1, ux1);
462
- return [tCx, tCy, tRx, tRy, tRotation];
463
- }
464
- _inversePoint(point, matrix = this._currentTransform) {
465
- return this._transformPoint(point, matrix.inverse());
456
+ [cx, cy] = this._transformPoint([cx, cy]);
457
+ const [u1x, u1y] = this._transformVector([Math.cos(rotation) * rx, Math.sin(rotation) * rx]);
458
+ const [u2x, u2y] = this._transformVector([-Math.sin(rotation) * ry, Math.cos(rotation) * ry]);
459
+ rx = Math.hypot(u1x, u1y);
460
+ ry = Math.hypot(u2x, u2y);
461
+ rotation = Math.atan2(u1y, u1x);
462
+ return [cx, cy, rx, ry, rotation];
466
463
  }
467
464
  get _translateX() {
468
465
  return this._currentTransform.e;
@@ -482,20 +479,6 @@ export default class PathContext extends Path {
482
479
  const { a, b } = this._currentTransform;
483
480
  return Math.atan2(b, a);
484
481
  }
485
- get _skewX() {
486
- const { c, d } = this._currentTransform;
487
- const cos = Math.cos(this._rotation);
488
- const sin = Math.sin(this._rotation);
489
- const m11 = c * cos + d * sin;
490
- return Math.atan(m11 / this._scaleX);
491
- }
492
- get _skewY() {
493
- const { a, b } = this._currentTransform;
494
- const cos = Math.cos(this._rotation);
495
- const sin = Math.sin(this._rotation);
496
- const m21 = a * sin - b * cos;
497
- return Math.atan(m21 / this._scaleY);
498
- }
499
482
  get _isTranslated() {
500
483
  return Math.abs(this._translateX) > EPSILON || Math.abs(this._translateY) > EPSILON;
501
484
  }
@@ -506,12 +489,6 @@ export default class PathContext extends Path {
506
489
  const { b, c } = this._currentTransform;
507
490
  return Math.abs(b) > EPSILON || Math.abs(c) > EPSILON;
508
491
  }
509
- get _isSkewed() {
510
- const { a, b, c, d } = this._currentTransform;
511
- const angleX = Math.atan2(b, a);
512
- const angleY = Math.atan2(-c, d);
513
- return Math.abs(angleX - angleY) > EPSILON;
514
- }
515
492
  get _isUniform() {
516
493
  return Math.abs(this._scaleX - this._scaleY) <= EPSILON;
517
494
  }
@@ -2,9 +2,18 @@ import { Curve, LineCurve, PolylineCurve, QuadraticBezierCurve, CubicBezierCurve
2
2
  import { type Vector2 } from '../geometry';
3
3
  import PathContext from './PathContext';
4
4
  import Path from './Path';
5
- export type PathSVGSerializationParams = {
5
+ /**
6
+ * Parameters used for SVG serialization of a path
7
+ */
8
+ export type PathSVGSerializationParameters = {
9
+ /**
10
+ * Flag indicating if given curve should be approximated into straight lines
11
+ */
6
12
  approximate?: boolean;
7
- curveResolution?: number;
13
+ /**
14
+ * Resolution used for approximations
15
+ */
16
+ resolution?: number;
8
17
  };
9
18
  /**
10
19
  * Utility class for manipulating connected curves and generating SVG path
@@ -19,10 +28,10 @@ export default class PathSVG extends PathContext {
19
28
  /**
20
29
  * Serialize this path into a SVG path string
21
30
  *
22
- * @param {object} [params]
31
+ * @param {PathSVGSerializationParameters} [params] Serialization parameters
23
32
  * @returns {string}
24
33
  */
25
- toString(params?: PathSVGSerializationParams): string;
34
+ toString(params?: PathSVGSerializationParameters): string;
26
35
  /**
27
36
  * Convert a {@link Curve} into spaced points
28
37
  *
@@ -35,77 +44,74 @@ export default class PathSVG extends PathContext {
35
44
  * Serialize a {@link Curve}
36
45
  *
37
46
  * @param {Curve} curve Curve to serialize
38
- * @param {object} [params] Serialization parameters
39
- * @param {boolean} [params.approximate] Flag indicating if given curve should be approximated into straight lines
40
- * @param {number} [params.curveResolution] Resolution used for curve approximations
41
- * @returns string
47
+ * @param {PathSVGSerializationParameters} [params] Serialization parameters
48
+ * @returns {string}
42
49
  */
43
- static serialize(curve: Curve<Vector2>, { approximate, curveResolution }?: PathSVGSerializationParams): string;
50
+ static serialize(curve: Curve<Vector2>, { approximate, resolution }?: PathSVGSerializationParameters): string;
44
51
  /**
45
52
  * Serialize a {@link LineCurve}
46
53
  *
47
54
  * @param {LineCurve} curve LineCurve to serialize
48
- * @returns string
55
+ * @returns {string}
49
56
  */
50
57
  static serializeLineCurve(curve: LineCurve): string;
51
58
  /**
52
59
  * Serialize a {@link PolylineCurve}
53
60
  *
54
61
  * @param {PolylineCurve} curve PolylineCurve to serialize
55
- * @returns string
62
+ * @returns {string}
56
63
  */
57
64
  static serializePolylineCurve(curve: PolylineCurve): string;
58
65
  /**
59
66
  * Serialize a {@link QuadraticBezierCurve}
60
67
  *
61
68
  * @param {QuadraticBezierCurve} curve QuadraticBezierCurve to serialize
62
- * @returns string
69
+ * @returns {string}
63
70
  */
64
71
  static serializeQuadraticBezierCurve(curve: QuadraticBezierCurve): string;
65
72
  /**
66
73
  * Serialize a {@link CubicBezierCurve}
67
74
  *
68
75
  * @param {CubicBezierCurve} curve CubicBezierCurve to serialize
69
- * @returns string
76
+ * @returns {string}
70
77
  */
71
78
  static serializeCubicBezierCurve(curve: CubicBezierCurve): string;
72
79
  /**
73
80
  * Serialize a {@link CatmullRomCurve} by approximating it into straight lines
74
81
  *
75
82
  * @param {CatmullRomCurve} curve CatmullRomCurve to serialize
76
- * @param {number} [curveResolution] Approximation resolution
77
- * @returns string
83
+ * @param {number} [resolution] Approximation resolution
84
+ * @returns {string}
78
85
  */
79
- static serializeCatmullRomCurve(curve: CatmullRomCurve, curveResolution?: number): string;
86
+ static serializeCatmullRomCurve(curve: CatmullRomCurve, resolution?: number): string;
80
87
  /**
81
88
  * Serialize a {@link SplineCurve} by approximating it into straight lines
82
89
  *
83
90
  * @param {SplineCurve} curve SplineCurve to serialize
84
- * @param {number} [curveResolution] Approximation resolution
85
- * @returns string
91
+ * @param {number} [resolution] Approximation resolution
92
+ * @returns {string}
86
93
  */
87
- static serializeSplineCurve(curve: SplineCurve, curveResolution?: number): string;
94
+ static serializeSplineCurve(curve: SplineCurve, resolution?: number): string;
88
95
  /**
89
96
  * Serialize an {@link EllipseCurve}
90
97
  *
91
98
  * @param {EllipseCurve} curve EllipseCurve to serialize
92
- * @returns string
99
+ * @returns {string}
93
100
  */
94
101
  static serializeEllipseCurve(curve: EllipseCurve): string;
95
102
  /**
96
103
  * Serialize an {@link ArcCurve}
97
104
  *
98
105
  * @param {ArcCurve} curve ArcCurve to serialize
99
- * @returns string
106
+ * @returns {string}
100
107
  */
101
108
  static serializeArcCurve(curve: ArcCurve): string;
102
109
  /**
103
110
  * Serialize an {@link Path}
104
111
  *
105
112
  * @param {Path} path Path to serialize
106
- * @param {object} [params]
107
- * @param {object} [params.curveResolution] Resolution used for curves approximations
108
- * @returns string
113
+ * @param {PathSVGSerializationParameters} [params] Serialization parameters
114
+ * @returns {string}
109
115
  */
110
- static serializePath(path: Path<Vector2>, params?: PathSVGSerializationParams): string;
116
+ static serializePath(path: Path<Vector2>, params?: PathSVGSerializationParameters): string;
111
117
  }
@@ -16,10 +16,10 @@ export default class PathSVG extends PathContext {
16
16
  /**
17
17
  * Serialize this path into a SVG path string
18
18
  *
19
- * @param {object} [params]
19
+ * @param {PathSVGSerializationParameters} [params] Serialization parameters
20
20
  * @returns {string}
21
21
  */
22
- toString(params = {}) {
22
+ toString(params) {
23
23
  return PathSVG.serialize(this, params);
24
24
  }
25
25
  /**
@@ -37,42 +37,40 @@ export default class PathSVG extends PathContext {
37
37
  * Serialize a {@link Curve}
38
38
  *
39
39
  * @param {Curve} curve Curve to serialize
40
- * @param {object} [params] Serialization parameters
41
- * @param {boolean} [params.approximate] Flag indicating if given curve should be approximated into straight lines
42
- * @param {number} [params.curveResolution] Resolution used for curve approximations
43
- * @returns string
40
+ * @param {PathSVGSerializationParameters} [params] Serialization parameters
41
+ * @returns {string}
44
42
  */
45
- static serialize(curve, { approximate, curveResolution } = {}) {
43
+ static serialize(curve, { approximate, resolution } = {}) {
46
44
  if (curve instanceof Path) {
47
- return PathSVG.serializePath(curve, { approximate, curveResolution });
45
+ return this.serializePath(curve, { approximate, resolution });
48
46
  }
49
47
  if (approximate === true) {
50
- const points = PathSVG.approximate(curve, curveResolution);
48
+ const points = this.approximate(curve, resolution);
51
49
  return points.map(([x, y]) => `L${x},${y}`).join(' ');
52
50
  }
53
51
  if (curve instanceof LineCurve) {
54
- return PathSVG.serializeLineCurve(curve);
52
+ return this.serializeLineCurve(curve);
55
53
  }
56
54
  if (curve instanceof PolylineCurve) {
57
- return PathSVG.serializePolylineCurve(curve);
55
+ return this.serializePolylineCurve(curve);
58
56
  }
59
57
  if (curve instanceof QuadraticBezierCurve) {
60
- return PathSVG.serializeQuadraticBezierCurve(curve);
58
+ return this.serializeQuadraticBezierCurve(curve);
61
59
  }
62
60
  if (curve instanceof CubicBezierCurve) {
63
- return PathSVG.serializeCubicBezierCurve(curve);
61
+ return this.serializeCubicBezierCurve(curve);
64
62
  }
65
63
  if (curve instanceof CatmullRomCurve) {
66
- return PathSVG.serializeCatmullRomCurve(curve, curveResolution);
64
+ return this.serializeCatmullRomCurve(curve, resolution);
67
65
  }
68
66
  if (curve instanceof SplineCurve) {
69
- return PathSVG.serializeSplineCurve(curve, curveResolution);
67
+ return this.serializeSplineCurve(curve, resolution);
70
68
  }
71
69
  if (curve instanceof EllipseCurve) {
72
- return PathSVG.serializeEllipseCurve(curve);
70
+ return this.serializeEllipseCurve(curve);
73
71
  }
74
72
  if (curve instanceof ArcCurve) {
75
- return PathSVG.serializeArcCurve(curve);
73
+ return this.serializeArcCurve(curve);
76
74
  }
77
75
  return '';
78
76
  }
@@ -80,7 +78,7 @@ export default class PathSVG extends PathContext {
80
78
  * Serialize a {@link LineCurve}
81
79
  *
82
80
  * @param {LineCurve} curve LineCurve to serialize
83
- * @returns string
81
+ * @returns {string}
84
82
  */
85
83
  static serializeLineCurve(curve) {
86
84
  const { x2, y2 } = curve;
@@ -90,7 +88,7 @@ export default class PathSVG extends PathContext {
90
88
  * Serialize a {@link PolylineCurve}
91
89
  *
92
90
  * @param {PolylineCurve} curve PolylineCurve to serialize
93
- * @returns string
91
+ * @returns {string}
94
92
  */
95
93
  static serializePolylineCurve(curve) {
96
94
  const { points } = curve;
@@ -100,7 +98,7 @@ export default class PathSVG extends PathContext {
100
98
  * Serialize a {@link QuadraticBezierCurve}
101
99
  *
102
100
  * @param {QuadraticBezierCurve} curve QuadraticBezierCurve to serialize
103
- * @returns string
101
+ * @returns {string}
104
102
  */
105
103
  static serializeQuadraticBezierCurve(curve) {
106
104
  const { cpx, cpy, x2, y2 } = curve;
@@ -110,7 +108,7 @@ export default class PathSVG extends PathContext {
110
108
  * Serialize a {@link CubicBezierCurve}
111
109
  *
112
110
  * @param {CubicBezierCurve} curve CubicBezierCurve to serialize
113
- * @returns string
111
+ * @returns {string}
114
112
  */
115
113
  static serializeCubicBezierCurve(curve) {
116
114
  const { cp1x, cp1y, cp2x, cp2y, x2, y2 } = curve;
@@ -120,29 +118,29 @@ export default class PathSVG extends PathContext {
120
118
  * Serialize a {@link CatmullRomCurve} by approximating it into straight lines
121
119
  *
122
120
  * @param {CatmullRomCurve} curve CatmullRomCurve to serialize
123
- * @param {number} [curveResolution] Approximation resolution
124
- * @returns string
121
+ * @param {number} [resolution] Approximation resolution
122
+ * @returns {string}
125
123
  */
126
- static serializeCatmullRomCurve(curve, curveResolution) {
127
- const points = PathSVG.approximate(curve, curveResolution);
124
+ static serializeCatmullRomCurve(curve, resolution) {
125
+ const points = this.approximate(curve, resolution);
128
126
  return points.map(([x, y]) => `L${x},${y}`).join(' ');
129
127
  }
130
128
  /**
131
129
  * Serialize a {@link SplineCurve} by approximating it into straight lines
132
130
  *
133
131
  * @param {SplineCurve} curve SplineCurve to serialize
134
- * @param {number} [curveResolution] Approximation resolution
135
- * @returns string
132
+ * @param {number} [resolution] Approximation resolution
133
+ * @returns {string}
136
134
  */
137
- static serializeSplineCurve(curve, curveResolution) {
138
- const points = PathSVG.approximate(curve, curveResolution);
135
+ static serializeSplineCurve(curve, resolution) {
136
+ const points = this.approximate(curve, resolution);
139
137
  return points.map(([x, y]) => `L${x},${y}`).join(' ');
140
138
  }
141
139
  /**
142
140
  * Serialize an {@link EllipseCurve}
143
141
  *
144
142
  * @param {EllipseCurve} curve EllipseCurve to serialize
145
- * @returns string
143
+ * @returns {string}
146
144
  */
147
145
  static serializeEllipseCurve(curve) {
148
146
  const { cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise } = curve;
@@ -167,29 +165,28 @@ export default class PathSVG extends PathContext {
167
165
  * Serialize an {@link ArcCurve}
168
166
  *
169
167
  * @param {ArcCurve} curve ArcCurve to serialize
170
- * @returns string
168
+ * @returns {string}
171
169
  */
172
170
  static serializeArcCurve(curve) {
173
- return PathSVG.serializeEllipseCurve(curve);
171
+ return this.serializeEllipseCurve(curve);
174
172
  }
175
173
  /**
176
174
  * Serialize an {@link Path}
177
175
  *
178
176
  * @param {Path} path Path to serialize
179
- * @param {object} [params]
180
- * @param {object} [params.curveResolution] Resolution used for curves approximations
181
- * @returns string
177
+ * @param {PathSVGSerializationParameters} [params] Serialization parameters
178
+ * @returns {string}
182
179
  */
183
- static serializePath(path, params = {}) {
184
- return path.subpaths
180
+ static serializePath(path, params) {
181
+ return path.curves
185
182
  .map((curve, index) => {
186
183
  let commands = ``;
187
- const previousPoint = path.subpaths[index - 1]?.getPoint(1);
184
+ const previousPoint = path.curves[index - 1]?.getPoint(1);
188
185
  const newPoint = curve.getPoint(0);
189
186
  if (index === 0 || !previousPoint?.equals(newPoint)) {
190
187
  commands += `M${newPoint.x},${newPoint.y}`;
191
188
  }
192
- commands += PathSVG.serialize(curve, params);
189
+ commands += this.serialize(curve, params);
193
190
  return commands;
194
191
  })
195
192
  .join(' ');
@@ -1,3 +1,3 @@
1
1
  export { default as Path } from './Path';
2
2
  export { default as PathContext } from './PathContext';
3
- export { default as PathSVG, type PathSVGSerializationParams } from './PathSVG';
3
+ export { default as PathSVG, type PathSVGSerializationParameters } from './PathSVG';