@johnoscott/isometric-engine 0.14.1 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/iso-editor.js +249 -203
- package/iso-render.js +200 -154
- package/iso-systems.js +210 -162
- package/isometric-diagram.js +389 -0
- package/js/iso-core/index.js +5 -0
- package/js/iso-core/mono.js +242 -0
- package/js/iso-editor/ui/canvas.js +23 -1
- package/js/iso-objects/ants.js +36 -0
- package/js/iso-objects/index.js +55 -1
- package/js/iso-objects/looks.js +25 -0
- package/js/iso-objects/mono-paint.js +96 -0
- package/js/iso-objects/motion-builder.js +130 -0
- package/js/iso-objects/motion-doc.js +421 -0
- package/js/iso-objects/motion-entry.js +34 -0
- package/js/iso-objects/motion-schema.js +83 -0
- package/js/iso-objects/motion.js +640 -0
- package/js/iso-objects/resolve.js +238 -0
- package/js/iso-objects/scene.js +15 -13
- package/js/iso-objects/timeline.js +593 -0
- package/js/iso-systems/compile.js +83 -1
- package/js/iso-systems/index.js +4 -1
- package/js/iso-systems/render.js +20 -0
- package/js/iso-systems/schema.json +40 -0
- package/js/iso-viewer/index.js +6 -0
- package/js/iso-viewer/page.js +40 -16
- package/js/iso-viewer/play.js +340 -0
- package/js/iso-viewer/player.js +149 -175
- package/js/iso-viewer/transport.js +289 -0
- package/package.json +84 -2
- package/src/iso-core/index.ts +1 -0
- package/src/iso-core/mono.ts +306 -0
- package/src/iso-core/theme.ts +2 -1
- package/src/iso-editor/ui/canvas.ts +14 -1
- package/src/iso-objects/ants.ts +53 -0
- package/src/iso-objects/compile.ts +4 -1
- package/src/iso-objects/index.ts +16 -1
- package/src/iso-objects/looks.ts +21 -0
- package/src/iso-objects/mono-paint.ts +97 -0
- package/src/iso-objects/motion-builder.ts +79 -0
- package/src/iso-objects/motion-doc.ts +309 -0
- package/src/iso-objects/motion-entry.ts +13 -0
- package/src/iso-objects/motion-schema.ts +55 -0
- package/src/iso-objects/motion.ts +617 -0
- package/src/iso-objects/resolve.ts +196 -0
- package/src/iso-objects/scene.ts +32 -13
- package/src/iso-objects/timeline.ts +493 -0
- package/src/iso-objects/types.ts +22 -2
- package/src/iso-systems/compile.ts +86 -4
- package/src/iso-systems/index.ts +4 -1
- package/src/iso-systems/render.ts +45 -0
- package/src/iso-systems/schema.json +40 -0
- package/src/iso-viewer/index.ts +4 -0
- package/src/iso-viewer/page.ts +28 -11
- package/src/iso-viewer/play.ts +280 -0
- package/src/iso-viewer/player.ts +132 -150
- package/src/iso-viewer/transport.ts +272 -0
- package/types/iso-core/index.d.ts +1 -0
- package/types/iso-core/mono.d.ts +61 -0
- package/types/iso-core/theme.d.ts +2 -0
- package/types/iso-objects/ants.d.ts +34 -0
- package/types/iso-objects/compile.d.ts +3 -0
- package/types/iso-objects/index.d.ts +16 -1
- package/types/iso-objects/looks.d.ts +4 -0
- package/types/iso-objects/mono-paint.d.ts +20 -0
- package/types/iso-objects/motion-builder.d.ts +105 -0
- package/types/iso-objects/motion-doc.d.ts +193 -0
- package/types/iso-objects/motion-entry.d.ts +11 -0
- package/types/iso-objects/motion-schema.d.ts +594 -0
- package/types/iso-objects/motion.d.ts +202 -0
- package/types/iso-objects/resolve.d.ts +20 -0
- package/types/iso-objects/scene.d.ts +14 -0
- package/types/iso-objects/timeline.d.ts +66 -0
- package/types/iso-objects/types.d.ts +34 -0
- package/types/iso-systems/compile.d.ts +25 -3
- package/types/iso-systems/index.d.ts +4 -1
- package/types/iso-systems/render.d.ts +25 -0
- package/types/iso-viewer/index.d.ts +4 -0
- package/types/iso-viewer/play.d.ts +37 -0
- package/types/iso-viewer/transport.d.ts +73 -0
- package/types/player/isometric-diagram.d.ts +24 -0
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
// A motion document bound to a drawing: a Timeline. at(ms) is what the drawing looks like then, as a SceneState (plain JSON,
|
|
2
|
+
// drawn by motion.ts's svgAt and paint); cues(ms) is the commentary. Pure: no clock, no DOM, no state kept between calls, so
|
|
3
|
+
// any moment can be asked in any order. Two clocks (docs/design/MOTION.md, 3.4): T, the timeline's, and A, the ambient one, for ants
|
|
4
|
+
// and for looping tokens and pulses: T says which are there, A where they are in their loop. at(T) is at(T, { ambient: T }).
|
|
5
|
+
|
|
6
|
+
import { didYouMean } from '../iso-core/errors.ts';
|
|
7
|
+
import type { CompiledScene } from './compile.ts';
|
|
8
|
+
import { ANT_SPEED, antDash, antsOf } from './ants.ts';
|
|
9
|
+
import { silhouette, type Rect } from './labels.ts';
|
|
10
|
+
import { motionModel, planOf, type ItemState, type MotionModel, type LabelState, type MotionPlan, type RouteState, type SceneState } from './motion.ts';
|
|
11
|
+
import { focusView } from './focus.ts';
|
|
12
|
+
import { checkMotion, curveAt, easeOf, msOf, readMs, springAt, type Focus, type MotionDiag, type MotionDoc, type MotionNode, type Say, type Sequence, type SeriesItem, type Transition } from './motion-doc.ts';
|
|
13
|
+
|
|
14
|
+
export interface BeatInfo { name: string; index: number; start: number; end: number; say?: Say }
|
|
15
|
+
export interface Cue { beat: string; chapter?: string; index: number; of: number; progress: number; say?: Say; color?: string }
|
|
16
|
+
export interface Timeline {
|
|
17
|
+
readonly ok: boolean; readonly diags: MotionDiag[];
|
|
18
|
+
/** ms */
|
|
19
|
+
readonly duration: number;
|
|
20
|
+
readonly beats: BeatInfo[];
|
|
21
|
+
readonly markers: Record<string, number>;
|
|
22
|
+
/** the document's own `ambient`: what at(ms) does with none given */
|
|
23
|
+
readonly ambient: 'on' | 'off';
|
|
24
|
+
/** whether anything in it runs on the ambient clock (ants, looping tokens and pulses): a host with none needs no frames while paused */
|
|
25
|
+
readonly loops: boolean;
|
|
26
|
+
/** what the drawing looks like at ms. `ambient`: the ambient clock's time (default: the same; a host that stopped it passes the time it
|
|
27
|
+
* stopped at, and what loops stands where it was), or 'off': it never ran, and each loop stands at its start */
|
|
28
|
+
at(ms: number, o?: { ambient?: number | 'off' }): SceneState;
|
|
29
|
+
cues(ms: number): Cue | undefined;
|
|
30
|
+
beatAt(ms: number): BeatInfo | undefined;
|
|
31
|
+
startOf(name: string): number | undefined;
|
|
32
|
+
}
|
|
33
|
+
export interface BindOptions {
|
|
34
|
+
/** the connectors' dashes, by kind: the theme's `kinds` (a dashed kind marches with its own dash) */
|
|
35
|
+
kinds?: Record<string, { dash?: 'solid' | 'dashed' | 'dotted'; ants?: [number, number] }>;
|
|
36
|
+
/** kinds whose connectors stand still (default: blocked, planned) */
|
|
37
|
+
still?: string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const r2 = (v: number) => Math.round(v * 100) / 100 || 0;
|
|
41
|
+
const r4 = (v: number) => Math.round(v * 10000) / 10000 || 0;
|
|
42
|
+
const clamp = (f: number) => Math.max(0, Math.min(1, f));
|
|
43
|
+
const mix = (a: number, b: number, f: number) => a + (b - a) * f;
|
|
44
|
+
const FADE = 250;
|
|
45
|
+
|
|
46
|
+
/** what a moment adds up to: opacities multiply, offsets add, the rest is the last said */
|
|
47
|
+
class Sum {
|
|
48
|
+
items = new Map<string, ItemState>();
|
|
49
|
+
labels = new Map<string, LabelState>();
|
|
50
|
+
routes = new Map<string, RouteState>();
|
|
51
|
+
tokens: NonNullable<SceneState['tokens']> = [];
|
|
52
|
+
pulses: NonNullable<SceneState['pulses']> = [];
|
|
53
|
+
outlines: NonNullable<SceneState['outlines']> = [];
|
|
54
|
+
variants: Record<string, Record<string, number>> = {};
|
|
55
|
+
marks = 1;
|
|
56
|
+
private at<T extends object>(m: Map<string, T>, id: string): T { let v = m.get(id); if (!v) m.set(id, (v = {} as T)); return v; }
|
|
57
|
+
item(id: string, s: ItemState) { const v = this.at(this.items, id); if (s.opacity !== undefined) v.opacity = (v.opacity ?? 1) * s.opacity; if (s.dx) v.dx = (v.dx ?? 0) + s.dx; if (s.dy) v.dy = (v.dy ?? 0) + s.dy; if (s.scale !== undefined) { v.scale = (v.scale ?? 1) * s.scale; v.about = s.about ?? v.about; } if (s.clip) v.clip = s.clip; }
|
|
58
|
+
label(id: string, s: LabelState) { const v = this.at(this.labels, id); if (s.opacity !== undefined) v.opacity = (v.opacity ?? 1) * s.opacity; if (s.dx) v.dx = (v.dx ?? 0) + s.dx; if (s.dy) v.dy = (v.dy ?? 0) + s.dy; if (s.typed !== undefined) v.typed = Math.min(v.typed ?? 1, s.typed); }
|
|
59
|
+
route(id: string, s: RouteState) { const v = this.at(this.routes, id); if (s.opacity !== undefined) v.opacity = (v.opacity ?? 1) * s.opacity; if (s.drawn !== undefined && s.drawn < (v.drawn ?? 1)) { v.drawn = s.drawn; v.by = s.by; } if (s.ants) v.ants = s.ants; }
|
|
60
|
+
add(s: SceneState) {
|
|
61
|
+
for (const [id, x] of Object.entries(s.items ?? {})) this.item(id, x);
|
|
62
|
+
for (const [id, x] of Object.entries(s.labels ?? {})) this.label(id, x);
|
|
63
|
+
for (const [id, x] of Object.entries(s.routes ?? {})) this.route(id, x);
|
|
64
|
+
if (s.marks) this.marks *= s.marks.opacity;
|
|
65
|
+
this.tokens.push(...s.tokens ?? []); this.pulses.push(...s.pulses ?? []);
|
|
66
|
+
}
|
|
67
|
+
state(camera?: [number, number, number, number]): SceneState {
|
|
68
|
+
const out: SceneState = {};
|
|
69
|
+
const rec = <T extends object>(m: Map<string, T>, clean: (v: T) => T) => { const o: Record<string, T> = {}; for (const [id, v] of m) { const c = clean(v); if (Object.keys(c).length) o[id] = c; } return o; };
|
|
70
|
+
const some = (o: object) => Object.keys(o).length > 0;
|
|
71
|
+
const op = (v: number | undefined) => (v === undefined || r4(v) >= 1 ? {} : { opacity: r4(Math.max(0, v)) });
|
|
72
|
+
const move = (v: { dx?: number; dy?: number }) => ({ ...(r2(v.dx ?? 0) ? { dx: r2(v.dx!) } : {}), ...(r2(v.dy ?? 0) ? { dy: r2(v.dy!) } : {}) });
|
|
73
|
+
const items = rec(this.items, v => ({ ...op(v.opacity), ...move(v), ...(v.scale !== undefined && r4(v.scale) !== 1 ? { scale: r4(v.scale), ...(v.about ? { about: v.about } : {}) } : {}), ...(v.clip ? { clip: v.clip } : {}) }));
|
|
74
|
+
const labels = rec(this.labels, v => ({ ...op(v.opacity), ...(v.typed !== undefined && v.typed < 1 ? { typed: r4(v.typed) } : {}), ...move(v) }));
|
|
75
|
+
const routes = rec(this.routes, v => {
|
|
76
|
+
const hidden = v.opacity !== undefined && r4(v.opacity) <= 0, part = v.drawn !== undefined && v.drawn < 1;
|
|
77
|
+
return { ...op(v.opacity), ...(part ? { drawn: r4(v.drawn!), ...(v.by ? { by: v.by } : {}) } : {}), ...(v.ants && !hidden && !part ? { ants: v.ants } : {}) };
|
|
78
|
+
});
|
|
79
|
+
if (some(items)) out.items = items;
|
|
80
|
+
if (some(labels)) out.labels = labels;
|
|
81
|
+
if (some(routes)) out.routes = routes;
|
|
82
|
+
if (r4(this.marks) < 1) out.marks = { opacity: r4(this.marks) };
|
|
83
|
+
if (this.tokens.length) out.tokens = this.tokens;
|
|
84
|
+
if (this.pulses.length) out.pulses = this.pulses;
|
|
85
|
+
if (this.outlines.length) out.outlines = this.outlines;
|
|
86
|
+
if (some(this.variants)) out.variants = this.variants;
|
|
87
|
+
if (camera) out.camera = camera;
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** What binding needs to know of a drawing, as plain JSON: for a page that has the drawing and no compiler. */
|
|
93
|
+
export interface MotionSource {
|
|
94
|
+
model: MotionModel;
|
|
95
|
+
/** each connector's kind */
|
|
96
|
+
kinds: Record<string, string>;
|
|
97
|
+
/** each thing's outline */
|
|
98
|
+
outlines: Record<string, [number, number][]>;
|
|
99
|
+
/** the looks each thing declares */
|
|
100
|
+
looks?: Record<string, string[]>;
|
|
101
|
+
ants?: ReturnType<typeof antsOf>;
|
|
102
|
+
/** the notes on nothing (JSON keeps no undefined) */
|
|
103
|
+
free?: string[];
|
|
104
|
+
}
|
|
105
|
+
export function motionSource(scene: CompiledScene): MotionSource {
|
|
106
|
+
const ants = antsOf(scene.raw), looks = scene.rendered.looks, model = motionModel(scene), free = Object.keys(model.notes).filter(n => model.notes[n] === undefined);
|
|
107
|
+
return {
|
|
108
|
+
model, kinds: Object.fromEntries(scene.routes.map(r => [r.id, r.kind])),
|
|
109
|
+
outlines: Object.fromEntries(scene.layout.items.map(p => [p.id, silhouette(p.box).map(q => [r2(q[0]), r2(q[1])] as [number, number])])),
|
|
110
|
+
...(looks && Object.keys(looks).length ? { looks } : {}), ...(ants ? { ants } : {}), ...(free.length ? { free } : {}),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function bindMotion(drawing: CompiledScene | MotionSource, doc: MotionDoc, opts: BindOptions = {}): Timeline {
|
|
115
|
+
const src = 'model' in drawing ? drawing : motionSource(drawing);
|
|
116
|
+
const m: MotionModel = src.free ? { ...src.model, notes: { ...src.model.notes, ...Object.fromEntries(src.free.map(n => [n, undefined])) } } : src.model;
|
|
117
|
+
const diags = checkMotion(doc), fps = doc.fps, fileAnts = src.ants;
|
|
118
|
+
const none = (): Timeline => ({ ok: false, diags, duration: 0, beats: [], markers: {}, ambient: 'on', loops: false, at: () => ({}), cues: () => undefined, beatAt: () => undefined, startOf: () => undefined });
|
|
119
|
+
if (diags.length) return none();
|
|
120
|
+
const err = (path: string, message: string, hint?: string) => { diags.push({ severity: 'error', path, message, ...(hint ? { hint } : {}) }); };
|
|
121
|
+
const ms = (t: unknown, otherwise: number) => (t === undefined ? otherwise : msOf(t, fps)!);
|
|
122
|
+
|
|
123
|
+
// ---------- what is in the drawing ----------
|
|
124
|
+
const pieces = new Map(m.pieces.map(p => [p.id, p])), routes = new Set(m.routes.map(r => r.id)), notes = new Set(Object.keys(m.notes));
|
|
125
|
+
const kindOf = new Map(Object.entries(src.kinds));
|
|
126
|
+
const all = [...pieces.keys(), ...routes, ...notes];
|
|
127
|
+
const known = new Set(all);
|
|
128
|
+
/** what moves and fades with a thing or a connector: its own label, and the notes on it */
|
|
129
|
+
const notesOn = new Map<string, string[]>();
|
|
130
|
+
for (const [n, on] of Object.entries(m.notes)) if (on !== undefined) notesOn.set(on, [...(notesOn.get(on) ?? []), n]);
|
|
131
|
+
const what = { any: known, thing: new Set(pieces.keys()), route: routes };
|
|
132
|
+
const words = { any: 'thing, connector or note', thing: 'thing', route: 'connector' };
|
|
133
|
+
const need = (id: string, path: string, kind: keyof typeof what = 'any') => { if (!what[kind].has(id)) { const d = didYouMean(id, what[kind]); err(path, `no ${words[kind]} "${id}" in this drawing`, d ? `did you mean "${d}"?` : undefined); } };
|
|
134
|
+
const needAll = (v: string[] | undefined, path: string, kind: keyof typeof what = 'any') => v?.forEach((id, i) => need(id, `${path}/${i}`, kind));
|
|
135
|
+
const outline = new Map(Object.entries(src.outlines));
|
|
136
|
+
|
|
137
|
+
// ---------- how long things are ----------
|
|
138
|
+
const plans = new Map<object, MotionPlan>();
|
|
139
|
+
function nodeEnd(n: MotionNode, path: string): number {
|
|
140
|
+
const from = 'from' in n ? ms(n.from, 0) : 0, dur = 'duration' in n ? ms(n.duration, NaN) : NaN, given = Number.isFinite(dur);
|
|
141
|
+
if ('sequence' in n) { const s = n.sequence; return ms(s.from, 0) + seqLength(s, `${path}/sequence`); }
|
|
142
|
+
if ('series' in n) return layoutSeries(n.series, `${path}/series`).end;
|
|
143
|
+
if ('layers' in n) return Math.max(0, ...n.layers.map((x, i) => nodeEnd(x, `${path}/layers/${i}`)));
|
|
144
|
+
if ('assemble' in n || 'disassemble' in n) {
|
|
145
|
+
const kind = 'assemble' in n ? 'assemble' : 'disassemble', o = ('assemble' in n ? n.assemble : n.disassemble) as object;
|
|
146
|
+
let p = plans.get(n);
|
|
147
|
+
if (!p) plans.set(n, (p = planOf(m, kind, { seed: doc.seed ?? 1, ...o, ...(given ? { duration: dur } : {}) })));
|
|
148
|
+
return from + p.duration;
|
|
149
|
+
}
|
|
150
|
+
// draw's and lift's duration is each thing's, one after another by the stagger
|
|
151
|
+
if ('draw' in n) return from + (given ? dur : 400) + Math.max(0, n.draw.length - 1) * ms(n.stagger, 0);
|
|
152
|
+
if ('lift' in n) return from + Math.max(given ? dur : 750, ...(n.opacity?.times.map(t => msOf(t, fps)!) ?? [])) + Math.max(0, n.lift.length - 1) * ms(n.stagger, 0);
|
|
153
|
+
if (given) return from + dur;
|
|
154
|
+
if ('travel' in n) return n.loop ? from : from + n.travel.length * ms(n.each, 1150);
|
|
155
|
+
if ('pulse' in n) return n.loop ? from : from + 1500;
|
|
156
|
+
if ('type' in n) return from + 400;
|
|
157
|
+
if ('camera' in n) return from + 900;
|
|
158
|
+
if ('move' in n) return from + Math.max(0, ...[n.x, n.y, n.scale, n.opacity].flatMap(c => c?.times.map(t => msOf(t, fps)!) ?? []));
|
|
159
|
+
return from; // (ants, focus, highlight, a loop: as long as what it is in)
|
|
160
|
+
}
|
|
161
|
+
const contents = (s: Sequence, path: string) => Math.max(0, ...(s.children ?? []).map((n, i) => nodeEnd(n, `${path}/children/${i}`)), ...(s.layers ?? []).map((n, i) => nodeEnd(n, `${path}/layers/${i}`)), s.series ? layoutSeries(s.series, `${path}/series`).end : 0);
|
|
162
|
+
/** one run of what is in it, on its own clock */
|
|
163
|
+
const runOf = new Map<Sequence, number>();
|
|
164
|
+
function inner(s: Sequence, path: string): number {
|
|
165
|
+
const have = runOf.get(s); if (have !== undefined) return have;
|
|
166
|
+
let d: number;
|
|
167
|
+
if (s.duration !== undefined && typeof s.duration === 'object') d = readMs(s.say, s.duration.read);
|
|
168
|
+
else d = s.duration !== undefined ? ms(s.duration, 0) : contents(s, path);
|
|
169
|
+
if (s.duration === undefined && d <= 0 && s.say) d = readMs(s.say, true);
|
|
170
|
+
if (d <= 0) err(`${path}/duration`, 'how long is it? (nothing in it says)');
|
|
171
|
+
runOf.set(s, d);
|
|
172
|
+
return d;
|
|
173
|
+
}
|
|
174
|
+
/** how long it is where it stands */
|
|
175
|
+
const seqLength = (s: Sequence, path: string): number => {
|
|
176
|
+
const d = inner(s, path), rate = s.playbackRate ?? 1;
|
|
177
|
+
if (s.loop === true) return d / rate; // (an endless loop is as long as what it is in lets it be; alone, one run)
|
|
178
|
+
return (typeof s.loop === 'number' ? d * s.loop : d) / rate;
|
|
179
|
+
};
|
|
180
|
+
interface Placed { s: Sequence; path: string; start: number; end: number; into?: { t: Transition; length: number } }
|
|
181
|
+
function layoutSeries(items: SeriesItem[], path: string): { list: Placed[]; end: number } {
|
|
182
|
+
const list: Placed[] = [];
|
|
183
|
+
let at = 0, into: Placed['into'];
|
|
184
|
+
items.forEach((it, i) => {
|
|
185
|
+
if ('transition' in it) {
|
|
186
|
+
const t = it.transition, g = t.timing;
|
|
187
|
+
const length = t.presentation === 'none' ? 0 : 'linear' in g ? ms(g.linear, 0) : ms(g.spring.duration, settleMs(g.spring));
|
|
188
|
+
into = { t, length };
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const s = it.sequence, p = `${path}/${i}/sequence`, len = seqLength(s, p);
|
|
192
|
+
const start = Math.max(0, at + ms(s.offset, 0) - (into?.length ?? 0));
|
|
193
|
+
if (into && list.length && into.length > Math.min(list[list.length - 1].end - list[list.length - 1].start, len)) err(`${path}/${i - 1}/transition/timing`, 'a transition is no longer than either side of it');
|
|
194
|
+
list.push({ s, path: p, start, end: start + len, ...(into ? { into } : {}) });
|
|
195
|
+
at = start + len; into = undefined;
|
|
196
|
+
});
|
|
197
|
+
return { list, end: Math.max(0, ...list.map(x => x.end)) };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const top = layoutSeries(doc.series ?? [], '/series');
|
|
201
|
+
const extra = Math.max(0, ...(doc.layers ?? []).map((n, i) => nodeEnd(n, `/layers/${i}`)));
|
|
202
|
+
const duration = Math.max(top.end, extra);
|
|
203
|
+
if (duration <= 0 && !diags.length) err('', 'a motion document has a series, or layers, that last');
|
|
204
|
+
|
|
205
|
+
// ---------- its ids, and what is only a top beat's ----------
|
|
206
|
+
const starts = new Map<string, number>();
|
|
207
|
+
interface CamCue { at: number; over: number; ease: (f: number) => number; to: [number, number, number, number] }
|
|
208
|
+
const cams: CamCue[] = [];
|
|
209
|
+
const fit = m.viewBox;
|
|
210
|
+
const framed = (b: Rect, margin: number, zoom?: number): [number, number, number, number] => {
|
|
211
|
+
const k = zoom !== undefined ? 1 / zoom : Math.max((b.w + margin * 2) / fit[2], (b.h + margin * 2) / fit[3]);
|
|
212
|
+
return [b.x + b.w / 2 - (fit[2] * k) / 2, b.y + b.h / 2 - (fit[3] * k) / 2, fit[2] * k, fit[3] * k];
|
|
213
|
+
};
|
|
214
|
+
/** abs: the document's time of a local 0, and how fast local time runs; undefined: inside a loop */
|
|
215
|
+
function visit(n: MotionNode, path: string, abs: { at: number; rate: number } | undefined) {
|
|
216
|
+
const at = (t: number) => (abs ? { at: abs.at + t / abs.rate, rate: abs.rate } : undefined);
|
|
217
|
+
if ('sequence' in n) { seq(n.sequence, `${path}/sequence`, at(ms(n.sequence.from, 0)), false); return; }
|
|
218
|
+
if ('series' in n) { for (const p of layoutSeries(n.series, `${path}/series`).list) seq(p.s, p.path, at(p.start), false); return; }
|
|
219
|
+
if ('layers' in n) { n.layers.forEach((x, i) => visit(x, `${path}/layers/${i}`, abs)); return; }
|
|
220
|
+
if ('travel' in n) n.travel.forEach((x, i) => (Array.isArray(x) ? x : [x]).forEach((id, j) => need(id.split(':')[0], `${path}/travel/${i}${Array.isArray(x) ? `/${j}` : ''}`, 'route')));
|
|
221
|
+
else if ('pulse' in n) needAll(n.pulse, `${path}/pulse`, 'thing');
|
|
222
|
+
else if ('ants' in n) { if (n.ants !== true) needAll(n.ants, `${path}/ants`, 'route'); }
|
|
223
|
+
else if ('draw' in n) needAll(n.draw, `${path}/draw`, 'route');
|
|
224
|
+
else if ('type' in n) needAll(n.type, `${path}/type`);
|
|
225
|
+
else if ('move' in n) need(n.move, `${path}/move`, 'thing');
|
|
226
|
+
else if ('lift' in n) needAll(n.lift, `${path}/lift`, 'thing');
|
|
227
|
+
else if ('focus' in n) focusIds(n.focus, `${path}/focus`);
|
|
228
|
+
else if ('highlight' in n) needAll(n.highlight, `${path}/highlight`, 'thing');
|
|
229
|
+
else if ('camera' in n) {
|
|
230
|
+
const c = n.camera, over = ms(n.duration, 900);
|
|
231
|
+
if (!abs) { err(`${path}/camera`, 'a camera can\'t be in a loop'); return; }
|
|
232
|
+
let to: [number, number, number, number] = fit;
|
|
233
|
+
if (c !== 'fit') {
|
|
234
|
+
const f = c.focus;
|
|
235
|
+
if (Array.isArray(f) && typeof f[0] === 'number') to = framed({ x: f[0] as number, y: f[1] as number, w: f[2] as number, h: f[3] as number }, c.margin ?? 0, c.zoom);
|
|
236
|
+
else {
|
|
237
|
+
const ids = (typeof f === 'string' ? [f] : f) as string[];
|
|
238
|
+
ids.forEach((id, i) => need(id, `${path}/camera/focus${typeof f === 'string' ? '' : `/${i}`}`, 'thing'));
|
|
239
|
+
const bs = ids.map(id => pieces.get(id)?.bbox).filter((b): b is Rect => !!b);
|
|
240
|
+
if (bs.length) { const x = Math.min(...bs.map(b => b.x)), y = Math.min(...bs.map(b => b.y)); to = framed({ x, y, w: Math.max(...bs.map(b => b.x + b.w)) - x, h: Math.max(...bs.map(b => b.y + b.h)) - y }, c.margin ?? 40, c.zoom); }
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
cams.push({ at: abs.at + ms(n.from, 0) / abs.rate, over: over / abs.rate, ease: easeOf(n.easing, over, 'settle'), to });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const focusIds = (f: Focus | undefined, path: string) => { if (f) { needAll(f.lit, `${path}/lit`); needAll(f.always, `${path}/always`); } };
|
|
247
|
+
function seq(s: Sequence, path: string, abs: { at: number; rate: number } | undefined, isTop: boolean) {
|
|
248
|
+
if (s.name !== undefined && abs) starts.set(s.name, abs.at);
|
|
249
|
+
if (!isTop) for (const k of ['show', 'hide', 'say', 'enter', 'focus', 'highlight', 'levels', 'variants', 'ants'] as const) if (s[k] !== undefined) err(`${path}/${k}`, `${k} is for a sequence of the document's own series`, k === 'focus' || k === 'highlight' || k === 'ants' ? `here, a child: { ${k}: … }` : undefined);
|
|
250
|
+
needAll(s.show, `${path}/show`); needAll(s.hide, `${path}/hide`); needAll(s.highlight, `${path}/highlight`, 'thing'); focusIds(s.focus, `${path}/focus`);
|
|
251
|
+
for (const id of Object.keys(s.levels ?? {})) if (id !== '*') need(id, `${path}/levels/${id}`);
|
|
252
|
+
for (const [id, name] of Object.entries(s.variants ?? {})) {
|
|
253
|
+
need(id, `${path}/variants/${id}`, 'thing');
|
|
254
|
+
const have = src.looks?.[id] ?? [];
|
|
255
|
+
if (pieces.has(id) && name !== '' && !have.includes(name)) { const d = didYouMean(name, have); err(`${path}/variants/${id}`, have.length ? `${id} has no look "${name}" (it has ${have.join(', ')})` : `${id} declares no looks (variants: in the diagram)`, d ? `did you mean "${d}"?` : undefined); }
|
|
256
|
+
}
|
|
257
|
+
if (Array.isArray(s.ants)) needAll(s.ants, `${path}/ants`, 'route');
|
|
258
|
+
const rate = s.playbackRate ?? 1;
|
|
259
|
+
const local = abs && !s.loop ? { at: abs.at - ms(s.trimBefore, 0) / (abs.rate * rate), rate: abs.rate * rate } : undefined;
|
|
260
|
+
(s.children ?? []).forEach((n, i) => visit(n, `${path}/children/${i}`, local));
|
|
261
|
+
(s.layers ?? []).forEach((n, i) => visit(n, `${path}/layers/${i}`, local));
|
|
262
|
+
if (s.series) for (const p of layoutSeries(s.series, `${path}/series`).list) seq(p.s, p.path, local && { at: local.at + p.start / local.rate, rate: local.rate }, false);
|
|
263
|
+
}
|
|
264
|
+
for (const p of top.list) seq(p.s, p.path, { at: p.start, rate: 1 }, true);
|
|
265
|
+
(doc.layers ?? []).forEach((n, i) => visit(n, `/layers/${i}`, { at: 0, rate: 1 }));
|
|
266
|
+
cams.sort((a, b) => a.at - b.at);
|
|
267
|
+
const markers: Record<string, number> = {};
|
|
268
|
+
for (const [k, v] of Object.entries(doc.markers ?? {})) {
|
|
269
|
+
const t = msOf(v, fps);
|
|
270
|
+
if (t !== undefined) { markers[k] = t; continue; }
|
|
271
|
+
const s = String(v), hit = [...starts.keys()].filter(x => s === x || s.startsWith(x + '+') || s.startsWith(x + '-')).sort((a, b) => b.length - a.length)[0];
|
|
272
|
+
if (hit === undefined) { err(`/markers/${k}`, `"${s}" isn't a moment of this document`); continue; }
|
|
273
|
+
const rest = s.slice(hit.length), off = rest ? msOf(rest.slice(1), fps) : 0;
|
|
274
|
+
if (off === undefined) { err(`/markers/${k}`, `"${rest}" isn't a time`); continue; }
|
|
275
|
+
markers[k] = starts.get(hit)! + (rest[0] === '-' ? -off : off);
|
|
276
|
+
}
|
|
277
|
+
if (diags.length) return none();
|
|
278
|
+
|
|
279
|
+
// ---------- the beats: what is there, what is lit, as the fold of every beat to each ----------
|
|
280
|
+
const beats: BeatInfo[] = top.list.map((p, i) => ({ name: p.s.name ?? `beat-${i + 1}`, index: i, start: p.start, end: p.end, ...(p.s.say ? { say: p.s.say } : {}) }));
|
|
281
|
+
const managed = new Map<string, boolean>(); // id → whether it is there before the first beat
|
|
282
|
+
for (const p of top.list) { for (const id of p.s.show ?? []) if (!managed.has(id)) managed.set(id, false); for (const id of p.s.hide ?? []) if (!managed.has(id)) managed.set(id, true); }
|
|
283
|
+
const folds: Map<string, boolean>[] = [new Map(managed)];
|
|
284
|
+
for (const p of top.list) { const v = new Map(folds[folds.length - 1]); for (const id of p.s.show ?? []) v.set(id, true); for (const id of p.s.hide ?? []) v.set(id, false); folds.push(v); }
|
|
285
|
+
/** the look each thing has in beat k (folds' own, from before the first) */
|
|
286
|
+
const looked: Map<string, string>[] = [new Map()];
|
|
287
|
+
for (const p of top.list) { const v = new Map(looked[looked.length - 1]); for (const [id, n] of Object.entries(p.s.variants ?? {})) v.set(id, n); looked.push(v); }
|
|
288
|
+
/** how much a focus leaves of an id */
|
|
289
|
+
const left = (f: Focus | undefined, id: string) => (!f || f.lit.includes(id) || f.always?.includes(id) || (f.scope === 'links' && pieces.has(id)) ? 1 : f.level ?? 0.12);
|
|
290
|
+
/** an id's share in beat k (-1: before the first) */
|
|
291
|
+
const level = (k: number, id: string) => { const v = k < 0 ? undefined : top.list[k].s.levels; return v ? v[id] ?? v['*'] ?? 1 : 1; };
|
|
292
|
+
const share = (k: number, id: string) => (folds[k + 1].get(id) === false ? 0 : 1) * (k < 0 ? 1 : left(top.list[k].s.focus, id)) * level(k, id);
|
|
293
|
+
/** a note goes with what it is on; what the document says of the note itself is on top of that */
|
|
294
|
+
const noteShare = (k: number, n: string): number => {
|
|
295
|
+
const on = m.notes[n];
|
|
296
|
+
if (on === undefined) return share(k, n);
|
|
297
|
+
const v = k < 0 ? undefined : top.list[k].s.levels?.[n];
|
|
298
|
+
return share(k, on) * (folds[k + 1].get(n) === false ? 0 : 1) * (v ?? 1);
|
|
299
|
+
};
|
|
300
|
+
const beatIndex = (T: number) => { let k = -1; for (let i = 0; i < top.list.length; i++) if (top.list[i].start <= T) k = i; return k; };
|
|
301
|
+
/** how much of beat k has come in at T (1: all of it) */
|
|
302
|
+
const entered = (k: number, T: number) => {
|
|
303
|
+
const p = top.list[k], lt = T - p.start;
|
|
304
|
+
if (p.into && p.into.length > 0) { const g = p.into.t.timing; return 'spring' in g ? (lt >= p.into.length ? 1 : clamp(springAt(lt, { ...g.spring, overshootClamping: true }) / springAt(p.into.length, { ...g.spring, overshootClamping: true }))) : clamp(lt / p.into.length); }
|
|
305
|
+
const e = p.s.enter; if (!e) return 1;
|
|
306
|
+
const over = ms(e.fade, 0);
|
|
307
|
+
return over <= 0 ? 1 : easeOf(e.easing, over, 'ease')(clamp(lt / over));
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
// ---------- a moment ----------
|
|
311
|
+
type Ctx = { sum: Sum; shown: (id: string) => boolean; off: boolean; A: number };
|
|
312
|
+
function each(n: MotionNode, t: number, a: number, end: number, c: Ctx): void {
|
|
313
|
+
if ('sequence' in n) { const s = n.sequence, from = ms(s.from, 0); run(s, t - from, a - from, runLength(s, end - from), c); return; }
|
|
314
|
+
if ('series' in n) { for (const p of layoutSeries(n.series, '').list) run(p.s, t - p.start, a - p.start, p.end - p.start, c); return; }
|
|
315
|
+
if ('layers' in n) { for (const x of n.layers) each(x, t, a, end, c); return; }
|
|
316
|
+
const from = ms('from' in n ? n.from : undefined, 0), lt = t - from;
|
|
317
|
+
if ('assemble' in n || 'disassemble' in n) { c.sum.add(plans.get(n)!.at(lt)); return; }
|
|
318
|
+
if ('draw' in n) {
|
|
319
|
+
const one = Math.max(1, ms(n.duration, 400)), gap = ms(n.stagger, 0), e = easeOf(n.easing, one, 'ease-out');
|
|
320
|
+
n.draw.forEach((id, i) => { const f = e(clamp((lt - i * gap) / one)); if (f <= 0) { c.sum.route(id, { opacity: 0, drawn: 0 }); c.sum.label(id, { opacity: 0 }); } else if (f < 1) { c.sum.route(id, { drawn: f, ...(n.by ? { by: n.by } : {}) }); c.sum.label(id, { opacity: f }); } });
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if ('type' in n) { const d = ms(n.duration, 400), f = easeOf(n.easing, d, 'linear')(clamp(lt / Math.max(1, d))); if (f < 1) for (const id of n.type) c.sum.label(id, f <= 0 ? { opacity: 0 } : { typed: f }); return; }
|
|
324
|
+
if ('move' in n) {
|
|
325
|
+
const g = (q: typeof n.x) => (q ? curveAt(q, lt, fps) : undefined), dx = g(n.x) ?? 0, dy = g(n.y) ?? 0, sc = g(n.scale), op = g(n.opacity);
|
|
326
|
+
c.sum.item(n.move, { dx, dy, ...(sc === undefined ? {} : { scale: sc, about: pieces.get(n.move)!.foot }), ...(op === undefined ? {} : { opacity: clamp(op) }) });
|
|
327
|
+
for (const id of [n.move, ...notesOn.get(n.move) ?? []]) c.sum.label(id, { dx, dy, ...(op === undefined ? {} : { opacity: clamp(op) }) });
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if ('lift' in n) {
|
|
331
|
+
const gap = ms(n.stagger, 0), one = Math.max(1, ms(n.duration, 750)), e = easeOf(n.easing, one, 'ease-out'), down = n.to === 'down', N = n.lift.length;
|
|
332
|
+
n.lift.forEach((id, i) => {
|
|
333
|
+
const own = lt - (down ? N - 1 - i : i) * gap, f = e(clamp(own / one)), dy = (i + 1) * n.by * (down ? f : -f), o = n.opacity ? clamp(curveAt(n.opacity, own, fps)) : 1;
|
|
334
|
+
if (!dy && o >= 1) return;
|
|
335
|
+
c.sum.item(id, { dy, opacity: o });
|
|
336
|
+
for (const x of [id, ...notesOn.get(id) ?? []]) c.sum.label(x, { dy, opacity: o });
|
|
337
|
+
});
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if ('camera' in n) return;
|
|
341
|
+
// what lasts: from its start to its duration's end, or to the end of what it is in
|
|
342
|
+
const lasts = n.duration === undefined ? Math.max(0, end - from) : ms(n.duration, 0);
|
|
343
|
+
if (lt < 0 || lt > lasts) return;
|
|
344
|
+
if ('focus' in n || 'highlight' in n) {
|
|
345
|
+
const fade = Math.max(1, ms(n.fade, FADE)), amount = clamp(Math.min(lt / fade, n.duration === undefined ? 1 : (lasts - lt) / fade));
|
|
346
|
+
const e = easeOf(n.easing, fade, 'linear')(amount);
|
|
347
|
+
if ('focus' in n) dim(n.focus, e, c.sum); else for (const id of n.highlight) if (c.shown(id)) ring(id, e, c.sum);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if ('ants' in n) { march(n.ants === true ? [...routes] : n.ants, c, n.speed, n.dash); return; }
|
|
351
|
+
if ('pulse' in n) {
|
|
352
|
+
const d = 1500, p = n.loop ? turned(a - from, d, c) / d : lt / Math.max(1, n.duration === undefined ? d : lasts);
|
|
353
|
+
if (p < 0 || p >= 1) return;
|
|
354
|
+
for (const id of n.pulse) if (c.shown(id)) { const b = pieces.get(id)!, rx = b.bbox.w * 0.5 * (0.7 + p * 0.6); c.sum.pulses.push({ x: r2(b.foot[0]), y: r2(b.foot[1]), rx: r2(rx), ry: r2(rx * 0.58), opacity: r4(1 - p) }); }
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if ('travel' in n) {
|
|
358
|
+
const one = ms(n.each, 1150), hold = ms(n.hold, n.loop ? 700 : 0), period = n.travel.length * one + hold;
|
|
359
|
+
const q = n.loop ? turned(a - from, period, c) : lt, i = Math.floor(q / one);
|
|
360
|
+
if (i >= n.travel.length) return;
|
|
361
|
+
const part = (q - i * one) / one, f = easeOf(n.easing, one, 'ease-in-out')(part), group = n.travel[i];
|
|
362
|
+
for (const x of Array.isArray(group) ? group : [group]) { const [id, kind] = x.split(':'); if (c.shown(id)) c.sum.tokens.push({ route: id, at: r4((kind ?? kindOf.get(id)) === 'blocked' ? stalled(part) : f), ...(kind ? { kind } : {}) }); }
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/** a blocked token gets most of the way, stalls and falls back a little, as the page's steps have it */
|
|
366
|
+
const stalled = (f: number) => (f < 0.7 ? f / 0.7 : 1 - ((f - 0.7) / 0.3) * 0.25);
|
|
367
|
+
/** where in its turn a loop is: with the ambient off, at its start */
|
|
368
|
+
const turned = (x: number, period: number, c: Ctx) => (c.off ? 0 : ((x % period) + period) % period);
|
|
369
|
+
const ring = (id: string, amount: number, sum: Sum) => { if (r4(amount) > 0 && outline.has(id)) sum.outlines.push({ id, points: outline.get(id)!, ...(r4(amount) < 1 ? { opacity: r4(amount) } : {}) }); };
|
|
370
|
+
const dim = (f: Focus, amount: number, sum: Sum) => { for (const id of all) { const on = notes.has(id) && !f.lit.includes(id) && !f.always?.includes(id) ? m.notes[id] : undefined, o = mix(1, left(f, on ?? id), amount); if (o >= 1) continue; if (pieces.has(id)) { sum.item(id, { opacity: o }); sum.label(id, { opacity: o }); } else if (routes.has(id)) { sum.route(id, { opacity: o }); sum.label(id, { opacity: o }); } else sum.label(id, { opacity: o }); } };
|
|
371
|
+
const march = (ids: string[], c: Ctx, speed = ANT_SPEED, dash?: [number, number]) => {
|
|
372
|
+
for (const id of ids) {
|
|
373
|
+
const d = antDash(kindOf.get(id) ?? '', { dash, kinds: opts.kinds, still: opts.still });
|
|
374
|
+
if (!d) continue;
|
|
375
|
+
const period = d[0] + d[1], o = turned((speed * c.A) / 1000, period, c);
|
|
376
|
+
c.sum.route(id, { ants: { dash: d, offset: r2(o) } });
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
const runLength = (s: Sequence, rest: number) => (s.loop === true ? Math.max(0, rest) : seqLength(s, ''));
|
|
380
|
+
/** what a sequence shows at its own time t (a: the ambient time, from its start) */
|
|
381
|
+
function run(s: Sequence, t: number, a: number, length: number, c: Ctx): void {
|
|
382
|
+
if (t < 0 || t > length || (t === length && length < duration && length > 0 && s.loop)) return;
|
|
383
|
+
const rate = s.playbackRate ?? 1, one = inner(s, ''), trim = ms(s.trimBefore, 0);
|
|
384
|
+
const turn = s.loop && one > 0 ? (t * rate) % one : t * rate, lt = turn + trim, la = a * rate + trim;
|
|
385
|
+
for (const n of s.children ?? []) each(n, lt, la, one + trim, c);
|
|
386
|
+
for (const n of s.layers ?? []) each(n, lt, la, one + trim, c);
|
|
387
|
+
if (s.series) for (const p of layoutSeries(s.series, '').list) run(p.s, lt - p.start, la - p.start, p.end - p.start, c);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function at(ms0: number, o: { ambient?: number | 'off' } = {}): SceneState {
|
|
391
|
+
const T = Math.max(0, Math.min(duration, Number.isFinite(ms0) ? ms0 : 0));
|
|
392
|
+
const off = o.ambient === 'off' || (doc.ambient === 'off' && o.ambient === undefined), A = typeof o.ambient === 'number' ? o.ambient : T;
|
|
393
|
+
const sum = new Sum(), k = beatIndex(T), f = k < 0 ? 1 : entered(k, T);
|
|
394
|
+
const of = (id: string, g: (k: number, id: string) => number) => (k < 0 ? g(-1, id) : f >= 1 ? g(k, id) : mix(g(k - 1, id), g(k, id), f));
|
|
395
|
+
const shown = (id: string) => (k < 0 ? folds[0] : folds[k + 1]).get(id) !== false;
|
|
396
|
+
if (managed.size || top.list.some(p => p.s.focus || p.s.levels)) for (const id of all) {
|
|
397
|
+
const v = of(id, notes.has(id) ? noteShare : share);
|
|
398
|
+
if (v >= 1) continue;
|
|
399
|
+
if (pieces.has(id)) sum.item(id, { opacity: v });
|
|
400
|
+
if (routes.has(id)) sum.route(id, { opacity: v });
|
|
401
|
+
sum.label(id, { opacity: v });
|
|
402
|
+
}
|
|
403
|
+
// looks: the one before gives way to this beat's as the beat comes in
|
|
404
|
+
if (k >= 0) for (const id of looked[looked.length - 1].keys()) {
|
|
405
|
+
const was = looked[k].get(id) ?? '', now = looked[k + 1].get(id) ?? '', v: Record<string, number> = {};
|
|
406
|
+
if (was && f < 1) v[was] = r4(1 - f);
|
|
407
|
+
if (now) v[now] = r4((v[now] ?? 0) + f);
|
|
408
|
+
for (const n of Object.keys(v)) if (v[n] <= 0) delete v[n];
|
|
409
|
+
if (Object.keys(v).length) sum.variants[id] = v;
|
|
410
|
+
}
|
|
411
|
+
// highlights: the beat's come in with it, the one before's go out
|
|
412
|
+
if (k >= 0) {
|
|
413
|
+
const now = new Set(top.list[k].s.highlight ?? []), was = new Set(k > 0 && f < 1 ? top.list[k - 1].s.highlight ?? [] : []);
|
|
414
|
+
for (const id of new Set([...was, ...now])) ring(id, mix(was.has(id) ? 1 : 0, now.has(id) ? 1 : 0, f), sum);
|
|
415
|
+
}
|
|
416
|
+
const c: Ctx = { sum, shown, off, A };
|
|
417
|
+
// a beat's own ants, else the file's
|
|
418
|
+
const ants = k >= 0 ? top.list[k].s.ants : undefined, file = ants === undefined ? fileAnts : undefined;
|
|
419
|
+
if (ants) march(ants === true ? [...routes] : ants, c);
|
|
420
|
+
else if (file) march(file.ids ?? [...routes], c, file.speed, file.dash);
|
|
421
|
+
top.list.forEach((p, i) => { if (T >= p.start && (T < p.end || (i === k && T <= p.end))) run(p.s, T - p.start, A - p.start, p.end - p.start, c); });
|
|
422
|
+
for (const n of doc.layers ?? []) each(n, T, A, duration, c);
|
|
423
|
+
// the camera: each cue from where the one before had got it
|
|
424
|
+
let view: [number, number, number, number] | undefined;
|
|
425
|
+
for (const q of cams) {
|
|
426
|
+
if (q.at > T) break;
|
|
427
|
+
const u = q.over <= 0 ? 1 : q.ease(clamp((T - q.at) / q.over)), was = view ?? fit;
|
|
428
|
+
if (u >= 1) { view = q.to; continue; }
|
|
429
|
+
// even in log-zoom; the middle goes with the zoom, so that a point's path is straight on the screen
|
|
430
|
+
const z0 = was[2], z1 = q.to[2], z = Math.exp(mix(Math.log(z0), Math.log(z1), u)), g = Math.abs(z1 - z0) < 1e-6 ? u : (z - z0) / (z1 - z0);
|
|
431
|
+
const cx = mix(was[0] + was[2] / 2, q.to[0] + q.to[2] / 2, g), cy = mix(was[1] + was[3] / 2, q.to[1] + q.to[3] / 2, g), h = (z * fit[3]) / fit[2];
|
|
432
|
+
view = [cx - z / 2, cy - h / 2, z, h];
|
|
433
|
+
}
|
|
434
|
+
const cam = view?.map(r2) as [number, number, number, number] | undefined;
|
|
435
|
+
return sum.state(cam && cam.some((v, i) => Math.abs(v - fit[i]) > 0.005) ? cam : undefined);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// whether anything anywhere is the ambient clock's
|
|
439
|
+
const moving = (n: unknown): boolean => {
|
|
440
|
+
if (!n || typeof n !== 'object') return false;
|
|
441
|
+
if (Array.isArray(n)) return n.some(moving);
|
|
442
|
+
const o = n as Record<string, unknown>;
|
|
443
|
+
if (o.ants !== undefined && o.ants !== false) return true;
|
|
444
|
+
if (o.loop === true && ('travel' in o || 'pulse' in o)) return true;
|
|
445
|
+
return Object.values(o).some(moving);
|
|
446
|
+
};
|
|
447
|
+
const loops = fileAnts !== undefined || moving(doc.series) || moving(doc.layers);
|
|
448
|
+
const beatAt = (t: number) => { const k = beatIndex(Math.max(0, Math.min(duration, t))); return k < 0 ? undefined : beats[k]; };
|
|
449
|
+
return {
|
|
450
|
+
ok: true, diags, duration, beats, markers, ambient: doc.ambient ?? 'on', loops, at, beatAt,
|
|
451
|
+
startOf: n => starts.get(n) ?? beats.find(b => b.name === n)?.start,
|
|
452
|
+
cues(t) {
|
|
453
|
+
const b = beatAt(t);
|
|
454
|
+
if (!b) return undefined;
|
|
455
|
+
const T = Math.max(0, Math.min(duration, t));
|
|
456
|
+
return { beat: b.name, index: b.index, of: beats.length, progress: r4(clamp((T - b.start) / Math.max(1, b.end - b.start))), ...(b.say ? { say: b.say, ...(b.say.chapter !== undefined ? { chapter: b.say.chapter } : {}), ...(b.say.color !== undefined ? { color: b.say.color } : {}) } : {}) };
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
/** how long a spring takes to come to rest, ms: its envelope under half a percent */
|
|
461
|
+
function settleMs(c: { mass?: number; damping?: number; stiffness?: number }): number {
|
|
462
|
+
const m = c.mass ?? 1, k = c.stiffness ?? 100, d = c.damping ?? 10, w0 = Math.sqrt(k / m), z = Math.min(1, d / (2 * Math.sqrt(k * m)));
|
|
463
|
+
return Math.ceil((Math.log(200) / (z * w0)) * 1000 * (z >= 1 ? 1.6 : 1));
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** A drawing's own steps as a motion document, so that every drawing with steps has a timeline. */
|
|
467
|
+
export function motionOfSteps(scene: CompiledScene): MotionDoc {
|
|
468
|
+
const { steps, vis } = scene.steps, full = scene.rendered.viewBox;
|
|
469
|
+
let zoomed = false;
|
|
470
|
+
const m = motionModel(scene), ids = new Set([...m.pieces.map(p => p.id), ...m.routes.map(r => r.id), ...Object.keys(m.notes)]), has = (id: string) => ids.has(id);
|
|
471
|
+
// (a note on nothing is no connector's)
|
|
472
|
+
const free = Object.keys(m.notes).filter(n => m.notes[n] === undefined);
|
|
473
|
+
const series: SeriesItem[] = steps.map((st, i) => {
|
|
474
|
+
const show: string[] = [], hide: string[] = [];
|
|
475
|
+
// (an id first shown is hidden until then, and one first hidden is there until then: so only the changes are said)
|
|
476
|
+
for (const [id, v] of Object.entries(vis)) { if (i === 0 ? !v.includes(true) : v[i - 1] && !v[i]) hide.push(id); else if (i > 0 ? v[i] && !v[i - 1] : false) show.push(id); }
|
|
477
|
+
const children: MotionNode[] = [];
|
|
478
|
+
const token = (t: { route: string; kind: string }) => `${t.route}:${t.kind}`;
|
|
479
|
+
if (st.phases.length) children.push({ travel: st.phases.map(g => (g.length === 1 ? token(g[0]) : g.map(token))), each: 1150, hold: 700, loop: true });
|
|
480
|
+
const pulse = ((scene.raw.steps ?? [])[i] as { pulse?: string[] } | undefined)?.pulse?.filter(id => scene.layout.byId.has(id)) ?? [];
|
|
481
|
+
if (pulse.length) children.push({ pulse, loop: true });
|
|
482
|
+
// a step's focus is where the camera goes, and back out with the first step that has none
|
|
483
|
+
if (st.focus) { const [x, y, w, h] = focusView(full, st.focus).map(r2); children.push({ camera: { focus: [x, y, w, h] }, duration: 900, easing: 'ease-in-out' }); zoomed = true; } else if (zoomed) { children.push({ camera: 'fit', duration: 900, easing: 'ease-in-out' }); zoomed = false; }
|
|
484
|
+
const s: Sequence = { name: `step-${st.n}`, duration: st.duration, say: { title: st.heading, ...(st.caption ? { text: st.caption } : {}), ...(st.command ? { code: st.command } : {}) } };
|
|
485
|
+
if (show.length) s.show = show;
|
|
486
|
+
if (hide.length) s.hide = hide;
|
|
487
|
+
// what a step doesn't touch dims, as on its page: the connectors and notes, or everything (the scene's `dim`)
|
|
488
|
+
s.focus = scene.raw.dim === 'all' ? { lit: st.lit.filter(has), scope: 'all', level: 0.14 } : { lit: st.lit.filter(has), scope: 'links', level: 0.12, ...(free.length ? { always: free } : {}) };
|
|
489
|
+
if (children.length) s.children = children;
|
|
490
|
+
return { sequence: s };
|
|
491
|
+
});
|
|
492
|
+
return { isomotion: 1, ...(series.length ? { series } : {}) };
|
|
493
|
+
}
|
package/src/iso-objects/types.ts
CHANGED
|
@@ -8,6 +8,15 @@ export type { RawRole, RawKind, Theme };
|
|
|
8
8
|
export type Side = 'n' | 'e' | 's' | 'w';
|
|
9
9
|
export type StepRef = string | number;
|
|
10
10
|
|
|
11
|
+
/** how one piece (and, a region's, its contents) moves when a drawing assembles or disassembles (motion.ts) */
|
|
12
|
+
export interface RawMotion {
|
|
13
|
+
style?: 'edge' | 'drop' | 'rise' | 'fly' | 'pop' | 'scatter' | 'build' | 'fade';
|
|
14
|
+
out?: 'explode' | 'lift-off' | 'sink' | 'scatter' | 'fly-out' | 'collapse' | 'fade' | 'reverse';
|
|
15
|
+
easing?: 'linear' | 'ease' | 'ease-in' | 'ease-out' | 'settle' | 'spring' | 'bounce';
|
|
16
|
+
from?: 'left' | 'right' | 'top' | 'bottom';
|
|
17
|
+
/** ms after its turn, and ms its own move takes */
|
|
18
|
+
delay?: number; duration?: number;
|
|
19
|
+
}
|
|
11
20
|
export interface RawPlacement {
|
|
12
21
|
in?: string; at?: [number, number];
|
|
13
22
|
'right-of'?: string; 'left-of'?: string; below?: string; behind?: string;
|
|
@@ -19,13 +28,18 @@ export interface RawNode extends RawPlacement {
|
|
|
19
28
|
id: string; asset?: string; role?: string; label?: string; sub?: string;
|
|
20
29
|
size?: number[]; z?: number; deco?: RawDeco[]; ghost?: boolean; face?: string; facing?: number;
|
|
21
30
|
'label-at'?: { side?: 'above' | 'right' | 'left' | 'below'; dx?: number; dy?: number };
|
|
22
|
-
nodes?: RawNode[];
|
|
31
|
+
nodes?: RawNode[]; motion?: RawMotion;
|
|
32
|
+
/** other looks of it, by name, for a motion to switch to: its own keys, over what it is */
|
|
33
|
+
variants?: Record<string, RawVariant>;
|
|
23
34
|
}
|
|
35
|
+
/** what a look may change: never the thing's box, so nothing else moves */
|
|
36
|
+
export interface RawVariant { role?: string; ghost?: boolean; label?: string; sub?: string; face?: string; deco?: RawDeco[] }
|
|
24
37
|
export interface RawGate { side: Side; at?: number; width?: number; height?: number; node?: string }
|
|
25
38
|
export interface RawRegion extends RawPlacement {
|
|
26
39
|
id: string; kind: RegionKindName; label?: string; 'label-style'?: 'face' | 'face-right' | 'floor' | 'none'; ghost?: boolean;
|
|
27
40
|
role?: string; size?: number[]; padding?: number; height?: number; z?: number; flow?: 'row' | 'column';
|
|
28
|
-
gate?: RawGate; around?: string[]; regions?: RawRegion[]; nodes?: RawNode[];
|
|
41
|
+
gate?: RawGate; around?: string[]; regions?: RawRegion[]; nodes?: RawNode[]; motion?: RawMotion;
|
|
42
|
+
variants?: Record<string, RawVariant>;
|
|
29
43
|
}
|
|
30
44
|
export interface RawNote {
|
|
31
45
|
floor?: string; face?: string; label?: string; sub?: string; at?: number[]; on?: string; kind?: string;
|
|
@@ -36,12 +50,18 @@ export interface RawScene {
|
|
|
36
50
|
regions?: RawRegion[]; nodes?: RawNode[]; routes?: unknown[]; notes?: RawNote[]; steps?: unknown[];
|
|
37
51
|
legend?: 'auto' | false | string[]; badges?: boolean; loop?: boolean; after?: string;
|
|
38
52
|
grid?: GridDef; assemble?: boolean; dim?: 'flows' | 'all';
|
|
53
|
+
/** the scene's motion document (its `isomotion: 1` may be left out) */
|
|
54
|
+
motion?: Record<string, unknown>;
|
|
55
|
+
/** its connectors march: all that may, these, or these with a speed and a dash (ants.ts) */
|
|
56
|
+
ants?: boolean | string[] | { ids?: string[]; speed?: number; dash?: [number, number] };
|
|
39
57
|
}
|
|
40
58
|
export interface GridDef { snap?: number; show?: boolean; step?: number }
|
|
41
59
|
export interface RawDiagram {
|
|
42
60
|
isodiag: 1; title?: string; lede?: string; pills?: string[]; theme?: string;
|
|
43
61
|
roles?: Record<string, RawRole>; kinds?: Record<string, RawKind>; after?: string; footer?: string;
|
|
44
62
|
grid?: GridDef; assemble?: boolean;
|
|
63
|
+
/** how a self-contained drawing of it is painted, where the host doesn't say */
|
|
64
|
+
treatment?: 'mono';
|
|
45
65
|
scenes: RawScene[];
|
|
46
66
|
}
|
|
47
67
|
|