toosoon-utils 4.2.2 → 4.3.0

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 (42) hide show
  1. package/README.md +499 -575
  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 +1 -1
  18. package/lib/extras/frame-rate/FrameRate.js +2 -2
  19. package/lib/extras/geometry/Vector.d.ts +1 -1
  20. package/lib/extras/geometry/Vector2.d.ts +24 -11
  21. package/lib/extras/geometry/Vector2.js +41 -23
  22. package/lib/extras/geometry/Vector3.d.ts +12 -5
  23. package/lib/extras/geometry/Vector3.js +23 -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 -17
  27. package/lib/extras/paths/PathContext.js +122 -144
  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.js +1 -1
  32. package/lib/maths.d.ts +19 -13
  33. package/lib/maths.js +23 -17
  34. package/lib/prng.d.ts +4 -4
  35. package/lib/prng.js +4 -4
  36. package/lib/random.d.ts +4 -4
  37. package/lib/random.js +4 -4
  38. package/lib/strings.d.ts +14 -8
  39. package/lib/strings.js +14 -8
  40. package/lib/tsconfig.tsbuildinfo +1 -1
  41. package/lib/types.d.ts +15 -8
  42. package/package.json +14 -14
@@ -19,6 +19,7 @@ export default class Vector2 implements Vector<Vec2> {
19
19
  * Y-axis value of this vector
20
20
  */
21
21
  y: number;
22
+ [Symbol.iterator](): Iterator<number>;
22
23
  /**
23
24
  * @param {number} [x=0] X-axis value
24
25
  * @param {number} [y=0] Y-axis value
@@ -35,7 +36,7 @@ export default class Vector2 implements Vector<Vec2> {
35
36
  /**
36
37
  * Set a given scalar value to all values of this vector
37
38
  *
38
- * @param {number} scalar Value to set for all vector values
39
+ * @param {number} scalar Value to set for all values
39
40
  * @returns {this}
40
41
  */
41
42
  setScalar(scalar: number): this;
@@ -193,19 +194,26 @@ export default class Vector2 implements Vector<Vec2> {
193
194
  */
194
195
  rotateAround(center: Vec2, angle: number): this;
195
196
  /**
196
- * Interpolate this vector values between a given vector and this vector
197
+ * Linearly interpolate this vector values towards a given vector values
197
198
  *
198
- * @param {Vector2|Point2} vector Vector to interpolate values towards
199
199
  * @param {number} t Normalized time value to interpolate
200
+ * @param {Vector2|Point2} vector Vector to interpolate values towards
200
201
  * @returns {this}
201
202
  */
202
- lerp([x, y]: Vec2, t: number): this;
203
+ lerp(t: number, [x, y]: Vec2): this;
203
204
  /**
204
205
  * Convert this vector to a unit vector
205
206
  *
206
207
  * @returns {this}
207
208
  */
208
209
  normalize(): this;
210
+ /**
211
+ * Transform this vector by a given matrix
212
+ *
213
+ * @param {DOMMatrix} matrix Matrix to apply
214
+ * @returns {this}
215
+ */
216
+ applyMatrix(matrix: DOMMatrix): this;
209
217
  /**
210
218
  * Set this vector values to the same direction but with a given length
211
219
  *
@@ -304,7 +312,7 @@ export default class Vector2 implements Vector<Vec2> {
304
312
  * Set this vector values from a given array
305
313
  *
306
314
  * @param {number[]} values Values to set
307
- * @returns
315
+ * @returns {this}
308
316
  */
309
317
  fromArray([x, y]: number[]): this;
310
318
  /**
@@ -328,6 +336,16 @@ export default class Vector2 implements Vector<Vec2> {
328
336
  * @returns {Vector2}
329
337
  */
330
338
  clone(): Vector2;
339
+ /**
340
+ * X-axis value of this vector
341
+ */
342
+ set width(width: number);
343
+ get width(): number;
344
+ /**
345
+ * Y-axis value of this vector
346
+ */
347
+ set height(height: number);
348
+ get height(): number;
331
349
  /**
332
350
  * Add two vectors
333
351
  *
@@ -370,7 +388,7 @@ export default class Vector2 implements Vector<Vec2> {
370
388
  */
371
389
  static rotate([vx, vy]: Vec2, [cx, cy]: Vec2, angle: number): Point2;
372
390
  /**
373
- * Interpolate a point between two vectors
391
+ * Linearly interpolate a point between two vectors
374
392
  *
375
393
  * @param {number} t Normalized time value to interpolate
376
394
  * @param {Vector2|Point2} min Minimum boundaries
@@ -471,10 +489,5 @@ export default class Vector2 implements Vector<Vec2> {
471
489
  * @returns {Point2}
472
490
  */
473
491
  static fromCircularCoords(angle: number, radius?: number): Point2;
474
- set width(width: number);
475
- get width(): number;
476
- set height(height: number);
477
- get height(): number;
478
- [Symbol.iterator](): Iterator<number>;
479
492
  }
480
493
  export {};
@@ -18,6 +18,10 @@ export default class Vector2 {
18
18
  * Y-axis value of this vector
19
19
  */
20
20
  y;
21
+ *[Symbol.iterator]() {
22
+ yield this.x;
23
+ yield this.y;
24
+ }
21
25
  /**
22
26
  * @param {number} [x=0] X-axis value
23
27
  * @param {number} [y=0] Y-axis value
@@ -41,7 +45,7 @@ export default class Vector2 {
41
45
  /**
42
46
  * Set a given scalar value to all values of this vector
43
47
  *
44
- * @param {number} scalar Value to set for all vector values
48
+ * @param {number} scalar Value to set for all values
45
49
  * @returns {this}
46
50
  */
47
51
  setScalar(scalar) {
@@ -306,13 +310,13 @@ export default class Vector2 {
306
310
  return this.set(...Vector2.rotate(this, center, angle));
307
311
  }
308
312
  /**
309
- * Interpolate this vector values between a given vector and this vector
313
+ * Linearly interpolate this vector values towards a given vector values
310
314
  *
311
- * @param {Vector2|Point2} vector Vector to interpolate values towards
312
315
  * @param {number} t Normalized time value to interpolate
316
+ * @param {Vector2|Point2} vector Vector to interpolate values towards
313
317
  * @returns {this}
314
318
  */
315
- lerp([x, y], t) {
319
+ lerp(t, [x, y]) {
316
320
  this.x += (x - this.x) * t;
317
321
  this.y += (y - this.y) * t;
318
322
  return this;
@@ -325,6 +329,18 @@ export default class Vector2 {
325
329
  normalize() {
326
330
  return this.divideScalar(this.length() || 1);
327
331
  }
332
+ /**
333
+ * Transform this vector by a given matrix
334
+ *
335
+ * @param {DOMMatrix} matrix Matrix to apply
336
+ * @returns {this}
337
+ */
338
+ applyMatrix(matrix) {
339
+ const { x, y } = matrix.transformPoint({ x: this.x, y: this.y });
340
+ this.x = x;
341
+ this.y = y;
342
+ return this;
343
+ }
328
344
  /**
329
345
  * Set this vector values to the same direction but with a given length
330
346
  *
@@ -455,7 +471,7 @@ export default class Vector2 {
455
471
  * Set this vector values from a given array
456
472
  *
457
473
  * @param {number[]} values Values to set
458
- * @returns
474
+ * @returns {this}
459
475
  */
460
476
  fromArray([x, y]) {
461
477
  this.x = x;
@@ -491,6 +507,24 @@ export default class Vector2 {
491
507
  clone() {
492
508
  return new Vector2(this.x, this.y);
493
509
  }
510
+ /**
511
+ * X-axis value of this vector
512
+ */
513
+ set width(width) {
514
+ this.x = width;
515
+ }
516
+ get width() {
517
+ return this.x;
518
+ }
519
+ /**
520
+ * Y-axis value of this vector
521
+ */
522
+ set height(height) {
523
+ this.y = height;
524
+ }
525
+ get height() {
526
+ return this.y;
527
+ }
494
528
  /**
495
529
  * Add two vectors
496
530
  *
@@ -557,7 +591,7 @@ export default class Vector2 {
557
591
  return [x, y];
558
592
  }
559
593
  /**
560
- * Interpolate a point between two vectors
594
+ * Linearly interpolate a point between two vectors
561
595
  *
562
596
  * @param {number} t Normalized time value to interpolate
563
597
  * @param {Vector2|Point2} min Minimum boundaries
@@ -627,7 +661,7 @@ export default class Vector2 {
627
661
  * @returns {number} Computed Euclidean distance
628
662
  */
629
663
  static distance(vector1, vector2) {
630
- return Math.sqrt(Vector2.squaredDistance(vector1, vector2));
664
+ return Math.sqrt(this.squaredDistance(vector1, vector2));
631
665
  }
632
666
  /**
633
667
  * Calculate the squared distance between two vectors
@@ -690,20 +724,4 @@ export default class Vector2 {
690
724
  const y = radius * Math.sin(angle);
691
725
  return [x, y];
692
726
  }
693
- set width(width) {
694
- this.x = width;
695
- }
696
- get width() {
697
- return this.x;
698
- }
699
- set height(height) {
700
- this.y = height;
701
- }
702
- get height() {
703
- return this.y;
704
- }
705
- *[Symbol.iterator]() {
706
- yield this.x;
707
- yield this.y;
708
- }
709
727
  }
@@ -23,6 +23,7 @@ export default class Vector3 implements Vector<Vec3> {
23
23
  * Z-axis value of this vector
24
24
  */
25
25
  z: number;
26
+ [Symbol.iterator](): Iterator<number>;
26
27
  /**
27
28
  * @param {number} [x=0] X-axis value
28
29
  * @param {number} [y=0] Y-axis value
@@ -198,19 +199,26 @@ export default class Vector3 implements Vector<Vec3> {
198
199
  */
199
200
  negate(): this;
200
201
  /**
201
- * Interpolate this vector values between a given vector and this vector
202
+ * Linearly interpolate this vector values towards a given vector values
202
203
  *
203
- * @param {Vector3|Point3} vector Vector to interpolate values towards
204
204
  * @param {number} t Normalized time value to interpolate
205
+ * @param {Vector3|Point3} vector Vector to interpolate values towards
205
206
  * @returns {this}
206
207
  */
207
- lerp([x, y, z]: Vec3, t: number): this;
208
+ lerp(t: number, [x, y, z]: Vec3): this;
208
209
  /**
209
210
  * Convert this vector to a unit vector
210
211
  *
211
212
  * @returns {this}
212
213
  */
213
214
  normalize(): this;
215
+ /**
216
+ * Transform this vector by a given matrix
217
+ *
218
+ * @param {DOMMatrix} matrix Matrix to apply
219
+ * @returns {this}
220
+ */
221
+ applyMatrix(matrix: DOMMatrix): this;
214
222
  /**
215
223
  * Set this vector values to the same direction but with a given length
216
224
  *
@@ -377,7 +385,7 @@ export default class Vector3 implements Vector<Vec3> {
377
385
  */
378
386
  static divide([x1, y1, z1]: Vec3, [x2, y2, z2]: Vec3): Point3;
379
387
  /**
380
- * Interpolate a point between two vectors
388
+ * Linearly interpolate a point between two vectors
381
389
  *
382
390
  * @param {number} t Normalized time value to interpolate
383
391
  * @param {Vector3|Point3} min Minimum boundaries
@@ -481,6 +489,5 @@ export default class Vector3 implements Vector<Vec3> {
481
489
  * @returns {Point3}
482
490
  */
483
491
  static fromCylindricalCoords(theta: number, y: number, radius?: number): Point3;
484
- [Symbol.iterator](): Iterator<number>;
485
492
  }
486
493
  export {};
@@ -22,6 +22,11 @@ export default class Vector3 {
22
22
  * Z-axis value of this vector
23
23
  */
24
24
  z;
25
+ *[Symbol.iterator]() {
26
+ yield this.x;
27
+ yield this.y;
28
+ yield this.z;
29
+ }
25
30
  /**
26
31
  * @param {number} [x=0] X-axis value
27
32
  * @param {number} [y=0] Y-axis value
@@ -339,13 +344,13 @@ export default class Vector3 {
339
344
  return this;
340
345
  }
341
346
  /**
342
- * Interpolate this vector values between a given vector and this vector
347
+ * Linearly interpolate this vector values towards a given vector values
343
348
  *
344
- * @param {Vector3|Point3} vector Vector to interpolate values towards
345
349
  * @param {number} t Normalized time value to interpolate
350
+ * @param {Vector3|Point3} vector Vector to interpolate values towards
346
351
  * @returns {this}
347
352
  */
348
- lerp([x, y, z], t) {
353
+ lerp(t, [x, y, z]) {
349
354
  this.x += (x - this.x) * t;
350
355
  this.y += (y - this.y) * t;
351
356
  this.z += (z - this.z) * t;
@@ -359,6 +364,19 @@ export default class Vector3 {
359
364
  normalize() {
360
365
  return this.divideScalar(this.length() || 1);
361
366
  }
367
+ /**
368
+ * Transform this vector by a given matrix
369
+ *
370
+ * @param {DOMMatrix} matrix Matrix to apply
371
+ * @returns {this}
372
+ */
373
+ applyMatrix(matrix) {
374
+ const { x, y, z } = matrix.transformPoint({ x: this.x, y: this.y, z: this.z });
375
+ this.x = x;
376
+ this.y = y;
377
+ this.z = z;
378
+ return this;
379
+ }
362
380
  /**
363
381
  * Set this vector values to the same direction but with a given length
364
382
  *
@@ -605,7 +623,7 @@ export default class Vector3 {
605
623
  return [x, y, z];
606
624
  }
607
625
  /**
608
- * Interpolate a point between two vectors
626
+ * Linearly interpolate a point between two vectors
609
627
  *
610
628
  * @param {number} t Normalized time value to interpolate
611
629
  * @param {Vector3|Point3} min Minimum boundaries
@@ -678,7 +696,7 @@ export default class Vector3 {
678
696
  * @returns {number} Computed Euclidean distance
679
697
  */
680
698
  static distance(vector1, vector2) {
681
- return Math.sqrt(Vector3.squaredDistance(vector1, vector2));
699
+ return Math.sqrt(this.squaredDistance(vector1, vector2));
682
700
  }
683
701
  /**
684
702
  * Calculate the squared distance between two vectors
@@ -757,9 +775,4 @@ export default class Vector3 {
757
775
  const z = radius * Math.cos(theta);
758
776
  return [x, y, z];
759
777
  }
760
- *[Symbol.iterator]() {
761
- yield this.x;
762
- yield this.y;
763
- yield this.z;
764
- }
765
778
  }
@@ -7,13 +7,13 @@ import { type Vector, Vector2, Vector3 } from '../geometry';
7
7
  * @class Path
8
8
  * @extends Curve
9
9
  */
10
- export default class Path<V extends Vector = Vector2 | Vector3, S extends Curve<V> = Curve<V>> extends Curve<V> {
10
+ export default class Path<V extends Vector = Vector2 | Vector3, C extends Curve<V> = Curve<V>> extends Curve<V> {
11
11
  readonly isPath = true;
12
12
  readonly type: string;
13
13
  /**
14
14
  * Array of curves composing this path
15
15
  */
16
- subpaths: S[];
16
+ curves: C[];
17
17
  /**
18
18
  * Array of points composing this path
19
19
  */
@@ -30,7 +30,7 @@ export default class Path<V extends Vector = Vector2 | Vector3, S extends Curve<
30
30
  *
31
31
  * @param {Curve} curve Curve to add
32
32
  */
33
- add(curve: S): void;
33
+ add(curve: C): void;
34
34
  /**
35
35
  * Interpolate a point on this path
36
36
  *
@@ -12,7 +12,7 @@ export default class Path extends Curve {
12
12
  /**
13
13
  * Array of curves composing this path
14
14
  */
15
- subpaths = [];
15
+ curves = [];
16
16
  /**
17
17
  * Array of points composing this path
18
18
  */
@@ -31,7 +31,7 @@ export default class Path extends Curve {
31
31
  * @param {Curve} curve Curve to add
32
32
  */
33
33
  add(curve) {
34
- this.subpaths.push(curve);
34
+ this.curves.push(curve);
35
35
  }
36
36
  /**
37
37
  * Interpolate a point on this path
@@ -46,7 +46,7 @@ export default class Path extends Curve {
46
46
  while (i < curveLengths.length) {
47
47
  if (curveLengths[i] >= d) {
48
48
  const delta = curveLengths[i] - d;
49
- const curve = this.subpaths[i];
49
+ const curve = this.curves[i];
50
50
  const segmentLength = curve.getLength();
51
51
  const u = segmentLength === 0 ? 0 : 1 - delta / segmentLength;
52
52
  return curve.getPointAt(u);
@@ -54,7 +54,7 @@ export default class Path extends Curve {
54
54
  i++;
55
55
  }
56
56
  console.warn(`Path.getPoint()`, `No point found in curve.`, this);
57
- return this.subpaths[0].getPoint(0);
57
+ return this.curves[0].getPoint(0);
58
58
  }
59
59
  /**
60
60
  * Compute the curve shape into an array of points
@@ -65,8 +65,8 @@ export default class Path extends Curve {
65
65
  getPoints(divisions = 40) {
66
66
  const points = [];
67
67
  let lastPoint = null;
68
- for (let i = 0; i < this.subpaths.length; i++) {
69
- const curve = this.subpaths[i];
68
+ for (let i = 0; i < this.curves.length; i++) {
69
+ const curve = this.curves[i];
70
70
  let resolution = divisions;
71
71
  if (curve instanceof LineCurve || curve instanceof LineCurve3) {
72
72
  resolution = 1;
@@ -80,7 +80,7 @@ export default class Path extends Curve {
80
80
  else if (curve instanceof EllipseCurve) {
81
81
  resolution *= 2;
82
82
  }
83
- const pts = this.subpaths[i].getPoints(resolution);
83
+ const pts = this.curves[i].getPoints(resolution);
84
84
  for (let j = 0; j < pts.length; j++) {
85
85
  const point = pts[j];
86
86
  if (point?.equals(lastPoint))
@@ -127,13 +127,13 @@ export default class Path extends Curve {
127
127
  * @returns {number[]}
128
128
  */
129
129
  getCurveLengths() {
130
- if (this._cacheArcLengths.length === this.subpaths.length) {
130
+ if (this._cacheArcLengths.length === this.curves.length) {
131
131
  return this._cacheArcLengths;
132
132
  }
133
133
  const lengths = [];
134
134
  let sums = 0;
135
- for (let i = 0, j = this.subpaths.length; i < j; i++) {
136
- sums += this.subpaths[i].getLength();
135
+ for (let i = 0, j = this.curves.length; i < j; i++) {
136
+ sums += this.curves[i].getLength();
137
137
  lengths.push(sums);
138
138
  }
139
139
  this._cacheArcLengths = lengths;
@@ -10,14 +10,8 @@ import Path from './Path';
10
10
  * @implements CanvasRenderingContext2D
11
11
  */
12
12
  export default class PathContext extends Path<Vector2> implements CanvasRenderingContext2D {
13
- /**
14
- * Path current position
15
- */
16
- currentPosition: Vector2;
17
- /**
18
- * Path current transformation matrix
19
- */
20
- currentTransform: DOMMatrix;
13
+ protected _currentPosition: Vector2;
14
+ protected _currentTransform: DOMMatrix;
21
15
  private _transformStack;
22
16
  /**
23
17
  * Create a path from a given list of points
@@ -139,7 +133,7 @@ export default class PathContext extends Path<Vector2> implements CanvasRenderin
139
133
  arc(cx: number, cy: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this;
140
134
  /**
141
135
  * Draw an Arc curve from the current position, tangential to the 2 segments created by both control points
142
- * Add an instance of {@link ArcCurve} to this path
136
+ * Add an instance of {@link EllipseCurve} to this path
143
137
  *
144
138
  * @param {number} x1 X-axis coordinate of the first control point
145
139
  * @param {number} y1 Y-axis coordinate of the first control point
@@ -185,22 +179,18 @@ export default class PathContext extends Path<Vector2> implements CanvasRenderin
185
179
  reset(): void;
186
180
  protected _hasCurrentPosition(): boolean;
187
181
  protected _setCurrentPosition(x: number, y: number): this;
188
- protected _transformPoint(point: Point2, matrix?: DOMMatrix): Point2;
189
- protected _transformPoints(points: Point2[], matrix?: DOMMatrix): Point2[];
190
- protected _transformVector(vector: Point2, matrix?: DOMMatrix): Point2;
191
- protected _transformEllipse(cx: number, cy: number, rx: number, ry: number, rotation: number, matrix?: DOMMatrix): [number, number, number, number, number];
192
- protected _inversePoint(point: Point2): Point2;
182
+ protected _transformPoint(point: Point2): Point2;
183
+ protected _transformPoints(points: Point2[]): Point2[];
184
+ protected _transformVector(vector: Point2): Point2;
185
+ protected _transformEllipse(cx: number, cy: number, rx: number, ry: number, rotation: number): [number, number, number, number, number];
193
186
  protected get _translateX(): number;
194
187
  protected get _translateY(): number;
195
188
  protected get _scaleX(): number;
196
189
  protected get _scaleY(): number;
197
190
  protected get _rotation(): number;
198
- protected get _skewX(): number;
199
- protected get _skewY(): number;
200
191
  protected get _isTranslated(): boolean;
201
192
  protected get _isScaled(): boolean;
202
193
  protected get _isRotated(): boolean;
203
- protected get _isSkewed(): boolean;
204
194
  protected get _isUniform(): boolean;
205
195
  protected get _isIdentity(): boolean;
206
196
  canvas: CanvasRenderingContext2D['canvas'];