@wardic/core 1.0.0-rc.9 → 1.0.1
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/Batch.d.ts +244 -0
- package/dist/Batch.d.ts.map +1 -0
- package/dist/Batch.js +1862 -0
- package/dist/Batch.js.map +1 -0
- package/dist/DragRecognizer.d.ts +16 -1
- package/dist/DragRecognizer.d.ts.map +1 -1
- package/dist/DragRecognizer.js +87 -1
- package/dist/DragRecognizer.js.map +1 -1
- package/dist/InputSource.d.ts +21 -0
- package/dist/InputSource.d.ts.map +1 -1
- package/dist/InputSource.js.map +1 -1
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +4 -67
- package/dist/Machine.js.map +1 -1
- package/dist/Playback.d.ts.map +1 -1
- package/dist/Playback.js +7 -6
- package/dist/Playback.js.map +1 -1
- package/dist/Recognizer.d.ts +35 -2
- package/dist/Recognizer.d.ts.map +1 -1
- package/dist/Recognizer.js +211 -16
- package/dist/Recognizer.js.map +1 -1
- package/dist/Reorder.d.ts +7 -5
- package/dist/Reorder.d.ts.map +1 -1
- package/dist/Reorder.js +60 -23
- package/dist/Reorder.js.map +1 -1
- package/dist/Sequence.d.ts +7 -2
- package/dist/Sequence.d.ts.map +1 -1
- package/dist/Sequence.js +8 -2
- package/dist/Sequence.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/keyframes.d.ts +40 -0
- package/dist/internal/keyframes.d.ts.map +1 -0
- package/dist/internal/keyframes.js +70 -0
- package/dist/internal/keyframes.js.map +1 -0
- package/dist/internal/playhead.d.ts +16 -0
- package/dist/internal/playhead.d.ts.map +1 -0
- package/dist/internal/playhead.js +6 -0
- package/dist/internal/playhead.js.map +1 -0
- package/dist/internal/stepper.d.ts +6 -0
- package/dist/internal/stepper.d.ts.map +1 -1
- package/dist/internal/stepper.js +16 -0
- package/dist/internal/stepper.js.map +1 -1
- package/dist/internal/watch.d.ts +6 -0
- package/dist/internal/watch.d.ts.map +1 -1
- package/dist/internal/watch.js +7 -0
- package/dist/internal/watch.js.map +1 -1
- package/package.json +2 -2
- package/src/Batch.ts +2128 -0
- package/src/DragRecognizer.ts +97 -2
- package/src/InputSource.ts +25 -0
- package/src/Machine.ts +4 -93
- package/src/Playback.ts +7 -13
- package/src/Recognizer.ts +242 -17
- package/src/Reorder.ts +83 -27
- package/src/Sequence.ts +20 -5
- package/src/index.ts +1 -0
- package/src/internal/keyframes.ts +98 -0
- package/src/internal/playhead.ts +19 -0
- package/src/internal/stepper.ts +16 -0
- package/src/internal/watch.ts +15 -0
package/dist/Batch.js
ADDED
|
@@ -0,0 +1,1862 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Batch: many Subjects animated on the same keys as one run on the Frame loop, for a list, a grid or
|
|
3
|
+
* a chart's points that move together. Each member's values, velocities and targets are kept by
|
|
4
|
+
* index, so a new target turns every member toward it at the speed it already has, and a change
|
|
5
|
+
* costs an array write per member, not an animation each. An Environment's writer draws each member
|
|
6
|
+
* as it moves. A change can be driven by a Playhead, as Playback controls drive an animation, toward
|
|
7
|
+
* targets (`animate`) or through each member's key's own stops (`keyframes`, a sequence's Segment).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import * as Batch from "@wardic/core/Batch"
|
|
11
|
+
*
|
|
12
|
+
* const grid = Batch.make({ keys: ["x", "y"], writer, transition: { type: "spring", visualDuration: 0.4 } })
|
|
13
|
+
* const slots = [first, second, third].map((tile) => Batch.enter(grid, tile, { x: 0, y: 0 }))
|
|
14
|
+
* Batch.retarget(grid, slots, (slot, k) => cellOf(slot)[k])
|
|
15
|
+
*/
|
|
16
|
+
// #665, ADR-0069 decisions 10–18 and *Amendment (#665)*. Tier 2 (ADR-0008): for binding and Environment
|
|
17
|
+
// authors; an app reaches a Batch through its binding's `createBatch`. State by index in typed arrays,
|
|
18
|
+
// a member's keys at `slot * stride + k` (#663's shape); stepped by `Spring.matrixFor`'s exact step
|
|
19
|
+
// (#370), the probe's loop (`.tmp/564-canvas/perf/group.ts` on `probe/564-canvas`).
|
|
20
|
+
import * as Arr from "effect/Array";
|
|
21
|
+
import * as Effect from "effect/Effect";
|
|
22
|
+
import { identity } from "effect/Function";
|
|
23
|
+
import * as Inspectable from "effect/Inspectable";
|
|
24
|
+
import * as Iter from "effect/Iterable";
|
|
25
|
+
import * as Match from "effect/Match";
|
|
26
|
+
import { pipeArguments } from "effect/Pipeable";
|
|
27
|
+
import * as Predicate from "effect/Predicate";
|
|
28
|
+
import * as UndefinedOr from "effect/UndefinedOr";
|
|
29
|
+
import * as Machine from "./Machine.js";
|
|
30
|
+
import * as Scalar from "./Scalar.js";
|
|
31
|
+
import * as Spring from "./Spring.js";
|
|
32
|
+
import { dual } from "./internal/dual.js";
|
|
33
|
+
import { tickOf, writableOf } from "./internal/playhead.js";
|
|
34
|
+
import { resolveKeyframes } from "./internal/keyframes.js";
|
|
35
|
+
import { checked, fn, literals, nonNegative, positive, union } from "./internal/options.js";
|
|
36
|
+
import { keyframeAt, passAt, stopTimesOf } from "./internal/stepper.js";
|
|
37
|
+
const TypeId = "~@wardic/core/Batch";
|
|
38
|
+
// --- The table: the transitions a Batch's keys run -----------------------------
|
|
39
|
+
//
|
|
40
|
+
// ADR-0069, *Amendment (#665)*. Each member's key records its rule: the index of the transition it runs.
|
|
41
|
+
// An entry is held by the keys that run it, and freed for reuse once none does, so the table holds only
|
|
42
|
+
// what its keys run now, whatever bag each change brings. The Batch's own transitions are pinned.
|
|
43
|
+
/**
|
|
44
|
+
* An entry's kind: a spring, a tween, a spring that ends at its `duration`, a freed entry, or a driven
|
|
45
|
+
* change's keyframes, each of its keys through stops of its own.
|
|
46
|
+
*/
|
|
47
|
+
// #359: a spring set by duration lands there. #665, Q36: keyframes, a Sequence's Segment of a Batch.
|
|
48
|
+
const SPRING = 0;
|
|
49
|
+
const TWEEN = 1;
|
|
50
|
+
const TIMED = 2;
|
|
51
|
+
const FREE = 3;
|
|
52
|
+
const KEYFRAMES = 4;
|
|
53
|
+
/** Numbers in a spring entry's matrix for this frame: `dd`, `dv`, `vd`, `vv`. */
|
|
54
|
+
const MATRIX = 4;
|
|
55
|
+
/** How many entries a table holds before it grows. */
|
|
56
|
+
const TABLE_START = 8;
|
|
57
|
+
/** The most entries a table holds at once: a rule is a `Uint16Array`'s. */
|
|
58
|
+
const TABLE_MAX = 65536;
|
|
59
|
+
// --- A slot's flags -------------------------------------------------------------
|
|
60
|
+
/** A member holds it, from its `enter` to its `leave`. */
|
|
61
|
+
const LIVE = 1;
|
|
62
|
+
/** It moves in the plain list: every key on the Batch's own spring. */
|
|
63
|
+
const IN_PLAIN = 2;
|
|
64
|
+
/** It moves in the ruled list: each key by its rule. */
|
|
65
|
+
const IN_RULED = 4;
|
|
66
|
+
/** A change counted it among its movers: a slot given twice moves once. */
|
|
67
|
+
const COUNTED = 8;
|
|
68
|
+
/**
|
|
69
|
+
* A change started it partway (`startAt`), read at its first step: the keys that change turned have
|
|
70
|
+
* `elapsed` `NaN` until then. It's in `startList`, which each step's start pass reads before the loops.
|
|
71
|
+
*/
|
|
72
|
+
const STARTS = 16;
|
|
73
|
+
/** The start pass drew it this step: its loop keeps it, unstepped, and clears the flag. */
|
|
74
|
+
const STARTED = 32;
|
|
75
|
+
const Proto = {
|
|
76
|
+
[TypeId]: { _S: identity },
|
|
77
|
+
...Inspectable.BaseProto,
|
|
78
|
+
toJSON() {
|
|
79
|
+
return {
|
|
80
|
+
_id: "Batch",
|
|
81
|
+
keys: this.keys,
|
|
82
|
+
members: this.used - this.free.length,
|
|
83
|
+
moving: this.plainCount + this.ruledCount,
|
|
84
|
+
transitions: this.entries.length - this.freeRules.length,
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
pipe() {
|
|
88
|
+
return pipeArguments(this, arguments);
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
/** Whether `u` is a Batch. */
|
|
92
|
+
export const isBatch = (u) => Predicate.hasProperty(u, TypeId);
|
|
93
|
+
const stateOf = (batch) => batch;
|
|
94
|
+
const noop = () => { };
|
|
95
|
+
/** No spring: a tween's entry's. */
|
|
96
|
+
const NO_SPRING = Spring.resolve({});
|
|
97
|
+
/** No tween: a spring's entry's. */
|
|
98
|
+
const NO_TWEEN = Machine.resolveTween({});
|
|
99
|
+
/** The transitions a Batch runs: springs and tweens alone (ADR-0069, decision 11), no keyframes, inertia or decay. */
|
|
100
|
+
const RunType = /*#__PURE__*/ literals(["spring", "tween"]);
|
|
101
|
+
/** A change's delay: seconds, or a function of a mover's rank and how many move. */
|
|
102
|
+
const Delay = /*#__PURE__*/ union([nonNegative, fn("a function of a mover's rank and how many move")]);
|
|
103
|
+
/** A spring's entry: one that ends at its duration is timed (#359). */
|
|
104
|
+
const springEntry = (transition) => {
|
|
105
|
+
const spring = Spring.resolve(transition);
|
|
106
|
+
if (Spring.endsOnTime(spring))
|
|
107
|
+
return { kind: TIMED, delay: spring.delay, spring, tween: NO_TWEEN };
|
|
108
|
+
return { kind: SPRING, delay: spring.delay, spring, tween: NO_TWEEN };
|
|
109
|
+
};
|
|
110
|
+
/** A tween's entry. */
|
|
111
|
+
const tweenEntry = (transition) => {
|
|
112
|
+
const tween = Machine.resolveTween(transition);
|
|
113
|
+
return { kind: TWEEN, delay: tween.delay, spring: NO_SPRING, tween };
|
|
114
|
+
};
|
|
115
|
+
/** A checked transition's entry, by its `type`. */
|
|
116
|
+
const entryByType = /*#__PURE__*/ Match.type().pipe(Match.discriminator("type")("tween", tweenEntry), Match.orElse(springEntry));
|
|
117
|
+
/** `transition`, resolved for the table: its `type` checked, and its bag (`Spring.resolve`, `resolveTween`). */
|
|
118
|
+
const entryFor = (transition) => {
|
|
119
|
+
checked(RunType, transition.type, "type");
|
|
120
|
+
return entryByType(transition);
|
|
121
|
+
};
|
|
122
|
+
/** Whether `transitions` is one transition, not one per key. */
|
|
123
|
+
const isTransition = (transitions) => Predicate.hasProperty(transitions, "type");
|
|
124
|
+
/** `transition`'s rule in `state`'s table: its entry, or a new one at its first use, in a freed entry if there is one. */
|
|
125
|
+
const ruleFor = (state, transition) => {
|
|
126
|
+
const known = state.ruleByBag.get(transition);
|
|
127
|
+
if (Predicate.isNotUndefined(known))
|
|
128
|
+
return known;
|
|
129
|
+
return entryRuleOf(state, transition, entryFor(transition));
|
|
130
|
+
};
|
|
131
|
+
/** A new entry for `bag` in `state`'s table: its rule, a freed one if there is one. */
|
|
132
|
+
const entryRuleOf = (state, bag, entry) => {
|
|
133
|
+
const freed = state.freeRules.pop();
|
|
134
|
+
let rule = state.entries.length;
|
|
135
|
+
if (Predicate.isNotUndefined(freed))
|
|
136
|
+
rule = freed;
|
|
137
|
+
if (rule === TABLE_MAX) {
|
|
138
|
+
throw new Machine.OptionError({ message: `Expected at most ${TABLE_MAX} transitions running at once\n at ["transition"]` });
|
|
139
|
+
}
|
|
140
|
+
if (rule === state.kinds.length) {
|
|
141
|
+
state.kinds = grown(state.kinds, rule * 2);
|
|
142
|
+
state.matrices = grown(state.matrices, rule * 2 * MATRIX);
|
|
143
|
+
state.uses = grown(state.uses, rule * 2);
|
|
144
|
+
state.dts = grown(state.dts, rule * 2);
|
|
145
|
+
}
|
|
146
|
+
state.entries[rule] = entry;
|
|
147
|
+
state.drivers[rule] = undefined;
|
|
148
|
+
state.bags[rule] = bag;
|
|
149
|
+
state.kinds[rule] = entry.kind;
|
|
150
|
+
state.uses[rule] = 0;
|
|
151
|
+
state.ruleByBag.set(bag, rule);
|
|
152
|
+
return rule;
|
|
153
|
+
};
|
|
154
|
+
/** One more key runs `rule`. */
|
|
155
|
+
const hold = (state, rule) => {
|
|
156
|
+
if (rule < state.pinned)
|
|
157
|
+
return;
|
|
158
|
+
state.uses[rule] = state.uses[rule] + 1;
|
|
159
|
+
};
|
|
160
|
+
/** One key fewer runs `rule`: once none does, its entry is freed. */
|
|
161
|
+
const release = (state, rule) => {
|
|
162
|
+
if (rule < state.pinned)
|
|
163
|
+
return;
|
|
164
|
+
if (state.kinds[rule] === FREE)
|
|
165
|
+
return;
|
|
166
|
+
if (state.uses[rule] > 0)
|
|
167
|
+
state.uses[rule] = state.uses[rule] - 1;
|
|
168
|
+
if (state.uses[rule] > 0)
|
|
169
|
+
return;
|
|
170
|
+
const bag = state.bags[rule];
|
|
171
|
+
// Forgotten at a rest already (`forgetTransitions`).
|
|
172
|
+
if (Predicate.isUndefined(bag))
|
|
173
|
+
return;
|
|
174
|
+
state.kinds[rule] = FREE;
|
|
175
|
+
state.ruleByBag.delete(bag);
|
|
176
|
+
state.bags[rule] = undefined;
|
|
177
|
+
state.freeRules.push(rule);
|
|
178
|
+
};
|
|
179
|
+
/** `transitions`' bag for each of `state`'s keys, by index: its own, or its `default`; `undefined` where it gives neither. */
|
|
180
|
+
// Once a change, per key.
|
|
181
|
+
const bagsOf = (state, transitions) => {
|
|
182
|
+
if (isTransition(transitions))
|
|
183
|
+
return Arr.map(state.keys, () => transitions);
|
|
184
|
+
return Arr.map(state.keys, (key) => {
|
|
185
|
+
const given = transitions[key];
|
|
186
|
+
if (Predicate.isNotUndefined(given))
|
|
187
|
+
return given;
|
|
188
|
+
return transitions["default"];
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
/** `transitions`' rule for each of `state`'s keys, by index; a key it doesn't name, nor its `default`, takes `fallback`'s. */
|
|
192
|
+
const rulesOf = (state, transitions, fallback) => Arr.map(bagsOf(state, transitions), (bag, k) => {
|
|
193
|
+
if (Predicate.isUndefined(bag))
|
|
194
|
+
return fallback(k);
|
|
195
|
+
return ruleFor(state, bag);
|
|
196
|
+
});
|
|
197
|
+
/** A key `make`'s transitions leave without one: every key needs its own or the `default`. */
|
|
198
|
+
const unnamedKey = (state) => (k) => {
|
|
199
|
+
throw new Machine.OptionError({ message: `Expected a transition for each key, or a "default"\n at ["transition"]["${state.keys[k]}"]` });
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* A Batch of no members yet, on `options.keys`, drawn by `options.writer`.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* const grid = Batch.make({ keys: ["x", "y"], writer, transition: { type: "spring" } })
|
|
206
|
+
*/
|
|
207
|
+
export const make = (options) => {
|
|
208
|
+
Arr.forEach(options.keys, (key, k) => {
|
|
209
|
+
if (options.writer.writes(key))
|
|
210
|
+
return;
|
|
211
|
+
throw new Machine.OptionError({ message: `Expected a key its writer draws, got ${JSON.stringify(key)}\n at ["keys"][${k}]` });
|
|
212
|
+
});
|
|
213
|
+
const capacity = Math.ceil(checked(positive, UndefinedOr.match(options.capacity, { onUndefined: () => 16, onDefined: identity }), "capacity"));
|
|
214
|
+
const stride = options.keys.length;
|
|
215
|
+
const size = capacity * stride;
|
|
216
|
+
const state = {
|
|
217
|
+
keys: options.keys,
|
|
218
|
+
stride,
|
|
219
|
+
indexOfKey: Arr.reduce(options.keys, {}, (indices, key, k) => {
|
|
220
|
+
indices[key] = k;
|
|
221
|
+
return indices;
|
|
222
|
+
}),
|
|
223
|
+
capacity,
|
|
224
|
+
used: 0,
|
|
225
|
+
subjects: [],
|
|
226
|
+
positions: new Float64Array(size),
|
|
227
|
+
velocities: new Float64Array(size),
|
|
228
|
+
targets: new Float64Array(size),
|
|
229
|
+
elapsed: new Float64Array(size),
|
|
230
|
+
delays: new Float64Array(size),
|
|
231
|
+
restDeltas: new Float64Array(size),
|
|
232
|
+
restSpeeds: new Float64Array(size),
|
|
233
|
+
origins: new Float64Array(size),
|
|
234
|
+
originVelocities: new Float64Array(size),
|
|
235
|
+
rules: new Uint16Array(size),
|
|
236
|
+
pending: new Float64Array(size),
|
|
237
|
+
movers: new Int32Array(capacity),
|
|
238
|
+
flags: new Uint8Array(capacity),
|
|
239
|
+
free: [],
|
|
240
|
+
startAts: [],
|
|
241
|
+
startList: [],
|
|
242
|
+
plainList: new Int32Array(capacity),
|
|
243
|
+
plainCount: 0,
|
|
244
|
+
ruledList: new Int32Array(capacity),
|
|
245
|
+
ruledCount: 0,
|
|
246
|
+
entries: [],
|
|
247
|
+
kinds: new Uint8Array(TABLE_START),
|
|
248
|
+
matrices: new Float64Array(TABLE_START * MATRIX),
|
|
249
|
+
uses: new Uint32Array(TABLE_START),
|
|
250
|
+
drivers: [],
|
|
251
|
+
dts: new Float64Array(TABLE_START),
|
|
252
|
+
animations: new Set(),
|
|
253
|
+
bags: [],
|
|
254
|
+
ruleByBag: new Map(),
|
|
255
|
+
freeRules: [],
|
|
256
|
+
pinned: 0,
|
|
257
|
+
ownRules: [],
|
|
258
|
+
write: options.writer.forKeys(options.keys),
|
|
259
|
+
flush: UndefinedOr.match(options.writer.flush, { onUndefined: () => noop, onDefined: identity }),
|
|
260
|
+
onFrame: UndefinedOr.match(options.onFrame, { onUndefined: () => noop, onDefined: identity }),
|
|
261
|
+
frame: 0,
|
|
262
|
+
running: 0,
|
|
263
|
+
runs: 0,
|
|
264
|
+
};
|
|
265
|
+
// The Batch's own transitions, pinned: the first entries, rule 0 the first key's.
|
|
266
|
+
state.ownRules = rulesOf(state, options.transition, unnamedKey(state));
|
|
267
|
+
state.pinned = state.entries.length;
|
|
268
|
+
return Object.assign(Object.create(Proto), state);
|
|
269
|
+
};
|
|
270
|
+
/** A change's rule for each key, when its transitions are one or one per key; the Batch's own else. */
|
|
271
|
+
// Once a change.
|
|
272
|
+
const changeRulesOf = (state, transitions) => {
|
|
273
|
+
if (Predicate.isUndefined(transitions))
|
|
274
|
+
return state.ownRules;
|
|
275
|
+
if (Predicate.isFunction(transitions))
|
|
276
|
+
return state.ownRules;
|
|
277
|
+
return rulesOf(state, transitions, (k) => state.ownRules[k]);
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* A change's rule for a member's key, when its transition is a function of them: the bag it gives,
|
|
281
|
+
* the last one's rule again when it's the same, while the table still holds it.
|
|
282
|
+
*/
|
|
283
|
+
// Most members give the bag the last one gave: one comparison, where `ruleFor` is a Map lookup per
|
|
284
|
+
// member's key (#665's Chromium profile, 1,000 members).
|
|
285
|
+
const ruleAtOf = (state, given) => {
|
|
286
|
+
if (!Predicate.isFunction(given))
|
|
287
|
+
return undefined;
|
|
288
|
+
let lastBag = undefined;
|
|
289
|
+
let lastRule = 0;
|
|
290
|
+
return (slot, k) => {
|
|
291
|
+
const bag = given(slot, k);
|
|
292
|
+
if (bag === lastBag && state.bags[lastRule] === bag)
|
|
293
|
+
return lastRule;
|
|
294
|
+
lastBag = bag;
|
|
295
|
+
lastRule = ruleFor(state, bag);
|
|
296
|
+
return lastRule;
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
/** No delay: a change's that gives none. */
|
|
300
|
+
const NO_DELAY = () => 0;
|
|
301
|
+
/** A change's delay as a function of a mover's rank and how many move: once a change. */
|
|
302
|
+
const delayOfChange = /*#__PURE__*/ Match.type().pipe(Match.when(Predicate.isUndefined, () => NO_DELAY), Match.when(Predicate.isNumber, (seconds) => () => seconds), Match.orElse((delayOf) => delayOf));
|
|
303
|
+
/**
|
|
304
|
+
* A new member, `subject`, starting from `start`'s values (a key it doesn't give starts at 0): its
|
|
305
|
+
* slot, which every other operation takes.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* const slot = Batch.enter(grid, tile, { x: 0, y: 0 })
|
|
309
|
+
*/
|
|
310
|
+
export const enter = dual(3, (self, subject, start) => {
|
|
311
|
+
const state = stateOf(self);
|
|
312
|
+
const slot = slotFor(state);
|
|
313
|
+
state.subjects[slot] = subject;
|
|
314
|
+
// A start a member that left never took isn't this one's.
|
|
315
|
+
state.flags[slot] = (state.flags[slot] & ~(STARTS | STARTED)) | LIVE;
|
|
316
|
+
const base = slot * state.stride;
|
|
317
|
+
Arr.forEach(state.keys, (key, k) => {
|
|
318
|
+
const given = start[key];
|
|
319
|
+
let value = 0;
|
|
320
|
+
if (Predicate.isNotUndefined(given))
|
|
321
|
+
value = given;
|
|
322
|
+
const at = base + k;
|
|
323
|
+
state.positions[at] = value;
|
|
324
|
+
state.targets[at] = value;
|
|
325
|
+
// Its origin too: a tween steps every key of a member it moves, and one no change has turned lerps
|
|
326
|
+
// from its origin to its target, both its start (#712).
|
|
327
|
+
state.origins[at] = value;
|
|
328
|
+
state.velocities[at] = 0;
|
|
329
|
+
// A reused slot keeps nothing of the member before: at rest on its start, on its own transition.
|
|
330
|
+
state.elapsed[at] = 0;
|
|
331
|
+
state.delays[at] = 0;
|
|
332
|
+
release(state, state.rules[at]);
|
|
333
|
+
state.rules[at] = state.ownRules[k];
|
|
334
|
+
});
|
|
335
|
+
return slot;
|
|
336
|
+
});
|
|
337
|
+
/**
|
|
338
|
+
* The member at `slot` leaves: it isn't written again, and its slot goes to the next to enter.
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* Batch.leave(grid, slot)
|
|
342
|
+
*/
|
|
343
|
+
// Its slot stays in its moving list until the next step drops it: no search per leave.
|
|
344
|
+
export const leave = dual(2, (self, slot) => {
|
|
345
|
+
const state = stateOf(self);
|
|
346
|
+
if ((state.flags[slot] & LIVE) === 0)
|
|
347
|
+
return;
|
|
348
|
+
state.flags[slot] = state.flags[slot] & ~LIVE;
|
|
349
|
+
state.subjects[slot] = undefined;
|
|
350
|
+
state.free.push(slot);
|
|
351
|
+
});
|
|
352
|
+
/**
|
|
353
|
+
* The member at `slot` is drawn on `subject` from now on: its values, speed, targets and delays carry
|
|
354
|
+
* on as they were. For an element its framework made again for the same member.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* Batch.replace(grid, slot, element)
|
|
358
|
+
*/
|
|
359
|
+
export const replace = dual(3, (self, slot, subject) => {
|
|
360
|
+
const state = stateOf(self);
|
|
361
|
+
if ((state.flags[slot] & LIVE) === 0)
|
|
362
|
+
return;
|
|
363
|
+
state.subjects[slot] = subject;
|
|
364
|
+
});
|
|
365
|
+
/** A slot for a new member: one a `leave` freed, else the next, the arrays doubled when they're full. */
|
|
366
|
+
const slotFor = (state) => {
|
|
367
|
+
const freed = state.free.pop();
|
|
368
|
+
if (Predicate.isNotUndefined(freed))
|
|
369
|
+
return freed;
|
|
370
|
+
const slot = state.used;
|
|
371
|
+
if (slot === state.capacity)
|
|
372
|
+
grow(state, state.capacity * 2);
|
|
373
|
+
state.used = slot + 1;
|
|
374
|
+
return slot;
|
|
375
|
+
};
|
|
376
|
+
/** `from` copied into a new array of `size`: a grown Batch's. */
|
|
377
|
+
const grown = (from, size) => {
|
|
378
|
+
const to = new from.constructor(size);
|
|
379
|
+
to.set(from);
|
|
380
|
+
return to;
|
|
381
|
+
};
|
|
382
|
+
/** `state`'s arrays grown to hold `capacity` members, every member's state copied across. */
|
|
383
|
+
const grow = (state, capacity) => {
|
|
384
|
+
const size = capacity * state.stride;
|
|
385
|
+
state.capacity = capacity;
|
|
386
|
+
state.positions = grown(state.positions, size);
|
|
387
|
+
state.velocities = grown(state.velocities, size);
|
|
388
|
+
state.targets = grown(state.targets, size);
|
|
389
|
+
state.elapsed = grown(state.elapsed, size);
|
|
390
|
+
state.delays = grown(state.delays, size);
|
|
391
|
+
state.restDeltas = grown(state.restDeltas, size);
|
|
392
|
+
state.restSpeeds = grown(state.restSpeeds, size);
|
|
393
|
+
state.origins = grown(state.origins, size);
|
|
394
|
+
state.originVelocities = grown(state.originVelocities, size);
|
|
395
|
+
state.rules = grown(state.rules, size);
|
|
396
|
+
state.pending = grown(state.pending, size);
|
|
397
|
+
state.movers = grown(state.movers, capacity);
|
|
398
|
+
state.flags = grown(state.flags, capacity);
|
|
399
|
+
state.plainList = grown(state.plainList, capacity);
|
|
400
|
+
state.ruledList = grown(state.ruledList, capacity);
|
|
401
|
+
};
|
|
402
|
+
/**
|
|
403
|
+
* The key at `at` turned on `rule` toward `goal`, from `start` at `speed`, waiting `wait` past its
|
|
404
|
+
* transition's own delay: its rule held, and its start, its speed then and its rest thresholds kept.
|
|
405
|
+
* A driven change's, at its play; `retarget` keeps the same writes inline, less the speed.
|
|
406
|
+
*/
|
|
407
|
+
// Per key a driven change plays: plain writes.
|
|
408
|
+
const turn = (state, at, rule, goal, start, speed, wait) => {
|
|
409
|
+
const entry = state.entries[rule];
|
|
410
|
+
const before = state.rules[at];
|
|
411
|
+
if (before !== rule) {
|
|
412
|
+
hold(state, rule);
|
|
413
|
+
release(state, before);
|
|
414
|
+
state.rules[at] = rule;
|
|
415
|
+
}
|
|
416
|
+
state.targets[at] = goal;
|
|
417
|
+
state.origins[at] = start;
|
|
418
|
+
state.originVelocities[at] = speed;
|
|
419
|
+
state.delays[at] = entry.delay + wait;
|
|
420
|
+
const distance = goal - start;
|
|
421
|
+
state.restDeltas[at] = Spring.restDeltaFor(entry.spring, distance);
|
|
422
|
+
state.restSpeeds[at] = Spring.restSpeedFor(entry.spring, distance);
|
|
423
|
+
// A spring that ends at its duration starts from rest, as Motion's (#359).
|
|
424
|
+
if (entry.kind !== TIMED)
|
|
425
|
+
return;
|
|
426
|
+
state.velocities[at] = 0;
|
|
427
|
+
state.originVelocities[at] = 0;
|
|
428
|
+
};
|
|
429
|
+
/**
|
|
430
|
+
* One change: each of `slots`' members turned toward `targetOf(slot, k)` for its `k`th key (`NaN`
|
|
431
|
+
* keeps that key's target). A member whose targets are unchanged gets nothing. It returns how many
|
|
432
|
+
* members it sets off from rest.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* const grid = Batch.make({ keys: ["x", "y"], writer, transition: { type: "spring" } })
|
|
436
|
+
* Batch.retarget(grid, slots, (slot, k) => cellOf(slot)[k], { delay: Stagger.make(0.02) })
|
|
437
|
+
*/
|
|
438
|
+
export const retarget = dual(isBatch, (self, slots, targetOf, options) => {
|
|
439
|
+
const state = stateOf(self);
|
|
440
|
+
const { stride, pending, movers, targets, flags } = state;
|
|
441
|
+
// Per member per key at a change, a Batch's own loops over its typed arrays, as its step's (ADR-0025,
|
|
442
|
+
// *Amendment (#665)*): `Arr.forEach` over the slots cost a closure call each. The first pass keeps
|
|
443
|
+
// each new target and finds the members that move, so the second can count them for their delays.
|
|
444
|
+
let n = 0;
|
|
445
|
+
const given = slots.length;
|
|
446
|
+
const fromOf = UndefinedOr.map(options, (bag) => bag.from);
|
|
447
|
+
const starts = Predicate.isNotUndefined(fromOf);
|
|
448
|
+
const speedOf = UndefinedOr.map(options, (bag) => bag.velocity);
|
|
449
|
+
for (let g = 0; g < given; g++) {
|
|
450
|
+
const slot = slots[g];
|
|
451
|
+
const flag = flags[slot];
|
|
452
|
+
// A member that left takes no change; one given twice moves once.
|
|
453
|
+
if ((flag & LIVE) === 0)
|
|
454
|
+
continue;
|
|
455
|
+
if ((flag & COUNTED) !== 0)
|
|
456
|
+
continue;
|
|
457
|
+
const base = slot * stride;
|
|
458
|
+
let moves = false;
|
|
459
|
+
for (let k = 0; k < stride; k++) {
|
|
460
|
+
const at = base + k;
|
|
461
|
+
let target = targetOf(slot, k);
|
|
462
|
+
// A key started from a value given (#665's PR 4): there now, at its speed, toward its target, the
|
|
463
|
+
// one it has unless a new one's given. A member it's given for moves, so it's set off below: its
|
|
464
|
+
// target is cleared here, so it turns there even onto the one it had.
|
|
465
|
+
if (starts) {
|
|
466
|
+
const jump = fromOf(slot, k);
|
|
467
|
+
if (!Number.isNaN(jump)) {
|
|
468
|
+
if (Number.isNaN(target))
|
|
469
|
+
target = targets[at];
|
|
470
|
+
state.positions[at] = jump;
|
|
471
|
+
state.velocities[at] = 0;
|
|
472
|
+
if (Predicate.isNotUndefined(speedOf))
|
|
473
|
+
state.velocities[at] = speedOf(slot, k);
|
|
474
|
+
targets[at] = Number.NaN;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
pending[at] = target;
|
|
478
|
+
if (!Number.isNaN(target) && target !== targets[at])
|
|
479
|
+
moves = true;
|
|
480
|
+
}
|
|
481
|
+
if (!moves)
|
|
482
|
+
continue;
|
|
483
|
+
flags[slot] = flag | COUNTED;
|
|
484
|
+
movers[n] = slot;
|
|
485
|
+
n++;
|
|
486
|
+
}
|
|
487
|
+
// A change that moves nothing touches no transition.
|
|
488
|
+
if (n === 0)
|
|
489
|
+
return 0;
|
|
490
|
+
const transitions = UndefinedOr.map(options, (bag) => bag.transition);
|
|
491
|
+
const rules = changeRulesOf(state, transitions);
|
|
492
|
+
const ruleAt = ruleAtOf(state, transitions);
|
|
493
|
+
const delay = UndefinedOr.map(options, (bag) => bag.delay);
|
|
494
|
+
if (Predicate.isNotUndefined(delay))
|
|
495
|
+
checked(Delay, delay, "delay");
|
|
496
|
+
const delayOf = delayOfChange(delay);
|
|
497
|
+
// This change's transitions are held while it runs, so a key it moves off one doesn't free one it
|
|
498
|
+
// gives another key; one it gives no key is freed after.
|
|
499
|
+
const held = Arr.dedupe(rules);
|
|
500
|
+
Arr.forEach(held, (rule) => hold(state, rule));
|
|
501
|
+
const driven = state.animations.size > 0;
|
|
502
|
+
// Started partway (#706): each key it turns waits, `elapsed` `NaN`, for its member's first step to read where.
|
|
503
|
+
const startAt = UndefinedOr.map(options, (bag) => bag.startAt);
|
|
504
|
+
let fresh = 0;
|
|
505
|
+
if (Predicate.isNotUndefined(startAt))
|
|
506
|
+
fresh = Number.NaN;
|
|
507
|
+
let setOff = 0;
|
|
508
|
+
for (let rank = 0; rank < n; rank++) {
|
|
509
|
+
const slot = movers[rank];
|
|
510
|
+
const base = slot * stride;
|
|
511
|
+
const wait = delayOf(rank, n);
|
|
512
|
+
// Whether a key it turns runs anything but the Batch's own spring.
|
|
513
|
+
let ruled = false;
|
|
514
|
+
for (let k = 0; k < stride; k++) {
|
|
515
|
+
const at = base + k;
|
|
516
|
+
const target = pending[at];
|
|
517
|
+
if (Number.isNaN(target))
|
|
518
|
+
continue;
|
|
519
|
+
if (target === targets[at])
|
|
520
|
+
continue;
|
|
521
|
+
let rule = rules[k];
|
|
522
|
+
if (Predicate.isNotUndefined(ruleAt))
|
|
523
|
+
rule = ruleAt(slot, k);
|
|
524
|
+
const entry = state.entries[rule];
|
|
525
|
+
if (rule !== 0 || entry.kind !== SPRING)
|
|
526
|
+
ruled = true;
|
|
527
|
+
// Taken from a driven change its Playhead holds, it starts from rest (#464).
|
|
528
|
+
if (driven && heldAt(state, at))
|
|
529
|
+
state.velocities[at] = 0;
|
|
530
|
+
// `turn`'s body, inline, less its start's speed, which a driven change sets at its play and a start
|
|
531
|
+
// partway at its first step.
|
|
532
|
+
const before = state.rules[at];
|
|
533
|
+
if (before !== rule) {
|
|
534
|
+
hold(state, rule);
|
|
535
|
+
release(state, before);
|
|
536
|
+
state.rules[at] = rule;
|
|
537
|
+
}
|
|
538
|
+
targets[at] = target;
|
|
539
|
+
state.origins[at] = state.positions[at];
|
|
540
|
+
state.elapsed[at] = fresh;
|
|
541
|
+
state.delays[at] = entry.delay + wait;
|
|
542
|
+
const distance = target - state.positions[at];
|
|
543
|
+
state.restDeltas[at] = Spring.restDeltaFor(entry.spring, distance);
|
|
544
|
+
state.restSpeeds[at] = Spring.restSpeedFor(entry.spring, distance);
|
|
545
|
+
// A spring that ends at its duration starts from rest, as Motion's (#359).
|
|
546
|
+
if (entry.kind === TIMED)
|
|
547
|
+
state.velocities[at] = 0;
|
|
548
|
+
}
|
|
549
|
+
let flag = flags[slot] & ~COUNTED;
|
|
550
|
+
if (Predicate.isNotUndefined(startAt)) {
|
|
551
|
+
if ((flag & STARTS) === 0)
|
|
552
|
+
state.startList.push(slot);
|
|
553
|
+
flag = flag | STARTS;
|
|
554
|
+
state.startAts[slot] = startAt;
|
|
555
|
+
}
|
|
556
|
+
flags[slot] = flag;
|
|
557
|
+
if ((flag & (IN_PLAIN | IN_RULED)) === 0)
|
|
558
|
+
setOff++;
|
|
559
|
+
// Into the ruled list, out of the plain one (which drops it at its next step); else into the plain
|
|
560
|
+
// list, if it's in neither.
|
|
561
|
+
if (ruled && (flag & IN_RULED) === 0) {
|
|
562
|
+
flags[slot] = (flag & ~IN_PLAIN) | IN_RULED;
|
|
563
|
+
state.ruledList[state.ruledCount] = slot;
|
|
564
|
+
state.ruledCount = state.ruledCount + 1;
|
|
565
|
+
}
|
|
566
|
+
if (!ruled && (flag & (IN_PLAIN | IN_RULED)) === 0) {
|
|
567
|
+
flags[slot] = flag | IN_PLAIN;
|
|
568
|
+
state.plainList[state.plainCount] = slot;
|
|
569
|
+
state.plainCount = state.plainCount + 1;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
Arr.forEach(held, (rule) => release(state, rule));
|
|
573
|
+
return setOff;
|
|
574
|
+
});
|
|
575
|
+
/** Whether any member moves: one `step` would move. */
|
|
576
|
+
export const isMoving = (self) => {
|
|
577
|
+
const state = stateOf(self);
|
|
578
|
+
return state.plainCount + state.ruledCount > 0;
|
|
579
|
+
};
|
|
580
|
+
/**
|
|
581
|
+
* Steps every moving member by `dt` seconds and writes each one: whether nothing moves now. The run
|
|
582
|
+
* on the Frame loop calls it each tick.
|
|
583
|
+
*/
|
|
584
|
+
// Per frame (ADR-0025, ADR-0030 decisions 10 and 11): `dt` clamped once (#78), then the two moving
|
|
585
|
+
// lists. Each steps a key by `springValues`' exact step, or `Machine.tween`'s, its spring's matrix
|
|
586
|
+
// worked out once a frame: called per key, `matrixFor` cost the step 2.5× (#665).
|
|
587
|
+
export const step = (self, dt) => {
|
|
588
|
+
const state = stateOf(self);
|
|
589
|
+
const clamped = Math.min(dt, Machine.MAX_DT);
|
|
590
|
+
state.frame = state.frame + 1;
|
|
591
|
+
if (state.startList.length > 0)
|
|
592
|
+
startPass(state, clamped);
|
|
593
|
+
if (state.plainCount > 0)
|
|
594
|
+
stepPlain(state, clamped);
|
|
595
|
+
if (state.ruledCount > 0)
|
|
596
|
+
stepRuled(state, clamped);
|
|
597
|
+
if (state.animations.size > 0)
|
|
598
|
+
Iter.forEach(state.animations, checkLanding);
|
|
599
|
+
state.flush();
|
|
600
|
+
state.onFrame();
|
|
601
|
+
// Counted after `onFrame`: a change it makes keeps the run going.
|
|
602
|
+
if (state.plainCount + state.ruledCount > 0)
|
|
603
|
+
return false;
|
|
604
|
+
// A driven change playing, paused at rest, still holds its entries.
|
|
605
|
+
if (state.entries.length > state.pinned && state.animations.size === 0)
|
|
606
|
+
forgetTransitions(state);
|
|
607
|
+
return true;
|
|
608
|
+
};
|
|
609
|
+
/**
|
|
610
|
+
* Writes every member where it is now, with no step, then flushes the writer: a change that started its
|
|
611
|
+
* members from their targets (`retarget`'s `from`), drawn now, before the page paints, where the run would
|
|
612
|
+
* draw it at its next tick.
|
|
613
|
+
*/
|
|
614
|
+
// #712: a Batch Reorder group's first read of its element places its items at once. Once a change, over
|
|
615
|
+
// every slot (ADR-0025, *Amendment (#665)*): a counted loop.
|
|
616
|
+
export const draw = (self) => {
|
|
617
|
+
const state = stateOf(self);
|
|
618
|
+
const { flags, subjects, positions, stride, write } = state;
|
|
619
|
+
for (let slot = 0; slot < state.used; slot++) {
|
|
620
|
+
if ((flags[slot] & LIVE) === 0)
|
|
621
|
+
continue;
|
|
622
|
+
write(subjects[slot], positions, slot * stride);
|
|
623
|
+
}
|
|
624
|
+
state.flush();
|
|
625
|
+
};
|
|
626
|
+
/**
|
|
627
|
+
* At rest, every transition but the Batch's own is forgotten: no member moves, so no rule is read
|
|
628
|
+
* until a change sets it again.
|
|
629
|
+
*/
|
|
630
|
+
// Once a rest, over every member's key (#665).
|
|
631
|
+
const forgetTransitions = (state) => {
|
|
632
|
+
const { pinned, stride } = state;
|
|
633
|
+
state.entries.length = pinned;
|
|
634
|
+
state.bags.length = pinned;
|
|
635
|
+
state.drivers.length = pinned;
|
|
636
|
+
state.freeRules.length = 0;
|
|
637
|
+
state.uses.fill(0);
|
|
638
|
+
state.ruleByBag.clear();
|
|
639
|
+
Arr.forEach(state.bags, (bag, rule) => state.ruleByBag.set(UndefinedOr.getOrThrow(bag), rule));
|
|
640
|
+
// Rest is not the per-frame path: a counted loop over every member's keys, once.
|
|
641
|
+
for (let at = 0; at < state.used * stride; at++)
|
|
642
|
+
state.rules[at] = state.ownRules[at % stride];
|
|
643
|
+
};
|
|
644
|
+
/** The members in the plain list, every key on the Batch's own spring, by one matrix. */
|
|
645
|
+
// Per frame. The ruled list's loop below, less its read of each key's rule and kind (#665).
|
|
646
|
+
const stepPlain = (state, clamped) => {
|
|
647
|
+
const { stride, positions, velocities, targets, elapsed, delays, restDeltas, restSpeeds, plainList, flags, subjects, write } = state;
|
|
648
|
+
const spring = state.entries[0].spring;
|
|
649
|
+
const m = Spring.matrixFor(spring, clamped);
|
|
650
|
+
const dd = m.dd;
|
|
651
|
+
const dv = m.dv;
|
|
652
|
+
const vd = m.vd;
|
|
653
|
+
const vv = m.vv;
|
|
654
|
+
const count = state.plainCount;
|
|
655
|
+
let kept = 0;
|
|
656
|
+
for (let a = 0; a < count; a++) {
|
|
657
|
+
const slot = plainList[a];
|
|
658
|
+
const flag = flags[slot];
|
|
659
|
+
// A member a change gave another transition is the ruled list's now; one that left is dropped here,
|
|
660
|
+
// unwritten. One the start pass drew this step is kept, unstepped (#706).
|
|
661
|
+
if (flag !== (LIVE | IN_PLAIN)) {
|
|
662
|
+
if (flag === (LIVE | IN_PLAIN | STARTED)) {
|
|
663
|
+
flags[slot] = LIVE | IN_PLAIN;
|
|
664
|
+
plainList[kept] = slot;
|
|
665
|
+
kept++;
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
if ((flag & IN_PLAIN) !== 0)
|
|
669
|
+
flags[slot] = flag & ~IN_PLAIN;
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
const base = slot * stride;
|
|
673
|
+
let still = true;
|
|
674
|
+
for (let k = 0; k < stride; k++) {
|
|
675
|
+
const at = base + k;
|
|
676
|
+
const goal = targets[at];
|
|
677
|
+
const time = elapsed[at] + clamped;
|
|
678
|
+
let position = positions[at];
|
|
679
|
+
let speed = velocities[at];
|
|
680
|
+
// Only the part of this tick past the delay moves it: all of it, or, the frame its delay ends, a part.
|
|
681
|
+
const simDt = Math.max(0, Math.min(clamped, time - delays[at]));
|
|
682
|
+
if (simDt === clamped) {
|
|
683
|
+
const d = position - goal;
|
|
684
|
+
position = goal + dd * d + dv * speed;
|
|
685
|
+
speed = vd * d + vv * speed;
|
|
686
|
+
}
|
|
687
|
+
if (simDt > 0 && simDt < clamped) {
|
|
688
|
+
const part = Spring.matrixFor(spring, simDt);
|
|
689
|
+
const d = position - goal;
|
|
690
|
+
position = goal + part.dd * d + part.dv * speed;
|
|
691
|
+
speed = part.vd * d + part.vv * speed;
|
|
692
|
+
}
|
|
693
|
+
const settled = simDt > 0 && Math.abs(speed) <= restSpeeds[at] && Math.abs(position - goal) <= restDeltas[at];
|
|
694
|
+
if (settled) {
|
|
695
|
+
position = goal;
|
|
696
|
+
speed = 0;
|
|
697
|
+
}
|
|
698
|
+
if (!settled)
|
|
699
|
+
still = false;
|
|
700
|
+
positions[at] = position;
|
|
701
|
+
velocities[at] = speed;
|
|
702
|
+
elapsed[at] = time;
|
|
703
|
+
}
|
|
704
|
+
write(subjects[slot], positions, base);
|
|
705
|
+
// At rest: still its member's, in neither list.
|
|
706
|
+
if (still) {
|
|
707
|
+
flags[slot] = LIVE;
|
|
708
|
+
continue;
|
|
709
|
+
}
|
|
710
|
+
plainList[kept] = slot;
|
|
711
|
+
kept++;
|
|
712
|
+
}
|
|
713
|
+
state.plainCount = kept;
|
|
714
|
+
};
|
|
715
|
+
/** A tween's step for the key at `at`, written in place: whether it has landed. */
|
|
716
|
+
// Per frame: `Machine.tween`'s value, eased from the key's start, its speed from its last frame.
|
|
717
|
+
const stepTween = (state, at, tween, time, dt) => {
|
|
718
|
+
const delay = state.delays[at];
|
|
719
|
+
// By its duration as given: a 0 s tween lands as its delay ends, at that very time (#706), where its
|
|
720
|
+
// resolved duration, at least `Number.EPSILON` so it divides, would hold it a step longer.
|
|
721
|
+
const landed = time >= delay + tween.seconds;
|
|
722
|
+
// Landed, at its target.
|
|
723
|
+
let next = state.targets[at];
|
|
724
|
+
if (!landed)
|
|
725
|
+
next = Scalar.lerp(state.origins[at], state.targets[at], tween.ease(Scalar.clamp(Math.max(0, time - delay) / tween.duration, 0, 1)));
|
|
726
|
+
if (landed)
|
|
727
|
+
state.velocities[at] = 0;
|
|
728
|
+
if (!landed && dt > 0)
|
|
729
|
+
state.velocities[at] = (next - state.positions[at]) / dt;
|
|
730
|
+
state.positions[at] = next;
|
|
731
|
+
return landed;
|
|
732
|
+
};
|
|
733
|
+
/** The members in the ruled list, each key by the transition its rule names: a spring's matrix, or a tween. */
|
|
734
|
+
// Per frame. Each spring entry's matrix for this `dt`, once, then plain reads by rule (#665).
|
|
735
|
+
const stepRuled = (state, clamped) => {
|
|
736
|
+
const { stride, entries, kinds, matrices, dts, drivers, rules, positions, velocities, targets, elapsed, delays, restDeltas, restSpeeds, ruledList, flags, subjects, write } = state;
|
|
737
|
+
// Each entry's step: the frame's, or a driven change's at its Playhead's speed, which moves its time;
|
|
738
|
+
// then each spring's matrix for it, once.
|
|
739
|
+
let driven = false;
|
|
740
|
+
for (let e = 0; e < entries.length; e++) {
|
|
741
|
+
const kind = kinds[e];
|
|
742
|
+
if (kind === FREE)
|
|
743
|
+
continue;
|
|
744
|
+
let dt = clamped;
|
|
745
|
+
const driver = drivers[e];
|
|
746
|
+
if (Predicate.isNotUndefined(driver)) {
|
|
747
|
+
driven = true;
|
|
748
|
+
dt = drivenStep(driver, clamped, state.frame);
|
|
749
|
+
}
|
|
750
|
+
dts[e] = dt;
|
|
751
|
+
// A step of 0 s too, the identity: the loop below moves a spring by its matrix whenever all its step
|
|
752
|
+
// is past the delay, which 0 s is, and the matrix a 0 s step skipped was the last frame's, or zeros,
|
|
753
|
+
// landing every spring on its target (a Clock's tick can be 0 s; #706).
|
|
754
|
+
if (kind === TWEEN || dt < 0)
|
|
755
|
+
continue;
|
|
756
|
+
const m = Spring.matrixFor(entries[e].spring, dt);
|
|
757
|
+
matrices[e * MATRIX] = m.dd;
|
|
758
|
+
matrices[e * MATRIX + 1] = m.dv;
|
|
759
|
+
matrices[e * MATRIX + 2] = m.vd;
|
|
760
|
+
matrices[e * MATRIX + 3] = m.vv;
|
|
761
|
+
}
|
|
762
|
+
// While a driven change plays, each key steps by its entry's own step, out of line: read per key, it
|
|
763
|
+
// cost every ruled member's step 5% (Bun and Chromium, 10,000 members; #665).
|
|
764
|
+
if (driven) {
|
|
765
|
+
stepRuledDriven(state);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
const count = state.ruledCount;
|
|
769
|
+
let kept = 0;
|
|
770
|
+
for (let a = 0; a < count; a++) {
|
|
771
|
+
const slot = ruledList[a];
|
|
772
|
+
const flag = flags[slot];
|
|
773
|
+
// A member that left is dropped here, unwritten; one the start pass drew this step is kept, unstepped (#706).
|
|
774
|
+
if (flag !== (LIVE | IN_RULED)) {
|
|
775
|
+
if ((flag & LIVE) === 0) {
|
|
776
|
+
flags[slot] = flag & ~IN_RULED;
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
if ((flag & STARTED) !== 0) {
|
|
780
|
+
flags[slot] = flag & ~STARTED;
|
|
781
|
+
ruledList[kept] = slot;
|
|
782
|
+
kept++;
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
const base = slot * stride;
|
|
787
|
+
let still = true;
|
|
788
|
+
for (let k = 0; k < stride; k++) {
|
|
789
|
+
const at = base + k;
|
|
790
|
+
const rule = rules[at];
|
|
791
|
+
const kind = kinds[rule];
|
|
792
|
+
const time = elapsed[at] + clamped;
|
|
793
|
+
elapsed[at] = time;
|
|
794
|
+
// A tween, out of line.
|
|
795
|
+
if (kind === TWEEN) {
|
|
796
|
+
if (!stepTween(state, at, entries[rule].tween, time, clamped))
|
|
797
|
+
still = false;
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
// A spring, as `springValues`': only the part of this tick past the delay moves it.
|
|
801
|
+
const goal = targets[at];
|
|
802
|
+
const delay = delays[at];
|
|
803
|
+
let position = positions[at];
|
|
804
|
+
let speed = velocities[at];
|
|
805
|
+
const simDt = Math.max(0, Math.min(clamped, time - delay));
|
|
806
|
+
if (simDt === clamped) {
|
|
807
|
+
const mi = rule * MATRIX;
|
|
808
|
+
const d = position - goal;
|
|
809
|
+
position = goal + matrices[mi] * d + matrices[mi + 1] * speed;
|
|
810
|
+
speed = matrices[mi + 2] * d + matrices[mi + 3] * speed;
|
|
811
|
+
}
|
|
812
|
+
if (simDt > 0 && simDt < clamped) {
|
|
813
|
+
const part = Spring.matrixFor(entries[rule].spring, simDt);
|
|
814
|
+
const d = position - goal;
|
|
815
|
+
position = goal + part.dd * d + part.dv * speed;
|
|
816
|
+
speed = part.vd * d + part.vv * speed;
|
|
817
|
+
}
|
|
818
|
+
// A spring rests by its thresholds; one that ends at its duration, there (#359).
|
|
819
|
+
let settled = simDt > 0 && Math.abs(speed) <= restSpeeds[at] && Math.abs(position - goal) <= restDeltas[at];
|
|
820
|
+
if (kind === TIMED)
|
|
821
|
+
settled = time - delay >= entries[rule].spring.duration;
|
|
822
|
+
if (settled) {
|
|
823
|
+
position = goal;
|
|
824
|
+
speed = 0;
|
|
825
|
+
}
|
|
826
|
+
if (!settled)
|
|
827
|
+
still = false;
|
|
828
|
+
positions[at] = position;
|
|
829
|
+
velocities[at] = speed;
|
|
830
|
+
}
|
|
831
|
+
write(subjects[slot], positions, base);
|
|
832
|
+
// At rest: still its member's, in neither list (a member is in one at most).
|
|
833
|
+
if (still) {
|
|
834
|
+
flags[slot] = LIVE;
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
ruledList[kept] = slot;
|
|
838
|
+
kept++;
|
|
839
|
+
}
|
|
840
|
+
state.ruledCount = kept;
|
|
841
|
+
};
|
|
842
|
+
/** The members in the ruled list while a driven change plays: each key by its entry's step this frame, forwards or back. */
|
|
843
|
+
// Per frame, while a driven change plays: `stepRuled`'s loop, by each entry's `dt`.
|
|
844
|
+
const stepRuledDriven = (state) => {
|
|
845
|
+
const { stride, entries, kinds, matrices, dts, rules, positions, velocities, targets, elapsed, delays, restDeltas, restSpeeds, ruledList, flags, subjects, write } = state;
|
|
846
|
+
const count = state.ruledCount;
|
|
847
|
+
let kept = 0;
|
|
848
|
+
for (let a = 0; a < count; a++) {
|
|
849
|
+
const slot = ruledList[a];
|
|
850
|
+
const flag = flags[slot];
|
|
851
|
+
// A member that left is dropped here, unwritten; one the start pass drew this step is kept, unstepped (#706).
|
|
852
|
+
if (flag !== (LIVE | IN_RULED)) {
|
|
853
|
+
if ((flag & LIVE) === 0) {
|
|
854
|
+
flags[slot] = flag & ~IN_RULED;
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
857
|
+
if ((flag & STARTED) !== 0) {
|
|
858
|
+
flags[slot] = flag & ~STARTED;
|
|
859
|
+
ruledList[kept] = slot;
|
|
860
|
+
kept++;
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
const base = slot * stride;
|
|
865
|
+
let still = true;
|
|
866
|
+
for (let k = 0; k < stride; k++) {
|
|
867
|
+
const at = base + k;
|
|
868
|
+
const rule = rules[at];
|
|
869
|
+
const kind = kinds[rule];
|
|
870
|
+
const dt = dts[rule];
|
|
871
|
+
// Played backwards: its value worked out again where its Playhead has gone back to, out of line.
|
|
872
|
+
if (dt < 0) {
|
|
873
|
+
if (!stepBack(state, at, rule, dt))
|
|
874
|
+
still = false;
|
|
875
|
+
continue;
|
|
876
|
+
}
|
|
877
|
+
const time = elapsed[at] + dt;
|
|
878
|
+
elapsed[at] = time;
|
|
879
|
+
// A tween, out of line.
|
|
880
|
+
if (kind === TWEEN) {
|
|
881
|
+
if (!stepTween(state, at, entries[rule].tween, time, dt))
|
|
882
|
+
still = false;
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
// Keyframes, out of line: only a driven change's.
|
|
886
|
+
if (kind === KEYFRAMES) {
|
|
887
|
+
if (!stepKeyframes(state, at, rule, time, dt))
|
|
888
|
+
still = false;
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
891
|
+
// A spring, as `springValues`': only the part of this tick past the delay moves it.
|
|
892
|
+
const goal = targets[at];
|
|
893
|
+
const delay = delays[at];
|
|
894
|
+
let position = positions[at];
|
|
895
|
+
let speed = velocities[at];
|
|
896
|
+
const simDt = Math.max(0, Math.min(dt, time - delay));
|
|
897
|
+
if (simDt > 0 && simDt === dt) {
|
|
898
|
+
const mi = rule * MATRIX;
|
|
899
|
+
const d = position - goal;
|
|
900
|
+
position = goal + matrices[mi] * d + matrices[mi + 1] * speed;
|
|
901
|
+
speed = matrices[mi + 2] * d + matrices[mi + 3] * speed;
|
|
902
|
+
}
|
|
903
|
+
if (simDt > 0 && simDt < dt) {
|
|
904
|
+
const part = Spring.matrixFor(entries[rule].spring, simDt);
|
|
905
|
+
const d = position - goal;
|
|
906
|
+
position = goal + part.dd * d + part.dv * speed;
|
|
907
|
+
speed = part.vd * d + part.vv * speed;
|
|
908
|
+
}
|
|
909
|
+
// A spring rests by its thresholds; one that ends at its duration, there (#359). Held by a paused
|
|
910
|
+
// Playhead, one already on its target is at rest.
|
|
911
|
+
let settled = simDt > 0 && Math.abs(speed) <= restSpeeds[at] && Math.abs(position - goal) <= restDeltas[at];
|
|
912
|
+
if (kind === TIMED)
|
|
913
|
+
settled = time - delay >= entries[rule].spring.duration;
|
|
914
|
+
if (dt === 0)
|
|
915
|
+
settled = speed === 0 && position === goal && time >= delay;
|
|
916
|
+
if (settled) {
|
|
917
|
+
position = goal;
|
|
918
|
+
speed = 0;
|
|
919
|
+
}
|
|
920
|
+
if (!settled)
|
|
921
|
+
still = false;
|
|
922
|
+
positions[at] = position;
|
|
923
|
+
velocities[at] = speed;
|
|
924
|
+
}
|
|
925
|
+
write(subjects[slot], positions, base);
|
|
926
|
+
// At rest: still its member's, in neither list (a member is in one at most).
|
|
927
|
+
if (still) {
|
|
928
|
+
flags[slot] = LIVE;
|
|
929
|
+
continue;
|
|
930
|
+
}
|
|
931
|
+
ruledList[kept] = slot;
|
|
932
|
+
kept++;
|
|
933
|
+
}
|
|
934
|
+
state.ruledCount = kept;
|
|
935
|
+
};
|
|
936
|
+
// --- A change driven by a Playhead ---------------------------------------------
|
|
937
|
+
//
|
|
938
|
+
// #665, ADR-0069 decision 15: what `createAnimate`'s Playback controls act on when they animate a Batch.
|
|
939
|
+
// The change keeps each of its keys' start, so a seek works its value out where it'd be, exactly, from
|
|
940
|
+
// the spring's closed form (`Spring.matrixFor` over the time) or the tween's curve. Its transitions are
|
|
941
|
+
// entries of its own whose keys step at its Playhead's speed. A later change of one of its keys takes it,
|
|
942
|
+
// as a ward's Retarget takes an Imperative animation's (#433): the change can't land then. Keyframes
|
|
943
|
+
// (Q36): a Sequence's Segment of a Batch, each key through its own stops, on one entry of the change's.
|
|
944
|
+
const AnimationTypeId = "~@wardic/core/Batch/Animation";
|
|
945
|
+
const AnimationProto = {
|
|
946
|
+
[AnimationTypeId]: AnimationTypeId,
|
|
947
|
+
...Inspectable.BaseProto,
|
|
948
|
+
toJSON() {
|
|
949
|
+
return { _id: "Batch.Animation", keys: this.ats.length, playing: this.playing };
|
|
950
|
+
},
|
|
951
|
+
pipe() {
|
|
952
|
+
return pipeArguments(this, arguments);
|
|
953
|
+
},
|
|
954
|
+
};
|
|
955
|
+
/** Whether `u` is a Batch's Animation. */
|
|
956
|
+
export const isAnimation = (u) => Predicate.hasProperty(u, AnimationTypeId);
|
|
957
|
+
const animationOf = (self) => self;
|
|
958
|
+
/** The slot whose key is at `at`. */
|
|
959
|
+
const slotAt = (state, at) => Math.floor(at / state.stride);
|
|
960
|
+
/** A step's time, a frame at 60 Hz: what a tween's speed is worked out over, and `durationOf` steps by. */
|
|
961
|
+
const FRAME = Machine.CATCH_UP_DT;
|
|
962
|
+
/**
|
|
963
|
+
* A driven change's tick this step: the frame's at its Playhead's speed, 0 while it's paused. Worked out
|
|
964
|
+
* once a step, however many entries it drives, which moves its time and its Playhead's.
|
|
965
|
+
*/
|
|
966
|
+
// Per frame, per driven entry: as `Playback.drive`'s, its own time kept and written (#433).
|
|
967
|
+
const drivenStep = (animation, clamped, frame) => {
|
|
968
|
+
if (animation.frame === frame)
|
|
969
|
+
return animation.dt;
|
|
970
|
+
animation.frame = frame;
|
|
971
|
+
const head = animation.options.head;
|
|
972
|
+
let by = 0;
|
|
973
|
+
// Played from partway through, its first step lands there, whatever the `dt`, as `Playback.drive`'s does.
|
|
974
|
+
const landing = animation.landing;
|
|
975
|
+
animation.landing = false;
|
|
976
|
+
if (!landing && !head.paused)
|
|
977
|
+
by = tickOf(head, clamped, Machine.MAX_DT);
|
|
978
|
+
animation.dt = by;
|
|
979
|
+
animation.time = Math.max(0, animation.time + by);
|
|
980
|
+
writableOf(head).time = animation.time;
|
|
981
|
+
return by;
|
|
982
|
+
};
|
|
983
|
+
/** Whether the key at `at` is held by a paused Playhead: it hands on no speed, as a held element's frames read 0 (#464). */
|
|
984
|
+
// Per key per change, while a driven change plays.
|
|
985
|
+
const heldAt = (state, at) => {
|
|
986
|
+
const driver = state.drivers[state.rules[at]];
|
|
987
|
+
if (Predicate.isUndefined(driver))
|
|
988
|
+
return false;
|
|
989
|
+
return driver.options.head.paused;
|
|
990
|
+
};
|
|
991
|
+
/**
|
|
992
|
+
* The key at `at`'s value and speed at `time` into its change, from its start: a spring's by its closed
|
|
993
|
+
* form, a tween's by its curve, keyframes' by their stops. Whether it has landed there. A spring under its
|
|
994
|
+
* rest thresholds lands there when it `rests`; else it's left where its closed form has it, to land at its
|
|
995
|
+
* next step (a start partway, drawn where the Appear animation it takes over drew it, #706).
|
|
996
|
+
*/
|
|
997
|
+
const valueAt = (state, at, rule, time, rests) => {
|
|
998
|
+
const entry = state.entries[rule];
|
|
999
|
+
if (entry.kind === KEYFRAMES)
|
|
1000
|
+
return keyframesAt(state, at, rule, time);
|
|
1001
|
+
const delay = state.delays[at];
|
|
1002
|
+
const goal = state.targets[at];
|
|
1003
|
+
const from = state.origins[at];
|
|
1004
|
+
const active = Math.max(0, time - delay);
|
|
1005
|
+
if (entry.kind === TWEEN) {
|
|
1006
|
+
const tween = entry.tween;
|
|
1007
|
+
const landed = time >= delay + tween.seconds;
|
|
1008
|
+
// Landed, at its target, as `stepTween`'s.
|
|
1009
|
+
let value = goal;
|
|
1010
|
+
if (!landed)
|
|
1011
|
+
value = Scalar.lerp(from, goal, tween.ease(Scalar.clamp(active / tween.duration, 0, 1)));
|
|
1012
|
+
const before = Scalar.lerp(from, goal, tween.ease(Scalar.clamp(Math.max(0, active - FRAME) / tween.duration, 0, 1)));
|
|
1013
|
+
state.positions[at] = value;
|
|
1014
|
+
state.velocities[at] = (value - before) / FRAME;
|
|
1015
|
+
if (landed)
|
|
1016
|
+
state.velocities[at] = 0;
|
|
1017
|
+
return landed;
|
|
1018
|
+
}
|
|
1019
|
+
if (active === 0) {
|
|
1020
|
+
state.positions[at] = from;
|
|
1021
|
+
state.velocities[at] = state.originVelocities[at];
|
|
1022
|
+
return false;
|
|
1023
|
+
}
|
|
1024
|
+
const m = Spring.matrixFor(entry.spring, active);
|
|
1025
|
+
const d = from - goal;
|
|
1026
|
+
const v0 = state.originVelocities[at];
|
|
1027
|
+
let position = goal + m.dd * d + m.dv * v0;
|
|
1028
|
+
let speed = m.vd * d + m.vv * v0;
|
|
1029
|
+
let landed = rests && Math.abs(speed) <= state.restSpeeds[at] && Math.abs(position - goal) <= state.restDeltas[at];
|
|
1030
|
+
if (entry.kind === TIMED)
|
|
1031
|
+
landed = active >= entry.spring.duration;
|
|
1032
|
+
if (landed) {
|
|
1033
|
+
position = goal;
|
|
1034
|
+
speed = 0;
|
|
1035
|
+
}
|
|
1036
|
+
state.positions[at] = position;
|
|
1037
|
+
state.velocities[at] = speed;
|
|
1038
|
+
return landed;
|
|
1039
|
+
};
|
|
1040
|
+
/**
|
|
1041
|
+
* Each step's start pass, before the loops: each `STARTS` member where its change's `startAt` says, drawn,
|
|
1042
|
+
* and flagged `STARTED` so its loop keeps it unstepped; one given `NaN` moves from its start, its loop
|
|
1043
|
+
* stepping it as any other.
|
|
1044
|
+
*/
|
|
1045
|
+
// Once a member, at its first step after a change that starts it partway (#706): out of the loops, which
|
|
1046
|
+
// only clear a flag, so their code is the same size it was (Bun's step was 5–9% slower with this in them).
|
|
1047
|
+
const startPass = (state, dt) => {
|
|
1048
|
+
const { flags, startList } = state;
|
|
1049
|
+
// Over the members a change started partway since the last step: a counted loop, as the step's.
|
|
1050
|
+
for (let i = 0; i < startList.length; i++) {
|
|
1051
|
+
const slot = startList[i];
|
|
1052
|
+
const flag = flags[slot];
|
|
1053
|
+
if ((flag & STARTS) === 0)
|
|
1054
|
+
continue;
|
|
1055
|
+
flags[slot] = flag & ~STARTS;
|
|
1056
|
+
// A member that left takes no start, nor keeps one.
|
|
1057
|
+
if ((flag & LIVE) === 0) {
|
|
1058
|
+
state.startAts[slot] = undefined;
|
|
1059
|
+
continue;
|
|
1060
|
+
}
|
|
1061
|
+
if (startedPartway(state, slot, dt))
|
|
1062
|
+
flags[slot] = flags[slot] | STARTED;
|
|
1063
|
+
}
|
|
1064
|
+
startList.length = 0;
|
|
1065
|
+
};
|
|
1066
|
+
/**
|
|
1067
|
+
* A member's start partway: where its change's `startAt` says, or, given `NaN`, nowhere yet (its keys wait
|
|
1068
|
+
* at its start, and its loop moves it this step). Whether it started there, drawn: the keys the change
|
|
1069
|
+
* turned at that time, unlanded (`valueAt`'s `rests`), from the speed they had when it came; its other
|
|
1070
|
+
* moving keys a step on from where they are, as its loop would.
|
|
1071
|
+
*/
|
|
1072
|
+
const startedPartway = (state, slot, dt) => {
|
|
1073
|
+
const { stride, elapsed, rules, positions, velocities, targets, originVelocities, startAts } = state;
|
|
1074
|
+
const startAt = UndefinedOr.getOrThrow(startAts[slot]);
|
|
1075
|
+
startAts[slot] = undefined;
|
|
1076
|
+
const time = startAt(slot);
|
|
1077
|
+
const base = slot * stride;
|
|
1078
|
+
const started = !Number.isNaN(time);
|
|
1079
|
+
for (let k = 0; k < stride; k++) {
|
|
1080
|
+
const at = base + k;
|
|
1081
|
+
const turned = Number.isNaN(elapsed[at]);
|
|
1082
|
+
// From its start, given `NaN`: its loop moves it.
|
|
1083
|
+
if (turned && !started)
|
|
1084
|
+
elapsed[at] = 0;
|
|
1085
|
+
if (!started)
|
|
1086
|
+
continue;
|
|
1087
|
+
if (turned) {
|
|
1088
|
+
// Nothing has moved it since its change: its speed is the one it came with.
|
|
1089
|
+
originVelocities[at] = velocities[at];
|
|
1090
|
+
elapsed[at] = time;
|
|
1091
|
+
valueAt(state, at, rules[at], time, false);
|
|
1092
|
+
continue;
|
|
1093
|
+
}
|
|
1094
|
+
if (velocities[at] === 0 && positions[at] === targets[at])
|
|
1095
|
+
continue;
|
|
1096
|
+
stepKey(state, at, rules[at], dt);
|
|
1097
|
+
}
|
|
1098
|
+
if (started)
|
|
1099
|
+
state.write(state.subjects[slot], positions, base);
|
|
1100
|
+
return started;
|
|
1101
|
+
};
|
|
1102
|
+
/**
|
|
1103
|
+
* The key at `at` a step of `dt` on, as the loops step it: a tween's, keyframes', or a spring's by its
|
|
1104
|
+
* matrix for the part past its delay, landing by its thresholds or its duration.
|
|
1105
|
+
*/
|
|
1106
|
+
// Rare: a key the loops don't reach this step, of a member started partway that another change moves.
|
|
1107
|
+
const stepKey = (state, at, rule, dt) => {
|
|
1108
|
+
const entry = state.entries[rule];
|
|
1109
|
+
const time = state.elapsed[at] + dt;
|
|
1110
|
+
state.elapsed[at] = time;
|
|
1111
|
+
if (entry.kind === TWEEN) {
|
|
1112
|
+
stepTween(state, at, entry.tween, time, dt);
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
if (entry.kind === KEYFRAMES) {
|
|
1116
|
+
stepKeyframes(state, at, rule, time, dt);
|
|
1117
|
+
return;
|
|
1118
|
+
}
|
|
1119
|
+
// The loops' spring step: only the part of this step past the delay moves it.
|
|
1120
|
+
const delay = state.delays[at];
|
|
1121
|
+
const goal = state.targets[at];
|
|
1122
|
+
const simDt = Math.max(0, Math.min(dt, time - delay));
|
|
1123
|
+
let position = state.positions[at];
|
|
1124
|
+
let speed = state.velocities[at];
|
|
1125
|
+
if (simDt > 0) {
|
|
1126
|
+
const m = Spring.matrixFor(entry.spring, simDt);
|
|
1127
|
+
const d = position - goal;
|
|
1128
|
+
position = goal + m.dd * d + m.dv * speed;
|
|
1129
|
+
speed = m.vd * d + m.vv * speed;
|
|
1130
|
+
}
|
|
1131
|
+
let settled = simDt > 0 && Math.abs(speed) <= state.restSpeeds[at] && Math.abs(position - goal) <= state.restDeltas[at];
|
|
1132
|
+
if (entry.kind === TIMED)
|
|
1133
|
+
settled = time - delay >= entry.spring.duration;
|
|
1134
|
+
if (settled) {
|
|
1135
|
+
position = goal;
|
|
1136
|
+
speed = 0;
|
|
1137
|
+
}
|
|
1138
|
+
state.positions[at] = position;
|
|
1139
|
+
state.velocities[at] = speed;
|
|
1140
|
+
};
|
|
1141
|
+
/** A key played backwards by `dt`: its value where its Playhead went back to; at its start, at rest there. */
|
|
1142
|
+
// Per frame, per key of a change played backwards: rare, so out of line.
|
|
1143
|
+
const stepBack = (state, at, rule, dt) => {
|
|
1144
|
+
const time = Math.max(0, state.elapsed[at] + dt);
|
|
1145
|
+
state.elapsed[at] = time;
|
|
1146
|
+
valueAt(state, at, rule, time, true);
|
|
1147
|
+
if (time > 0)
|
|
1148
|
+
return false;
|
|
1149
|
+
state.positions[at] = state.origins[at];
|
|
1150
|
+
state.velocities[at] = 0;
|
|
1151
|
+
return true;
|
|
1152
|
+
};
|
|
1153
|
+
/** Whether `animation` still moves the key at its `i`th: on its entry, toward its target. */
|
|
1154
|
+
const holds = (animation, i) => {
|
|
1155
|
+
const at = animation.ats[i];
|
|
1156
|
+
return animation.state.rules[at] === animation.rules[i] && animation.state.targets[at] === animation.goals[i];
|
|
1157
|
+
};
|
|
1158
|
+
/** It stops playing: its entries step at the frame's pace again, and the table may let them go. */
|
|
1159
|
+
const finish = (animation) => {
|
|
1160
|
+
if (!animation.playing)
|
|
1161
|
+
return;
|
|
1162
|
+
animation.playing = false;
|
|
1163
|
+
const { state } = animation;
|
|
1164
|
+
state.animations.delete(animation);
|
|
1165
|
+
Arr.forEach(animation.held, (rule) => {
|
|
1166
|
+
state.drivers[rule] = undefined;
|
|
1167
|
+
release(state, rule);
|
|
1168
|
+
});
|
|
1169
|
+
};
|
|
1170
|
+
/** Whether the key at `animation`'s `i`th is at rest at its target, past its delay. */
|
|
1171
|
+
// Once a step, per key of a playing change: plain reads.
|
|
1172
|
+
const keyAtRest = (animation, i) => {
|
|
1173
|
+
const { state } = animation;
|
|
1174
|
+
const at = animation.ats[i];
|
|
1175
|
+
return state.velocities[at] === 0 && state.positions[at] === animation.goals[i] && state.elapsed[at] >= state.delays[at];
|
|
1176
|
+
};
|
|
1177
|
+
/** The members `animation` moves nothing of, told they landed, once, at its first play (`onMemberLanded`). */
|
|
1178
|
+
const tellIdle = (animation) => {
|
|
1179
|
+
if (animation.idleTold)
|
|
1180
|
+
return;
|
|
1181
|
+
animation.idleTold = true;
|
|
1182
|
+
const tell = animation.options.onMemberLanded;
|
|
1183
|
+
if (Predicate.isUndefined(tell))
|
|
1184
|
+
return;
|
|
1185
|
+
Arr.forEach(animation.idle, (slot) => tell(slot));
|
|
1186
|
+
};
|
|
1187
|
+
/**
|
|
1188
|
+
* Each member `animation` moves told, once, as every key of it it still moves has landed (`onMemberLanded`):
|
|
1189
|
+
* at its target, past its delay. One whose keys a later change took all is never told.
|
|
1190
|
+
*/
|
|
1191
|
+
// Once a step, per key of a playing change that tells its members: counted loops with plain reads.
|
|
1192
|
+
const tellMembers = (animation, tell) => {
|
|
1193
|
+
const { state, firsts, told, ats } = animation;
|
|
1194
|
+
const n = told.length;
|
|
1195
|
+
for (let r = 0; r < n; r++) {
|
|
1196
|
+
if (told[r] === 1)
|
|
1197
|
+
continue;
|
|
1198
|
+
let kept = false;
|
|
1199
|
+
let atRest = true;
|
|
1200
|
+
for (let i = firsts[r]; i < firsts[r + 1]; i++) {
|
|
1201
|
+
if (!holds(animation, i))
|
|
1202
|
+
continue;
|
|
1203
|
+
kept = true;
|
|
1204
|
+
if (!keyAtRest(animation, i)) {
|
|
1205
|
+
atRest = false;
|
|
1206
|
+
break;
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
if (!kept || !atRest)
|
|
1210
|
+
continue;
|
|
1211
|
+
told[r] = 1;
|
|
1212
|
+
tell(slotAt(state, ats[firsts[r]]));
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
/**
|
|
1216
|
+
* Whether `animation` has landed, once a step while it plays: every key it still moves at its target,
|
|
1217
|
+
* past its delay; or, played backwards, back at its start. It ends then. A key a later change took keeps
|
|
1218
|
+
* it from landing (#433's latest-wins, as a ward's): it ends unheard, once the keys it kept are at rest.
|
|
1219
|
+
*/
|
|
1220
|
+
const checkLanding = (animation) => {
|
|
1221
|
+
const { state, options } = animation;
|
|
1222
|
+
const backwards = options.head.speed < 0;
|
|
1223
|
+
const tell = options.onMemberLanded;
|
|
1224
|
+
if (Predicate.isNotUndefined(tell) && !backwards)
|
|
1225
|
+
tellMembers(animation, tell);
|
|
1226
|
+
const count = animation.ats.length;
|
|
1227
|
+
let atRest = true;
|
|
1228
|
+
let held = 0;
|
|
1229
|
+
// Once a step, per key of a playing change: a counted loop with plain reads.
|
|
1230
|
+
for (let i = 0; i < count; i++) {
|
|
1231
|
+
if (!holds(animation, i))
|
|
1232
|
+
continue;
|
|
1233
|
+
held++;
|
|
1234
|
+
if (backwards)
|
|
1235
|
+
continue;
|
|
1236
|
+
if (!keyAtRest(animation, i))
|
|
1237
|
+
atRest = false;
|
|
1238
|
+
}
|
|
1239
|
+
if (held > 0 && backwards && animation.time > 0)
|
|
1240
|
+
return;
|
|
1241
|
+
if (!atRest)
|
|
1242
|
+
return;
|
|
1243
|
+
// Played back to its start, each key it still moves rests there, its target with it: a later step of the
|
|
1244
|
+
// member, for a change of another key, leaves it there.
|
|
1245
|
+
if (backwards) {
|
|
1246
|
+
for (let i = 0; i < count; i++) {
|
|
1247
|
+
if (!holds(animation, i))
|
|
1248
|
+
continue;
|
|
1249
|
+
const at = animation.ats[i];
|
|
1250
|
+
state.targets[at] = state.origins[at];
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
finish(animation);
|
|
1254
|
+
if (held < count)
|
|
1255
|
+
return;
|
|
1256
|
+
UndefinedOr.map(options.onLanded, (told) => told());
|
|
1257
|
+
};
|
|
1258
|
+
/** A driven change's transition for a member's key, before its copy: its function's, or its own or `default`, else the key's own. */
|
|
1259
|
+
const animationBagsOf = (state, given) => {
|
|
1260
|
+
if (Predicate.isFunction(given))
|
|
1261
|
+
return given;
|
|
1262
|
+
// The Batch's own entries are transitions: `make`'s.
|
|
1263
|
+
const own = Arr.map(state.ownRules, (rule) => UndefinedOr.getOrThrow(state.bags[rule]));
|
|
1264
|
+
if (Predicate.isUndefined(given))
|
|
1265
|
+
return (_, k) => own[k];
|
|
1266
|
+
const bags = Arr.map(bagsOf(state, given), (bag, k) => UndefinedOr.match(bag, { onUndefined: () => own[k], onDefined: identity }));
|
|
1267
|
+
return (_, k) => bags[k];
|
|
1268
|
+
};
|
|
1269
|
+
/**
|
|
1270
|
+
* A change of `slots`' members toward `targetOf(slot, k)`, driven by `options.head`: nothing moves until
|
|
1271
|
+
* it's played. Its controls are `play`, `stop`, `cancel` and `durationOf`.
|
|
1272
|
+
*
|
|
1273
|
+
* @example
|
|
1274
|
+
* const head = Playback.make()
|
|
1275
|
+
* const shuffle = Batch.animate(grid, slots, (slot, k) => cellOf(slot)[k], { head, transition: { type: "spring" } })
|
|
1276
|
+
* Batch.play(shuffle)
|
|
1277
|
+
*/
|
|
1278
|
+
export const animate = dual(isBatch, (self, slots, targetOf, options) => {
|
|
1279
|
+
const state = stateOf(self);
|
|
1280
|
+
if (Predicate.isNotUndefined(options.delay))
|
|
1281
|
+
checked(Delay, options.delay, "delay");
|
|
1282
|
+
const bagOf = animationBagsOf(state, options.transition);
|
|
1283
|
+
// A copy of each transition it runs, made once: most members' keys give the bag the last one gave.
|
|
1284
|
+
const copies = new Map();
|
|
1285
|
+
let lastGiven = undefined;
|
|
1286
|
+
let lastCopy = undefined;
|
|
1287
|
+
const bag = (slot, k) => {
|
|
1288
|
+
const given = bagOf(slot, k);
|
|
1289
|
+
if (given === lastGiven)
|
|
1290
|
+
return lastCopy;
|
|
1291
|
+
let made = copies.get(given);
|
|
1292
|
+
if (Predicate.isUndefined(made)) {
|
|
1293
|
+
made = { ...given };
|
|
1294
|
+
copies.set(given, made);
|
|
1295
|
+
}
|
|
1296
|
+
lastGiven = given;
|
|
1297
|
+
lastCopy = made;
|
|
1298
|
+
return made;
|
|
1299
|
+
};
|
|
1300
|
+
return animationFor(state, options, slots, { bag, targetOf, keyframesOf: undefined });
|
|
1301
|
+
});
|
|
1302
|
+
/** A driven change, unplayed: worked out at its first play, toward targets or through keyframes. */
|
|
1303
|
+
const animationFor = (state, options, slots, moves) => {
|
|
1304
|
+
const animation = {
|
|
1305
|
+
state,
|
|
1306
|
+
options,
|
|
1307
|
+
slots,
|
|
1308
|
+
...moves,
|
|
1309
|
+
ats: new Int32Array(0),
|
|
1310
|
+
starts: new Float64Array(0),
|
|
1311
|
+
speeds: new Float64Array(0),
|
|
1312
|
+
goals: new Float64Array(0),
|
|
1313
|
+
waits: new Float64Array(0),
|
|
1314
|
+
rules: new Uint16Array(0),
|
|
1315
|
+
held: [],
|
|
1316
|
+
started: false,
|
|
1317
|
+
playing: false,
|
|
1318
|
+
duration: undefined,
|
|
1319
|
+
time: 0,
|
|
1320
|
+
frame: 0,
|
|
1321
|
+
dt: 0,
|
|
1322
|
+
landing: false,
|
|
1323
|
+
tracks: [],
|
|
1324
|
+
indexAt: new Int32Array(0),
|
|
1325
|
+
firsts: new Int32Array(0),
|
|
1326
|
+
told: new Uint8Array(0),
|
|
1327
|
+
idle: [],
|
|
1328
|
+
idleTold: false,
|
|
1329
|
+
};
|
|
1330
|
+
return Object.assign(Object.create(AnimationProto), animation);
|
|
1331
|
+
};
|
|
1332
|
+
/** A `keyframes` change's transition for a key: never read, its tracks run its keys. */
|
|
1333
|
+
const UNREAD_TRANSITION = { type: "tween" };
|
|
1334
|
+
const NO_BAG = () => UNREAD_TRANSITION;
|
|
1335
|
+
/** A `keyframes` change's target for a key: never read, its tracks end its keys. */
|
|
1336
|
+
const NO_TARGET = () => Number.NaN;
|
|
1337
|
+
/**
|
|
1338
|
+
* A change of `slots`' members through each key's own keyframes, `keyframesOf(slot, k)` (`undefined`
|
|
1339
|
+
* for a key it leaves), driven by `options.head`: each key as `Machine.keyframes` would run its stops,
|
|
1340
|
+
* frame for frame. Nothing moves until it's played; its controls are `animate`'s. A Sequence's Segment
|
|
1341
|
+
* of a Batch, its members' stops placed over the whole sequence, runs on it.
|
|
1342
|
+
*
|
|
1343
|
+
* @example
|
|
1344
|
+
* const head = Playback.make()
|
|
1345
|
+
* const wave = Batch.keyframes(grid, slots, (slot) => ({ stops: [Number.NaN, -20, 0], transition: { duration: 0.6, delay: slot * 0.05 } }), { head })
|
|
1346
|
+
* Batch.play(wave)
|
|
1347
|
+
*/
|
|
1348
|
+
// #665, Q36: one entry of its own, every key it moves on it, each key's track by its index.
|
|
1349
|
+
export const keyframes = dual(isBatch, (self, slots, keyframesOf, options) => {
|
|
1350
|
+
return animationFor(stateOf(self), options, slots, { bag: NO_BAG, targetOf: NO_TARGET, keyframesOf });
|
|
1351
|
+
});
|
|
1352
|
+
/** A key's keyframes resolved at its first play: its stops, each `NaN` the stop before, the first `current`; `undefined` for none. */
|
|
1353
|
+
// Once a play, per key: statements.
|
|
1354
|
+
const trackOf = (keyframes, current) => {
|
|
1355
|
+
const given = keyframes.stops;
|
|
1356
|
+
if (given.length === 0)
|
|
1357
|
+
return undefined;
|
|
1358
|
+
const resolved = resolveKeyframes(keyframes.transition);
|
|
1359
|
+
// One stop holds, as `Machine.keyframes`' does: it's there twice.
|
|
1360
|
+
const stops = new Float64Array(Math.max(2, given.length));
|
|
1361
|
+
let before = current;
|
|
1362
|
+
for (let i = 0; i < given.length; i++) {
|
|
1363
|
+
let stop = given[i];
|
|
1364
|
+
if (Number.isNaN(stop))
|
|
1365
|
+
stop = before;
|
|
1366
|
+
stops[i] = stop;
|
|
1367
|
+
before = stop;
|
|
1368
|
+
}
|
|
1369
|
+
if (given.length === 1)
|
|
1370
|
+
stops[1] = stops[0];
|
|
1371
|
+
const run = resolved.run;
|
|
1372
|
+
return {
|
|
1373
|
+
stops,
|
|
1374
|
+
stopTimes: stopTimesOf(stops.length, resolved.times, run.tween.duration),
|
|
1375
|
+
eases: resolved.eases,
|
|
1376
|
+
run,
|
|
1377
|
+
reverse: resolved.reverse,
|
|
1378
|
+
delay: resolved.delay,
|
|
1379
|
+
end: resolved.delay + run.total,
|
|
1380
|
+
};
|
|
1381
|
+
};
|
|
1382
|
+
/** {@link trackValue}'s pass, written in place: read right after, never across a call. */
|
|
1383
|
+
// Module state (#460, Q7): per-frame scratch, written and read within one key's step, never across one.
|
|
1384
|
+
const TRACK_PASS = { within: 0, reversed: false, flipped: false };
|
|
1385
|
+
/** A track's value `time` seconds from its start, as `Machine.keyframes`' Frame then: its first stop through its delay. */
|
|
1386
|
+
// Per frame, per key of a keyframes change: as `Machine.keyframes`' step, its delay held first (`Machine.delay`).
|
|
1387
|
+
const trackValue = (track, time) => {
|
|
1388
|
+
passAt(Math.max(0, time - track.delay), track.run, track.reverse, TRACK_PASS);
|
|
1389
|
+
return keyframeAt(track.stops, track.stopTimes, track.eases, TRACK_PASS, track.run.tween.duration);
|
|
1390
|
+
};
|
|
1391
|
+
/** Where a track ends: its value at its end, or, for one that never ends, its last stop, which it never rests on. */
|
|
1392
|
+
const trackGoal = (track) => {
|
|
1393
|
+
if (Number.isFinite(track.end))
|
|
1394
|
+
return trackValue(track, track.end);
|
|
1395
|
+
return track.stops[track.stops.length - 1];
|
|
1396
|
+
};
|
|
1397
|
+
/** The key at `at`'s track, in the keyframes change driving its entry `rule`; `undefined` once that change has ended. */
|
|
1398
|
+
const trackAt = (state, at, rule) => {
|
|
1399
|
+
const driver = state.drivers[rule];
|
|
1400
|
+
if (Predicate.isUndefined(driver))
|
|
1401
|
+
return undefined;
|
|
1402
|
+
return driver.tracks[driver.indexAt[at]];
|
|
1403
|
+
};
|
|
1404
|
+
/** The key at `at`'s keyframes `time` seconds in: its value, its speed over the frame before, and whether it has ended. */
|
|
1405
|
+
// At a play and played backwards: out of line.
|
|
1406
|
+
const keyframesAt = (state, at, rule, time) => {
|
|
1407
|
+
const track = trackAt(state, at, rule);
|
|
1408
|
+
if (Predicate.isUndefined(track))
|
|
1409
|
+
return true;
|
|
1410
|
+
const value = trackValue(track, time);
|
|
1411
|
+
const before = trackValue(track, Math.max(0, time - FRAME));
|
|
1412
|
+
const landed = time >= track.end;
|
|
1413
|
+
state.positions[at] = value;
|
|
1414
|
+
state.velocities[at] = (value - before) / FRAME;
|
|
1415
|
+
if (landed)
|
|
1416
|
+
state.velocities[at] = 0;
|
|
1417
|
+
return landed;
|
|
1418
|
+
};
|
|
1419
|
+
/**
|
|
1420
|
+
* A keyframes key's step to `time`, by `dt`, written in place: whether it has ended. Its speed is its
|
|
1421
|
+
* move over the step. One whose change has ended rests where it is, its target.
|
|
1422
|
+
*/
|
|
1423
|
+
// Per frame, per key of a keyframes change.
|
|
1424
|
+
const stepKeyframes = (state, at, rule, time, dt) => {
|
|
1425
|
+
const track = trackAt(state, at, rule);
|
|
1426
|
+
if (Predicate.isUndefined(track)) {
|
|
1427
|
+
state.positions[at] = state.targets[at];
|
|
1428
|
+
state.velocities[at] = 0;
|
|
1429
|
+
return true;
|
|
1430
|
+
}
|
|
1431
|
+
const next = trackValue(track, time);
|
|
1432
|
+
const landed = time >= track.end;
|
|
1433
|
+
if (landed)
|
|
1434
|
+
state.velocities[at] = 0;
|
|
1435
|
+
if (!landed && dt > 0)
|
|
1436
|
+
state.velocities[at] = (next - state.positions[at]) / dt;
|
|
1437
|
+
state.positions[at] = next;
|
|
1438
|
+
return landed;
|
|
1439
|
+
};
|
|
1440
|
+
/** Its change worked out, once, at its first play: the keys it moves, where each starts, its target and its wait. */
|
|
1441
|
+
const startOf = (animation) => {
|
|
1442
|
+
if (Predicate.isNotUndefined(animation.keyframesOf))
|
|
1443
|
+
return keyframesStartOf(animation, animation.keyframesOf);
|
|
1444
|
+
const { state, slots, targetOf, options } = animation;
|
|
1445
|
+
const { stride, flags, positions, velocities, targets } = state;
|
|
1446
|
+
const from = options.from;
|
|
1447
|
+
const given = slots.length;
|
|
1448
|
+
const most = given * stride;
|
|
1449
|
+
const ats = new Int32Array(most);
|
|
1450
|
+
const starts = new Float64Array(most);
|
|
1451
|
+
const speeds = new Float64Array(most);
|
|
1452
|
+
const goals = new Float64Array(most);
|
|
1453
|
+
// Each mover's first key in `ats`, by rank, and one past the last mover's.
|
|
1454
|
+
const firsts = new Int32Array(given + 1);
|
|
1455
|
+
const driven = state.animations.size > 0;
|
|
1456
|
+
let count = 0;
|
|
1457
|
+
let n = 0;
|
|
1458
|
+
const idle = [];
|
|
1459
|
+
// Per member per key, once an animation, as a change's (ADR-0025, *Amendment (#665)*): counted loops
|
|
1460
|
+
// over its typed arrays. A slot given twice moves once.
|
|
1461
|
+
for (let g = 0; g < given; g++) {
|
|
1462
|
+
const slot = slots[g];
|
|
1463
|
+
const flag = flags[slot];
|
|
1464
|
+
if ((flag & LIVE) === 0)
|
|
1465
|
+
continue;
|
|
1466
|
+
if ((flag & COUNTED) !== 0)
|
|
1467
|
+
continue;
|
|
1468
|
+
const first = count;
|
|
1469
|
+
for (let k = 0; k < stride; k++) {
|
|
1470
|
+
const at = slot * stride + k;
|
|
1471
|
+
const target = targetOf(slot, k);
|
|
1472
|
+
if (Number.isNaN(target))
|
|
1473
|
+
continue;
|
|
1474
|
+
let start = positions[at];
|
|
1475
|
+
let speed = velocities[at];
|
|
1476
|
+
if (driven && heldAt(state, at))
|
|
1477
|
+
speed = 0;
|
|
1478
|
+
let jumps = false;
|
|
1479
|
+
if (Predicate.isNotUndefined(from)) {
|
|
1480
|
+
const stop = from(slot, k);
|
|
1481
|
+
if (!Number.isNaN(stop)) {
|
|
1482
|
+
start = stop;
|
|
1483
|
+
speed = 0;
|
|
1484
|
+
jumps = true;
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
if (!jumps && target === targets[at])
|
|
1488
|
+
continue;
|
|
1489
|
+
ats[count] = at;
|
|
1490
|
+
starts[count] = start;
|
|
1491
|
+
speeds[count] = speed;
|
|
1492
|
+
goals[count] = target;
|
|
1493
|
+
count++;
|
|
1494
|
+
}
|
|
1495
|
+
if (count === first) {
|
|
1496
|
+
// Counted too, so a slot given twice is idle once.
|
|
1497
|
+
flags[slot] = flag | COUNTED;
|
|
1498
|
+
idle.push(slot);
|
|
1499
|
+
continue;
|
|
1500
|
+
}
|
|
1501
|
+
flags[slot] = flag | COUNTED;
|
|
1502
|
+
firsts[n] = first;
|
|
1503
|
+
n++;
|
|
1504
|
+
}
|
|
1505
|
+
firsts[n] = count;
|
|
1506
|
+
// Each mover's keys wait by its rank among the movers.
|
|
1507
|
+
const delayOf = delayOfChange(options.delay);
|
|
1508
|
+
const waits = new Float64Array(count);
|
|
1509
|
+
for (let rank = 0; rank < n; rank++) {
|
|
1510
|
+
const slot = slotAt(state, ats[firsts[rank]]);
|
|
1511
|
+
flags[slot] = flags[slot] & ~COUNTED;
|
|
1512
|
+
const wait = delayOf(rank, n);
|
|
1513
|
+
for (let i = firsts[rank]; i < firsts[rank + 1]; i++)
|
|
1514
|
+
waits[i] = wait;
|
|
1515
|
+
}
|
|
1516
|
+
animation.ats = ats.slice(0, count);
|
|
1517
|
+
animation.starts = starts.slice(0, count);
|
|
1518
|
+
animation.speeds = speeds.slice(0, count);
|
|
1519
|
+
animation.goals = goals.slice(0, count);
|
|
1520
|
+
animation.waits = waits;
|
|
1521
|
+
animation.rules = new Uint16Array(count);
|
|
1522
|
+
animation.firsts = firsts.slice(0, n + 1);
|
|
1523
|
+
animation.told = new Uint8Array(n);
|
|
1524
|
+
for (let s = 0; s < idle.length; s++)
|
|
1525
|
+
flags[idle[s]] = flags[idle[s]] & ~COUNTED;
|
|
1526
|
+
animation.idle = idle;
|
|
1527
|
+
animation.started = true;
|
|
1528
|
+
};
|
|
1529
|
+
/**
|
|
1530
|
+
* A keyframes change worked out, once, at its first play: each key it moves, its track, where it starts
|
|
1531
|
+
* and ends, and its end as its wait, so it lands no sooner.
|
|
1532
|
+
*/
|
|
1533
|
+
const keyframesStartOf = (animation, keyframesOf) => {
|
|
1534
|
+
const { state, slots } = animation;
|
|
1535
|
+
const { stride, flags, positions } = state;
|
|
1536
|
+
const given = slots.length;
|
|
1537
|
+
const most = given * stride;
|
|
1538
|
+
const ats = new Int32Array(most);
|
|
1539
|
+
const starts = new Float64Array(most);
|
|
1540
|
+
const goals = new Float64Array(most);
|
|
1541
|
+
const ends = new Float64Array(most);
|
|
1542
|
+
const counted = new Int32Array(given);
|
|
1543
|
+
const indexAt = new Int32Array(state.used * stride);
|
|
1544
|
+
const tracks = [];
|
|
1545
|
+
let count = 0;
|
|
1546
|
+
let n = 0;
|
|
1547
|
+
// Per member per key, once an animation, as a change's: counted loops. A slot given twice moves once.
|
|
1548
|
+
for (let g = 0; g < given; g++) {
|
|
1549
|
+
const slot = slots[g];
|
|
1550
|
+
const flag = flags[slot];
|
|
1551
|
+
if ((flag & LIVE) === 0)
|
|
1552
|
+
continue;
|
|
1553
|
+
if ((flag & COUNTED) !== 0)
|
|
1554
|
+
continue;
|
|
1555
|
+
flags[slot] = flag | COUNTED;
|
|
1556
|
+
counted[n] = slot;
|
|
1557
|
+
n++;
|
|
1558
|
+
for (let k = 0; k < stride; k++) {
|
|
1559
|
+
const at = slot * stride + k;
|
|
1560
|
+
const keyframes = keyframesOf(slot, k);
|
|
1561
|
+
if (Predicate.isUndefined(keyframes))
|
|
1562
|
+
continue;
|
|
1563
|
+
const track = trackOf(keyframes, positions[at]);
|
|
1564
|
+
if (Predicate.isUndefined(track))
|
|
1565
|
+
continue;
|
|
1566
|
+
ats[count] = at;
|
|
1567
|
+
starts[count] = trackValue(track, 0);
|
|
1568
|
+
goals[count] = trackGoal(track);
|
|
1569
|
+
ends[count] = track.end;
|
|
1570
|
+
indexAt[at] = count;
|
|
1571
|
+
tracks.push(track);
|
|
1572
|
+
count++;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
for (let c = 0; c < n; c++)
|
|
1576
|
+
flags[counted[c]] = flags[counted[c]] & ~COUNTED;
|
|
1577
|
+
animation.ats = ats.slice(0, count);
|
|
1578
|
+
animation.starts = starts.slice(0, count);
|
|
1579
|
+
animation.speeds = new Float64Array(count);
|
|
1580
|
+
animation.goals = goals.slice(0, count);
|
|
1581
|
+
animation.waits = ends.slice(0, count);
|
|
1582
|
+
animation.rules = new Uint16Array(count);
|
|
1583
|
+
animation.tracks = tracks;
|
|
1584
|
+
animation.indexAt = indexAt;
|
|
1585
|
+
animation.started = true;
|
|
1586
|
+
};
|
|
1587
|
+
/** A keyframes change's entry: every key it moves is on it, its own, so its Playhead reaches them alone. */
|
|
1588
|
+
const KEYFRAMES_ENTRY = { kind: KEYFRAMES, delay: 0, spring: NO_SPRING, tween: NO_TWEEN };
|
|
1589
|
+
/** `animation`'s rule for each key it moves: its own keyframes entry, or each key's transition's. */
|
|
1590
|
+
// Once a play, per key it moves.
|
|
1591
|
+
const playRules = (animation) => {
|
|
1592
|
+
const { state } = animation;
|
|
1593
|
+
const count = animation.ats.length;
|
|
1594
|
+
if (Predicate.isNotUndefined(animation.keyframesOf)) {
|
|
1595
|
+
let rule = state.ruleByBag.get(animation);
|
|
1596
|
+
if (Predicate.isUndefined(rule))
|
|
1597
|
+
rule = entryRuleOf(state, animation, KEYFRAMES_ENTRY);
|
|
1598
|
+
animation.rules.fill(rule);
|
|
1599
|
+
return;
|
|
1600
|
+
}
|
|
1601
|
+
for (let i = 0; i < count; i++) {
|
|
1602
|
+
const at = animation.ats[i];
|
|
1603
|
+
const slot = slotAt(state, at);
|
|
1604
|
+
animation.rules[i] = ruleFor(state, animation.bag(slot, at - slot * state.stride));
|
|
1605
|
+
}
|
|
1606
|
+
};
|
|
1607
|
+
/**
|
|
1608
|
+
* Plays `animation` from its Playhead's time: from its start at 0, its change worked out then, and from
|
|
1609
|
+
* wherever a seek put the time, every key it moves taken again there. It returns how many members it
|
|
1610
|
+
* sets off from rest. One that moves nothing lands at once, as an element's call with no key does.
|
|
1611
|
+
*/
|
|
1612
|
+
export const play = (self) => {
|
|
1613
|
+
const animation = animationOf(self);
|
|
1614
|
+
const { state, options } = animation;
|
|
1615
|
+
if (!animation.started)
|
|
1616
|
+
startOf(animation);
|
|
1617
|
+
finish(animation);
|
|
1618
|
+
tellIdle(animation);
|
|
1619
|
+
const count = animation.ats.length;
|
|
1620
|
+
if (count === 0) {
|
|
1621
|
+
UndefinedOr.map(options.onLanded, (told) => told());
|
|
1622
|
+
return 0;
|
|
1623
|
+
}
|
|
1624
|
+
const time = options.head.time;
|
|
1625
|
+
animation.playing = true;
|
|
1626
|
+
animation.time = time;
|
|
1627
|
+
animation.landing = time > 0;
|
|
1628
|
+
state.animations.add(animation);
|
|
1629
|
+
// Once a play, per key it moves: each key's entry, then each entry held once for it and driven by it.
|
|
1630
|
+
playRules(animation);
|
|
1631
|
+
animation.held = Arr.dedupe(Arr.fromIterable(animation.rules));
|
|
1632
|
+
Arr.forEach(animation.held, (rule) => {
|
|
1633
|
+
hold(state, rule);
|
|
1634
|
+
state.drivers[rule] = animation;
|
|
1635
|
+
});
|
|
1636
|
+
let setOff = 0;
|
|
1637
|
+
for (let i = 0; i < count; i++) {
|
|
1638
|
+
const at = animation.ats[i];
|
|
1639
|
+
const rule = animation.rules[i];
|
|
1640
|
+
turn(state, at, rule, animation.goals[i], animation.starts[i], animation.speeds[i], animation.waits[i]);
|
|
1641
|
+
state.elapsed[at] = time;
|
|
1642
|
+
valueAt(state, at, rule, time, true);
|
|
1643
|
+
setOff += moving(state, slotAt(state, at));
|
|
1644
|
+
}
|
|
1645
|
+
return setOff;
|
|
1646
|
+
};
|
|
1647
|
+
/** The member at `slot` moving in the ruled list, so the next step draws it and rests it: 1 when that sets it off from rest. */
|
|
1648
|
+
const moving = (state, slot) => {
|
|
1649
|
+
const flag = state.flags[slot];
|
|
1650
|
+
if ((flag & IN_RULED) !== 0)
|
|
1651
|
+
return 0;
|
|
1652
|
+
state.flags[slot] = (flag & ~IN_PLAIN) | IN_RULED;
|
|
1653
|
+
state.ruledList[state.ruledCount] = slot;
|
|
1654
|
+
state.ruledCount = state.ruledCount + 1;
|
|
1655
|
+
if ((flag & IN_PLAIN) !== 0)
|
|
1656
|
+
return 0;
|
|
1657
|
+
return 1;
|
|
1658
|
+
};
|
|
1659
|
+
/** The key at `at` put at rest at `value`, drawn there at the next step and resting there, a tween's too. */
|
|
1660
|
+
const restAt = (state, at, value) => {
|
|
1661
|
+
state.positions[at] = value;
|
|
1662
|
+
state.targets[at] = value;
|
|
1663
|
+
state.origins[at] = value;
|
|
1664
|
+
state.velocities[at] = 0;
|
|
1665
|
+
// Past any delay and duration: its next step lands it where it is.
|
|
1666
|
+
state.elapsed[at] = Number.POSITIVE_INFINITY;
|
|
1667
|
+
moving(state, slotAt(state, at));
|
|
1668
|
+
};
|
|
1669
|
+
/** Ends `animation` where it is: the keys it moves stay there, at rest. */
|
|
1670
|
+
export const stop = (self) => {
|
|
1671
|
+
const animation = animationOf(self);
|
|
1672
|
+
if (!animation.playing)
|
|
1673
|
+
return;
|
|
1674
|
+
const { state } = animation;
|
|
1675
|
+
// Once a stop, per key it moves: one a later change took stays that change's.
|
|
1676
|
+
for (let i = 0; i < animation.ats.length; i++) {
|
|
1677
|
+
if (!holds(animation, i))
|
|
1678
|
+
continue;
|
|
1679
|
+
const at = animation.ats[i];
|
|
1680
|
+
restAt(state, at, state.positions[at]);
|
|
1681
|
+
}
|
|
1682
|
+
finish(animation);
|
|
1683
|
+
};
|
|
1684
|
+
/** Ends `animation`, and puts the keys it moved back where it started them, at rest. */
|
|
1685
|
+
export const cancel = (self) => {
|
|
1686
|
+
const animation = animationOf(self);
|
|
1687
|
+
const { state } = animation;
|
|
1688
|
+
// Once a cancel, per key it moved: one a later change took stays that change's.
|
|
1689
|
+
for (let i = 0; i < animation.ats.length; i++) {
|
|
1690
|
+
if (state.targets[animation.ats[i]] !== animation.goals[i])
|
|
1691
|
+
continue;
|
|
1692
|
+
restAt(state, animation.ats[i], animation.starts[i]);
|
|
1693
|
+
}
|
|
1694
|
+
finish(animation);
|
|
1695
|
+
};
|
|
1696
|
+
/**
|
|
1697
|
+
* How long a key runs, stepped at 60 Hz: the frames until its time, summed as its steps sum it, less
|
|
1698
|
+
* `after`, reaches `seconds`. A tween or keyframes ends at its end from its start, as `Machine.durationOf`
|
|
1699
|
+
* steps one; a spring set by its duration, its duration past its delay, as its step reads it. `Infinity`
|
|
1700
|
+
* for one not done by `Machine.MAX_DURATION`.
|
|
1701
|
+
*/
|
|
1702
|
+
// 105 frames sum to 1.7499999999999987: a key ending at 1.75 lands at the 106th, where rounding to frames said 105.
|
|
1703
|
+
const framesUntil = (seconds, after = 0) => {
|
|
1704
|
+
const most = Math.ceil(Machine.MAX_DURATION / FRAME);
|
|
1705
|
+
let time = 0;
|
|
1706
|
+
// Once a duration, per distinct end: a counted loop.
|
|
1707
|
+
for (let f = 0; f <= most; f++) {
|
|
1708
|
+
if (time - after >= seconds)
|
|
1709
|
+
return f * FRAME;
|
|
1710
|
+
time += FRAME;
|
|
1711
|
+
}
|
|
1712
|
+
return Number.POSITIVE_INFINITY;
|
|
1713
|
+
};
|
|
1714
|
+
/**
|
|
1715
|
+
* How long `animation` runs, in seconds: its longest key's, a spring's until it rests, stepped from its
|
|
1716
|
+
* start at 60 Hz as `Machine.durationOf` steps a machine. Worked out once.
|
|
1717
|
+
*/
|
|
1718
|
+
// Stepped here, not through `Machine.durationOf`, which would build a machine for each member's key.
|
|
1719
|
+
export const durationOf = (self) => {
|
|
1720
|
+
const animation = animationOf(self);
|
|
1721
|
+
if (Predicate.isNotUndefined(animation.duration))
|
|
1722
|
+
return animation.duration;
|
|
1723
|
+
const { state } = animation;
|
|
1724
|
+
if (!animation.started)
|
|
1725
|
+
startOf(animation);
|
|
1726
|
+
const most = Math.ceil(Machine.MAX_DURATION / FRAME);
|
|
1727
|
+
let longest = 0;
|
|
1728
|
+
// Keyframes: each key's end, its wait; a sequence's keys share theirs.
|
|
1729
|
+
if (Predicate.isNotUndefined(animation.keyframesOf)) {
|
|
1730
|
+
const known = new Map();
|
|
1731
|
+
for (let i = 0; i < animation.waits.length; i++) {
|
|
1732
|
+
const end = animation.waits[i];
|
|
1733
|
+
let seconds = known.get(end);
|
|
1734
|
+
if (Predicate.isUndefined(seconds)) {
|
|
1735
|
+
seconds = framesUntil(end);
|
|
1736
|
+
known.set(end, seconds);
|
|
1737
|
+
}
|
|
1738
|
+
longest = Math.max(longest, seconds);
|
|
1739
|
+
}
|
|
1740
|
+
animation.duration = longest;
|
|
1741
|
+
return longest;
|
|
1742
|
+
}
|
|
1743
|
+
// Once an animation, per key: a counted loop to its rest.
|
|
1744
|
+
for (let i = 0; i < animation.ats.length; i++) {
|
|
1745
|
+
const at = animation.ats[i];
|
|
1746
|
+
const slot = slotAt(state, at);
|
|
1747
|
+
const entry = entryFor(animation.bag(slot, at - slot * state.stride));
|
|
1748
|
+
const delay = entry.delay + animation.waits[i];
|
|
1749
|
+
const goal = animation.goals[i];
|
|
1750
|
+
const distance = goal - animation.starts[i];
|
|
1751
|
+
if (entry.kind === TWEEN) {
|
|
1752
|
+
longest = Math.max(longest, framesUntil(delay + entry.tween.duration));
|
|
1753
|
+
continue;
|
|
1754
|
+
}
|
|
1755
|
+
if (entry.kind === TIMED) {
|
|
1756
|
+
longest = Math.max(longest, framesUntil(entry.spring.duration, delay));
|
|
1757
|
+
continue;
|
|
1758
|
+
}
|
|
1759
|
+
const restDelta = Spring.restDeltaFor(entry.spring, distance);
|
|
1760
|
+
const restSpeed = Spring.restSpeedFor(entry.spring, distance);
|
|
1761
|
+
let position = animation.starts[i];
|
|
1762
|
+
let speed = animation.speeds[i];
|
|
1763
|
+
let time = 0;
|
|
1764
|
+
let steps = most;
|
|
1765
|
+
for (let f = 1; f <= most; f++) {
|
|
1766
|
+
time += FRAME;
|
|
1767
|
+
const simDt = Math.max(0, Math.min(FRAME, time - delay));
|
|
1768
|
+
if (simDt > 0) {
|
|
1769
|
+
const m = Spring.matrixFor(entry.spring, simDt);
|
|
1770
|
+
const d = position - goal;
|
|
1771
|
+
position = goal + m.dd * d + m.dv * speed;
|
|
1772
|
+
speed = m.vd * d + m.vv * speed;
|
|
1773
|
+
if (Math.abs(speed) <= restSpeed && Math.abs(position - goal) <= restDelta) {
|
|
1774
|
+
steps = f;
|
|
1775
|
+
break;
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
longest = Math.max(longest, steps * FRAME);
|
|
1780
|
+
}
|
|
1781
|
+
animation.duration = longest;
|
|
1782
|
+
return longest;
|
|
1783
|
+
};
|
|
1784
|
+
/** A sink that keeps nothing: a Batch's run writes as it steps. */
|
|
1785
|
+
const ignore = () => undefined;
|
|
1786
|
+
/** `self`'s run numbered `id`: a step each tick, until nothing moves. */
|
|
1787
|
+
const machineOf = (self, id) => {
|
|
1788
|
+
const state = stateOf(self);
|
|
1789
|
+
return Machine.make({
|
|
1790
|
+
settles: true,
|
|
1791
|
+
span: { name: "wardic.batch", attributes: { keys: state.stride } },
|
|
1792
|
+
start: () => {
|
|
1793
|
+
const frame = { value: 0, done: false };
|
|
1794
|
+
// Per frame (ADR-0025): the step, and its count of members moving.
|
|
1795
|
+
return (dt) => {
|
|
1796
|
+
const done = step(self, dt);
|
|
1797
|
+
frame.value = state.plainCount + state.ruledCount;
|
|
1798
|
+
if (!done)
|
|
1799
|
+
return frame;
|
|
1800
|
+
frame.done = true;
|
|
1801
|
+
// Marked ended in the step that rests, so a change later in the same tick starts the next run.
|
|
1802
|
+
if (state.running === id)
|
|
1803
|
+
state.running = 0;
|
|
1804
|
+
return frame;
|
|
1805
|
+
};
|
|
1806
|
+
},
|
|
1807
|
+
});
|
|
1808
|
+
};
|
|
1809
|
+
/**
|
|
1810
|
+
* Runs `self` on the Frame loop while any member moves, from the next tick: each tick steps every
|
|
1811
|
+
* moving member and writes it. It succeeds once nothing moves. While one runs, another does nothing,
|
|
1812
|
+
* so a binding calls it after each change.
|
|
1813
|
+
*
|
|
1814
|
+
* @example
|
|
1815
|
+
* Batch.retarget(grid, slots, targetOf)
|
|
1816
|
+
* runtime.runFork(Batch.run(grid))
|
|
1817
|
+
*/
|
|
1818
|
+
// One run, not a fiber per member (ADR-0069, decision 10). Its span is `wardic.batch`.
|
|
1819
|
+
export const run = (self) => Effect.suspend(() => {
|
|
1820
|
+
const state = stateOf(self);
|
|
1821
|
+
if (state.running !== 0)
|
|
1822
|
+
return Effect.void;
|
|
1823
|
+
if (state.plainCount + state.ruledCount === 0)
|
|
1824
|
+
return Effect.void;
|
|
1825
|
+
const id = state.runs + 1;
|
|
1826
|
+
state.runs = id;
|
|
1827
|
+
state.running = id;
|
|
1828
|
+
return Effect.ensuring(Effect.asVoid(Machine.run(machineOf(self, id), ignore)), Effect.sync(() => {
|
|
1829
|
+
if (state.running === id)
|
|
1830
|
+
state.running = 0;
|
|
1831
|
+
}));
|
|
1832
|
+
});
|
|
1833
|
+
/** `key`'s index among `state`'s keys, or a throw naming it. */
|
|
1834
|
+
const keyAt = (state, key) => {
|
|
1835
|
+
const k = state.indexOfKey[key];
|
|
1836
|
+
if (Predicate.isNotUndefined(k))
|
|
1837
|
+
return k;
|
|
1838
|
+
throw new Machine.OptionError({ message: `Expected one of the Batch's keys, got ${JSON.stringify(key)}\n at ["key"]` });
|
|
1839
|
+
};
|
|
1840
|
+
/** A member's value for `key` now. */
|
|
1841
|
+
// Per frame: a sink may read it for every member.
|
|
1842
|
+
export const get = (self, slot, key) => {
|
|
1843
|
+
const state = stateOf(self);
|
|
1844
|
+
return state.positions[slot * state.stride + keyAt(state, key)];
|
|
1845
|
+
};
|
|
1846
|
+
/** A member's target for `key` now: where a change last turned it, toward which it moves or rests. */
|
|
1847
|
+
// At a change, per key of a member a driver holds (#665's PR 4): whether the change moves it.
|
|
1848
|
+
export const target = (self, slot, key) => {
|
|
1849
|
+
const state = stateOf(self);
|
|
1850
|
+
return state.targets[slot * state.stride + keyAt(state, key)];
|
|
1851
|
+
};
|
|
1852
|
+
/** A member's velocity for `key` now, per second: what an animation that takes the key over starts with. */
|
|
1853
|
+
// Per frame, as `get`.
|
|
1854
|
+
export const velocity = (self, slot, key) => {
|
|
1855
|
+
const state = stateOf(self);
|
|
1856
|
+
const at = slot * state.stride + keyAt(state, key);
|
|
1857
|
+
// Held by a paused Playhead, it reads 0 (#464).
|
|
1858
|
+
if (state.animations.size > 0 && heldAt(state, at))
|
|
1859
|
+
return 0;
|
|
1860
|
+
return state.velocities[at];
|
|
1861
|
+
};
|
|
1862
|
+
//# sourceMappingURL=Batch.js.map
|