@sarmal/core 0.5.0 → 0.6.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.
- package/dist/auto-init.cjs +216 -60
- package/dist/auto-init.cjs.map +1 -1
- package/dist/auto-init.d.cts +1 -2
- package/dist/auto-init.d.ts +1 -2
- package/dist/auto-init.js +215 -59
- package/dist/auto-init.js.map +1 -1
- package/dist/index.cjs +326 -58
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +228 -163
- package/dist/index.d.ts +228 -163
- package/dist/index.js +325 -57
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,171 +1,223 @@
|
|
|
1
1
|
interface Point {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
4
|
}
|
|
5
5
|
interface CurveDef {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
6
|
+
name: string;
|
|
7
|
+
fn: (t: number, time: number, params: Record<string, number>) => Point;
|
|
8
|
+
/**
|
|
9
|
+
* @default (Math.PI * 2)
|
|
10
|
+
*/
|
|
11
|
+
period?: number;
|
|
12
|
+
/**
|
|
13
|
+
* @default 1
|
|
14
|
+
*/
|
|
15
|
+
speed?: number;
|
|
16
|
+
/**
|
|
17
|
+
* To indicate a compatible library version when providing the curve definitions
|
|
18
|
+
* Intended for potential backwards compatibility scenarios and futureproofing
|
|
19
|
+
*/
|
|
20
|
+
version?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Skeleton rendering mode:
|
|
23
|
+
* - 'static': Skeleton is computed once at init from `fn(t, 0)` and cached
|
|
24
|
+
* - 'live': Skeleton is recomputed each frame using `fn(t, actualTime)` specifically for curves whose shape drifts *over time*
|
|
25
|
+
* @default "static"
|
|
26
|
+
*/
|
|
27
|
+
skeleton?: "static" | "live";
|
|
28
|
+
/**
|
|
29
|
+
* An **override** function for computing a skeleton independent of `fn`
|
|
30
|
+
* If provided, this function is used instead of `fn` to sample the skeleton,
|
|
31
|
+
* and the result is cached just like like 'static' mode
|
|
32
|
+
* @param t The parametric time value from `0` to `period`
|
|
33
|
+
* @returns The point on the skeleton at time `t`
|
|
34
|
+
*/
|
|
35
|
+
skeletonFn?: (t: number) => Point;
|
|
36
36
|
}
|
|
37
37
|
type SeekOptions = {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Decides whether the sarmal trail should be cleared when `seek` is called
|
|
40
|
+
*/
|
|
41
|
+
clearTrail?: boolean;
|
|
42
42
|
};
|
|
43
43
|
type SeekWithTrailOptions = {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
44
|
+
/**
|
|
45
|
+
* When true, the trail wraps around the period boundary,
|
|
46
|
+
* which results in a full trail even near `t=0`
|
|
47
|
+
* By default, the trail stops at `t=0`, which results in a partial trail near the start
|
|
48
|
+
* @default false
|
|
49
|
+
*/
|
|
50
|
+
wrap?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Time gap between each trail point (in same units as `t`)
|
|
53
|
+
* Smaller value means a trail that is more dense
|
|
54
|
+
* @default period / trailLength
|
|
55
|
+
*/
|
|
56
|
+
step?: number;
|
|
57
|
+
};
|
|
58
|
+
type MorpStrategy = "raw" | "normalized";
|
|
59
|
+
type MorphOptions = {
|
|
60
|
+
/**
|
|
61
|
+
* Duration of the morph transition in milliseconds
|
|
62
|
+
* @default 300
|
|
63
|
+
*/
|
|
64
|
+
duration?: number;
|
|
65
|
+
/**
|
|
66
|
+
* Strategy for lerping between curves with different periods:
|
|
67
|
+
* - 'normalized': maps `t` proportionally into each curve's period (smooth for all period ratios)
|
|
68
|
+
* - 'raw': uses the same `t` for both curves (can produce incoherent results for mismatched periods)
|
|
69
|
+
* @default 'normalized'
|
|
70
|
+
*/
|
|
71
|
+
morphStrategy?: MorpStrategy;
|
|
57
72
|
};
|
|
58
73
|
interface Engine {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Advances the Sarmal simulation by the given delta time (dt) in seconds.
|
|
76
|
+
* Internally, this increases the simulation time `t` by `speed * dt`,
|
|
77
|
+
* wraps `t` at `period`, evaluates the curve's parametric function `fn(t)`,
|
|
78
|
+
* and appends the new point to the trail.
|
|
79
|
+
* Returns the pre-allocated trail buffer, which has the *same* reference every call
|
|
80
|
+
* ! Do not use `Array.length` to determine size
|
|
81
|
+
* @param deltaTime Delta time in seconds (typically frame time from **requestAnimationFrame** or similar)
|
|
82
|
+
*/
|
|
83
|
+
tick(deltaTime: number): Array<Point>;
|
|
84
|
+
/**
|
|
85
|
+
* Number of valid points in the trail buffer returned by the last `tick()` call
|
|
86
|
+
* ! Use this instead of `trail.length`
|
|
87
|
+
*/
|
|
88
|
+
readonly trailCount: number;
|
|
89
|
+
/**
|
|
90
|
+
* Resets the simulation state, by clearing the trail and reverting internal time `t` to 0.
|
|
91
|
+
* The next call to `tick` will start fresh from the beginning of the curve.
|
|
92
|
+
*/
|
|
93
|
+
reset(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the *skeleton* of the curve.
|
|
96
|
+
* In technicality, it just represents the complete traversal of the curve over one full period,
|
|
97
|
+
* which is sampled at points from `t=0` to `t=period`
|
|
98
|
+
*
|
|
99
|
+
* For "static" skeletons, this returns the same array on every call
|
|
100
|
+
* For "live" skeletons, this returns a different array each frame
|
|
101
|
+
* For `skeletonFn` overrides, this returns the skeleton from that function, which is *always* static
|
|
102
|
+
*
|
|
103
|
+
* The number of sample points is automatically derived from the curve's period.
|
|
104
|
+
*/
|
|
105
|
+
getSarmalSkeleton(): Array<Point>;
|
|
106
|
+
readonly isLiveSkeleton: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Sets the simulation time `t` directly to the specified value.
|
|
109
|
+
* By default, the trail is preserved
|
|
110
|
+
* @param t The time value to seek to (will be wrapped into [0, period))
|
|
111
|
+
*/
|
|
112
|
+
seek(t: number, options?: SeekOptions): void;
|
|
113
|
+
/**
|
|
114
|
+
* Seeks to `t` and rebuilds the trail as if the animation naturally arrived there from `t=0`
|
|
115
|
+
* @param t The time value to seek to (will be wrapped into [0, period))
|
|
116
|
+
*/
|
|
117
|
+
seekWithTrail(t: number, options?: SeekWithTrailOptions): void;
|
|
118
|
+
/**
|
|
119
|
+
* Begins a smooth transition from the current curve to `target`
|
|
120
|
+
* Saves the current curve as `curveA`, registers `target` as `curveB`, and resets `morphAlpha` to `0`
|
|
121
|
+
*
|
|
122
|
+
* If called while a morph is already in progress,
|
|
123
|
+
* the interpolated state is frozen and becomes the new `curveA`
|
|
124
|
+
* @param target The curve to transition to
|
|
125
|
+
* @param strategy 'normalized' maps t proportionally into each curve's period (default), 'raw' uses the same t
|
|
126
|
+
*/
|
|
127
|
+
startMorph(target: CurveDef, strategy?: MorpStrategy): void;
|
|
128
|
+
/**
|
|
129
|
+
* Sets the interpolation amount between `curveA` and `curveB`.
|
|
130
|
+
* 0 = full curveA
|
|
131
|
+
* 1 = full curveB
|
|
132
|
+
* Called by the renderer each frame as `actualTime` advances
|
|
133
|
+
* @param alpha A value in [0, 1]
|
|
134
|
+
*/
|
|
135
|
+
setMorphAlpha(alpha: number): void;
|
|
136
|
+
/**
|
|
137
|
+
* Finalises the morph: `curveB` becomes the new active curve and `morphAlpha` is reset to `null`
|
|
138
|
+
* ! Called by the renderer when alpha reaches `1`
|
|
139
|
+
*/
|
|
140
|
+
completeMorph(): void;
|
|
141
|
+
/**
|
|
142
|
+
* Current interpolation progress between `curveA` and `curveB`
|
|
143
|
+
* `null` when no morph is in progress
|
|
144
|
+
*/
|
|
145
|
+
readonly morphAlpha: number | null;
|
|
103
146
|
}
|
|
104
147
|
interface SarmalInstance {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
148
|
+
start(): void;
|
|
149
|
+
stop(): void;
|
|
150
|
+
/** Resets the engine and clears the trail */
|
|
151
|
+
reset(): void;
|
|
152
|
+
/** Stops the animation and cleans up resources */
|
|
153
|
+
destroy(): void;
|
|
154
|
+
/**
|
|
155
|
+
* Sets the simulation time `t` directly to the specified value.
|
|
156
|
+
* By default, the trail is preserved
|
|
157
|
+
* @param t The time value to seek to (will be wrapped into [0, period))
|
|
158
|
+
*/
|
|
159
|
+
seek(t: number, options?: SeekOptions): void;
|
|
160
|
+
/**
|
|
161
|
+
* Seeks to `t` and rebuilds the trail as if the animation naturally arrived there from `t=0`
|
|
162
|
+
* @param t The time value to seek to (will be wrapped into [0, period))
|
|
163
|
+
*/
|
|
164
|
+
seekWithTrail(t: number, options?: SeekWithTrailOptions): void;
|
|
165
|
+
/**
|
|
166
|
+
* Smoothly transitions from the current curve to `target`.
|
|
167
|
+
* The trail naturally reflects the new curve as new points are added.
|
|
168
|
+
* @param target The curve to transition to
|
|
169
|
+
* @param options.duration How long the morph takes in milliseconds (default: 300)
|
|
170
|
+
* @param options.morphStrategy 'normalized' uses proportional t mapping (default), 'raw' uses same t
|
|
171
|
+
* @returns Promise that resolves when the morph is complete
|
|
172
|
+
*/
|
|
173
|
+
morphTo(target: CurveDef, options?: MorphOptions): Promise<void>;
|
|
122
174
|
}
|
|
123
175
|
interface RendererOptions {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
176
|
+
/** Target canvas element that will contain the Sarmal */
|
|
177
|
+
canvas: HTMLCanvasElement;
|
|
178
|
+
engine: Engine;
|
|
179
|
+
/**
|
|
180
|
+
* @default '#ffffff'
|
|
181
|
+
*/
|
|
182
|
+
skeletonColor?: string;
|
|
183
|
+
/**
|
|
184
|
+
* @default '#ffffff'
|
|
185
|
+
*/
|
|
186
|
+
trailColor?: string;
|
|
187
|
+
/**
|
|
188
|
+
* @default '#ffffff'
|
|
189
|
+
*/
|
|
190
|
+
headColor?: string;
|
|
191
|
+
/** @default 4 */
|
|
192
|
+
headRadius?: number;
|
|
193
|
+
/** @default 20 */
|
|
194
|
+
glowSize?: number;
|
|
143
195
|
}
|
|
144
196
|
interface SarmalOptions extends Omit<RendererOptions, "canvas" | "engine"> {
|
|
145
|
-
|
|
146
|
-
|
|
197
|
+
/** @default 120 */
|
|
198
|
+
trailLength?: number;
|
|
147
199
|
}
|
|
148
200
|
|
|
149
201
|
interface SVGRendererOptions {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
202
|
+
/** Container element that will contain the SVG */
|
|
203
|
+
container: Element;
|
|
204
|
+
engine: Engine;
|
|
205
|
+
/** @default '#ffffff' */
|
|
206
|
+
skeletonColor?: string;
|
|
207
|
+
/** @default '#ffffff' */
|
|
208
|
+
trailColor?: string;
|
|
209
|
+
/** @default '#ffffff' */
|
|
210
|
+
headColor?: string;
|
|
211
|
+
/** @default 4 */
|
|
212
|
+
headRadius?: number;
|
|
213
|
+
/** @default 20 */
|
|
214
|
+
glowSize?: number;
|
|
215
|
+
/** @default 'Loading' */
|
|
216
|
+
ariaLabel?: string;
|
|
165
217
|
}
|
|
166
218
|
interface SVGSarmalOptions extends Omit<SVGRendererOptions, "container" | "engine"> {
|
|
167
|
-
|
|
168
|
-
|
|
219
|
+
/** @default 120 */
|
|
220
|
+
trailLength?: number;
|
|
169
221
|
}
|
|
170
222
|
/**
|
|
171
223
|
* Creates a live SVG renderer for sarmal animations
|
|
@@ -183,20 +235,12 @@ declare function createSVGRenderer(options: SVGRendererOptions): SarmalInstance;
|
|
|
183
235
|
* sarmal.start()
|
|
184
236
|
* ```
|
|
185
237
|
*/
|
|
186
|
-
declare function createSarmalSVG(
|
|
238
|
+
declare function createSarmalSVG(
|
|
239
|
+
container: Element,
|
|
240
|
+
curveDef: CurveDef,
|
|
241
|
+
options?: SVGSarmalOptions,
|
|
242
|
+
): SarmalInstance;
|
|
187
243
|
|
|
188
|
-
/**
|
|
189
|
-
* Creates the core simulation engine for a sarmal
|
|
190
|
-
*
|
|
191
|
-
* it runs a clock (time `t`), asks the curve for the current Point position at that time,
|
|
192
|
-
* and remembers the last N positions so the renderer can draw the trail
|
|
193
|
-
*
|
|
194
|
-
* The engine is only responsible for math coordinates,
|
|
195
|
-
* so it is not responsible for drawing or colors
|
|
196
|
-
*
|
|
197
|
-
* @param curveDef A curve definition
|
|
198
|
-
* @param trailLength default: `120`
|
|
199
|
-
*/
|
|
200
244
|
declare function createEngine(curveDef: CurveDef, trailLength?: number): Engine;
|
|
201
245
|
|
|
202
246
|
/**
|
|
@@ -217,6 +261,27 @@ declare const curves: Record<string, CurveDef>;
|
|
|
217
261
|
* sarmal.start()
|
|
218
262
|
* ```
|
|
219
263
|
*/
|
|
220
|
-
declare function createSarmal(
|
|
264
|
+
declare function createSarmal(
|
|
265
|
+
canvas: HTMLCanvasElement,
|
|
266
|
+
curveDef: CurveDef,
|
|
267
|
+
options?: SarmalOptions,
|
|
268
|
+
): SarmalInstance;
|
|
221
269
|
|
|
222
|
-
export {
|
|
270
|
+
export {
|
|
271
|
+
type CurveDef,
|
|
272
|
+
type Engine,
|
|
273
|
+
type Point,
|
|
274
|
+
type RendererOptions,
|
|
275
|
+
type SVGRendererOptions,
|
|
276
|
+
type SVGSarmalOptions,
|
|
277
|
+
type SarmalInstance,
|
|
278
|
+
type SarmalOptions,
|
|
279
|
+
type SeekOptions,
|
|
280
|
+
type SeekWithTrailOptions,
|
|
281
|
+
createEngine,
|
|
282
|
+
createRenderer,
|
|
283
|
+
createSVGRenderer,
|
|
284
|
+
createSarmal,
|
|
285
|
+
createSarmalSVG,
|
|
286
|
+
curves,
|
|
287
|
+
};
|