@johnoscott/isometric-engine 0.14.1 → 0.15.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 +199 -190
- package/iso-render.js +157 -148
- package/iso-systems.js +170 -159
- package/js/iso-objects/ants.js +36 -0
- package/js/iso-objects/index.js +47 -1
- package/js/iso-objects/looks.js +25 -0
- package/js/iso-objects/motion-builder.js +130 -0
- package/js/iso-objects/motion-doc.js +500 -0
- package/js/iso-objects/motion-entry.js +32 -0
- package/js/iso-objects/motion.js +635 -0
- package/js/iso-objects/resolve.js +238 -0
- package/js/iso-objects/scene.js +15 -13
- package/js/iso-objects/timeline.js +553 -0
- package/js/iso-systems/compile.js +59 -1
- package/js/iso-systems/index.js +4 -1
- package/js/iso-systems/render.js +20 -0
- package/js/iso-systems/schema.json +39 -0
- package/js/iso-viewer/page.js +2 -1
- package/js/iso-viewer/player.js +23 -34
- package/package.json +46 -1
- package/src/iso-objects/ants.ts +53 -0
- package/src/iso-objects/compile.ts +4 -1
- package/src/iso-objects/index.ts +14 -1
- package/src/iso-objects/looks.ts +21 -0
- package/src/iso-objects/motion-builder.ts +79 -0
- package/src/iso-objects/motion-doc.ts +361 -0
- package/src/iso-objects/motion-entry.ts +12 -0
- package/src/iso-objects/motion.ts +616 -0
- package/src/iso-objects/resolve.ts +196 -0
- package/src/iso-objects/scene.ts +29 -13
- package/src/iso-objects/timeline.ts +443 -0
- package/src/iso-objects/types.ts +20 -2
- package/src/iso-systems/compile.ts +58 -3
- package/src/iso-systems/index.ts +4 -1
- package/src/iso-systems/render.ts +41 -0
- package/src/iso-systems/schema.json +39 -0
- package/src/iso-viewer/page.ts +2 -1
- package/src/iso-viewer/player.ts +24 -41
- package/types/iso-objects/ants.d.ts +33 -0
- package/types/iso-objects/compile.d.ts +3 -0
- package/types/iso-objects/index.d.ts +14 -1
- package/types/iso-objects/looks.d.ts +4 -0
- package/types/iso-objects/motion-builder.d.ts +105 -0
- package/types/iso-objects/motion-doc.d.ts +787 -0
- package/types/iso-objects/motion-entry.d.ts +10 -0
- package/types/iso-objects/motion.d.ts +200 -0
- package/types/iso-objects/resolve.d.ts +20 -0
- package/types/iso-objects/scene.d.ts +11 -0
- package/types/iso-objects/timeline.d.ts +46 -0
- package/types/iso-objects/types.d.ts +32 -0
- package/types/iso-systems/compile.d.ts +20 -3
- package/types/iso-systems/index.d.ts +4 -1
- package/types/iso-systems/render.d.ts +21 -0
|
@@ -0,0 +1,443 @@
|
|
|
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 LabelState, type MotionPlan, type RouteState, type SceneState } from './motion.ts';
|
|
11
|
+
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';
|
|
12
|
+
|
|
13
|
+
export interface BeatInfo { name: string; index: number; start: number; end: number; say?: Say }
|
|
14
|
+
export interface Cue { beat: string; chapter?: string; index: number; of: number; progress: number; say?: Say; color?: string }
|
|
15
|
+
export interface Timeline {
|
|
16
|
+
readonly ok: boolean; readonly diags: MotionDiag[];
|
|
17
|
+
/** ms */
|
|
18
|
+
readonly duration: number;
|
|
19
|
+
readonly beats: BeatInfo[];
|
|
20
|
+
readonly markers: Record<string, number>;
|
|
21
|
+
/** 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
|
|
22
|
+
* stopped at, and what loops stands where it was), or 'off': it never ran, and each loop stands at its start */
|
|
23
|
+
at(ms: number, o?: { ambient?: number | 'off' }): SceneState;
|
|
24
|
+
cues(ms: number): Cue | undefined;
|
|
25
|
+
beatAt(ms: number): BeatInfo | undefined;
|
|
26
|
+
startOf(name: string): number | undefined;
|
|
27
|
+
}
|
|
28
|
+
export interface BindOptions {
|
|
29
|
+
/** the connectors' dashes, by kind: the theme's `kinds` (a dashed kind marches with its own dash) */
|
|
30
|
+
kinds?: Record<string, { dash?: 'solid' | 'dashed' | 'dotted' }>;
|
|
31
|
+
/** kinds whose connectors stand still (default: blocked, planned) */
|
|
32
|
+
still?: string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const r2 = (v: number) => Math.round(v * 100) / 100 || 0;
|
|
36
|
+
const r4 = (v: number) => Math.round(v * 10000) / 10000 || 0;
|
|
37
|
+
const clamp = (f: number) => Math.max(0, Math.min(1, f));
|
|
38
|
+
const mix = (a: number, b: number, f: number) => a + (b - a) * f;
|
|
39
|
+
const FADE = 250;
|
|
40
|
+
|
|
41
|
+
/** what a moment adds up to: opacities multiply, offsets add, the rest is the last said */
|
|
42
|
+
class Sum {
|
|
43
|
+
items = new Map<string, ItemState>();
|
|
44
|
+
labels = new Map<string, LabelState>();
|
|
45
|
+
routes = new Map<string, RouteState>();
|
|
46
|
+
tokens: NonNullable<SceneState['tokens']> = [];
|
|
47
|
+
pulses: NonNullable<SceneState['pulses']> = [];
|
|
48
|
+
outlines: NonNullable<SceneState['outlines']> = [];
|
|
49
|
+
variants: Record<string, Record<string, number>> = {};
|
|
50
|
+
marks = 1;
|
|
51
|
+
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; }
|
|
52
|
+
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; }
|
|
53
|
+
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); }
|
|
54
|
+
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; }
|
|
55
|
+
add(s: SceneState) {
|
|
56
|
+
for (const [id, x] of Object.entries(s.items ?? {})) this.item(id, x);
|
|
57
|
+
for (const [id, x] of Object.entries(s.labels ?? {})) this.label(id, x);
|
|
58
|
+
for (const [id, x] of Object.entries(s.routes ?? {})) this.route(id, x);
|
|
59
|
+
if (s.marks) this.marks *= s.marks.opacity;
|
|
60
|
+
this.tokens.push(...s.tokens ?? []); this.pulses.push(...s.pulses ?? []);
|
|
61
|
+
}
|
|
62
|
+
state(camera?: [number, number, number, number]): SceneState {
|
|
63
|
+
const out: SceneState = {};
|
|
64
|
+
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; };
|
|
65
|
+
const some = (o: object) => Object.keys(o).length > 0;
|
|
66
|
+
const op = (v: number | undefined) => (v === undefined || r4(v) >= 1 ? {} : { opacity: r4(Math.max(0, v)) });
|
|
67
|
+
const move = (v: { dx?: number; dy?: number }) => ({ ...(r2(v.dx ?? 0) ? { dx: r2(v.dx!) } : {}), ...(r2(v.dy ?? 0) ? { dy: r2(v.dy!) } : {}) });
|
|
68
|
+
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 } : {}) }));
|
|
69
|
+
const labels = rec(this.labels, v => ({ ...op(v.opacity), ...(v.typed !== undefined && v.typed < 1 ? { typed: r4(v.typed) } : {}), ...move(v) }));
|
|
70
|
+
const routes = rec(this.routes, v => {
|
|
71
|
+
const hidden = v.opacity !== undefined && r4(v.opacity) <= 0, part = v.drawn !== undefined && v.drawn < 1;
|
|
72
|
+
return { ...op(v.opacity), ...(part ? { drawn: r4(v.drawn!), ...(v.by ? { by: v.by } : {}) } : {}), ...(v.ants && !hidden && !part ? { ants: v.ants } : {}) };
|
|
73
|
+
});
|
|
74
|
+
if (some(items)) out.items = items;
|
|
75
|
+
if (some(labels)) out.labels = labels;
|
|
76
|
+
if (some(routes)) out.routes = routes;
|
|
77
|
+
if (r4(this.marks) < 1) out.marks = { opacity: r4(this.marks) };
|
|
78
|
+
if (this.tokens.length) out.tokens = this.tokens;
|
|
79
|
+
if (this.pulses.length) out.pulses = this.pulses;
|
|
80
|
+
if (this.outlines.length) out.outlines = this.outlines;
|
|
81
|
+
if (some(this.variants)) out.variants = this.variants;
|
|
82
|
+
if (camera) out.camera = camera;
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function bindMotion(scene: CompiledScene, doc: MotionDoc, opts: BindOptions = {}): Timeline {
|
|
88
|
+
const diags = checkMotion(doc), m = motionModel(scene), fps = doc.fps, fileAnts = antsOf(scene.raw);
|
|
89
|
+
const none = (): Timeline => ({ ok: false, diags, duration: 0, beats: [], markers: {}, at: () => ({}), cues: () => undefined, beatAt: () => undefined, startOf: () => undefined });
|
|
90
|
+
if (diags.length) return none();
|
|
91
|
+
const err = (path: string, message: string, hint?: string) => { diags.push({ severity: 'error', path, message, ...(hint ? { hint } : {}) }); };
|
|
92
|
+
const ms = (t: unknown, otherwise: number) => (t === undefined ? otherwise : msOf(t, fps)!);
|
|
93
|
+
|
|
94
|
+
// ---------- what is in the drawing ----------
|
|
95
|
+
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));
|
|
96
|
+
const kindOf = new Map(scene.routes.map(r => [r.id, r.kind]));
|
|
97
|
+
const all = [...pieces.keys(), ...routes, ...notes];
|
|
98
|
+
const known = new Set(all);
|
|
99
|
+
/** what moves and fades with a thing or a connector: its own label, and the notes on it */
|
|
100
|
+
const notesOn = new Map<string, string[]>();
|
|
101
|
+
for (const [n, on] of Object.entries(m.notes)) if (on !== undefined) notesOn.set(on, [...(notesOn.get(on) ?? []), n]);
|
|
102
|
+
const what = { any: known, thing: new Set(pieces.keys()), route: routes };
|
|
103
|
+
const words = { any: 'thing, connector or note', thing: 'thing', route: 'connector' };
|
|
104
|
+
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); } };
|
|
105
|
+
const needAll = (v: string[] | undefined, path: string, kind: keyof typeof what = 'any') => v?.forEach((id, i) => need(id, `${path}/${i}`, kind));
|
|
106
|
+
const outline = new Map(scene.layout.items.map(p => [p.id, silhouette(p.box).map(q => [r2(q[0]), r2(q[1])] as [number, number])]));
|
|
107
|
+
|
|
108
|
+
// ---------- how long things are ----------
|
|
109
|
+
const plans = new Map<object, MotionPlan>();
|
|
110
|
+
function nodeEnd(n: MotionNode, path: string): number {
|
|
111
|
+
const from = 'from' in n ? ms(n.from, 0) : 0, dur = 'duration' in n ? ms(n.duration, NaN) : NaN, given = Number.isFinite(dur);
|
|
112
|
+
if ('sequence' in n) { const s = n.sequence; return ms(s.from, 0) + seqLength(s, `${path}/sequence`); }
|
|
113
|
+
if ('series' in n) return layoutSeries(n.series, `${path}/series`).end;
|
|
114
|
+
if ('layers' in n) return Math.max(0, ...n.layers.map((x, i) => nodeEnd(x, `${path}/layers/${i}`)));
|
|
115
|
+
if ('assemble' in n || 'disassemble' in n) {
|
|
116
|
+
const kind = 'assemble' in n ? 'assemble' : 'disassemble', o = ('assemble' in n ? n.assemble : n.disassemble) as object;
|
|
117
|
+
let p = plans.get(n);
|
|
118
|
+
if (!p) plans.set(n, (p = planOf(m, kind, { seed: doc.seed ?? 1, ...o, ...(given ? { duration: dur } : {}) })));
|
|
119
|
+
return from + p.duration;
|
|
120
|
+
}
|
|
121
|
+
// draw's and lift's duration is each thing's, one after another by the stagger
|
|
122
|
+
if ('draw' in n) return from + (given ? dur : 400) + Math.max(0, n.draw.length - 1) * ms(n.stagger, 0);
|
|
123
|
+
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);
|
|
124
|
+
if (given) return from + dur;
|
|
125
|
+
if ('travel' in n) return n.loop ? from : from + n.travel.length * ms(n.each, 1150);
|
|
126
|
+
if ('pulse' in n) return n.loop ? from : from + 1500;
|
|
127
|
+
if ('type' in n) return from + 400;
|
|
128
|
+
if ('camera' in n) return from + 900;
|
|
129
|
+
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)!) ?? []));
|
|
130
|
+
return from; // (ants, focus, highlight, a loop: as long as what it is in)
|
|
131
|
+
}
|
|
132
|
+
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);
|
|
133
|
+
/** one run of what is in it, on its own clock */
|
|
134
|
+
const runOf = new Map<Sequence, number>();
|
|
135
|
+
function inner(s: Sequence, path: string): number {
|
|
136
|
+
const have = runOf.get(s); if (have !== undefined) return have;
|
|
137
|
+
let d: number;
|
|
138
|
+
if (s.duration !== undefined && typeof s.duration === 'object') d = readMs(s.say, s.duration.read);
|
|
139
|
+
else d = s.duration !== undefined ? ms(s.duration, 0) : contents(s, path);
|
|
140
|
+
if (s.duration === undefined && d <= 0 && s.say) d = readMs(s.say, true);
|
|
141
|
+
if (d <= 0) err(`${path}/duration`, 'how long is it? (nothing in it says)');
|
|
142
|
+
runOf.set(s, d);
|
|
143
|
+
return d;
|
|
144
|
+
}
|
|
145
|
+
/** how long it is where it stands */
|
|
146
|
+
const seqLength = (s: Sequence, path: string): number => {
|
|
147
|
+
const d = inner(s, path), rate = s.playbackRate ?? 1;
|
|
148
|
+
if (s.loop === true) return d / rate; // (an endless loop is as long as what it is in lets it be; alone, one run)
|
|
149
|
+
return (typeof s.loop === 'number' ? d * s.loop : d) / rate;
|
|
150
|
+
};
|
|
151
|
+
interface Placed { s: Sequence; path: string; start: number; end: number; into?: { t: Transition; length: number } }
|
|
152
|
+
function layoutSeries(items: SeriesItem[], path: string): { list: Placed[]; end: number } {
|
|
153
|
+
const list: Placed[] = [];
|
|
154
|
+
let at = 0, into: Placed['into'];
|
|
155
|
+
items.forEach((it, i) => {
|
|
156
|
+
if ('transition' in it) {
|
|
157
|
+
const t = it.transition, g = t.timing;
|
|
158
|
+
const length = t.presentation === 'none' ? 0 : 'linear' in g ? ms(g.linear, 0) : ms(g.spring.duration, settleMs(g.spring));
|
|
159
|
+
into = { t, length };
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const s = it.sequence, p = `${path}/${i}/sequence`, len = seqLength(s, p);
|
|
163
|
+
const start = Math.max(0, at + ms(s.offset, 0) - (into?.length ?? 0));
|
|
164
|
+
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');
|
|
165
|
+
list.push({ s, path: p, start, end: start + len, ...(into ? { into } : {}) });
|
|
166
|
+
at = start + len; into = undefined;
|
|
167
|
+
});
|
|
168
|
+
return { list, end: Math.max(0, ...list.map(x => x.end)) };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const top = layoutSeries(doc.series ?? [], '/series');
|
|
172
|
+
const extra = Math.max(0, ...(doc.layers ?? []).map((n, i) => nodeEnd(n, `/layers/${i}`)));
|
|
173
|
+
const duration = Math.max(top.end, extra);
|
|
174
|
+
if (duration <= 0 && !diags.length) err('', 'a motion document has a series, or layers, that last');
|
|
175
|
+
|
|
176
|
+
// ---------- its ids, and what is only a top beat's ----------
|
|
177
|
+
const starts = new Map<string, number>();
|
|
178
|
+
interface CamCue { at: number; over: number; ease: (f: number) => number; to: [number, number, number, number] }
|
|
179
|
+
const cams: CamCue[] = [];
|
|
180
|
+
const fit = m.viewBox;
|
|
181
|
+
const framed = (b: Rect, margin: number, zoom?: number): [number, number, number, number] => {
|
|
182
|
+
const k = zoom !== undefined ? 1 / zoom : Math.max((b.w + margin * 2) / fit[2], (b.h + margin * 2) / fit[3]);
|
|
183
|
+
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];
|
|
184
|
+
};
|
|
185
|
+
/** abs: the document's time of a local 0, and how fast local time runs; undefined: inside a loop */
|
|
186
|
+
function visit(n: MotionNode, path: string, abs: { at: number; rate: number } | undefined) {
|
|
187
|
+
const at = (t: number) => (abs ? { at: abs.at + t / abs.rate, rate: abs.rate } : undefined);
|
|
188
|
+
if ('sequence' in n) { seq(n.sequence, `${path}/sequence`, at(ms(n.sequence.from, 0)), false); return; }
|
|
189
|
+
if ('series' in n) { for (const p of layoutSeries(n.series, `${path}/series`).list) seq(p.s, p.path, at(p.start), false); return; }
|
|
190
|
+
if ('layers' in n) { n.layers.forEach((x, i) => visit(x, `${path}/layers/${i}`, abs)); return; }
|
|
191
|
+
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')));
|
|
192
|
+
else if ('pulse' in n) needAll(n.pulse, `${path}/pulse`, 'thing');
|
|
193
|
+
else if ('ants' in n) { if (n.ants !== true) needAll(n.ants, `${path}/ants`, 'route'); }
|
|
194
|
+
else if ('draw' in n) needAll(n.draw, `${path}/draw`, 'route');
|
|
195
|
+
else if ('type' in n) needAll(n.type, `${path}/type`);
|
|
196
|
+
else if ('move' in n) need(n.move, `${path}/move`, 'thing');
|
|
197
|
+
else if ('lift' in n) needAll(n.lift, `${path}/lift`, 'thing');
|
|
198
|
+
else if ('focus' in n) focusIds(n.focus, `${path}/focus`);
|
|
199
|
+
else if ('highlight' in n) needAll(n.highlight, `${path}/highlight`, 'thing');
|
|
200
|
+
else if ('camera' in n) {
|
|
201
|
+
const c = n.camera, over = ms(n.duration, 900);
|
|
202
|
+
if (!abs) { err(`${path}/camera`, 'a camera can\'t be in a loop'); return; }
|
|
203
|
+
let to: [number, number, number, number] = fit;
|
|
204
|
+
if (c !== 'fit') {
|
|
205
|
+
const f = c.focus;
|
|
206
|
+
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);
|
|
207
|
+
else {
|
|
208
|
+
const ids = (typeof f === 'string' ? [f] : f) as string[];
|
|
209
|
+
ids.forEach((id, i) => need(id, `${path}/camera/focus${typeof f === 'string' ? '' : `/${i}`}`, 'thing'));
|
|
210
|
+
const bs = ids.map(id => pieces.get(id)?.bbox).filter((b): b is Rect => !!b);
|
|
211
|
+
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); }
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
cams.push({ at: abs.at + ms(n.from, 0) / abs.rate, over: over / abs.rate, ease: easeOf(n.easing, over, 'settle'), to });
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const focusIds = (f: Focus | undefined, path: string) => { if (f) { needAll(f.lit, `${path}/lit`); needAll(f.always, `${path}/always`); } };
|
|
218
|
+
function seq(s: Sequence, path: string, abs: { at: number; rate: number } | undefined, isTop: boolean) {
|
|
219
|
+
if (s.name !== undefined && abs) starts.set(s.name, abs.at);
|
|
220
|
+
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);
|
|
221
|
+
needAll(s.show, `${path}/show`); needAll(s.hide, `${path}/hide`); needAll(s.highlight, `${path}/highlight`, 'thing'); focusIds(s.focus, `${path}/focus`);
|
|
222
|
+
for (const id of Object.keys(s.levels ?? {})) if (id !== '*') need(id, `${path}/levels/${id}`);
|
|
223
|
+
for (const [id, name] of Object.entries(s.variants ?? {})) {
|
|
224
|
+
need(id, `${path}/variants/${id}`, 'thing');
|
|
225
|
+
const have = scene.rendered.looks?.[id] ?? [];
|
|
226
|
+
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); }
|
|
227
|
+
}
|
|
228
|
+
if (Array.isArray(s.ants)) needAll(s.ants, `${path}/ants`, 'route');
|
|
229
|
+
const rate = s.playbackRate ?? 1;
|
|
230
|
+
const local = abs && !s.loop ? { at: abs.at - ms(s.trimBefore, 0) / (abs.rate * rate), rate: abs.rate * rate } : undefined;
|
|
231
|
+
(s.children ?? []).forEach((n, i) => visit(n, `${path}/children/${i}`, local));
|
|
232
|
+
(s.layers ?? []).forEach((n, i) => visit(n, `${path}/layers/${i}`, local));
|
|
233
|
+
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);
|
|
234
|
+
}
|
|
235
|
+
for (const p of top.list) seq(p.s, p.path, { at: p.start, rate: 1 }, true);
|
|
236
|
+
(doc.layers ?? []).forEach((n, i) => visit(n, `/layers/${i}`, { at: 0, rate: 1 }));
|
|
237
|
+
cams.sort((a, b) => a.at - b.at);
|
|
238
|
+
const markers: Record<string, number> = {};
|
|
239
|
+
for (const [k, v] of Object.entries(doc.markers ?? {})) {
|
|
240
|
+
const t = msOf(v, fps);
|
|
241
|
+
if (t !== undefined) { markers[k] = t; continue; }
|
|
242
|
+
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];
|
|
243
|
+
if (hit === undefined) { err(`/markers/${k}`, `"${s}" isn't a moment of this document`); continue; }
|
|
244
|
+
const rest = s.slice(hit.length), off = rest ? msOf(rest.slice(1), fps) : 0;
|
|
245
|
+
if (off === undefined) { err(`/markers/${k}`, `"${rest}" isn't a time`); continue; }
|
|
246
|
+
markers[k] = starts.get(hit)! + (rest[0] === '-' ? -off : off);
|
|
247
|
+
}
|
|
248
|
+
if (diags.length) return none();
|
|
249
|
+
|
|
250
|
+
// ---------- the beats: what is there, what is lit, as the fold of every beat to each ----------
|
|
251
|
+
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 } : {}) }));
|
|
252
|
+
const managed = new Map<string, boolean>(); // id → whether it is there before the first beat
|
|
253
|
+
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); }
|
|
254
|
+
const folds: Map<string, boolean>[] = [new Map(managed)];
|
|
255
|
+
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); }
|
|
256
|
+
/** the look each thing has in beat k (folds' own, from before the first) */
|
|
257
|
+
const looked: Map<string, string>[] = [new Map()];
|
|
258
|
+
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); }
|
|
259
|
+
/** how much a focus leaves of an id */
|
|
260
|
+
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);
|
|
261
|
+
/** an id's share in beat k (-1: before the first) */
|
|
262
|
+
const level = (k: number, id: string) => { const v = k < 0 ? undefined : top.list[k].s.levels; return v ? v[id] ?? v['*'] ?? 1 : 1; };
|
|
263
|
+
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);
|
|
264
|
+
/** a note goes with what it is on; what the document says of the note itself is on top of that */
|
|
265
|
+
const noteShare = (k: number, n: string): number => {
|
|
266
|
+
const on = m.notes[n];
|
|
267
|
+
if (on === undefined) return share(k, n);
|
|
268
|
+
const v = k < 0 ? undefined : top.list[k].s.levels?.[n];
|
|
269
|
+
return share(k, on) * (folds[k + 1].get(n) === false ? 0 : 1) * (v ?? 1);
|
|
270
|
+
};
|
|
271
|
+
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; };
|
|
272
|
+
/** how much of beat k has come in at T (1: all of it) */
|
|
273
|
+
const entered = (k: number, T: number) => {
|
|
274
|
+
const p = top.list[k], lt = T - p.start;
|
|
275
|
+
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); }
|
|
276
|
+
const e = p.s.enter; if (!e) return 1;
|
|
277
|
+
const over = ms(e.fade, 0);
|
|
278
|
+
return over <= 0 ? 1 : easeOf(e.easing, over, 'ease')(clamp(lt / over));
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
// ---------- a moment ----------
|
|
282
|
+
type Ctx = { sum: Sum; shown: (id: string) => boolean; off: boolean; A: number };
|
|
283
|
+
function each(n: MotionNode, t: number, a: number, end: number, c: Ctx): void {
|
|
284
|
+
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; }
|
|
285
|
+
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; }
|
|
286
|
+
if ('layers' in n) { for (const x of n.layers) each(x, t, a, end, c); return; }
|
|
287
|
+
const from = ms('from' in n ? n.from : undefined, 0), lt = t - from;
|
|
288
|
+
if ('assemble' in n || 'disassemble' in n) { c.sum.add(plans.get(n)!.at(lt)); return; }
|
|
289
|
+
if ('draw' in n) {
|
|
290
|
+
const one = Math.max(1, ms(n.duration, 400)), gap = ms(n.stagger, 0), e = easeOf(n.easing, one, 'ease-out');
|
|
291
|
+
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 }); } });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
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; }
|
|
295
|
+
if ('move' in n) {
|
|
296
|
+
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);
|
|
297
|
+
c.sum.item(n.move, { dx, dy, ...(sc === undefined ? {} : { scale: sc, about: pieces.get(n.move)!.foot }), ...(op === undefined ? {} : { opacity: clamp(op) }) });
|
|
298
|
+
for (const id of [n.move, ...notesOn.get(n.move) ?? []]) c.sum.label(id, { dx, dy, ...(op === undefined ? {} : { opacity: clamp(op) }) });
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if ('lift' in n) {
|
|
302
|
+
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;
|
|
303
|
+
n.lift.forEach((id, i) => {
|
|
304
|
+
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;
|
|
305
|
+
if (!dy && o >= 1) return;
|
|
306
|
+
c.sum.item(id, { dy, opacity: o });
|
|
307
|
+
for (const x of [id, ...notesOn.get(id) ?? []]) c.sum.label(x, { dy, opacity: o });
|
|
308
|
+
});
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
if ('camera' in n) return;
|
|
312
|
+
// what lasts: from its start to its duration's end, or to the end of what it is in
|
|
313
|
+
const lasts = n.duration === undefined ? Math.max(0, end - from) : ms(n.duration, 0);
|
|
314
|
+
if (lt < 0 || lt > lasts) return;
|
|
315
|
+
if ('focus' in n || 'highlight' in n) {
|
|
316
|
+
const fade = Math.max(1, ms(n.fade, FADE)), amount = clamp(Math.min(lt / fade, n.duration === undefined ? 1 : (lasts - lt) / fade));
|
|
317
|
+
const e = easeOf(n.easing, fade, 'linear')(amount);
|
|
318
|
+
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);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if ('ants' in n) { march(n.ants === true ? [...routes] : n.ants, c, n.speed, n.dash); return; }
|
|
322
|
+
if ('pulse' in n) {
|
|
323
|
+
const d = 1500, p = n.loop ? turned(a - from, d, c) / d : lt / Math.max(1, n.duration === undefined ? d : lasts);
|
|
324
|
+
if (p < 0 || p >= 1) return;
|
|
325
|
+
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) }); }
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if ('travel' in n) {
|
|
329
|
+
const one = ms(n.each, 1150), hold = ms(n.hold, n.loop ? 700 : 0), period = n.travel.length * one + hold;
|
|
330
|
+
const q = n.loop ? turned(a - from, period, c) : lt, i = Math.floor(q / one);
|
|
331
|
+
if (i >= n.travel.length) return;
|
|
332
|
+
const f = easeOf(n.easing, one, 'ease-in-out')((q - i * one) / one), group = n.travel[i];
|
|
333
|
+
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(f), ...(kind ? { kind } : {}) }); }
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/** where in its turn a loop is: with the ambient off, at its start */
|
|
337
|
+
const turned = (x: number, period: number, c: Ctx) => (c.off ? 0 : ((x % period) + period) % period);
|
|
338
|
+
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) } : {}) }); };
|
|
339
|
+
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 }); } };
|
|
340
|
+
const march = (ids: string[], c: Ctx, speed = ANT_SPEED, dash?: [number, number]) => {
|
|
341
|
+
for (const id of ids) {
|
|
342
|
+
const d = antDash(kindOf.get(id) ?? '', { dash, kinds: opts.kinds, still: opts.still });
|
|
343
|
+
if (!d) continue;
|
|
344
|
+
const period = d[0] + d[1], o = turned((speed * c.A) / 1000, period, c);
|
|
345
|
+
c.sum.route(id, { ants: { dash: d, offset: r2(o) } });
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
const runLength = (s: Sequence, rest: number) => (s.loop === true ? Math.max(0, rest) : seqLength(s, ''));
|
|
349
|
+
/** what a sequence shows at its own time t (a: the ambient time, from its start) */
|
|
350
|
+
function run(s: Sequence, t: number, a: number, length: number, c: Ctx): void {
|
|
351
|
+
if (t < 0 || t > length || (t === length && length < duration && length > 0 && s.loop)) return;
|
|
352
|
+
const rate = s.playbackRate ?? 1, one = inner(s, ''), trim = ms(s.trimBefore, 0);
|
|
353
|
+
const turn = s.loop && one > 0 ? (t * rate) % one : t * rate, lt = turn + trim, la = a * rate + trim;
|
|
354
|
+
for (const n of s.children ?? []) each(n, lt, la, one + trim, c);
|
|
355
|
+
for (const n of s.layers ?? []) each(n, lt, la, one + trim, c);
|
|
356
|
+
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);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function at(ms0: number, o: { ambient?: number | 'off' } = {}): SceneState {
|
|
360
|
+
const T = Math.max(0, Math.min(duration, Number.isFinite(ms0) ? ms0 : 0));
|
|
361
|
+
const off = o.ambient === 'off' || (doc.ambient === 'off' && o.ambient === undefined), A = typeof o.ambient === 'number' ? o.ambient : T;
|
|
362
|
+
const sum = new Sum(), k = beatIndex(T), f = k < 0 ? 1 : entered(k, T);
|
|
363
|
+
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));
|
|
364
|
+
const shown = (id: string) => (k < 0 ? folds[0] : folds[k + 1]).get(id) !== false;
|
|
365
|
+
if (managed.size || top.list.some(p => p.s.focus || p.s.levels)) for (const id of all) {
|
|
366
|
+
const v = of(id, notes.has(id) ? noteShare : share);
|
|
367
|
+
if (v >= 1) continue;
|
|
368
|
+
if (pieces.has(id)) sum.item(id, { opacity: v });
|
|
369
|
+
if (routes.has(id)) sum.route(id, { opacity: v });
|
|
370
|
+
sum.label(id, { opacity: v });
|
|
371
|
+
}
|
|
372
|
+
// looks: the one before gives way to this beat's as the beat comes in
|
|
373
|
+
if (k >= 0) for (const id of looked[looked.length - 1].keys()) {
|
|
374
|
+
const was = looked[k].get(id) ?? '', now = looked[k + 1].get(id) ?? '', v: Record<string, number> = {};
|
|
375
|
+
if (was && f < 1) v[was] = r4(1 - f);
|
|
376
|
+
if (now) v[now] = r4((v[now] ?? 0) + f);
|
|
377
|
+
for (const n of Object.keys(v)) if (v[n] <= 0) delete v[n];
|
|
378
|
+
if (Object.keys(v).length) sum.variants[id] = v;
|
|
379
|
+
}
|
|
380
|
+
// highlights: the beat's come in with it, the one before's go out
|
|
381
|
+
if (k >= 0) {
|
|
382
|
+
const now = new Set(top.list[k].s.highlight ?? []), was = new Set(k > 0 && f < 1 ? top.list[k - 1].s.highlight ?? [] : []);
|
|
383
|
+
for (const id of new Set([...was, ...now])) ring(id, mix(was.has(id) ? 1 : 0, now.has(id) ? 1 : 0, f), sum);
|
|
384
|
+
}
|
|
385
|
+
const c: Ctx = { sum, shown, off, A };
|
|
386
|
+
// a beat's own ants, else the file's
|
|
387
|
+
const ants = k >= 0 ? top.list[k].s.ants : undefined, file = ants === undefined ? fileAnts : undefined;
|
|
388
|
+
if (ants) march(ants === true ? [...routes] : ants, c);
|
|
389
|
+
else if (file) march(file.ids ?? [...routes], c, file.speed, file.dash);
|
|
390
|
+
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); });
|
|
391
|
+
for (const n of doc.layers ?? []) each(n, T, A, duration, c);
|
|
392
|
+
// the camera: each cue from where the one before had got it
|
|
393
|
+
let view: [number, number, number, number] | undefined;
|
|
394
|
+
for (const q of cams) {
|
|
395
|
+
if (q.at > T) break;
|
|
396
|
+
const u = q.over <= 0 ? 1 : q.ease(clamp((T - q.at) / q.over)), was = view ?? fit;
|
|
397
|
+
if (u >= 1) { view = q.to; continue; }
|
|
398
|
+
// even in log-zoom; the middle goes with the zoom, so that a point's path is straight on the screen
|
|
399
|
+
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);
|
|
400
|
+
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];
|
|
401
|
+
view = [cx - z / 2, cy - h / 2, z, h];
|
|
402
|
+
}
|
|
403
|
+
const cam = view?.map(r2) as [number, number, number, number] | undefined;
|
|
404
|
+
return sum.state(cam && cam.some((v, i) => Math.abs(v - fit[i]) > 0.005) ? cam : undefined);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const beatAt = (t: number) => { const k = beatIndex(Math.max(0, Math.min(duration, t))); return k < 0 ? undefined : beats[k]; };
|
|
408
|
+
return {
|
|
409
|
+
ok: true, diags, duration, beats, markers, at, beatAt,
|
|
410
|
+
startOf: n => starts.get(n) ?? beats.find(b => b.name === n)?.start,
|
|
411
|
+
cues(t) {
|
|
412
|
+
const b = beatAt(t);
|
|
413
|
+
if (!b) return undefined;
|
|
414
|
+
const T = Math.max(0, Math.min(duration, t));
|
|
415
|
+
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 } : {}) } : {}) };
|
|
416
|
+
},
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
/** how long a spring takes to come to rest, ms: its envelope under half a percent */
|
|
420
|
+
function settleMs(c: { mass?: number; damping?: number; stiffness?: number }): number {
|
|
421
|
+
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)));
|
|
422
|
+
return Math.ceil((Math.log(200) / (z * w0)) * 1000 * (z >= 1 ? 1.6 : 1));
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** A drawing's own steps as a motion document, so that every drawing with steps has a timeline. */
|
|
426
|
+
export function motionOfSteps(scene: CompiledScene): MotionDoc {
|
|
427
|
+
const { steps, vis } = scene.steps;
|
|
428
|
+
const series: SeriesItem[] = steps.map((st, i) => {
|
|
429
|
+
const show: string[] = [], hide: string[] = [];
|
|
430
|
+
// (an id first shown is hidden until then, and one first hidden is there until then: so only the changes are said)
|
|
431
|
+
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); }
|
|
432
|
+
const children: MotionNode[] = [];
|
|
433
|
+
if (st.phases.length) children.push({ travel: st.phases.map(g => (g.length === 1 ? g[0].route : g.map(t => t.route))), each: 1150, hold: 700, loop: true });
|
|
434
|
+
const pulse = ((scene.raw.steps ?? [])[i] as { pulse?: string[] } | undefined)?.pulse?.filter(id => scene.layout.byId.has(id)) ?? [];
|
|
435
|
+
if (pulse.length) children.push({ pulse, loop: true });
|
|
436
|
+
const s: Sequence = { name: `step-${st.n}`, duration: st.duration, say: { title: st.heading, ...(st.caption ? { text: st.caption } : {}), ...(st.command ? { code: st.command } : {}) } };
|
|
437
|
+
if (show.length) s.show = show;
|
|
438
|
+
if (hide.length) s.hide = hide;
|
|
439
|
+
if (children.length) s.children = children;
|
|
440
|
+
return { sequence: s };
|
|
441
|
+
});
|
|
442
|
+
return { isomotion: 1, ...(series.length ? { series } : {}) };
|
|
443
|
+
}
|
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,6 +50,10 @@ 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 {
|
|
@@ -7,11 +7,50 @@ import { place } from '../iso-objects/place.ts';
|
|
|
7
7
|
import { extendTheme, loadTheme } from '../iso-core/theme.ts';
|
|
8
8
|
import type { RawDiagram } from '../iso-objects/types.ts';
|
|
9
9
|
import { compileRaw, type Compiled } from '../iso-objects/compile.ts';
|
|
10
|
+
import { resolveSvg, type ThemeMode } from '../iso-objects/resolve.ts';
|
|
11
|
+
import { bindMotion } from '../iso-objects/timeline.ts';
|
|
12
|
+
import type { MotionDoc } from '../iso-objects/motion-doc.ts';
|
|
13
|
+
import { antsOf } from '../iso-objects/ants.ts';
|
|
14
|
+
import { drawLooks } from '../iso-objects/looks.ts';
|
|
15
|
+
import { drawingCss, GRID_CSS, MARKS_CSS } from '../iso-objects/css.ts';
|
|
10
16
|
import { systems } from './catalog.ts';
|
|
11
17
|
export type { Compiled, CompiledScene } from '../iso-objects/compile.ts';
|
|
12
18
|
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
export interface CompileOptions {
|
|
20
|
+
/** overrides every scene's grid snap (the editor's snap control) */
|
|
21
|
+
snap?: number;
|
|
22
|
+
/** which of the theme's modes a self-contained drawing is in (default: light) */
|
|
23
|
+
mode?: ThemeMode;
|
|
24
|
+
/** (0.15.0) self-contained drawings: each scene's SVG has its colours and text styles on its elements, in `mode`: no
|
|
25
|
+
* stylesheet, no custom properties, no hsl() or color-mix() (svgAt's frames too) */
|
|
26
|
+
resolveVars?: boolean;
|
|
27
|
+
/** a file with an error draws nothing: `scenes` is empty (true), or it throws ('throw'). Default: what could be laid
|
|
28
|
+
* out is in `scenes` all the same, with `ok` false */
|
|
29
|
+
strict?: boolean | 'throw';
|
|
30
|
+
/** ms each step lasts, for a video (default: by the length of its words) */
|
|
31
|
+
stepMs?: number;
|
|
32
|
+
}
|
|
33
|
+
export class CompileError extends Error {
|
|
34
|
+
diags: Diag[];
|
|
35
|
+
constructor(diags: Diag[]) { super(diags.filter(d => d.severity === 'error').map(d => `line ${d.line}: ${d.message}`).join('\n')); this.name = 'CompileError'; this.diags = diags; }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function compile(source: string, themeText: string, opts: CompileOptions = {}): Compiled {
|
|
39
|
+
const c = compileAll(source, themeText, opts);
|
|
40
|
+
if (!c.ok && opts.strict === 'throw') throw new CompileError(c.diags);
|
|
41
|
+
if (!c.ok && opts.strict) c.scenes = [];
|
|
42
|
+
if (opts.stepMs !== undefined) for (const s of c.scenes) for (const st of s.steps.steps) st.duration = opts.stepMs;
|
|
43
|
+
if (opts.resolveVars) for (const s of c.scenes) {
|
|
44
|
+
const o = { mode: opts.mode ?? 'light', theme: c.theme!, css: drawingCss(systems) + MARKS_CSS + GRID_CSS } as const;
|
|
45
|
+
s.rendered.svg = resolveSvg(s.rendered.svg, o);
|
|
46
|
+
if (s.rendered.variants) s.rendered.variants = Object.fromEntries(Object.entries(s.rendered.variants).map(([k, v]) => [k, resolveSvg(v, o)]));
|
|
47
|
+
s.rendered.inks = { surface: c.theme![o.mode].surface ?? c.theme!.light.surface, control: c.theme![o.mode].control ?? c.theme!.light.control,
|
|
48
|
+
kinds: Object.fromEntries(Object.entries(c.theme!.kinds).map(([k, v]) => [k, c.theme![o.mode][v.color] ?? v.color])) };
|
|
49
|
+
}
|
|
50
|
+
return c;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function compileAll(source: string, themeText: string, opts: { snap?: number }): Compiled {
|
|
15
54
|
const parsed = parse(source);
|
|
16
55
|
const diags: Diag[] = [...parsed.diags];
|
|
17
56
|
if (diags.some(d => d.severity === 'error')) return { ok: false, diags, catalog: systems, scenes: [] };
|
|
@@ -35,5 +74,21 @@ export function compile(source: string, themeText: string, opts: { snap?: number
|
|
|
35
74
|
return { ok: false, diags, catalog: systems, scenes: [] };
|
|
36
75
|
}
|
|
37
76
|
|
|
38
|
-
|
|
77
|
+
const c = compileRaw(doc, themeText, systems, parsed, source, { snap: opts.snap }, diags);
|
|
78
|
+
// a scene's own motion document: checked here, so that its faults are said where they are in the file
|
|
79
|
+
let faults = 0;
|
|
80
|
+
const say = (path: string, message: string, hint?: string) => { const at = parsed.locate(path); c.diags.push({ severity: 'error', message, line: at.line, col: at.col, path, ...(hint ? { hint } : {}) }); faults++; };
|
|
81
|
+
c.scenes.forEach((s, i) => {
|
|
82
|
+
drawLooks(s, c.theme!);
|
|
83
|
+
const ants = s.raw.ants, ids = antsOf(s.raw)?.ids, known = s.routes.map(r => r.id);
|
|
84
|
+
(ids ?? []).forEach((id, k) => { if (!known.includes(id)) say(`/scenes/${i}/ants${Array.isArray(ants) ? '' : '/ids'}/${k}`, `no connector "${id}" in this scene`, known.length ? `its connectors: ${known.slice(0, 8).join(', ')}${known.length > 8 ? ', …' : ''}` : undefined); });
|
|
85
|
+
if (!s.raw.motion) return;
|
|
86
|
+
const motion = { isomotion: 1, ...s.raw.motion } as MotionDoc, path = `/scenes/${i}/motion`;
|
|
87
|
+
const said = bindMotion(s, motion, { kinds: c.theme!.kinds }).diags;
|
|
88
|
+
for (const d of said) { const at = parsed.locate(path + d.path); c.diags.push({ severity: d.severity, message: d.message, line: at.line, col: at.col, path: path + d.path, ...(d.hint ? { hint: d.hint } : {}) }); }
|
|
89
|
+
faults += said.length;
|
|
90
|
+
if (!said.length) s.motion = motion;
|
|
91
|
+
});
|
|
92
|
+
if (faults) { c.ok = false; c.diags.sort((a, b) => a.line - b.line || a.col - b.col); }
|
|
93
|
+
return c;
|
|
39
94
|
}
|
package/src/iso-systems/index.ts
CHANGED
|
@@ -8,5 +8,8 @@ export { NODE_ASSETS, getAsset } from './nodes.ts';
|
|
|
8
8
|
export { REGION_KINDS, systems } from './catalog.ts';
|
|
9
9
|
export { DRAWING_CSS, BASE_CSS } from './css.ts';
|
|
10
10
|
export { validateSchema, schemaValidator } from './validate.ts';
|
|
11
|
-
export { compile } from './compile.ts';
|
|
11
|
+
export { compile, CompileError } from './compile.ts';
|
|
12
|
+
export type { CompileOptions } from './compile.ts';
|
|
13
|
+
export { renderSvg } from './render.ts';
|
|
14
|
+
export type { RenderSvgOptions } from './render.ts';
|
|
12
15
|
export type { Compiled, CompiledScene } from './compile.ts';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// One call for a pipeline (a static site, Markdown): a diagram's text to one SVG, as text, that needs nothing else: its colours
|
|
2
|
+
// and text styles on its elements, no stylesheet, no script. Node or a browser; no DOM.
|
|
3
|
+
|
|
4
|
+
import { antsOf, withAnts, type AntsOptions } from '../iso-objects/ants.ts';
|
|
5
|
+
import { svgAt } from '../iso-objects/motion.ts';
|
|
6
|
+
import { bindMotion } from '../iso-objects/timeline.ts';
|
|
7
|
+
import type { MotionDoc } from '../iso-objects/motion-doc.ts';
|
|
8
|
+
import { compile } from './compile.ts';
|
|
9
|
+
import type { ThemeMode } from '../iso-objects/resolve.ts';
|
|
10
|
+
|
|
11
|
+
export interface RenderSvgOptions {
|
|
12
|
+
/** the theme's text */
|
|
13
|
+
theme: string;
|
|
14
|
+
mode?: ThemeMode;
|
|
15
|
+
/** which scene: its id or its place (default: the first) */
|
|
16
|
+
scene?: string | number;
|
|
17
|
+
/** a moment of its motion, ms: the still without it */
|
|
18
|
+
t?: number;
|
|
19
|
+
/** the motion (default: the scene's own in the file) */
|
|
20
|
+
motion?: MotionDoc;
|
|
21
|
+
/** the ambient clock at that moment (default: t), or 'off': what loops stands at its start (a still's ants too) */
|
|
22
|
+
ambient?: number | 'off';
|
|
23
|
+
/** the still's connectors march, by an animation in the SVG itself (default: as the scene's `ants:` in the file says; a
|
|
24
|
+
* frame's are its motion's, and where that says nothing, the file's) */
|
|
25
|
+
ants?: boolean | Omit<AntsOptions, 'by' | 'kinds'>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Throws, with every fault and its line, when the file or its motion has one. */
|
|
29
|
+
export function renderSvg(source: string, o: RenderSvgOptions): string {
|
|
30
|
+
const c = compile(source, o.theme, { mode: o.mode ?? 'light', resolveVars: true, strict: 'throw' }), kinds = c.theme!.kinds;
|
|
31
|
+
const s = o.scene === undefined ? c.scenes[0] : typeof o.scene === 'number' ? c.scenes[o.scene] : c.scenes.find(x => x.id === o.scene);
|
|
32
|
+
if (!s) throw new Error(`no scene ${JSON.stringify(o.scene)} (there ${c.scenes.length === 1 ? 'is' : 'are'} ${c.scenes.map(x => x.id).join(', ')})`);
|
|
33
|
+
const ants = o.ants === undefined ? antsOf(s.raw) : o.ants === true ? antsOf(s.raw) ?? {} : o.ants || undefined;
|
|
34
|
+
if (o.t === undefined) return ants ? withAnts(s, { ...ants, by: o.ambient === 'off' ? 'none' : 'smil', kinds }) : s.rendered.svg;
|
|
35
|
+
if (o.ants) throw new Error('ants: at a moment they are its motion document\'s');
|
|
36
|
+
const doc = o.motion ?? s.motion;
|
|
37
|
+
if (!doc) throw new Error('t: this scene has no motion');
|
|
38
|
+
const tl = bindMotion(s, doc, { kinds });
|
|
39
|
+
if (!tl.ok) throw new Error(tl.diags.map(d => `${d.path}: ${d.message}${d.hint ? ` (${d.hint})` : ''}`).join('\n'));
|
|
40
|
+
return svgAt(s, tl.at(o.t, o.ambient === undefined ? {} : { ambient: o.ambient }));
|
|
41
|
+
}
|