toosoon-utils 4.0.6 → 4.1.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 (45) hide show
  1. package/README.md +716 -88
  2. package/lib/constants.d.ts +2 -0
  3. package/lib/constants.js +5 -3
  4. package/lib/extras/_wip/pool.d.ts +56 -0
  5. package/lib/extras/_wip/pool.js +67 -0
  6. package/lib/{classes/color-scale.d.ts → extras/color-scale/ColorScale.d.ts} +1 -1
  7. package/lib/{classes/color-scale.js → extras/color-scale/ColorScale.js} +2 -2
  8. package/lib/extras/curves/ArcCurve.d.ts +19 -0
  9. package/lib/extras/curves/ArcCurve.js +21 -0
  10. package/lib/extras/curves/CatmulRomCurve.d.ts +62 -0
  11. package/lib/extras/curves/CatmulRomCurve.js +75 -0
  12. package/lib/extras/curves/CubicBezierCurve.d.ts +62 -0
  13. package/lib/extras/curves/CubicBezierCurve.js +75 -0
  14. package/lib/extras/curves/Curve.d.ts +95 -0
  15. package/lib/extras/curves/Curve.js +174 -0
  16. package/lib/extras/curves/EllipseCurve.d.ts +63 -0
  17. package/lib/extras/curves/EllipseCurve.js +76 -0
  18. package/lib/extras/curves/LineCurve.d.ts +51 -0
  19. package/lib/extras/curves/LineCurve.js +71 -0
  20. package/lib/extras/curves/Path.d.ts +164 -0
  21. package/lib/extras/curves/Path.js +367 -0
  22. package/lib/extras/curves/PathCurve.d.ts +69 -0
  23. package/lib/extras/curves/PathCurve.js +148 -0
  24. package/lib/extras/curves/PathSVG.d.ts +41 -0
  25. package/lib/extras/curves/PathSVG.js +135 -0
  26. package/lib/extras/curves/PolylineCurve.d.ts +27 -0
  27. package/lib/extras/curves/PolylineCurve.js +39 -0
  28. package/lib/extras/curves/QuadraticBezierCurve.d.ts +52 -0
  29. package/lib/extras/curves/QuadraticBezierCurve.js +63 -0
  30. package/lib/extras/curves/SplineCurve.d.ts +24 -0
  31. package/lib/extras/curves/SplineCurve.js +38 -0
  32. package/lib/extras/curves/index.d.ts +12 -0
  33. package/lib/extras/curves/index.js +12 -0
  34. package/lib/{classes/frame-rate.js → extras/frame-rate/FrameRate.js} +1 -1
  35. package/lib/geometry.d.ts +87 -68
  36. package/lib/geometry.js +144 -140
  37. package/lib/prng.js +2 -1
  38. package/lib/random.d.ts +13 -13
  39. package/lib/random.js +14 -14
  40. package/lib/tsconfig.tsbuildinfo +1 -1
  41. package/lib/types.d.ts +3 -10
  42. package/package.json +4 -3
  43. /package/lib/{classes/_pool.d.ts → extras/_/pool.d.ts} +0 -0
  44. /package/lib/{classes/_pool.js → extras/_/pool.js} +0 -0
  45. /package/lib/{classes/frame-rate.d.ts → extras/frame-rate/FrameRate.d.ts} +0 -0
@@ -1,8 +1,10 @@
1
1
  export declare const EPSILON = 1e-10;
2
2
  export declare const PI: number;
3
3
  export declare const TWO_PI: number;
4
+ export declare const TAU: number;
4
5
  export declare const HALF_PI: number;
5
6
  export declare const QUARTER_PI: number;
