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.
- package/README.md +501 -603
- package/lib/colors.d.ts +147 -66
- package/lib/colors.js +149 -63
- package/lib/constants.js +1 -1
- package/lib/dom.d.ts +1 -1
- package/lib/dom.js +1 -1
- package/lib/extras/colors/Color.d.ts +406 -0
- package/lib/extras/colors/Color.js +546 -0
- package/lib/extras/colors/ColorPalette.d.ts +105 -0
- package/lib/extras/colors/ColorPalette.js +124 -0
- package/lib/extras/colors/ColorScale.d.ts +257 -0
- package/lib/extras/colors/ColorScale.js +347 -0
- package/lib/extras/colors/_ColorScale.d.ts +62 -0
- package/lib/extras/colors/_ColorScale.js +156 -0
- package/lib/extras/colors/index.d.ts +3 -0
- package/lib/extras/colors/index.js +3 -0
- package/lib/extras/frame-rate/FrameRate.d.ts +12 -9
- package/lib/extras/frame-rate/FrameRate.js +10 -7
- package/lib/extras/geometry/Vector.d.ts +1 -1
- package/lib/extras/geometry/Vector2.d.ts +17 -11
- package/lib/extras/geometry/Vector2.js +29 -23
- package/lib/extras/geometry/Vector3.d.ts +5 -5
- package/lib/extras/geometry/Vector3.js +10 -10
- package/lib/extras/paths/Path.d.ts +3 -3
- package/lib/extras/paths/Path.js +10 -10
- package/lib/extras/paths/PathContext.d.ts +7 -10
- package/lib/extras/paths/PathContext.js +79 -102
- package/lib/extras/paths/PathSVG.d.ts +31 -25
- package/lib/extras/paths/PathSVG.js +36 -39
- package/lib/extras/paths/index.d.ts +1 -1
- package/lib/geometry.d.ts +7 -7
- package/lib/geometry.js +13 -13
- package/lib/maths.d.ts +19 -13
- package/lib/maths.js +23 -17
- package/lib/prng.d.ts +4 -4
- package/lib/prng.js +4 -4
- package/lib/random.d.ts +4 -4
- package/lib/random.js +4 -4
- package/lib/strings.d.ts +14 -8
- package/lib/strings.js +14 -8
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +15 -8
- 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.
|
|
49
|
-
const endPoint = this.
|
|
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
|
-
|
|
65
|
-
this._setCurrentPosition(
|
|
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
|
-
|
|
81
|
+
[x, y] = this._transformPoint([x, y]);
|
|
78
82
|
if (!this._hasCurrentPosition())
|
|
79
|
-
return this._setCurrentPosition(
|
|
80
|
-
const curve = new LineCurve(this._currentPosition.x, this._currentPosition.y,
|
|
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(
|
|
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
|
-
|
|
97
|
+
points = this._transformPoints(points);
|
|
94
98
|
if (!this._hasCurrentPosition())
|
|
95
|
-
this._setCurrentPosition(...
|
|
96
|
-
const curve = new PolylineCurve([this._currentPosition.toArray()].concat(
|
|
99
|
+
this._setCurrentPosition(...points[0]);
|
|
100
|
+
const curve = new PolylineCurve([this._currentPosition.toArray()].concat(points));
|
|
97
101
|
this.add(curve);
|
|
98
|
-
this._setCurrentPosition(...
|
|
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
|
-
|
|
113
|
-
|
|
116
|
+
[cpx, cpy] = this._transformPoint([cpx, cpy]);
|
|
117
|
+
[x2, y2] = this._transformPoint([x2, y2]);
|
|
114
118
|
if (!this._hasCurrentPosition())
|
|
115
|
-
this._setCurrentPosition(
|
|
116
|
-
const curve = new QuadraticBezierCurve(this._currentPosition.x, this._currentPosition.y,
|
|
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(
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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(
|
|
139
|
-
const curve = new CubicBezierCurve(this._currentPosition.x, this._currentPosition.y,
|
|
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(
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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(
|
|
162
|
-
const curve = new CatmullRomCurve(this._currentPosition.x, this._currentPosition.y,
|
|
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(
|
|
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
|
-
|
|
179
|
+
points = this._transformPoints(points);
|
|
176
180
|
if (!this._hasCurrentPosition())
|
|
177
|
-
this._setCurrentPosition(...
|
|
178
|
-
const curve = new SplineCurve([this._currentPosition.toArray()].concat(
|
|
181
|
+
this._setCurrentPosition(...points[0]);
|
|
182
|
+
const curve = new SplineCurve([this._currentPosition.toArray()].concat(points));
|
|
179
183
|
this.add(curve);
|
|
180
|
-
this._setCurrentPosition(...
|
|
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
|
-
|
|
199
|
-
const start = EllipseCurve.interpolate(0,
|
|
200
|
-
const end = EllipseCurve.interpolate(1,
|
|
201
|
-
if (
|
|
202
|
-
this.
|
|
203
|
-
|
|
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 (
|
|
209
|
+
if (rx <= EPSILON && ry <= EPSILON)
|
|
208
210
|
return this;
|
|
209
|
-
const curve = new EllipseCurve(
|
|
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
|
-
|
|
231
|
-
const start = EllipseCurve.interpolate(0,
|
|
232
|
-
const end = EllipseCurve.interpolate(1,
|
|
233
|
-
if (
|
|
234
|
-
this.
|
|
235
|
-
|
|
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 (
|
|
239
|
+
if (radius <= EPSILON)
|
|
240
240
|
return this;
|
|
241
|
-
const curve = new ArcCurve(
|
|
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
|
|
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
|
|
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(
|
|
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
|
|
436
|
-
if (
|
|
435
|
+
_transformPoint(point) {
|
|
436
|
+
if (this._isIdentity)
|
|
437
437
|
return point;
|
|
438
|
-
const { x, y } =
|
|
438
|
+
const { x, y } = this._currentTransform.transformPoint({ x: point[0], y: point[1] });
|
|
439
439
|
return [x, y];
|
|
440
440
|
}
|
|
441
|
-
_transformPoints(points
|
|
442
|
-
if (
|
|
441
|
+
_transformPoints(points) {
|
|
442
|
+
if (this._isIdentity)
|
|
443
443
|
return points;
|
|
444
|
-
return points.map((point) => this._transformPoint(point
|
|
444
|
+
return points.map((point) => this._transformPoint(point));
|
|
445
445
|
}
|
|
446
|
-
_transformVector(vector
|
|
447
|
-
if (
|
|
446
|
+
_transformVector(vector) {
|
|
447
|
+
if (this._isIdentity)
|
|
448
448
|
return vector;
|
|
449
|
-
const [x0, y0] = this._transformPoint([0, 0]
|
|
450
|
-
const [vx, vy] = this._transformPoint(vector
|
|
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
|
|
454
|
-
if (
|
|
453
|
+
_transformEllipse(cx, cy, rx, ry, rotation) {
|
|
454
|
+
if (this._isIdentity)
|
|
455
455
|
return [cx, cy, rx, ry, rotation];
|
|
456
|
-
|
|
457
|
-
const [
|
|
458
|
-
const [
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
return [
|
|
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
|
-
|
|
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
|
-
|
|
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 {
|
|
31
|
+
* @param {PathSVGSerializationParameters} [params] Serialization parameters
|
|
23
32
|
* @returns {string}
|
|
24
33
|
*/
|
|
25
|
-
toString(params?:
|
|
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 {
|
|
39
|
-
* @
|
|
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,
|
|
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} [
|
|
77
|
-
* @returns string
|
|
83
|
+
* @param {number} [resolution] Approximation resolution
|
|
84
|
+
* @returns {string}
|
|
78
85
|
*/
|
|
79
|
-
static serializeCatmullRomCurve(curve: CatmullRomCurve,
|
|
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} [
|
|
85
|
-
* @returns string
|
|
91
|
+
* @param {number} [resolution] Approximation resolution
|
|
92
|
+
* @returns {string}
|
|
86
93
|
*/
|
|
87
|
-
static serializeSplineCurve(curve: SplineCurve,
|
|
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 {
|
|
107
|
-
* @
|
|
108
|
-
* @returns string
|
|
113
|
+
* @param {PathSVGSerializationParameters} [params] Serialization parameters
|
|
114
|
+
* @returns {string}
|
|
109
115
|
*/
|
|
110
|
-
static serializePath(path: Path<Vector2>, params?:
|
|
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 {
|
|
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 {
|
|
41
|
-
* @
|
|
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,
|
|
43
|
+
static serialize(curve, { approximate, resolution } = {}) {
|
|
46
44
|
if (curve instanceof Path) {
|
|
47
|
-
return
|
|
45
|
+
return this.serializePath(curve, { approximate, resolution });
|
|
48
46
|
}
|
|
49
47
|
if (approximate === true) {
|
|
50
|
-
const points =
|
|
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
|
|
52
|
+
return this.serializeLineCurve(curve);
|
|
55
53
|
}
|
|
56
54
|
if (curve instanceof PolylineCurve) {
|
|
57
|
-
return
|
|
55
|
+
return this.serializePolylineCurve(curve);
|
|
58
56
|
}
|
|
59
57
|
if (curve instanceof QuadraticBezierCurve) {
|
|
60
|
-
return
|
|
58
|
+
return this.serializeQuadraticBezierCurve(curve);
|
|
61
59
|
}
|
|
62
60
|
if (curve instanceof CubicBezierCurve) {
|
|
63
|
-
return
|
|
61
|
+
return this.serializeCubicBezierCurve(curve);
|
|
64
62
|
}
|
|
65
63
|
if (curve instanceof CatmullRomCurve) {
|
|
66
|
-
return
|
|
64
|
+
return this.serializeCatmullRomCurve(curve, resolution);
|
|
67
65
|
}
|
|
68
66
|
if (curve instanceof SplineCurve) {
|
|
69
|
-
return
|
|
67
|
+
return this.serializeSplineCurve(curve, resolution);
|
|
70
68
|
}
|
|
71
69
|
if (curve instanceof EllipseCurve) {
|
|
72
|
-
return
|
|
70
|
+
return this.serializeEllipseCurve(curve);
|
|
73
71
|
}
|
|
74
72
|
if (curve instanceof ArcCurve) {
|
|
75
|
-
return
|
|
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} [
|
|
124
|
-
* @returns string
|
|
121
|
+
* @param {number} [resolution] Approximation resolution
|
|
122
|
+
* @returns {string}
|
|
125
123
|
*/
|
|
126
|
-
static serializeCatmullRomCurve(curve,
|
|
127
|
-
const points =
|
|
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} [
|
|
135
|
-
* @returns string
|
|
132
|
+
* @param {number} [resolution] Approximation resolution
|
|
133
|
+
* @returns {string}
|
|
136
134
|
*/
|
|
137
|
-
static serializeSplineCurve(curve,
|
|
138
|
-
const points =
|
|
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
|
|
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 {
|
|
180
|
-
* @
|
|
181
|
-
* @returns string
|
|
177
|
+
* @param {PathSVGSerializationParameters} [params] Serialization parameters
|
|
178
|
+
* @returns {string}
|
|
182
179
|
*/
|
|
183
|
-
static serializePath(path, params
|
|
184
|
-
return path.
|
|
180
|
+
static serializePath(path, params) {
|
|
181
|
+
return path.curves
|
|
185
182
|
.map((curve, index) => {
|
|
186
183
|
let commands = ``;
|
|
187
|
-
const previousPoint = path.
|
|
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 +=
|
|
189
|
+
commands += this.serialize(curve, params);
|
|
193
190
|
return commands;
|
|
194
191
|
})
|
|
195
192
|
.join(' ');
|