@sarmal/core 0.9.7 → 0.10.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 (61) hide show
  1. package/dist/auto-init.cjs +171 -166
  2. package/dist/auto-init.cjs.map +1 -1
  3. package/dist/auto-init.d.cts +2 -1
  4. package/dist/auto-init.d.ts +2 -1
  5. package/dist/auto-init.js +170 -165
  6. package/dist/auto-init.js.map +1 -1
  7. package/dist/curves/artemis2.cjs +7 -10
  8. package/dist/curves/artemis2.d.cts +1 -1
  9. package/dist/curves/artemis2.d.ts +1 -1
  10. package/dist/curves/artemis2.js +6 -9
  11. package/dist/curves/astroid.cjs +4 -4
  12. package/dist/curves/astroid.d.cts +1 -1
  13. package/dist/curves/astroid.d.ts +1 -1
  14. package/dist/curves/astroid.js +3 -3
  15. package/dist/curves/deltoid.cjs +4 -4
  16. package/dist/curves/deltoid.d.cts +1 -1
  17. package/dist/curves/deltoid.d.ts +1 -1
  18. package/dist/curves/deltoid.js +3 -3
  19. package/dist/curves/epicycloid3.cjs +4 -4
  20. package/dist/curves/epicycloid3.d.cts +1 -1
  21. package/dist/curves/epicycloid3.d.ts +1 -1
  22. package/dist/curves/epicycloid3.js +3 -3
  23. package/dist/curves/epitrochoid7.cjs +5 -5
  24. package/dist/curves/epitrochoid7.d.cts +1 -1
  25. package/dist/curves/epitrochoid7.d.ts +1 -1
  26. package/dist/curves/epitrochoid7.js +4 -4
  27. package/dist/curves/index.cjs +28 -32
  28. package/dist/curves/index.d.cts +11 -11
  29. package/dist/curves/index.d.ts +11 -11
  30. package/dist/curves/index.js +28 -44
  31. package/dist/curves/lame.cjs +5 -6
  32. package/dist/curves/lame.d.cts +1 -1
  33. package/dist/curves/lame.d.ts +1 -1
  34. package/dist/curves/lame.js +4 -5
  35. package/dist/curves/lissajous32.cjs +4 -4
  36. package/dist/curves/lissajous32.d.cts +1 -1
  37. package/dist/curves/lissajous32.d.ts +1 -1
  38. package/dist/curves/lissajous32.js +3 -3
  39. package/dist/curves/lissajous43.cjs +4 -4
  40. package/dist/curves/lissajous43.d.cts +1 -1
  41. package/dist/curves/lissajous43.d.ts +1 -1
  42. package/dist/curves/lissajous43.js +3 -3
  43. package/dist/curves/rose3.cjs +4 -4
  44. package/dist/curves/rose3.d.cts +1 -1
  45. package/dist/curves/rose3.d.ts +1 -1
  46. package/dist/curves/rose3.js +3 -3
  47. package/dist/curves/rose5.cjs +4 -4
  48. package/dist/curves/rose5.d.cts +1 -1
  49. package/dist/curves/rose5.d.ts +1 -1
  50. package/dist/curves/rose5.js +3 -3
  51. package/dist/index.cjs +211 -283
  52. package/dist/index.cjs.map +1 -1
  53. package/dist/index.d.cts +31 -64
  54. package/dist/index.d.ts +31 -64
  55. package/dist/index.js +211 -300
  56. package/dist/index.js.map +1 -1
  57. package/dist/types-DcyISvnH.d.cts +230 -0
  58. package/dist/types-DcyISvnH.d.ts +230 -0
  59. package/package.json +2 -1
  60. package/dist/types-cR2xOewv.d.cts +0 -234
  61. package/dist/types-cR2xOewv.d.ts +0 -234