7
+ export declare const TAU_EPSILON: number;
6
8
  export declare const W3CX11: {
7
9
  aliceblue: number;
8
10
  antiquewhite: number;
package/lib/constants.js CHANGED
@@ -3,9 +3,11 @@
3
3
  // *********************
4
4
  export const EPSILON = 1e-10;
5
5
  export const PI = Math.PI;
6
- export const TWO_PI = Math.PI * 2;
7
- export const HALF_PI = Math.PI / 2;
8
- export const QUARTER_PI = Math.PI / 4;
6
+ export const TWO_PI = PI * 2;
7
+ export const TAU = PI * 2;
8
+ export const HALF_PI = PI / 2;
9
+ export const QUARTER_PI = PI / 4;
10
+ export const TAU_EPSILON = TAU - EPSILON;
9
11
  // *********************
10
12
  // Colors
11
13
  // *********************
@@ -0,0 +1,56 @@
1
+ export type PoolSettings = {
2
+ max?: number;
3
+ };
4
+ export declare const defaultSettings: Required<PoolSettings>;
5
+ type PoolItem = {
6
+ setup?: () => void;
7
+ reset?: () => void;
8
+ dispose?: () => void;
9
+ };
10
+ /**
11
+ * Abstract class for manipulating pool items
12
+ *
13
+ * @exports
14
+ * @class Pool
15
+ */
16
+ export default abstract class Pool<I extends PoolItem> {
17
+ items: I[];
18
+ pool: I[];
19
+ settings: Required<PoolSettings>;
20
+ constructor(settings?: PoolSettings);
21
+ /**
22
+ * Abstract method to implement custom item creation
23
+ *
24
+ * @returns {PoolItem}
25
+ */
26
+ protected abstract create(): I;
27
+ /**
28
+ * Add an item to the active items
29
+ *
30
+ * @param {PoolItem} item Item to add to the active items
31
+ */
32
+ protected add(item: I): void;
33
+ /**
34
+ * Remove an item from the active items
35
+ *
36
+ * @param {PoolItem} item Item to remove from the active items
37
+ */
38
+ protected remove(item: I): void;
39
+ /**
40
+ * Return an item from pool or create a new one
41
+ *
42
+ * @returns {PoolItem|undefined}
43
+ */
44
+ get(): I | undefined;
45
+ /**
46
+ * Release an item from the active items and add it to the pool
47
+ *
48
+ * @param {PoolItem} item Item to release
49
+ */
50
+ release(item: I): void;
51
+ /**
52
+ * Dispose all items
53
+ */
54
+ dispose(): void;
55
+ }
56
+ export {};
@@ -0,0 +1,67 @@
1
+ // *********************
2
+ // WIP
3
+ // *********************
4
+ export const defaultSettings = {
5
+ max: Infinity
6
+ };
7
+ /**
8
+ * Abstract class for manipulating pool items
9
+ *
10
+ * @exports
11
+ * @class Pool
12
+ */
13
+ export default class Pool {
14
+ items = [];
15
+ pool = [];
16
+ settings = { ...defaultSettings };
17
+ constructor(settings = { ...defaultSettings }) {
18
+ this.settings = Object.assign(this.settings, settings);
19
+ }
20
+ /**
21
+ * Add an item to the active items
22
+ *
23
+ * @param {PoolItem} item Item to add to the active items
24
+ */
25
+ add(item) {
26
+ this.items.push(item);
27
+ }
28
+ /**
29
+ * Remove an item from the active items
30
+ *
31
+ * @param {PoolItem} item Item to remove from the active items
32
+ */
33
+ remove(item) {
34
+ this.items = this.items.filter((_item) => _item !== item);
35
+ }
36
+ /**
37
+ * Return an item from pool or create a new one
38
+ *
39
+ * @returns {PoolItem|undefined}
40
+ */
41
+ get() {
42
+ if (this.items.length >= this.settings.max)
43
+ return;
44
+ const item = this.pool.pop() ?? this.create();
45
+ item.setup?.();
46
+ this.add(item);
47
+ return item;
48
+ }
49
+ /**
50
+ * Release an item from the active items and add it to the pool
51
+ *
52
+ * @param {PoolItem} item Item to release
53
+ */
54
+ release(item) {
55
+ this.pool.push(item);
56
+ item.reset?.();
57
+ this.remove(item);
58
+ }
59
+ /**
60
+ * Dispose all items
61
+ */
62
+ dispose() {
63
+ [...this.items, ...this.pool].forEach((item) => item.dispose?.());
64
+ this.items = [];
65
+ this.pool = [];
66
+ }
67
+ }
@@ -1,4 +1,4 @@
1
- import type { ColorRepresentation } from '../types';
1
+ import type { ColorRepresentation } from '../../types';
2
2
  export type ColorScaleSettings = {
3
3
  colorSpace: 'rgb';
4
4
  } | {
@@ -1,5 +1,5 @@
1
- import { lerp, triLerp } from '../maths';
2
- import { hclToRgb, hsbToRgb, hslToRgb, normalizeColor, rgbToHcl, rgbToHsb, rgbToHsl } from '../colors';
1
+ import { lerp, triLerp } from '../../maths';
2
+ import { hclToRgb, hsbToRgb, hslToRgb, normalizeColor, rgbToHcl, rgbToHsb, rgbToHsl } from '../../colors';
3
3
  export const defaultSettings = {
4
4
  colorSpace: 'rgb'
5
5
  };
@@ -0,0 +1,19 @@
1
+ import EllipseCurve from './EllipseCurve';
2
+ /**
3
+ * Utility class for manipulating Arc curves
4
+ *
5
+ * @exports
6
+ * @class ArcCurve
7
+ * @extends Curve
8
+ */
9
+ export default class ArcCurve extends EllipseCurve {
10
+ /**
11
+ * @param {number} cx X-axis coordinate of the center of the circle
12
+ * @param {number} cy Y-axis coordinate of the center of the circle
13
+ * @param {number} radius Radius of the circle
14
+ * @param {number} [startAngle] Rotation angle of the arc (in radians)
15
+ * @param {number} [endAngle] Rotation angle of the arc (in radians)
16
+ * @param {boolean} [counterclockwise] Flag indicating the direction of the arc
17
+ */
18
+ constructor(cx: number, cy: number, radius: number, startAngle?: number, endAngle?: number, counterclockwise?: boolean);
19
+ }
@@ -0,0 +1,21 @@
1
+ import EllipseCurve from './EllipseCurve';
2
+ /**
3
+ * Utility class for manipulating Arc curves
4
+ *
5
+ * @exports
6
+ * @class ArcCurve
7
+ * @extends Curve
8
+ */
9
+ export default class ArcCurve extends EllipseCurve {
10
+ /**
11
+ * @param {number} cx X-axis coordinate of the center of the circle
12
+ * @param {number} cy Y-axis coordinate of the center of the circle
13
+ * @param {number} radius Radius of the circle
14
+ * @param {number} [startAngle] Rotation angle of the arc (in radians)
15
+ * @param {number} [endAngle] Rotation angle of the arc (in radians)
16
+ * @param {boolean} [counterclockwise] Flag indicating the direction of the arc
17
+ */
18
+ constructor(cx, cy, radius, startAngle, endAngle, counterclockwise) {
19
+ super(cx, cy, radius, radius, 0, startAngle, endAngle, counterclockwise);
20
+ }
21
+ }
@@ -0,0 +1,62 @@
1
+ import type { Point } from 'src/types';
2
+ import Curve from './Curve';
3
+ /**
4
+ * Utility class for manipulating Catmull-Rom curves
5
+ *
6
+ * @exports
7
+ * @class CatmullRomCurve
8
+ * @extends Curve
9
+ */
10
+ export default class CatmullRomCurve extends Curve {
11
+ readonly type: string;
12
+ /**
13
+ * X-axis coordinate of the start point
14
+ */
15
+ x1: number;
16
+ /**
17
+ * Y-axis coordinate of the start point
18
+ */
19
+ y1: number;
20
+ /**
21
+ * X-axis coordinate of the first control point
22
+ */
23
+ cp1x: number;
24
+ /**
25
+ * Y-axis coordinate of the first control point
26
+ */
27
+ cp1y: number;
28
+ /**
29
+ * X-axis coordinate of the second control point
30
+ */
31
+ cp2x: number;
32
+ /**
33
+ * Y-axis coordinate of the second control point
34
+ */
35
+ cp2y: number;
36
+ /**
37
+ * X-axis coordinate of the end point
38
+ */
39
+ x2: number;
40
+ /**
41
+ * Y-axis coordinate of the end point
42
+ */
43
+ y2: number;
44
+ /**
45
+ * @param {number} x1 X-axis coordinate of the start point
46
+ * @param {number} y1 Y-axis coordinate of the start point
47
+ * @param {number} cp1x X-axis coordinate of the first control point
48
+ * @param {number} cp1y Y-axis coordinate of the first control point
49
+ * @param {number} cp2x X-axis coordinate of the second control point
50
+ * @param {number} cp2y Y-axis coordinate of the second control point
51
+ * @param {number} x2 X-axis coordinate of the end point
52
+ * @param {number} y2 Y-axis coordinate of the end point
53
+ */
54
+ constructor(x1: number, y1: number, cp1x: number, cp1y: number, cp2x: number, cp2y: number, x2: number, y2: number);
55
+ /**
56
+ * Interpolate a point on the Catmull-Rom curve
57
+ *
58
+ * @param {number} t Normalized time value to interpolate
59
+ * @returns {Point} Interpolated coordinates on the curve
60
+ */
61
+ getPoint(t: number): Point;
62
+ }
@@ -0,0 +1,75 @@
1
+ import { catmullRom } from 'src/geometry';
2
+ import Curve from './Curve';
3
+ /**
4
+ * Utility class for manipulating Catmull-Rom curves
5
+ *
6
+ * @exports
7
+ * @class CatmullRomCurve
8
+ * @extends Curve
9
+ */
10
+ export default class CatmullRomCurve extends Curve {
11
+ type = 'CatmullRomCurve';
12
+ /**
13
+ * X-axis coordinate of the start point
14
+ */
15
+ x1;
16
+ /**
17
+ * Y-axis coordinate of the start point
18
+ */
19
+ y1;
20
+ /**
21
+ * X-axis coordinate of the first control point
22
+ */
23
+ cp1x;
24
+ /**
25
+ * Y-axis coordinate of the first control point
26
+ */
27
+ cp1y;
28
+ /**
29
+ * X-axis coordinate of the second control point
30
+ */
31
+ cp2x;
32
+ /**
33
+ * Y-axis coordinate of the second control point
34
+ */
35
+ cp2y;
36
+ /**
37
+ * X-axis coordinate of the end point
38
+ */
39
+ x2;
40
+ /**
41
+ * Y-axis coordinate of the end point
42
+ */
43
+ y2;
44
+ /**
45
+ * @param {number} x1 X-axis coordinate of the start point
46
+ * @param {number} y1 Y-axis coordinate of the start point
47
+ * @param {number} cp1x X-axis coordinate of the first control point
48
+ * @param {number} cp1y Y-axis coordinate of the first control point
49
+ * @param {number} cp2x X-axis coordinate of the second control point
50
+ * @param {number} cp2y Y-axis coordinate of the second control point
51
+ * @param {number} x2 X-axis coordinate of the end point
52
+ * @param {number} y2 Y-axis coordinate of the end point
53
+ */
54
+ constructor(x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2) {
55
+ super();
56
+ this.x1 = x1;
57
+ this.y1 = y1;
58
+ this.cp1x = cp1x;
59
+ this.cp1y = cp1y;
60
+ this.cp2x = cp2x;
61
+ this.cp2y = cp2y;
62
+ this.x2 = x2;
63
+ this.y2 = y2;
64
+ }
65
+ /**
66
+ * Interpolate a point on the Catmull-Rom curve
67
+ *
68
+ * @param {number} t Normalized time value to interpolate
69
+ * @returns {Point} Interpolated coordinates on the curve
70
+ */
71
+ getPoint(t) {
72
+ const point = catmullRom(t, this.x1, this.y1, this.cp1x, this.cp1y, this.cp2x, this.cp2y, this.x2, this.y2);
73
+ return point;
74
+ }
75
+ }
@@ -0,0 +1,62 @@
1
+ import type { Point } from 'src/types';
2
+ import Curve from './Curve';
3
+ /**
4
+ * Utility class for manipulating Cubic Bézier curves
5
+ *
6
+ * @exports
7
+ * @class CubicBezierCurve
8
+ * @extends Curve
9
+ */
10
+ export default class CubicBezierCurve extends Curve {
11
+ readonly type: string;
12
+ /**
13
+ * X-axis coordinate of the start point
14
+ */
15
+ x1: number;
16
+ /**
17
+ * Y-axis coordinate of the start point
18
+ */
19
+ y1: number;
20
+ /**
21
+ * X-axis coordinate of the first control point
22
+ */
23
+ cp1x: number;
24
+ /**
25
+ * Y-axis coordinate of the first control point
26
+ */
27
+ cp1y: number;
28
+ /**
29
+ * X-axis coordinate of the second control point
30
+ */
31
+ cp2x: number;
32
+ /**
33
+ * Y-axis coordinate of the second control point
34
+ */
35
+ cp2y: number;
36
+ /**
37
+ * X-axis coordinate of the end point
38
+ */
39
+ x2: number;
40
+ /**
41
+ * Y-axis coordinate of the end point
42
+ */
43
+ y2: number;
44
+ /**
45
+ * @param {number} x1 X-axis coordinate of the start point
46
+ * @param {number} y1 Y-axis coordinate of the start point
47
+ * @param {number} cp1x X-axis coordinate of the first control point
48
+ * @param {number} cp1y Y-axis coordinate of the first control point
49
+ * @param {number} cp2x X-axis coordinate of the second control point
50
+ * @param {number} cp2y Y-axis coordinate of the second control point
51
+ * @param {number} x2 X-axis coordinate of the end point
52
+ * @param {number} y2 Y-axis coordinate of the end point
53
+ */
54
+ constructor(x1: number, y1: number, cp1x: number, cp1y: number, cp2x: number, cp2y: number, x2: number, y2: number);
55
+ /**
56
+ * Interpolate a point on the Cubic Bézier curve
57
+ *
58
+ * @param {number} t Normalized time value to interpolate
59
+ * @returns {Point} Interpolated coordinates on the curve
60
+ */
61
+ getPoint(t: number): Point;
62
+ }
@@ -0,0 +1,75 @@
1
+ import { cubicBezier } from 'src/geometry';
2
+ import Curve from './Curve';
3
+ /**
4
+ * Utility class for manipulating Cubic Bézier curves
5
+ *
6
+ * @exports
7
+ * @class CubicBezierCurve
8
+ * @extends Curve
9
+ */
10
+ export default class CubicBezierCurve extends Curve {
11
+ type = 'CubicBezierCurve';
12
+ /**
13
+ * X-axis coordinate of the start point
14
+ */
15
+ x1;
16
+ /**
17
+ * Y-axis coordinate of the start point
18
+ */
19
+ y1;
20
+ /**
21
+ * X-axis coordinate of the first control point
22
+ */
23
+ cp1x;
24
+ /**
25
+ * Y-axis coordinate of the first control point
26
+ */
27
+ cp1y;
28
+ /**
29
+ * X-axis coordinate of the second control point
30
+ */
31
+ cp2x;
32
+ /**
33
+ * Y-axis coordinate of the second control point
34
+ */
35
+ cp2y;
36
+ /**
37
+ * X-axis coordinate of the end point
38
+ */
39
+ x2;
40
+ /**
41
+ * Y-axis coordinate of the end point
42
+ */
43
+ y2;
44
+ /**
45
+ * @param {number} x1 X-axis coordinate of the start point
46
+ * @param {number} y1 Y-axis coordinate of the start point
47
+ * @param {number} cp1x X-axis coordinate of the first control point
48
+ * @param {number} cp1y Y-axis coordinate of the first control point
49
+ * @param {number} cp2x X-axis coordinate of the second control point
50
+ * @param {number} cp2y Y-axis coordinate of the second control point
51
+ * @param {number} x2 X-axis coordinate of the end point
52
+ * @param {number} y2 Y-axis coordinate of the end point
53
+ */
54
+ constructor(x1, y1, cp1x, cp1y, cp2x, cp2y, x2, y2) {
55
+ super();
56
+ this.x1 = x1;
57
+ this.y1 = y1;
58
+ this.cp1x = cp1x;
59
+ this.cp1y = cp1y;
60
+ this.cp2x = cp2x;
61
+ this.cp2y = cp2y;
62
+ this.x2 = x2;
63
+ this.y2 = y2;
64
+ }
65
+ /**
66
+ * Interpolate a point on the Cubic Bézier curve
67
+ *
68
+ * @param {number} t Normalized time value to interpolate
69
+ * @returns {Point} Interpolated coordinates on the curve
70
+ */
71
+ getPoint(t) {
72
+ const point = cubicBezier(t, this.x1, this.y1, this.cp1x, this.cp1y, this.cp2x, this.cp2y, this.x2, this.y2);
73
+ return point;
74
+ }
75
+ }
@@ -0,0 +1,95 @@
1
+ import type { Point } from 'src/types';
2
+ /**
3
+ * Utility abstract class for manipulating curves
4
+ *
5
+ * @exports
6
+ * @class Curve
7
+ * @abstract
8
+ */
9
+ export default abstract class Curve {
10
+ readonly type: string;
11
+ /**
12
+ * Amount of divisions when calculating the cumulative segment lengths of a curve
13
+ */
14
+ arcLengthDivisions: number;
15
+ /**
16
+ * Must be set to `true` if the curve parameters have changed
17
+ */
18
+ needsUpdate: boolean;
19
+ protected _cacheArcLengths: number[];
20
+ /**
21
+ * Interpolate a point on the curve
22
+ *
23
+ * @abstract
24
+ * @param {number} t Normalized time value to interpolate
25
+ * @returns {Point} Interpolated coordinates on the curve
26
+ */
27
+ abstract getPoint(t: number): Point;
28
+ /**
29
+ * Interpolate a point on the curve
30
+ *
31
+ * @param {number} u Normalized position value to interpolate
32
+ * @returns {Point} Interpolated coordinates on the curve
33
+ */
34
+ getPointAt(u: number): Point;
35
+ /**
36
+ * Compute the curve shape into an array of points
37
+ *
38
+ * @param {number} [divisions=5] Number of divisions
39
+ * @returns {Point[]}
40
+ */
41
+ getPoints(divisions?: number): Point[];
42
+ /**
43
+ * Compute the curve shape into an array of equi-spaced points across the entire curve
44
+ *
45
+ * @param {number} [divisions=5] Number of divisions
46
+ * @returns {Point[]}
47
+ */
48
+ getSpacedPoints(divisions?: number): Point[];
49
+ /**
50
+ * Compute the total arc length of the curve
51
+ *
52
+ * @returns {number}
53
+ */
54
+ getLength(): number;
55
+ /**
56
+ * Compute the cumulative segment lengths of the curve
57
+ *
58
+ * @param {number} [divisions=this.arcLengthDivisions] Number of divisions
59
+ * @returns {number[]}
60
+ */
61
+ getLengths(divisions?: number): number[];
62
+ /**
63
+ * Update the cached cumulative segment lengths
64
+ */
65
+ updateArcLengths(): void;
66
+ /**
67
+ * Re-map a normalized position value into normalized time
68
+ *
69
+ * @param {number} u Normalized position value to interpolate
70
+ * @param {number} [targetArcLength] Distance on the curve
71
+ * @returns {number} Updated interpolation value
72
+ */
73
+ getUtoTmapping(u: number, targetArcLength?: number): number;
74
+ /**
75
+ * Compute an unit vector tangent for the given normalized time value
76
+ *
77
+ * @param {number} t Normalized time value
78
+ * @returns {[number, number]} Tangent vector
79
+ */
80
+ getTangent(t: number): Point;
81
+ /**
82
+ * Compute an unit vector tangent for the given normalized position value
83
+ *
84
+ * @param {number} u Normalized position value
85
+ * @returns {[number, number]} Tangent vector
86
+ */
87
+ getTangentAt(u: number): [number, number];
88
+ /**
89
+ * Static method to check if given points are defining a closed curve
90
+ *
91
+ * @param {Point[]} points Points to check
92
+ * @returns {boolean} True if the curve is closed, false otherwise
93
+ */
94
+ static isClosed(points: Point[]): boolean;
95
+ }