motlab.js 0.0.0-stage → 0.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 (108) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/LICENSE +21 -0
  3. package/README.md +86 -2
  4. package/docs/stability.md +53 -0
  5. package/llms.txt +794 -0
  6. package/package.json +107 -6
  7. package/src/adapters/canvas.js +68 -0
  8. package/src/adapters/lottie.js +102 -0
  9. package/src/adapters/pixi.js +62 -0
  10. package/src/adapters/rapier.js +163 -0
  11. package/src/adapters/rive.js +316 -0
  12. package/src/adapters/three.js +81 -0
  13. package/src/addons/audio.js +422 -0
  14. package/src/addons/camera.js +319 -0
  15. package/src/addons/cel.js +498 -0
  16. package/src/addons/clone.js +299 -0
  17. package/src/addons/color.js +223 -0
  18. package/src/addons/deform.js +441 -0
  19. package/src/addons/dom.js +68 -0
  20. package/src/addons/edit.js +317 -0
  21. package/src/addons/fx.js +64 -0
  22. package/src/addons/game.js +106 -0
  23. package/src/addons/input.js +209 -0
  24. package/src/addons/machine.js +240 -0
  25. package/src/addons/media.js +581 -0
  26. package/src/addons/particles.js +179 -0
  27. package/src/addons/path.js +596 -0
  28. package/src/addons/physics.js +980 -0
  29. package/src/addons/player.js +170 -0
  30. package/src/addons/rig.js +851 -0
  31. package/src/addons/text.js +133 -0
  32. package/src/addons/trace.js +456 -0
  33. package/src/core/combinators.js +129 -0
  34. package/src/core/component.js +90 -0
  35. package/src/core/dmath.js +256 -0
  36. package/src/core/ease.js +136 -0
  37. package/src/core/guard.js +139 -0
  38. package/src/core/hash.js +74 -0
  39. package/src/core/markers.js +49 -0
  40. package/src/core/math.js +43 -0
  41. package/src/core/nodes.js +328 -0
  42. package/src/core/noise.js +340 -0
  43. package/src/core/path.js +295 -0
  44. package/src/core/rand.js +135 -0
  45. package/src/core/scene.js +678 -0
  46. package/src/core/simulate.js +289 -0
  47. package/src/core/spring.js +129 -0
  48. package/src/core/timeline.js +193 -0
  49. package/src/core/track.js +258 -0
  50. package/src/core/verify.js +208 -0
  51. package/src/core/version.js +2 -0
  52. package/src/index.js +19 -0
  53. package/src/renderer/canvas2d.js +950 -0
  54. package/src/renderer/pixels.js +57 -0
  55. package/src/renderer/svg.js +589 -0
  56. package/src/renderer/text-layout.js +80 -0
  57. package/src/renderer/webgl2.js +1331 -0
  58. package/types/adapters/canvas.d.ts +46 -0
  59. package/types/adapters/lottie.d.ts +45 -0
  60. package/types/adapters/pixi.d.ts +46 -0
  61. package/types/adapters/rapier.d.ts +116 -0
  62. package/types/adapters/rive.d.ts +62 -0
  63. package/types/adapters/three.d.ts +51 -0
  64. package/types/addons/audio.d.ts +124 -0
  65. package/types/addons/camera.d.ts +134 -0
  66. package/types/addons/cel.d.ts +215 -0
  67. package/types/addons/clone.d.ts +167 -0
  68. package/types/addons/color.d.ts +73 -0
  69. package/types/addons/deform.d.ts +112 -0
  70. package/types/addons/dom.d.ts +68 -0
  71. package/types/addons/edit.d.ts +293 -0
  72. package/types/addons/fx.d.ts +65 -0
  73. package/types/addons/game.d.ts +78 -0
  74. package/types/addons/input.d.ts +56 -0
  75. package/types/addons/machine.d.ts +111 -0
  76. package/types/addons/media.d.ts +117 -0
  77. package/types/addons/particles.d.ts +110 -0
  78. package/types/addons/path.d.ts +61 -0
  79. package/types/addons/physics.d.ts +265 -0
  80. package/types/addons/player.d.ts +48 -0
  81. package/types/addons/rig.d.ts +273 -0
  82. package/types/addons/text.d.ts +63 -0
  83. package/types/addons/trace.d.ts +110 -0
  84. package/types/core/combinators.d.ts +73 -0
  85. package/types/core/component.d.ts +50 -0
  86. package/types/core/dmath.d.ts +52 -0
  87. package/types/core/ease.d.ts +50 -0
  88. package/types/core/guard.d.ts +23 -0
  89. package/types/core/hash.d.ts +6 -0
  90. package/types/core/markers.d.ts +52 -0
  91. package/types/core/math.d.ts +38 -0
  92. package/types/core/nodes.d.ts +442 -0
  93. package/types/core/noise.d.ts +65 -0
  94. package/types/core/path.d.ts +82 -0
  95. package/types/core/rand.d.ts +47 -0
  96. package/types/core/scene.d.ts +189 -0
  97. package/types/core/simulate.d.ts +169 -0
  98. package/types/core/spring.d.ts +41 -0
  99. package/types/core/timeline.d.ts +86 -0
  100. package/types/core/track.d.ts +58 -0
  101. package/types/core/verify.d.ts +42 -0
  102. package/types/core/version.d.ts +2 -0
  103. package/types/index.d.ts +18 -0
  104. package/types/renderer/canvas2d.d.ts +184 -0
  105. package/types/renderer/pixels.d.ts +28 -0
  106. package/types/renderer/svg.d.ts +19 -0
  107. package/types/renderer/text-layout.d.ts +45 -0
  108. package/types/renderer/webgl2.d.ts +71 -0