@@ -0,0 +1,230 @@
1
+ interface Point {
2
+ x: number;
3
+ y: number;
4
+ }
5
+ interface CurveDef {
6
+ name: string;
7
+ /**
8
+ * The parametric function that defines the curve shape.
9
+ * @param t Current position along the curve, in [0, period)
10
+ * @param time Elapsed wall-clock time in seconds — always increasing, never resets on period wrap
11
+ * @param params Named parameter overrides.
12
+ * Always `{}` today — reserved for the upcoming parameterized-curves feature.
13
+ * Do NOT remove this parameter; it is intentional forward-compatible plumbing.
14
+ */
15
+ fn: (t: number, time: number, params: Record<string, number>) => Point;
16
+ /**
17
+ * @default (Math.PI * 2)
18
+ */
19
+ period?: number;
20
+ /**
21
+ * @default 1
22
+ */
23
+ speed?: number;
24
+ /**
25
+ * To indicate a compatible library version when providing the curve definitions
26
+ * Intended for potential backwards compatibility scenarios and futureproofing
27
+ */
28
+ version?: number;
29
+ /**
30
+ * Skeleton rendering mode:
31
+ * - 'static': Skeleton is computed once at init from `fn(t, 0)` and cached
32
+ * - 'live': Skeleton is recomputed each frame using `fn(t, actualTime)` specifically for curves whose shape drifts *over time*
33
+ * @default "static"
34
+ */
35
+ skeleton?: "static" | "live";
36
+ /**
37
+ * An **override** function for computing a skeleton independent of `fn`
38
+ * If provided, this function is used instead of `fn` to sample the skeleton,
39
+ * and the result is cached just like like 'static' mode
40
+ * @param t The parametric time value from `0` to `period`
41
+ * @returns The point on the skeleton at time `t`
42
+ */
43
+ skeletonFn?: (t: number) => Point;
44
+ }
45
+ type JumpOptions = {
46
+ /**
47
+ * When true, clears the trail on jump. When false (default), the trail is left as-is.
48
+ * @default false
49
+ */
50
+ clearTrail?: boolean;
51
+ };
52
+ type SeekOptions = {
53
+ /**
54
+ * When true, the trail wraps around the period boundary,
55
+ * which results in a full trail even near `t=0`
56
+ * By default, the trail stops at `t=0`, which results in a partial trail near the start
57
+ * @default false
58
+ */
59
+ wrap?: boolean;
60
+ /**
61
+ * Time gap between each trail point (in same units as `t`)
62
+ * Smaller value means a trail that is more dense
63
+ * @default period / trailLength
64
+ */
65
+ step?: number;
66
+ };
67
+ type MorpStrategy = "raw" | "normalized";
68
+ type MorphOptions = {
69
+ /**
70
+ * Duration of the morph transition in milliseconds
71
+ * @default 300
72
+ */
73
+ duration?: number;
74
+ /**
75
+ * Strategy for lerping between curves with different periods:
76
+ * - 'normalized': maps `t` proportionally into each curve's period (smooth for all period ratios)
77
+ * - 'raw': uses the same `t` for both curves (can produce incoherent results for mismatched periods)
78
+ * @default 'normalized'
79
+ */
80
+ morphStrategy?: MorpStrategy;
81
+ };
82
+ interface Engine {
83
+ /**
84
+ * Advances the Sarmal simulation by the given delta time (dt) in seconds.
85
+ * Internally, this increases the simulation time `t` by `speed * dt`,
86
+ * wraps `t` at `period`, evaluates the curve's parametric function `fn(t)`,
87
+ * and appends the new point to the trail.
88
+ * Returns the pre-allocated trail buffer, which has the *same* reference every call
89
+ * ! Do not use `Array.length` to determine size
90
+ * @param deltaTime Delta time in seconds (typically frame time from **requestAnimationFrame** or similar)
91
+ */
92
+ tick(deltaTime: number): Array<Point>;
93
+ /**
94
+ * Number of valid points in the trail buffer returned by the last `tick()` call
95
+ * ! Use this instead of `trail.length`
96
+ */
97
+ readonly trailCount: number;
98
+ /**
99
+ * Resets the simulation state, by clearing the trail and reverting internal time `t` to 0.
100
+ * The next call to `tick` will start fresh from the beginning of the curve.
101
+ */
102
+ reset(): void;
103
+ /**
104
+ * Returns the *skeleton* of the curve.
105
+ * In technicality, it just represents the complete traversal of the curve over one full period,
106
+ * which is sampled at points from `t=0` to `t=period`
107
+ *
108
+ * For "static" skeletons, this returns the same array on every call
109
+ * For "live" skeletons, this returns a different array each frame
110
+ * For `skeletonFn` overrides, this returns the skeleton from that function, which is *always* static
111
+ *
112
+ * The number of sample points is automatically derived from the curve's period.
113
+ */
114
+ getSarmalSkeleton(): Array<Point>;
115
+ readonly isLiveSkeleton: boolean;
116
+ /**
117
+ * Instantly moves the head to position `t`. Does NOT update `actualTime`.
118
+ * Trail is left untouched by default — pass `clearTrail: true` to wipe it.
119
+ * Use for morphing mid-flight, raw scrubbing, or any time you don't need trail context.
120
+ * @param t The position to jump to (will be wrapped into [0, period))
121
+ */
122
+ jump(t: number, options?: JumpOptions): void;
123
+ /**
124
+ * Moves to `t` AND reconstructs the trail as if the animation naturally arrived there from `t=0`.
125
+ * Also updates `actualTime` to match. Trail is always rebuilt from scratch.
126
+ * Use for initialisation or any jump where you want the trail to look meaningful.
127
+ * @param t The position to seek to (will be wrapped into [0, period))
128
+ */
129
+ seek(t: number, options?: SeekOptions): void;
130
+ /**
131
+ * Begins a smooth transition from the current curve to `target`
132
+ * Saves the current curve as `curveA`, registers `target` as `curveB`, and resets `morphAlpha` to `0`
133
+ *
134
+ * If called while a morph is already in progress,
135
+ * the interpolated state is frozen and becomes the new `curveA`
136
+ * @param target The curve to transition to
137
+ * @param strategy 'normalized' maps t proportionally into each curve's period (default), 'raw' uses the same t
138
+ */
139
+ startMorph(target: CurveDef, strategy?: MorpStrategy): void;
140
+ /**
141
+ * Sets the interpolation amount between `curveA` and `curveB`.
142
+ * 0 = full curveA
143
+ * 1 = full curveB
144
+ * Called by the renderer each frame as `actualTime` advances
145
+ * @param alpha A value in [0, 1]
146
+ */
147
+ setMorphAlpha(alpha: number): void;
148
+ /**
149
+ * Finalises the morph: `curveB` becomes the new active curve and `morphAlpha` is reset to `null`
150
+ * ! Called by the renderer when alpha reaches `1`
151
+ */
152
+ completeMorph(): void;
153
+ /**
154
+ * Current interpolation progress between `curveA` and `curveB`
155
+ * `null` when no morph is in progress
156
+ */
157
+ readonly morphAlpha: number | null;
158
+ }
159
+ interface SarmalInstance {
160
+ start(): void;
161
+ stop(): void;
162
+ /** Resets the engine and clears the trail */
163
+ reset(): void;
164
+ /** Stops the animation and cleans up resources */
165
+ destroy(): void;
166
+ /**
167
+ * Instantly moves the head to position `t`. Does NOT update `actualTime`.
168
+ * Trail is left untouched by default — pass `clearTrail: true` to wipe it.
169
+ * Use for morphing mid-flight, raw scrubbing, or any time you don't need trail context.
170
+ * @param t The position to jump to (will be wrapped into [0, period))
171
+ */
172
+ jump(t: number, options?: JumpOptions): void;
173
+ /**
174
+ * Moves to `t` AND reconstructs the trail as if the animation naturally arrived there from `t=0`.
175
+ * Also updates `actualTime` to match. Trail is always rebuilt from scratch.
176
+ * Use for initialisation or any jump where you want the trail to look meaningful.
177
+ * @param t The position to seek to (will be wrapped into [0, period))
178
+ */
179
+ seek(t: number, options?: SeekOptions): void;
180
+ /**
181
+ * Smoothly transitions from the current curve to `target`.
182
+ * The trail naturally reflects the new curve as new points are added.
183
+ * @param target The curve to transition to
184
+ * @param options.duration How long the morph takes in milliseconds (default: 300)
185
+ * @param options.morphStrategy 'normalized' uses proportional t mapping (default), 'raw' uses same t
186
+ * @returns Promise that resolves when the morph is complete
187
+ */
188
+ morphTo(target: CurveDef, options?: MorphOptions): Promise<void>;
189
+ }
190
+ /**
191
+ * With 'gradient-animated' the colors cycle along the trail over time
192
+ */
193
+ type TrailStyle = "default" | "gradient-static" | "gradient-animated";
194
+ type PalettePreset = "bard" | "sunset" | "ocean" | "ice" | "fire" | "forest";
195
+ interface RendererOptions {
196
+ /** Target canvas element that will contain the Sarmal */
197
+ canvas: HTMLCanvasElement;
198
+ engine: Engine;
199
+ /**
200
+ * @default '#ffffff'
201
+ */
202
+ skeletonColor?: string;
203
+ /**
204
+ * @default '#ffffff'
205
+ */
206
+ trailColor?: string;
207
+ /**
208
+ * @default '#ffffff'
209
+ */
210
+ headColor?: string;
211
+ /** @default 4 */
212
+ headRadius?: number;
213
+ /**
214
+ * Trail rendering style
215
+ * @default 'default'
216
+ */
217
+ trailStyle?: TrailStyle;
218
+ /**
219
+ * Color palette for gradient trails
220
+ * Can be a preset name or custom array of hex colors.
221
+ * @default 'bard' for animated, 'ice' for static
222
+ */
223
+ palette?: PalettePreset | string[];
224
+ }
225
+ interface SarmalOptions extends Omit<RendererOptions, "canvas" | "engine"> {
226
+ /** @default 120 */
227
+ trailLength?: number;
228
+ }
229
+
230
+ export type { CurveDef as C, Engine as E, JumpOptions as J, PalettePreset as P, RendererOptions as R, SarmalInstance as S, TrailStyle as T, SarmalOptions as a, Point as b, SeekOptions as c };
@@ -0,0 +1,230 @@
1
+ interface Point {
2
+ x: number;
3
+ y: number;
4
+ }
5
+ interface CurveDef {
6
+ name: string;
7
+ /**
8
+ * The parametric function that defines the curve shape.
9
+ * @param t Current position along the curve, in [0, period)
10
+ * @param time Elapsed wall-clock time in seconds — always increasing, never resets on period wrap
11
+ * @param params Named parameter overrides.
12
+ * Always `{}` today — reserved for the upcoming parameterized-curves feature.
13
+ * Do NOT remove this parameter; it is intentional forward-compatible plumbing.
14
+ */
15
+ fn: (t: number, time: number, params: Record<string, number>) => Point;
16
+ /**
17
+ * @default (Math.PI * 2)
18
+ */
19
+ period?: number;
20
+ /**
21
+ * @default 1
22
+ */
23
+ speed?: number;
24
+ /**
25
+ * To indicate a compatible library version when providing the curve definitions
26
+ * Intended for potential backwards compatibility scenarios and futureproofing
27
+ */
28
+ version?: number;
29
+ /**
30
+ * Skeleton rendering mode:
31
+ * - 'static': Skeleton is computed once at init from `fn(t, 0)` and cached
32
+ * - 'live': Skeleton is recomputed each frame using `fn(t, actualTime)` specifically for curves whose shape drifts *over time*
33
+ * @default "static"
34
+ */
35
+ skeleton?: "static" | "live";
36
+ /**
37
+ * An **override** function for computing a skeleton independent of `fn`
38
+ * If provided, this function is used instead of `fn` to sample the skeleton,
39
+ * and the result is cached just like like 'static' mode
40
+ * @param t The parametric time value from `0` to `period`
41
+ * @returns The point on the skeleton at time `t`
42
+ */
43
+ skeletonFn?: (t: number) => Point;
44
+ }
45
+ type JumpOptions = {
46
+ /**
47
+ * When true, clears the trail on jump. When false (default), the trail is left as-is.
48
+ * @default false
49
+ */
50
+ clearTrail?: boolean;
51
+ };
52
+ type SeekOptions = {
53
+ /**
54
+ * When true, the trail wraps around the period boundary,
55
+ * which results in a full trail even near `t=0`
56
+ * By default, the trail stops at `t=0`, which results in a partial trail near the start
57
+ * @default false
58
+ */
59
+ wrap?: boolean;
60
+ /**
61
+ * Time gap between each trail point (in same units as `t`)
62
+ * Smaller value means a trail that is more dense
63
+ * @default period / trailLength
64
+ */
65
+ step?: number;
66
+ };
67
+ type MorpStrategy = "raw" | "normalized";
68
+ type MorphOptions = {
69
+ /**
70
+ * Duration of the morph transition in milliseconds
71
+ * @default 300
72
+ */
73
+ duration?: number;
74
+ /**
75
+ * Strategy for lerping between curves with different periods:
76
+ * - 'normalized': maps `t` proportionally into each curve's period (smooth for all period ratios)
77
+ * - 'raw': uses the same `t` for both curves (can produce incoherent results for mismatched periods)
78
+ * @default 'normalized'
79
+ */
80
+ morphStrategy?: MorpStrategy;
81
+ };
82
+ interface Engine {
83
+ /**
84
+ * Advances the Sarmal simulation by the given delta time (dt) in seconds.
85
+ * Internally, this increases the simulation time `t` by `speed * dt`,
86
+ * wraps `t` at `period`, evaluates the curve's parametric function `fn(t)`,
87
+ * and appends the new point to the trail.
88
+ * Returns the pre-allocated trail buffer, which has the *same* reference every call
89
+ * ! Do not use `Array.length` to determine size
90
+ * @param deltaTime Delta time in seconds (typically frame time from **requestAnimationFrame** or similar)
91
+ */
92
+ tick(deltaTime: number): Array<Point>;
93
+ /**
94
+ * Number of valid points in the trail buffer returned by the last `tick()` call
95
+ * ! Use this instead of `trail.length`
96
+ */
97
+ readonly trailCount: number;
98
+ /**
99
+ * Resets the simulation state, by clearing the trail and reverting internal time `t` to 0.
100
+ * The next call to `tick` will start fresh from the beginning of the curve.
101
+ */
102
+ reset(): void;
103
+ /**
104
+ * Returns the *skeleton* of the curve.
105
+ * In technicality, it just represents the complete traversal of the curve over one full period,
106
+ * which is sampled at points from `t=0` to `t=period`
107
+ *
108
+ * For "static" skeletons, this returns the same array on every call
109
+ * For "live" skeletons, this returns a different array each frame
110
+ * For `skeletonFn` overrides, this returns the skeleton from that function, which is *always* static
111
+ *
112
+ * The number of sample points is automatically derived from the curve's period.
113
+ */
114
+ getSarmalSkeleton(): Array<Point>;
115
+ readonly isLiveSkeleton: boolean;
116
+ /**
117
+ * Instantly moves the head to position `t`. Does NOT update `actualTime`.
118
+ * Trail is left untouched by default — pass `clearTrail: true` to wipe it.
119
+ * Use for morphing mid-flight, raw scrubbing, or any time you don't need trail context.
120
+ * @param t The position to jump to (will be wrapped into [0, period))
121
+ */
122
+ jump(t: number, options?: JumpOptions): void;
123
+ /**
124
+ * Moves to `t` AND reconstructs the trail as if the animation naturally arrived there from `t=0`.
125
+ * Also updates `actualTime` to match. Trail is always rebuilt from scratch.
126
+ * Use for initialisation or any jump where you want the trail to look meaningful.
127
+ * @param t The position to seek to (will be wrapped into [0, period))
128
+ */
129
+ seek(t: number, options?: SeekOptions): void;
130
+ /**
131
+ * Begins a smooth transition from the current curve to `target`
132
+ * Saves the current curve as `curveA`, registers `target` as `curveB`, and resets `morphAlpha` to `0`
133
+ *
134
+ * If called while a morph is already in progress,
135
+ * the interpolated state is frozen and becomes the new `curveA`
136
+ * @param target The curve to transition to
137
+ * @param strategy 'normalized' maps t proportionally into each curve's period (default), 'raw' uses the same t
138
+ */
139
+ startMorph(target: CurveDef, strategy?: MorpStrategy): void;
140
+ /**
141
+ * Sets the interpolation amount between `curveA` and `curveB`.
142
+ * 0 = full curveA
143
+ * 1 = full curveB
144
+ * Called by the renderer each frame as `actualTime` advances
145
+ * @param alpha A value in [0, 1]
146
+ */
147
+ setMorphAlpha(alpha: number): void;
148
+ /**
149
+ * Finalises the morph: `curveB` becomes the new active curve and `morphAlpha` is reset to `null`
150
+ * ! Called by the renderer when alpha reaches `1`
151
+ */
152
+ completeMorph(): void;
153
+ /**
154
+ * Current interpolation progress between `curveA` and `curveB`
155
+ * `null` when no morph is in progress
156
+ */
157
+ readonly morphAlpha: number | null;
158
+ }
159
+ interface SarmalInstance {
160
+ start(): void;
161
+ stop(): void;
162
+ /** Resets the engine and clears the trail */
163
+ reset(): void;
164
+ /** Stops the animation and cleans up resources */
165
+ destroy(): void;
166
+ /**
167
+ * Instantly moves the head to position `t`. Does NOT update `actualTime`.
168
+ * Trail is left untouched by default — pass `clearTrail: true` to wipe it.
169
+ * Use for morphing mid-flight, raw scrubbing, or any time you don't need trail context.
170
+ * @param t The position to jump to (will be wrapped into [0, period))
171
+ */
172
+ jump(t: number, options?: JumpOptions): void;
173
+ /**
174
+ * Moves to `t` AND reconstructs the trail as if the animation naturally arrived there from `t=0`.
175
+ * Also updates `actualTime` to match. Trail is always rebuilt from scratch.
176
+ * Use for initialisation or any jump where you want the trail to look meaningful.
177
+ * @param t The position to seek to (will be wrapped into [0, period))
178
+ */
179
+ seek(t: number, options?: SeekOptions): void;
180
+ /**
181
+ * Smoothly transitions from the current curve to `target`.
182
+ * The trail naturally reflects the new curve as new points are added.
183
+ * @param target The curve to transition to
184
+ * @param options.duration How long the morph takes in milliseconds (default: 300)
185
+ * @param options.morphStrategy 'normalized' uses proportional t mapping (default), 'raw' uses same t
186
+ * @returns Promise that resolves when the morph is complete
187
+ */
188
+ morphTo(target: CurveDef, options?: MorphOptions): Promise<void>;
189
+ }
190
+ /**
191
+ * With 'gradient-animated' the colors cycle along the trail over time
192
+ */
193
+ type TrailStyle = "default" | "gradient-static" | "gradient-animated";
194
+ type PalettePreset = "bard" | "sunset" | "ocean" | "ice" | "fire" | "forest";
195
+ interface RendererOptions {
196
+ /** Target canvas element that will contain the Sarmal */
197
+ canvas: HTMLCanvasElement;
198
+ engine: Engine;
199
+ /**
200
+ * @default '#ffffff'
201
+ */
202
+ skeletonColor?: string;
203
+ /**
204
+ * @default '#ffffff'
205
+ */
206
+ trailColor?: string;
207
+ /**
208
+ * @default '#ffffff'
209
+ */
210
+ headColor?: string;
211
+ /** @default 4 */
212
+ headRadius?: number;
213
+ /**
214
+ * Trail rendering style
215
+ * @default 'default'
216
+ */
217
+ trailStyle?: TrailStyle;
218
+ /**
219
+ * Color palette for gradient trails
220
+ * Can be a preset name or custom array of hex colors.
221
+ * @default 'bard' for animated, 'ice' for static
222
+ */
223
+ palette?: PalettePreset | string[];
224
+ }
225
+ interface SarmalOptions extends Omit<RendererOptions, "canvas" | "engine"> {
226
+ /** @default 120 */
227
+ trailLength?: number;
228
+ }
229
+
230
+ export type { CurveDef as C, Engine as E, JumpOptions as J, PalettePreset as P, RendererOptions as R, SarmalInstance as S, TrailStyle as T, SarmalOptions as a, Point as b, SeekOptions as c };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sarmal/core",
3
- "version": "0.9.7",
3
+ "version": "0.10.0",
4
4
  "description": "Curve path based loading indicators and their renderer",
5
5
  "keywords": [
6
6
  "animation",
@@ -70,6 +70,7 @@
70
70
  },
71
71
  "devDependencies": {
72
72
  "@types/node": "^25.5.2",
73
+ "jsdom": "^29.0.2",
73
74
  "oxfmt": "^0.44.0",
74
75
  "oxlint": "^1.58.0",
75
76
  "tsup": "^8.5.1",