@@ -0,0 +1,293 @@
1
+ export type Node = import('../core/nodes.js').Node;
2
+ export type Clip = {
3
+ kind: 'clip';
4
+ media: string;
5
+ in: number;
6
+ duration: number;
7
+ name?: string;
8
+ };
9
+ export type Gap = {
10
+ kind: 'gap';
11
+ duration: number;
12
+ };
13
+ export type Item = Clip | Gap;
14
+ export type Track = {
15
+ kind: 'video' | 'audio';
16
+ items: readonly Item[];
17
+ };
18
+ export type Edit = {
19
+ fps: number;
20
+ tracks: readonly Track[];
21
+ /**
22
+ * Source lengths in seconds, for checking ranges.
23
+ */
24
+ media: Readonly<Record<string, {
25
+ duration: number;
26
+ }>>;
27
+ /**
28
+ * The longest track, in frames.
29
+ */
30
+ frames: number;
31
+ /**
32
+ * The same in seconds.
33
+ */
34
+ duration: number;
35
+ /**
36
+ * What a track shows at scene time t; null in a gap or
37
+ * past the end.
38
+ */
39
+ at: (t: number, track?: number) => Shot | null;
40
+ };
41
+ export type Shot = {
42
+ /**
43
+ * Index of the clip in its track.
44
+ */
45
+ item: number;
46
+ media: string;
47
+ /**
48
+ * Seconds into the media: the frame to show.
49
+ */
50
+ source: number;
51
+ /**
52
+ * When the clip starts on the edit, in seconds.
53
+ */
54
+ start: number;
55
+ /**
56
+ * Seconds since the clip started.
57
+ */
58
+ local: number;
59
+ };
60
+ /** @typedef {import('../core/nodes.js').Node} Node */
61
+ /** @typedef {{ kind: 'clip', media: string, in: number, duration: number, name?: string }} Clip In and duration in frames. */
62
+ /** @typedef {{ kind: 'gap', duration: number }} Gap */
63
+ /** @typedef {Clip | Gap} Item */
64
+ /** @typedef {{ kind: 'video' | 'audio', items: readonly Item[] }} Track */
65
+ /**
66
+ * @typedef {object} Edit
67
+ * @property {number} fps
68
+ * @property {readonly Track[]} tracks
69
+ * @property {Readonly<Record<string, { duration: number }>>} media Source lengths in seconds, for checking ranges.
70
+ * @property {number} frames The longest track, in frames.
71
+ * @property {number} duration The same in seconds.
72
+ * @property {(t: number, track?: number) => Shot | null} at What a track shows at scene time t; null in a gap or
73
+ * past the end.
74
+ */
75
+ /**
76
+ * @typedef {object} Shot
77
+ * @property {number} item Index of the clip in its track.
78
+ * @property {string} media
79
+ * @property {number} source Seconds into the media: the frame to show.
80
+ * @property {number} start When the clip starts on the edit, in seconds.
81
+ * @property {number} local Seconds since the clip started.
82
+ */
83
+ /**
84
+ * A clip of media from `in` for `duration`, both in seconds of the source; the edit rounds them to whole frames.
85
+ * @param {{ media: string, in?: number, duration: number, name?: string }} spec
86
+ */
87
+ export declare const clip: ({ media, in: from, duration, name }: {
88
+ media: string;
89
+ in?: number;
90
+ duration: number;
91
+ name?: string;
92
+ }) => Readonly<{
93
+ kind: "clip";
94
+ media: string;
95
+ in: number;
96
+ duration: number;
97
+ name?: string | undefined;
98
+ }>;
99
+ /** Empty time on a track, in seconds. @param {number} duration */
100
+ export declare const gap: (duration: number) => Readonly<{
101
+ kind: "gap";
102
+ duration: number;
103
+ }>;
104
+ /**
105
+ * Clips back to back from source ranges, such as the cuts an agent picked from a transcript.
106
+ * @param {string} media
107
+ * @param {ReadonlyArray<readonly [number, number]>} ranges [in, out] pairs in source seconds.
108
+ */
109
+ export declare const cuts: (media: string, ranges: ReadonlyArray<readonly [number, number]>) => Readonly<{
110
+ kind: "clip";
111
+ media: string;
112
+ in: number;
113
+ duration: number;
114
+ name?: string | undefined;
115
+ }>[];
116
+ export type SpecItem = ReturnType<typeof clip> | ReturnType<typeof gap>;
117
+ export type TrackSpec = {
118
+ kind?: 'video' | 'audio';
119
+ items: ReadonlyArray<SpecItem>;
120
+ };
121
+ /** @typedef {ReturnType<typeof clip> | ReturnType<typeof gap>} SpecItem Seconds, before the edit rounds to frames. */
122
+ /** @typedef {{ kind?: 'video' | 'audio', items: ReadonlyArray<SpecItem> }} TrackSpec */
123
+ /**
124
+ * @param {{ fps: number, tracks: ReadonlyArray<ReadonlyArray<SpecItem> | TrackSpec>,
125
+ * media?: Record<string, { duration: number }> }} spec Each track is a list of clips and gaps, or { kind, items }
126
+ * for an audio track. With media lengths, ranges past the end of their media are refused.
127
+ * @returns {Edit}
128
+ */
129
+ export declare function edit({ fps, tracks, media }: {
130
+ fps: number;
131
+ tracks: ReadonlyArray<ReadonlyArray<SpecItem> | TrackSpec>;
132
+ media?: Record<string, {
133
+ duration: number;
134
+ }>;
135
+ }): Edit;
136
+ /**
137
+ * Cuts the clip under scene time `at` into two, as a blade does.
138
+ * @param {Edit} e @param {{ at: number, track?: number }} options
139
+ */
140
+ export declare function split(e: Edit, { at, track }: {
141
+ at: number;
142
+ track?: number;
143
+ }): Edit;
144
+ /**
145
+ * Ripple trim: moves one edge of an item by `by` frames and shifts everything after it, so the track gets longer or
146
+ * shorter. A positive `by` on the in edge trims the start away; on the out edge it extends the end.
147
+ * @param {Edit} e @param {{ item: number, edge: 'in' | 'out', by: number, track?: number }} options
148
+ */
149
+ export declare function ripple(e: Edit, { item, edge, by, track }: {
150
+ item: number;
151
+ edge: 'in' | 'out';
152
+ by: number;
153
+ track?: number;
154
+ }): Edit;
155
+ /**
156
+ * Roll: moves the cut between item and the next by `by` frames. One gets longer and the other shorter, so the track
157
+ * keeps its length.
158
+ * @param {Edit} e @param {{ item: number, by: number, track?: number }} options
159
+ */
160
+ export declare function roll(e: Edit, { item, by, track }: {
161
+ item: number;
162
+ by: number;
163
+ track?: number;
164
+ }): Edit;
165
+ /**
166
+ * Slip: shows a different part of the media in the same place and for the same length.
167
+ * @param {Edit} e @param {{ item: number, by: number, track?: number }} options
168
+ */
169
+ export declare function slip(e: Edit, { item, by, track }: {
170
+ item: number;
171
+ by: number;
172
+ track?: number;
173
+ }): Edit;
174
+ /**
175
+ * Slide: moves a clip along the track by `by` frames, keeping its content. The item before gets longer and the one
176
+ * after shorter (or the other way), so the track keeps its length.
177
+ * @param {Edit} e @param {{ item: number, by: number, track?: number }} options
178
+ */
179
+ export declare function slide(e: Edit, { item, by, track }: {
180
+ item: number;
181
+ by: number;
182
+ track?: number;
183
+ }): Edit;
184
+ /**
185
+ * A video node that shows a track of the edit: the right media at the right frame, and nothing in gaps.
186
+ * @param {Edit} e
187
+ * @param {import('../core/nodes.js').CommonProps & { track?: number, w?: number, h?: number }} [props]
188
+ * @returns {Node}
189
+ */
190
+ export declare function footage(e: Edit, { track, ...props }?: import('../core/nodes.js').CommonProps & {
191
+ track?: number;
192
+ w?: number;
193
+ h?: number;
194
+ }): Node;
195
+ /**
196
+ * The edit as OpenTimelineIO JSON (Timeline.1), for editors and tools that read .otio files.
197
+ * @param {Edit} e
198
+ * @param {{ name?: string, urls?: Record<string, string> }} [options] urls maps media names to file paths or URLs.
199
+ */
200
+ export declare function toOTIO(e: Edit, { name, urls }?: {
201
+ name?: string;
202
+ urls?: Record<string, string>;
203
+ }): {
204
+ OTIO_SCHEMA: string;
205
+ metadata: {};
206
+ name: string;
207
+ global_start_time: null;
208
+ tracks: {
209
+ metadata: {};
210
+ effects: never[];
211
+ markers: never[];
212
+ enabled: boolean;
213
+ OTIO_SCHEMA: string;
214
+ name: string;
215
+ source_range: null;
216
+ children: {
217
+ metadata: {};
218
+ effects: never[];
219
+ markers: never[];
220
+ enabled: boolean;
221
+ OTIO_SCHEMA: string;
222
+ name: string;
223
+ kind: string;
224
+ source_range: null;
225
+ children: ({
226
+ metadata: {};
227
+ effects: never[];
228
+ markers: never[];
229
+ enabled: boolean;
230
+ OTIO_SCHEMA: string;
231
+ name: string;
232
+ source_range: {
233
+ OTIO_SCHEMA: string;
234
+ start_time: {
235
+ OTIO_SCHEMA: string;
236
+ rate: number;
237
+ value: number;
238
+ };
239
+ duration: {
240
+ OTIO_SCHEMA: string;
241
+ rate: number;
242
+ value: number;
243
+ };
244
+ };
245
+ } | {
246
+ metadata: {};
247
+ effects: never[];
248
+ markers: never[];
249
+ enabled: boolean;
250
+ OTIO_SCHEMA: string;
251
+ name: string;
252
+ source_range: {
253
+ OTIO_SCHEMA: string;
254
+ start_time: {
255
+ OTIO_SCHEMA: string;
256
+ rate: number;
257
+ value: number;
258
+ };
259
+ duration: {
260
+ OTIO_SCHEMA: string;
261
+ rate: number;
262
+ value: number;
263
+ };
264
+ };
265
+ media_references: {
266
+ DEFAULT_MEDIA: {
267
+ OTIO_SCHEMA: string;
268
+ name: string;
269
+ metadata: {};
270
+ available_range: null;
271
+ available_image_bounds: null;
272
+ target_url: string;
273
+ };
274
+ };
275
+ active_media_reference_key: string;
276
+ })[];
277
+ }[];
278
+ };
279
+ };
280
+ /**
281
+ * Reads OpenTimelineIO JSON: tracks of clips and gaps, at the edit's own rate.
282
+ * @param {any} json A parsed .otio file.
283
+ * @param {{ fps?: number, media?: (reference: { name: string, url: string }) => string }} [options] fps defaults to
284
+ * the first clip's rate; media names each clip's media, by default the reference's name or else its URL.
285
+ * @returns {Edit}
286
+ */
287
+ export declare function fromOTIO(json: any, { fps, media }?: {
288
+ fps?: number;
289
+ media?: (reference: {
290
+ name: string;
291
+ url: string;
292
+ }) => string;
293
+ }): Edit;
@@ -0,0 +1,65 @@
1
+ export type Value<T> = import('../core/math.js').Value<T>;
2
+ export type Effect = import('../core/nodes.js').Effect;
3
+ /** A blur of `radius` px. @param {Value<number>} [radius] Default 4. */
4
+ export declare const blur: (radius?: Value<number>) => import("../core/nodes.js").Effect;
5
+ /**
6
+ * Glow: what is brighter than `threshold` (0..1) spreads by `radius` px and is screened back on.
7
+ * @param {{ radius?: Value<number>, strength?: Value<number>, threshold?: Value<number> }} [options] Defaults 12, 1, 0.6.
8
+ */
9
+ export declare const glow: ({ radius, strength, threshold }?: {
10
+ radius?: Value<number>;
11
+ strength?: Value<number>;
12
+ threshold?: Value<number>;
13
+ }) => import("../core/nodes.js").Effect;
14
+ /**
15
+ * Diffusion: a blurred copy of the whole layer screened back on, the soft bloom of anime photography.
16
+ * @param {{ radius?: Value<number>, strength?: Value<number> }} [options] Defaults 16 and 0.5.
17
+ */
18
+ export declare const diffusion: ({ radius, strength }?: {
19
+ radius?: Value<number>;
20
+ strength?: Value<number>;
21
+ }) => import("../core/nodes.js").Effect;
22
+ /**
23
+ * Film grain that changes every frame but is the same each time a frame is drawn.
24
+ * @param {{ amount?: Value<number>, size?: Value<number>, seed?: Value<number> }} [options] Defaults 0.08, 1.5 px, 0.
25
+ */
26
+ export declare const grain: ({ amount, size, seed }?: {
27
+ amount?: Value<number>;
28
+ size?: Value<number>;
29
+ seed?: Value<number>;
30
+ }) => import("../core/nodes.js").Effect;
31
+ /**
32
+ * Para: a gradient from one colour to another laid over the layer, inside its own shape, with a blend mode.
33
+ * @param {{ from?: Value<string>, to?: Value<string>, angle?: Value<number>, blend?: Value<string>,
34
+ * opacity?: Value<number> }} [options] angle is the direction from `from` to `to`, in degrees clockwise from right
35
+ * (90 runs top to bottom). blend defaults to 'screen'.
36
+ */
37
+ export declare const para: ({ from, to, angle, blend, opacity }?: {
38
+ from?: Value<string>;
39
+ to?: Value<string>;
40
+ angle?: Value<number>;
41
+ blend?: Value<string>;
42
+ opacity?: Value<number>;
43
+ }) => import("../core/nodes.js").Effect;
44
+ /**
45
+ * Darkens toward the frame's edges.
46
+ * @param {{ strength?: Value<number>, radius?: Value<number> }} [options] strength defaults to 0.4; radius, the
47
+ * share of the way to the corners where darkening starts, to 0.7.
48
+ */
49
+ export declare const vignette: ({ strength, radius }?: {
50
+ strength?: Value<number>;
51
+ radius?: Value<number>;
52
+ }) => import("../core/nodes.js").Effect;
53
+ /**
54
+ * The satsuei finishing look as one list: diffusion, a warm light from above, a vignette and fine grain. Pass false
55
+ * for any part to leave it out, or options to change it.
56
+ * @param {{ diffusion?: false | Parameters<typeof diffusion>[0], light?: false | Parameters<typeof para>[0],
57
+ * vignette?: false | Parameters<typeof vignette>[0], grain?: false | Parameters<typeof grain>[0] }} [parts]
58
+ * @returns {Effect[]}
59
+ */
60
+ export declare function satsuei(parts?: {
61
+ diffusion?: false | Parameters<typeof diffusion>[0];
62
+ light?: false | Parameters<typeof para>[0];
63
+ vignette?: false | Parameters<typeof vignette>[0];
64
+ grain?: false | Parameters<typeof grain>[0];
65
+ }): Effect[];
@@ -0,0 +1,78 @@
1
+ export type Input = import('./input.js').Input;
2
+ export type TickInput = {
3
+ /**
4
+ * Actions down this tick.
5
+ */
6
+ held: Input;
7
+ /**
8
+ * Actions that went down this tick.
9
+ */
10
+ pressed: Record<string, boolean>;
11
+ /**
12
+ * Actions that came up this tick.
13
+ */
14
+ released: Record<string, boolean>;
15
+ };
16
+ export type TickContext = {
17
+ tick: number;
18
+ /**
19
+ * Scene time at the start of the tick.
20
+ */
21
+ t: number;
22
+ dt: number;
23
+ /**
24
+ * Records an event at this tick, such as a hit for a sound.
25
+ */
26
+ emit: (name: string, data?: unknown) => void;
27
+ };
28
+ export type GameEvent = {
29
+ tick: number;
30
+ t: number;
31
+ name: string;
32
+ data: unknown;
33
+ };
34
+ /** @typedef {import('./input.js').Input} Input */
35
+ /**
36
+ * What a step sees each tick.
37
+ * @typedef {object} TickInput
38
+ * @property {Input} held Actions down this tick.
39
+ * @property {Record<string, boolean>} pressed Actions that went down this tick.
40
+ * @property {Record<string, boolean>} released Actions that came up this tick.
41
+ */
42
+ /**
43
+ * @typedef {object} TickContext
44
+ * @property {number} tick
45
+ * @property {number} t Scene time at the start of the tick.
46
+ * @property {number} dt
47
+ * @property {(name: string, data?: unknown) => void} emit Records an event at this tick, such as a hit for a sound.
48
+ */
49
+ /** @typedef {{ tick: number, t: number, name: string, data: unknown }} GameEvent */
50
+ /**
51
+ * @template S
52
+ * @param {{ init: S | (() => S), step: (state: S, input: TickInput, context: TickContext) => S | void,
53
+ * input?: (tick: number) => Input | undefined, hz?: number, start?: number, lerp?: ((a: S, b: S, k: number) => S) | false,
54
+ * snapshotEvery?: number, params?: Record<string, unknown> }} spec input is a recorder() while playing live or an
55
+ * input track when replaying; hz defaults to 60.
56
+ */
57
+ export declare function game<S>({ init, step, input, hz, start, lerp, snapshotEvery, params }: {
58
+ init: S | (() => S);
59
+ step: (state: S, input: TickInput, context: TickContext) => S | void;
60
+ input?: (tick: number) => Input | undefined;
61
+ hz?: number;
62
+ start?: number;
63
+ lerp?: ((a: S, b: S, k: number) => S) | false;
64
+ snapshotEvery?: number;
65
+ params?: Record<string, unknown>;
66
+ }): Readonly<{
67
+ sim: import("../core/simulate.js").Simulation<S>;
68
+ hz: number;
69
+ /** @param {number} t */
70
+ at: (/** @type {number} */ t: number) => S;
71
+ /**
72
+ * Events emitted by ticks that end after `from` and up to `to`, in order, for a live player's sounds. Asking
73
+ * steps the game that far first.
74
+ * @param {number} from @param {number} to
75
+ * @returns {GameEvent[]}
76
+ */
77
+ events(from: number, to: number): GameEvent[];
78
+ }>;
@@ -0,0 +1,56 @@
1
+ export type Input = Record<string, boolean | number | readonly [number, number]>;
2
+ export type Binding = string;
3
+ /**
4
+ * Reads devices as named actions. Keys and the pointer are logged with their event times, so `readAt` can answer
5
+ * for any recent moment however slowly frames arrive; gamepads have no events and are read as they are now.
6
+ * @param {Record<string, Binding | Binding[]>} map Action name to one control or several.
7
+ * @param {{ target?: EventTarget, element?: Element, size?: readonly [number, number] }} [options] target receives
8
+ * key events (default window); element receives pointer events, and with size the pointer is reported in scene px.
9
+ */
10
+ export declare function controls(map: Record<string, Binding | Binding[]>, { target, element, size }?: {
11
+ target?: EventTarget;
12
+ element?: Element;
13
+ size?: readonly [number, number];
14
+ }): {
15
+ readAt: (time: number, since?: number) => Input;
16
+ /** The actions now. */
17
+ read: () => Input;
18
+ /** Stops listening. */
19
+ destroy(): void;
20
+ };
21
+ /**
22
+ * Records live input per tick. Used as a simulation's input: a tick asked for the first time takes what `read`
23
+ * returns for it and keeps it, and every later ask for that tick, seeking included, gets the kept value. That makes
24
+ * a live session a pure function of its recording.
25
+ * @param {(tick: number) => Input} read Called with the tick, such as tick => pad.readAt(start + tick * 1000 / hz,
26
+ * start + (tick - 1) * 1000 / hz), which reads what the player did at that tick's real time.
27
+ * @param {{ hz?: number }} [options] Only written into the recording, for whoever replays it.
28
+ */
29
+ export declare function recorder(read: (tick: number) => Input, { hz }?: {
30
+ hz?: number;
31
+ }): (((tick: number) => Input) & {
32
+ /** Ends recording: later ticks repeat the last input. */
33
+ stop(): void;
34
+ entries: () => [number, Input][];
35
+ /** An inputTrack of what was recorded, for replays. */
36
+ track: () => ((tick: number) => Input | undefined) & {
37
+ entries: readonly (readonly [number, Input])[];
38
+ };
39
+ /** Plain data to save or send: hz and the changed entries. */
40
+ toJSON: () => {
41
+ hz: number;
42
+ entries: [number, Input][];
43
+ };
44
+ }) & {
45
+ readonly size: number;
46
+ };
47
+ /**
48
+ * A recording saved with toJSON, back as an input track.
49
+ * @param {{ hz?: number, entries: [number, Input][] }} json
50
+ */
51
+ export declare const fromRecording: (json: {
52
+ hz?: number;
53
+ entries: [number, Input][];
54
+ }) => ((tick: number) => Input | undefined) & {
55
+ entries: readonly (readonly [number, Input])[];
56
+ };
@@ -0,0 +1,111 @@
1
+ export type Value<T> = import('../core/math.js').Value<T>;
2
+ export type Clip = Record<string, number | readonly number[] | ((t: number) => any)>;
3
+ export type Channels = Record<string, (t: number) => any>;
4
+ export type StateSpec = {
5
+ /**
6
+ * Input field to the state it leads to: a truthy input[field] moves there.
7
+ */
8
+ on?: Record<string, string>;
9
+ /**
10
+ * Conditions checked each tick, in order; time is seconds in this state.
11
+ */
12
+ when?: ReadonlyArray<readonly [(input: any, info: {
13
+ t: number;
14
+ time: number;
15
+ tick: number;
16
+ }) => boolean, string]>;
17
+ /**
18
+ * Seconds in this state before moving to `to`.
19
+ */
20
+ after?: number;
21
+ to?: string;
22
+ /**
23
+ * Crossfade seconds into this state, overriding the machine's.
24
+ */
25
+ fade?: number;
26
+ };
27
+ export type MachineState = {
28
+ state: string;
29
+ /**
30
+ * When it was entered, in scene time.
31
+ */
32
+ since: number;
33
+ /**
34
+ * The state before, while fading from it.
35
+ */
36
+ from: string | null;
37
+ /**
38
+ * When that one was entered.
39
+ */
40
+ fromSince: number;
41
+ /**
42
+ * 0..1 progress of the crossfade into `state`; 1 when there is none.
43
+ */
44
+ k: number;
45
+ };
46
+ /**
47
+ * @template T
48
+ * @typedef {import('../core/math.js').Value<T>} Value
49
+ */
50
+ /**
51
+ * A clip: animation Values by channel name, as layers() and rig({ animation }) take.
52
+ * @typedef {Record<string, number | readonly number[] | ((t: number) => any)>} Clip
53
+ */
54
+ /** A clip whose channels are all functions of t, as blend() and blendTree() return. @typedef {Record<string, (t: number) => any>} Channels */
55
+ /**
56
+ * @typedef {object} StateSpec
57
+ * @property {Record<string, string>} [on] Input field to the state it leads to: a truthy input[field] moves there.
58
+ * @property {ReadonlyArray<readonly [(input: any, info: { t: number, time: number, tick: number }) => boolean, string]>} [when]
59
+ * Conditions checked each tick, in order; time is seconds in this state.
60
+ * @property {number} [after] Seconds in this state before moving to `to`.
61
+ * @property {string} [to]
62
+ * @property {number} [fade] Crossfade seconds into this state, overriding the machine's.
63
+ */
64
+ /**
65
+ * @typedef {object} MachineState
66
+ * @property {string} state
67
+ * @property {number} since When it was entered, in scene time.
68
+ * @property {string | null} from The state before, while fading from it.
69
+ * @property {number} fromSince When that one was entered.
70
+ * @property {number} k 0..1 progress of the crossfade into `state`; 1 when there is none.
71
+ */
72
+ /**
73
+ * A state machine. Each tick it checks the current state's `on` inputs, then its `when` conditions, then `after`;
74
+ * the first that holds moves it, at most once a tick.
75
+ * @param {{ initial: string, states: Record<string, StateSpec>, input?: (tick: number, t: number) => any, hz?: number,
76
+ * start?: number, fade?: number }} spec hz defaults to 60; fade (seconds, default 0) crossfades clips in blend().
77
+ */
78
+ export declare function machine({ initial, states, input, hz, start, fade }: {
79
+ initial: string;
80
+ states: Record<string, StateSpec>;
81
+ input?: (tick: number, t: number) => any;
82
+ hz?: number;
83
+ start?: number;
84
+ fade?: number;
85
+ }): Readonly<{
86
+ /** The state at t, with the crossfade from the one before. */
87
+ at: (t: number) => MachineState;
88
+ /** @param {number} t */
89
+ state: (/** @type {number} */ t: number) => string;
90
+ /** Every transition up to scene time t, for timelines and debugging. @param {number} t */
91
+ log: (/** @type {number} */ t: number) => {
92
+ at: number;
93
+ from: string;
94
+ to: string;
95
+ }[];
96
+ /**
97
+ * One clip from a clip per state: each plays from the moment its state is entered, and the machine's fade mixes
98
+ * the outgoing one into the incoming.
99
+ * @param {Record<string, Clip>} clips By state name; a state without a clip contributes nothing.
100
+ * @returns {Channels}
101
+ */
102
+ blend(clips: Record<string, Clip>): Channels;
103
+ }>;
104
+ /**
105
+ * A blend tree: clips placed at key values of one or two params (walk at speed 1, run at speed 3), mixed linearly by
106
+ * where the params are now. With two params every combination of key values needs a clip.
107
+ * @param {Value<number> | ReadonlyArray<Value<number>>} inputs
108
+ * @param {ReadonlyArray<[number | readonly number[], Clip]>} keys [key value or values, clip] pairs.
109
+ * @returns {Channels}
110
+ */
111
+ export declare function blendTree(inputs: Value<number> | ReadonlyArray<Value<number>>, keys: ReadonlyArray<[number | readonly number[], Clip]>): Channels;