@johnoscott/isometric-engine 0.12.6 → 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.
Files changed (71) hide show
  1. package/EDITOR-GUIDE.md +9 -2
  2. package/README.md +1 -1
  3. package/assets/app.css +6 -0
  4. package/assets/marks.css +2 -0
  5. package/iso-editor.js +194 -184
  6. package/iso-render.js +157 -148
  7. package/iso-systems.js +170 -159
  8. package/js/iso-editor/make-way.js +62 -0
  9. package/js/iso-editor/ui/canvas.js +477 -216
  10. package/js/iso-editor/ui/palette-look.js +53 -0
  11. package/js/iso-editor/ui/setup.js +3 -0
  12. package/js/iso-objects/ants.js +36 -0
  13. package/js/iso-objects/index.js +47 -1
  14. package/js/iso-objects/looks.js +25 -0
  15. package/js/iso-objects/motion-builder.js +130 -0
  16. package/js/iso-objects/motion-doc.js +500 -0
  17. package/js/iso-objects/motion-entry.js +32 -0
  18. package/js/iso-objects/motion.js +635 -0
  19. package/js/iso-objects/resolve.js +238 -0
  20. package/js/iso-objects/scene.js +15 -13
  21. package/js/iso-objects/timeline.js +553 -0
  22. package/js/iso-systems/compile.js +59 -1
  23. package/js/iso-systems/index.js +4 -1
  24. package/js/iso-systems/render.js +20 -0
  25. package/js/iso-systems/schema.json +39 -0
  26. package/js/iso-viewer/page.js +2 -1
  27. package/js/iso-viewer/player.js +23 -34
  28. package/package.json +46 -1
  29. package/src/iso-editor/make-way.ts +89 -0
  30. package/src/iso-editor/ui/app.css +4 -0
  31. package/src/iso-editor/ui/canvas.ts +390 -170
  32. package/src/iso-editor/ui/marks.css +2 -0
  33. package/src/iso-editor/ui/palette-look.ts +51 -0
  34. package/src/iso-editor/ui/setup.ts +8 -0
  35. package/src/iso-objects/ants.ts +53 -0
  36. package/src/iso-objects/compile.ts +4 -1
  37. package/src/iso-objects/index.ts +14 -1
  38. package/src/iso-objects/looks.ts +21 -0
  39. package/src/iso-objects/motion-builder.ts +79 -0
  40. package/src/iso-objects/motion-doc.ts +361 -0
  41. package/src/iso-objects/motion-entry.ts +12 -0
  42. package/src/iso-objects/motion.ts +616 -0
  43. package/src/iso-objects/resolve.ts +196 -0
  44. package/src/iso-objects/scene.ts +29 -13
  45. package/src/iso-objects/timeline.ts +443 -0
  46. package/src/iso-objects/types.ts +20 -2
  47. package/src/iso-systems/compile.ts +58 -3
  48. package/src/iso-systems/index.ts +4 -1
  49. package/src/iso-systems/render.ts +41 -0
  50. package/src/iso-systems/schema.json +39 -0
  51. package/src/iso-viewer/page.ts +2 -1
  52. package/src/iso-viewer/player.ts +24 -41
  53. package/types/iso-editor/make-way.d.ts +40 -0
  54. package/types/iso-editor/ui/canvas.d.ts +33 -12
  55. package/types/iso-editor/ui/palette-look.d.ts +5 -0
  56. package/types/iso-editor/ui/setup.d.ts +5 -0
  57. package/types/iso-objects/ants.d.ts +33 -0
  58. package/types/iso-objects/compile.d.ts +3 -0
  59. package/types/iso-objects/index.d.ts +14 -1
  60. package/types/iso-objects/looks.d.ts +4 -0
  61. package/types/iso-objects/motion-builder.d.ts +105 -0
  62. package/types/iso-objects/motion-doc.d.ts +787 -0
  63. package/types/iso-objects/motion-entry.d.ts +10 -0
  64. package/types/iso-objects/motion.d.ts +200 -0
  65. package/types/iso-objects/resolve.d.ts +20 -0
  66. package/types/iso-objects/scene.d.ts +11 -0
  67. package/types/iso-objects/timeline.d.ts +46 -0
  68. package/types/iso-objects/types.d.ts +32 -0
  69. package/types/iso-systems/compile.d.ts +20 -3
  70. package/types/iso-systems/index.d.ts +4 -1
  71. package/types/iso-systems/render.d.ts +21 -0
@@ -0,0 +1,361 @@
1
+ // The motion document (docs/design/MOTION.md, 3.2): an animation as plain data, with the names and the model of Remotion's
2
+ // own primitives (Sequence, Series, interpolate, spring; from its documentation, no code of it). Here: its types, its JSON
3
+ // Schema, its times, easings and curves, and the checks of its shape. What it looks like on a drawing is timeline.ts.
4
+ // Pure: no clock, no DOM, no randomness that isn't seeded.
5
+
6
+ import { didYouMean } from '../iso-core/errors.ts';
7
+ import { ASSEMBLE_STYLES, DISASSEMBLE_STYLES, MOTION_ORDERS, type PlanOptions } from './motion.ts';
8
+
9
+ /** ms, or "1.2s", "450ms", "30f" (frames, at the document's fps) */
10
+ export type MotionTime = number | string;
11
+ export const EASING_NAMES = ['linear', 'ease', 'ease-in', 'ease-out', 'ease-in-out', 'settle', 'bounce'] as const;
12
+ export type EasingName = typeof EASING_NAMES[number];
13
+ export interface SpringConfig { mass?: number; damping?: number; stiffness?: number; overshootClamping?: boolean }
14
+ export type MotionEase = EasingName | { bezier: [number, number, number, number] } | { spring: SpringConfig };
15
+ export const EXTRAPOLATE = ['extend', 'clamp', 'wrap', 'identity'] as const;
16
+ export type Extrapolate = typeof EXTRAPOLATE[number];
17
+ /** a value over time: Remotion's interpolate, with ms for frames */
18
+ export interface Curve { times: MotionTime[]; values: number[]; easing?: MotionEase; extrapolateLeft?: Extrapolate; extrapolateRight?: Extrapolate }
19
+ /** the commentary of a beat */
20
+ export interface Say { chapter?: string; title?: string; text?: string; code?: string; facts?: [string, string][]; color?: string }
21
+ /** what a beat's focus dims: everything else, or the connectors and notes only */
22
+ export interface Focus { lit: string[]; scope?: 'all' | 'links'; level?: number; always?: string[] }
23
+ /** as long as it takes to read it: base + perChar × the text's length, between min and max */
24
+ export interface ReadTime { read: true | { base?: number; perChar?: number; min?: number; max?: number } }
25
+
26
+ interface ChildTiming { from?: MotionTime; duration?: MotionTime; easing?: MotionEase }
27
+ export interface Sequence {
28
+ name?: string; from?: MotionTime; duration?: MotionTime | ReadTime; loop?: boolean | number; playbackRate?: number; trimBefore?: MotionTime;
29
+ /** in a series: later (or, negative, earlier) than the end of the one before; those after it move too */
30
+ offset?: MotionTime;
31
+ say?: Say;
32
+ /** cumulative: what is there is the fold of every beat to here */
33
+ show?: string[]; hide?: string[];
34
+ focus?: Focus;
35
+ /** things ringed while the beat lasts */
36
+ highlight?: string[];
37
+ /** how much of a thing or a connector is there in this beat (0 to 1), as a phase has it; with `*`, every one not named */
38
+ levels?: Record<string, number>;
39
+ /** which of its looks a thing has from this beat on (cumulative, as show is); "" is its own */
40
+ variants?: Record<string, string>;
41
+ /** the ramp from the beat before (what a CSS transition would do) */
42
+ enter?: { fade: MotionTime; easing?: MotionEase };
43
+ /** marching dashes: every connector shown, or these; a beat's own */
44
+ ants?: boolean | string[];
45
+ children?: MotionNode[]; layers?: MotionNode[]; series?: SeriesItem[];
46
+ }
47
+ export interface Transition { presentation: 'fade' | 'none'; timing: { linear: MotionTime } | { spring: SpringConfig & { duration?: MotionTime } } }
48
+ export type SeriesItem = { sequence: Sequence } | { transition: Transition };
49
+ export type MotionNode =
50
+ | { sequence: Sequence }
51
+ | { series: SeriesItem[] }
52
+ | { layers: MotionNode[] }
53
+ | ({ assemble: Omit<PlanOptions, 'duration'> } & ChildTiming)
54
+ | ({ disassemble: Omit<PlanOptions, 'duration'> } & ChildTiming)
55
+ /** tokens along connectors: in sequence; a nested array is in parallel; "id:kind" colours one by a kind */
56
+ | ({ travel: (string | string[])[]; each?: MotionTime; hold?: MotionTime; loop?: boolean } & ChildTiming)
57
+ | ({ pulse: string[]; loop?: boolean } & ChildTiming)
58
+ | ({ ants: true | string[]; speed?: number; dash?: [number, number] } & ChildTiming)
59
+ | ({ draw: string[]; by?: 'stroke' | 'grow'; stagger?: MotionTime } & ChildTiming)
60
+ | ({ type: string[] } & ChildTiming)
61
+ | ({ move: string; x?: Curve; y?: Curve; scale?: Curve; opacity?: Curve } & { from?: MotionTime })
62
+ /** `opacity`: over each thing's own time, from its own start (a slab that appears as it rises) */
63
+ | ({ lift: string[]; by: number; stagger?: MotionTime; to?: 'up' | 'down'; opacity?: Curve } & ChildTiming)
64
+ /** `fade`: how long it comes in and goes out over (250) */
65
+ | ({ focus: Focus; fade?: MotionTime } & ChildTiming)
66
+ | ({ highlight: string[]; fade?: MotionTime } & ChildTiming)
67
+ | ({ camera: { focus: string | string[] | [number, number, number, number]; zoom?: number; margin?: number } | 'fit' } & ChildTiming);
68
+ export interface MotionDoc {
69
+ isomotion: 1; fps?: number; seed?: number; ambient?: 'on' | 'off';
70
+ series?: SeriesItem[]; layers?: MotionNode[];
71
+ /** named moments: ms, or "beat+1200" */
72
+ markers?: Record<string, MotionTime>;
73
+ }
74
+
75
+ export interface MotionDiag { severity: 'error' | 'warning'; path: string; message: string; hint?: string }
76
+
77
+ // ---------- times ----------
78
+
79
+ /** a time in ms, or undefined when it isn't one */
80
+ export function msOf(t: unknown, fps?: number): number | undefined {
81
+ if (typeof t === 'number') return Number.isFinite(t) ? t : undefined;
82
+ if (typeof t !== 'string') return undefined;
83
+ const m = /^(-?\d+(?:\.\d+)?)(ms|s|f)?$/.exec(t.trim());
84
+ if (!m) return undefined;
85
+ const v = Number(m[1]);
86
+ return m[2] === 's' ? v * 1000 : m[2] === 'f' ? (fps ? (v / fps) * 1000 : undefined) : v;
87
+ }
88
+ /** how long a beat's words take to read */
89
+ export function readMs(say: Say | undefined, r: ReadTime['read']): number {
90
+ const o = r === true ? {} : r, base = o.base ?? 2600, per = o.perChar ?? 40, min = o.min ?? 5600, max = o.max ?? 12500;
91
+ return Math.max(min, Math.min(max, base + (say?.text ?? '').length * per));
92
+ }
93
+
94
+ // ---------- easings and curves ----------
95
+
96
+ export const bezier = (x1: number, y1: number, x2: number, y2: number) => (x: number): number => {
97
+ if (x <= 0) return 0;
98
+ if (x >= 1) return 1;
99
+ const at = (a: number, b: number, t: number) => 3 * a * t * (1 - t) ** 2 + 3 * b * t * t * (1 - t) + t ** 3;
100
+ let lo = 0, hi = 1;
101
+ for (let i = 0; i < 50; i++) { const m = (lo + hi) / 2; if (at(x1, x2, m) < x) lo = m; else hi = m; }
102
+ return at(y1, y2, (lo + hi) / 2);
103
+ };
104
+ const bounce = (f: number) => {
105
+ if (f <= 0) return 0;
106
+ if (f >= 1) return 1;
107
+ const n = 7.5625, d = 2.75;
108
+ if (f < 1 / d) return n * f * f;
109
+ if (f < 2 / d) return n * (f - 1.5 / d) ** 2 + 0.75;
110
+ if (f < 2.5 / d) return n * (f - 2.25 / d) ** 2 + 0.9375;
111
+ return n * (f - 2.625 / d) ** 2 + 0.984375;
112
+ };
113
+ const NAMED: Record<EasingName, (f: number) => number> = {
114
+ linear: f => f, ease: bezier(0.25, 0.1, 0.25, 1), 'ease-in': bezier(0.42, 0, 1, 1), 'ease-out': bezier(0, 0, 0.58, 1), 'ease-in-out': bezier(0.42, 0, 0.58, 1),
115
+ settle: bezier(0.16, 0.84, 0.3, 1), bounce,
116
+ };
117
+ /** A damped spring from 0 to 1, t ms after it is let go: the closed form, no steps. */
118
+ export function springAt(t: number, c: SpringConfig = {}): number {
119
+ const m = c.mass ?? 1, k = c.stiffness ?? 100, d = c.damping ?? 10, s = Math.max(0, t) / 1000;
120
+ const w0 = Math.sqrt(k / m), z = d / (2 * Math.sqrt(k * m));
121
+ let x: number;
122
+ if (z < 1) { const w = w0 * Math.sqrt(1 - z * z); x = 1 - Math.exp(-z * w0 * s) * (Math.cos(w * s) + ((z * w0) / w) * Math.sin(w * s)); }
123
+ else x = 1 - Math.exp(-w0 * s) * (1 + w0 * s); // critically damped, and (as the documentation's) the most it is
124
+ return c.overshootClamping ? Math.min(1, x) : x;
125
+ }
126
+ /** An easing as a function of the share of its time done; a spring is given the time's length too. */
127
+ export function easeOf(e: MotionEase | undefined, duration: number, otherwise: EasingName = 'ease-in-out'): (f: number) => number {
128
+ const v = e ?? otherwise;
129
+ if (typeof v === 'string') return NAMED[v];
130
+ if ('bezier' in v) return bezier(...v.bezier);
131
+ return f => (f <= 0 ? 0 : f >= 1 ? 1 : springAt(f * duration, v.spring));
132
+ }
133
+ /** Remotion's interpolate, for a checked curve: piecewise, the easing in each piece, the ends as asked (extend by default). */
134
+ export function curveAt(c: Curve, t: number, fps?: number): number {
135
+ const ts = c.times.map(x => msOf(x, fps)!), vs = c.values, n = ts.length;
136
+ if (n === 1) return vs[0];
137
+ const edge = (how: Extrapolate, low: boolean): number | undefined => {
138
+ if (how === 'clamp') return low ? vs[0] : vs[n - 1];
139
+ if (how === 'identity') return t;
140
+ if (how === 'wrap') { const span = ts[n - 1] - ts[0]; return curveAt({ ...c, extrapolateLeft: 'clamp', extrapolateRight: 'clamp' }, ts[0] + ((((t - ts[0]) % span) + span) % span), fps); }
141
+ return undefined;
142
+ };
143
+ if (t < ts[0]) { const v = edge(c.extrapolateLeft ?? 'extend', true); if (v !== undefined) return v; }
144
+ if (t > ts[n - 1]) { const v = edge(c.extrapolateRight ?? 'extend', false); if (v !== undefined) return v; }
145
+ let i = 0;
146
+ while (i < n - 2 && t >= ts[i + 1]) i++;
147
+ const span = ts[i + 1] - ts[i], f = (t - ts[i]) / span;
148
+ // past an end it goes on as a line, whatever the easing
149
+ const g = f < 0 || f > 1 ? f : easeOf(c.easing, span, 'linear')(f);
150
+ return vs[i] + (vs[i + 1] - vs[i]) * g;
151
+ }
152
+
153
+ // ---------- the JSON Schema: for editors (completion, hover) and for tools; checkMotion says the same, with places ----------
154
+
155
+ const TIME = { anyOf: [{ type: 'number' }, { type: 'string', pattern: '^-?\\d+(\\.\\d+)?(ms|s|f)?$' }] } as const;
156
+ const IDS = { type: 'array', items: { type: 'string' } } as const;
157
+ const EASE = { anyOf: [{ enum: EASING_NAMES }, { type: 'object', required: ['bezier'], additionalProperties: false, properties: { bezier: { type: 'array', items: { type: 'number' }, minItems: 4, maxItems: 4 } } }, { type: 'object', required: ['spring'], additionalProperties: false, properties: { spring: { $ref: '#/$defs/spring' } } }] } as const;
158
+ const timing = { from: { $ref: '#/$defs/time' }, duration: { $ref: '#/$defs/time' }, easing: { $ref: '#/$defs/ease' } };
159
+ const node = (key: string, value: object, more: object = {}, withTiming = true) => ({ type: 'object', required: [key], additionalProperties: false, properties: { [key]: value, ...(withTiming ? timing : {}), ...more } });
160
+ export const MOTION_SCHEMA = {
161
+ $schema: 'https://json-schema.org/draft/2020-12/schema', title: 'isomotion/1',
162
+ type: 'object', required: ['isomotion'], additionalProperties: false,
163
+ properties: {
164
+ isomotion: { const: 1 }, fps: { type: 'number', exclusiveMinimum: 0, description: 'Frames a second, for lengths written "30f".' }, seed: { type: 'integer' },
165
+ ambient: { enum: ['on', 'off'], description: 'Off: the ants and the looping tokens and pulses stand still, each at its start.' },
166
+ series: { $ref: '#/$defs/series' }, layers: { $ref: '#/$defs/nodes' },
167
+ markers: { type: 'object', additionalProperties: { anyOf: [{ type: 'number' }, { type: 'string' }] } },
168
+ },
169
+ $defs: {
170
+ time: TIME, ids: IDS, ease: EASE,
171
+ spring: { type: 'object', additionalProperties: false, properties: { mass: { type: 'number', exclusiveMinimum: 0 }, damping: { type: 'number', exclusiveMinimum: 0 }, stiffness: { type: 'number', exclusiveMinimum: 0 }, overshootClamping: { type: 'boolean' } } },
172
+ curve: { type: 'object', required: ['times', 'values'], additionalProperties: false, properties: { times: { type: 'array', items: { $ref: '#/$defs/time' }, minItems: 1 }, values: { type: 'array', items: { type: 'number' }, minItems: 1 }, easing: { $ref: '#/$defs/ease' }, extrapolateLeft: { enum: EXTRAPOLATE }, extrapolateRight: { enum: EXTRAPOLATE } } },
173
+ say: { type: 'object', additionalProperties: false, properties: { chapter: { type: 'string' }, title: { type: 'string' }, text: { type: 'string' }, code: { type: 'string' }, color: { type: 'string' }, facts: { type: 'array', items: { type: 'array', items: { type: 'string' }, minItems: 2, maxItems: 2 } } } },
174
+ focus: { type: 'object', required: ['lit'], additionalProperties: false, properties: { lit: { $ref: '#/$defs/ids' }, scope: { enum: ['all', 'links'] }, level: { type: 'number', minimum: 0, maximum: 1 }, always: { $ref: '#/$defs/ids' } } },
175
+ sequence: {
176
+ type: 'object', additionalProperties: false, properties: {
177
+ name: { type: 'string', pattern: '^[A-Za-z][\\w-]*$' }, from: { $ref: '#/$defs/time' }, offset: { $ref: '#/$defs/time' }, trimBefore: { $ref: '#/$defs/time' },
178
+ duration: { anyOf: [{ $ref: '#/$defs/time' }, { type: 'object', required: ['read'], additionalProperties: false, properties: { read: { anyOf: [{ const: true }, { type: 'object', additionalProperties: false, properties: { base: { type: 'number' }, perChar: { type: 'number' }, min: { type: 'number' }, max: { type: 'number' } } }] } } }] },
179
+ loop: { anyOf: [{ type: 'boolean' }, { type: 'integer', minimum: 1 }] }, playbackRate: { type: 'number', exclusiveMinimum: 0 },
180
+ say: { $ref: '#/$defs/say' }, show: { $ref: '#/$defs/ids' }, hide: { $ref: '#/$defs/ids' }, focus: { $ref: '#/$defs/focus' }, highlight: { $ref: '#/$defs/ids' }, levels: { type: 'object', additionalProperties: { type: 'number', minimum: 0, maximum: 1 } }, variants: { type: 'object', additionalProperties: { type: 'string' } },
181
+ enter: { type: 'object', required: ['fade'], additionalProperties: false, properties: { fade: { $ref: '#/$defs/time' }, easing: { $ref: '#/$defs/ease' } } },
182
+ ants: { anyOf: [{ type: 'boolean' }, { $ref: '#/$defs/ids' }] }, children: { $ref: '#/$defs/nodes' }, layers: { $ref: '#/$defs/nodes' }, series: { $ref: '#/$defs/series' },
183
+ },
184
+ },
185
+ series: { type: 'array', items: { anyOf: [{ $ref: '#/$defs/seq' }, node('transition', { type: 'object', required: ['presentation', 'timing'], additionalProperties: false, properties: { presentation: { enum: ['fade', 'none'] }, timing: { anyOf: [{ type: 'object', required: ['linear'], additionalProperties: false, properties: { linear: { $ref: '#/$defs/time' } } }, { type: 'object', required: ['spring'], additionalProperties: false, properties: { spring: { type: 'object', additionalProperties: false, properties: { mass: { type: 'number', exclusiveMinimum: 0 }, damping: { type: 'number', exclusiveMinimum: 0 }, stiffness: { type: 'number', exclusiveMinimum: 0 }, overshootClamping: { type: 'boolean' }, duration: { $ref: '#/$defs/time' } } } } }] } } }, {}, false)] } },
186
+ seq: node('sequence', { $ref: '#/$defs/sequence' }, {}, false),
187
+ plan: { type: 'object' },
188
+ nodes: {
189
+ type: 'array', items: { anyOf: [
190
+ { $ref: '#/$defs/seq' }, node('series', { $ref: '#/$defs/series' }, {}, false), node('layers', { $ref: '#/$defs/nodes' }, {}, false),
191
+ node('assemble', { type: 'object', properties: { style: { enum: ASSEMBLE_STYLES }, order: { enum: MOTION_ORDERS } } }), node('disassemble', { type: 'object', properties: { style: { enum: DISASSEMBLE_STYLES }, order: { enum: MOTION_ORDERS } } }),
192
+ node('travel', { type: 'array', items: { anyOf: [{ type: 'string' }, { $ref: '#/$defs/ids' }] }, minItems: 1 }, { each: { $ref: '#/$defs/time' }, hold: { $ref: '#/$defs/time' }, loop: { type: 'boolean' } }),
193
+ node('pulse', { $ref: '#/$defs/ids' }, { loop: { type: 'boolean' } }),
194
+ node('ants', { anyOf: [{ const: true }, { $ref: '#/$defs/ids' }] }, { speed: { type: 'number' }, dash: { type: 'array', items: { type: 'number', exclusiveMinimum: 0 }, minItems: 2, maxItems: 2 } }),
195
+ node('draw', { $ref: '#/$defs/ids' }, { by: { enum: ['stroke', 'grow'] }, stagger: { $ref: '#/$defs/time' } }), node('type', { $ref: '#/$defs/ids' }),
196
+ { type: 'object', required: ['move'], additionalProperties: false, properties: { move: { type: 'string' }, from: { $ref: '#/$defs/time' }, x: { $ref: '#/$defs/curve' }, y: { $ref: '#/$defs/curve' }, scale: { $ref: '#/$defs/curve' }, opacity: { $ref: '#/$defs/curve' } } },
197
+ { type: 'object', required: ['lift', 'by'], additionalProperties: false, properties: { lift: { $ref: '#/$defs/ids' }, by: { type: 'number' }, stagger: { $ref: '#/$defs/time' }, to: { enum: ['up', 'down'] }, opacity: { $ref: '#/$defs/curve' }, ...timing } },
198
+ node('focus', { $ref: '#/$defs/focus' }, { fade: { $ref: '#/$defs/time' } }), node('highlight', { $ref: '#/$defs/ids' }, { fade: { $ref: '#/$defs/time' } }),
199
+ node('camera', { anyOf: [{ const: 'fit' }, { type: 'object', required: ['focus'], additionalProperties: false, properties: { focus: { anyOf: [{ type: 'string' }, { $ref: '#/$defs/ids' }, { type: 'array', items: { type: 'number' }, minItems: 4, maxItems: 4 }] }, zoom: { type: 'number', exclusiveMinimum: 0 }, margin: { type: 'number' } } }] }),
200
+ ] },
201
+ },
202
+ },
203
+ } as const;
204
+
205
+ // ---------- the checks ----------
206
+
207
+ const KINDS = ['sequence', 'series', 'layers', 'assemble', 'disassemble', 'travel', 'pulse', 'ants', 'draw', 'type', 'move', 'lift', 'focus', 'highlight', 'camera'] as const;
208
+ const LATER: Record<string, string> = { morph: 'the 2D morph is a later version\'s', diff: 'diffs are a later version\'s', switch: 'a look is a beat\'s: its variants' };
209
+ const TIMING = ['from', 'duration', 'easing'];
210
+ const KEYS: Record<string, string[]> = {
211
+ sequence: [], series: [], layers: [], assemble: TIMING, disassemble: TIMING, travel: [...TIMING, 'each', 'hold', 'loop'], pulse: [...TIMING, 'loop'], ants: [...TIMING, 'speed', 'dash'],
212
+ draw: [...TIMING, 'by', 'stagger'], type: TIMING, move: ['from', 'x', 'y', 'scale', 'opacity'], lift: [...TIMING, 'by', 'stagger', 'to', 'opacity'], focus: [...TIMING, 'fade'], highlight: [...TIMING, 'fade'], camera: TIMING,
213
+ };
214
+ const SEQ_KEYS = ['name', 'from', 'offset', 'duration', 'loop', 'playbackRate', 'trimBefore', 'say', 'show', 'hide', 'focus', 'highlight', 'levels', 'variants', 'enter', 'ants', 'children', 'layers', 'series'];
215
+ const isMap = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
216
+
217
+ /** What is wrong with a document's shape, each with its place (a JSON pointer). Its ids are bindMotion's to check. */
218
+ export function checkMotion(doc: unknown): MotionDiag[] {
219
+ const out: MotionDiag[] = [];
220
+ const err = (path: string, message: string, hint?: string) => { out.push({ severity: 'error', path, message, ...(hint ? { hint } : {}) }); };
221
+ const keys = (v: Record<string, unknown>, allowed: readonly string[], path: string) => { for (const k of Object.keys(v)) if (!allowed.includes(k)) { const d = didYouMean(k, allowed); err(`${path}/${k}`, `unknown key "${k}"`, d ? `did you mean "${d}"?` : undefined); } };
222
+ if (!isMap(doc)) { err('', 'a motion document is a map'); return out; }
223
+ keys(doc, ['isomotion', 'fps', 'seed', 'ambient', 'series', 'layers', 'markers'], '');
224
+ if (doc.isomotion !== 1) err('/isomotion', 'isomotion is 1');
225
+ const fps = typeof doc.fps === 'number' && doc.fps > 0 ? doc.fps : undefined;
226
+ if (doc.fps !== undefined && fps === undefined) err('/fps', 'fps is a number over 0');
227
+ if (doc.seed !== undefined && !Number.isInteger(doc.seed)) err('/seed', 'seed is a whole number');
228
+ if (doc.ambient !== undefined && doc.ambient !== 'on' && doc.ambient !== 'off') err('/ambient', 'ambient is "on" or "off"');
229
+ const time = (v: unknown, path: string, o: { negative?: boolean } = {}) => {
230
+ if (v === undefined) return;
231
+ if (typeof v === 'string' && /f$/.test(v) && fps === undefined) { err(path, `"${v}" is in frames: the document needs its fps`); return; }
232
+ const t = msOf(v, fps);
233
+ if (t === undefined) err(path, `${JSON.stringify(v)} isn't a time (ms, "1.2s", "450ms", "30f")`);
234
+ else if (t < 0 && !o.negative) err(path, 'a time can\'t be negative');
235
+ };
236
+ const oneOf = (v: unknown, list: readonly unknown[], path: string) => { if (v !== undefined && !list.includes(v)) { const d = typeof v === 'string' ? didYouMean(v, list as string[]) : undefined; err(path, `${JSON.stringify(v)} isn't one of ${list.join(', ')}`, d ? `did you mean "${d}"?` : undefined); } };
237
+ const num = (v: unknown, path: string, o: { min?: number; max?: number; over?: number } = {}) => { if (v === undefined) return; if (typeof v !== 'number' || !Number.isFinite(v) || (o.min !== undefined && v < o.min) || (o.max !== undefined && v > o.max) || (o.over !== undefined && v <= o.over)) err(path, `${JSON.stringify(v)} isn't a number${o.over !== undefined ? ` over ${o.over}` : o.min !== undefined ? ` from ${o.min}${o.max !== undefined ? ` to ${o.max}` : ''}` : ''}`); };
238
+ const ids = (v: unknown, path: string) => { if (v === undefined) return; if (!Array.isArray(v)) { err(path, 'a list of ids'); return; } v.forEach((x, i) => { if (typeof x !== 'string') err(`${path}/${i}`, 'an id'); }); };
239
+ const spring = (v: unknown, path: string, more: string[] = []) => { if (!isMap(v)) { err(path, 'a spring is a map'); return; } keys(v, ['mass', 'damping', 'stiffness', 'overshootClamping', ...more], path); for (const k of ['mass', 'damping', 'stiffness']) num(v[k], `${path}/${k}`, { over: 0 }); if (v.overshootClamping !== undefined && typeof v.overshootClamping !== 'boolean') err(`${path}/overshootClamping`, 'true or false'); };
240
+ const ease = (v: unknown, path: string) => {
241
+ if (v === undefined) return;
242
+ if (typeof v === 'string') { oneOf(v, EASING_NAMES, path); return; }
243
+ if (isMap(v) && 'bezier' in v) { keys(v, ['bezier'], path); if (!Array.isArray(v.bezier) || v.bezier.length !== 4 || v.bezier.some(x => typeof x !== 'number')) err(`${path}/bezier`, 'four numbers'); return; }
244
+ if (isMap(v) && 'spring' in v) { keys(v, ['spring'], path); spring(v.spring, `${path}/spring`); return; }
245
+ err(path, 'an easing is a name, { bezier: [a, b, c, d] } or { spring: { … } }');
246
+ };
247
+ const curve = (v: unknown, path: string) => {
248
+ if (v === undefined) return;
249
+ if (!isMap(v)) { err(path, 'a curve is { times, values }'); return; }
250
+ keys(v, ['times', 'values', 'easing', 'extrapolateLeft', 'extrapolateRight'], path);
251
+ const ts = v.times, vs = v.values;
252
+ if (!Array.isArray(ts) || !ts.length) err(`${path}/times`, 'a list of times'); else {
253
+ ts.forEach((t, i) => time(t, `${path}/times/${i}`, { negative: true }));
254
+ const n = ts.map(t => msOf(t, fps)); for (let i = 1; i < n.length; i++) if (n[i] !== undefined && n[i - 1] !== undefined && n[i]! <= n[i - 1]!) err(`${path}/times/${i}`, 'times go up');
255
+ }
256
+ if (!Array.isArray(vs) || vs.some(x => typeof x !== 'number')) err(`${path}/values`, 'a list of numbers'); else if (Array.isArray(ts) && vs.length !== ts.length) err(`${path}/values`, `${vs.length} values for ${ts.length} times`);
257
+ ease(v.easing, `${path}/easing`); oneOf(v.extrapolateLeft, EXTRAPOLATE, `${path}/extrapolateLeft`); oneOf(v.extrapolateRight, EXTRAPOLATE, `${path}/extrapolateRight`);
258
+ };
259
+ const focus = (v: unknown, path: string) => { if (v === undefined) return; if (!isMap(v)) { err(path, 'focus is { lit: [ids] }'); return; } keys(v, ['lit', 'scope', 'level', 'always'], path); if (v.lit === undefined) err(`${path}/lit`, 'what is lit'); ids(v.lit, `${path}/lit`); ids(v.always, `${path}/always`); oneOf(v.scope, ['all', 'links'], `${path}/scope`); num(v.level, `${path}/level`, { min: 0, max: 1 }); };
260
+ const text = (v: unknown, path: string) => { if (v !== undefined && typeof v !== 'string') err(path, 'text'); };
261
+ const names = new Map<string, string>();
262
+ function sequence(v: unknown, path: string, inSeries: boolean) {
263
+ if (!isMap(v)) { err(path, 'a sequence is a map'); return; }
264
+ keys(v, SEQ_KEYS, path);
265
+ if (v.name !== undefined) {
266
+ if (typeof v.name !== 'string' || !/^[A-Za-z][\w-]*$/.test(v.name)) err(`${path}/name`, 'a name is letters, digits, - and _, from a letter');
267
+ else if (names.has(v.name)) err(`${path}/name`, `"${v.name}" is already a name (${names.get(v.name)})`); else names.set(v.name, path);
268
+ }
269
+ if (inSeries && v.from !== undefined) err(`${path}/from`, 'in a series a sequence follows the one before', 'offset moves it');
270
+ if (!inSeries && v.offset !== undefined) err(`${path}/offset`, 'offset is for a series', 'use from');
271
+ time(v.from, `${path}/from`); time(v.offset, `${path}/offset`, { negative: true }); time(v.trimBefore, `${path}/trimBefore`);
272
+ if (isMap(v.duration)) {
273
+ keys(v.duration, ['read'], `${path}/duration`);
274
+ const r = v.duration.read;
275
+ if (r !== true && !isMap(r)) err(`${path}/duration/read`, 'true, or { base, perChar, min, max }');
276
+ else if (isMap(r)) { keys(r, ['base', 'perChar', 'min', 'max'], `${path}/duration/read`); for (const k of ['base', 'perChar', 'min', 'max']) num(r[k], `${path}/duration/read/${k}`, { min: 0 }); }
277
+ } else time(v.duration, `${path}/duration`);
278
+ if (v.loop !== undefined && typeof v.loop !== 'boolean' && !(Number.isInteger(v.loop) && (v.loop as number) >= 1)) err(`${path}/loop`, 'true, false, or how many times');
279
+ num(v.playbackRate, `${path}/playbackRate`, { over: 0 });
280
+ if (v.say !== undefined) {
281
+ if (!isMap(v.say)) err(`${path}/say`, 'say is a map'); else {
282
+ const s = v.say; keys(s, ['chapter', 'title', 'text', 'code', 'facts', 'color'], `${path}/say`);
283
+ for (const k of ['chapter', 'title', 'text', 'code', 'color']) text(s[k], `${path}/say/${k}`);
284
+ if (s.facts !== undefined && (!Array.isArray(s.facts) || s.facts.some(f => !Array.isArray(f) || f.length !== 2 || f.some(x => typeof x !== 'string')))) err(`${path}/say/facts`, 'a list of [name, value]');
285
+ }
286
+ }
287
+ for (const k of ['show', 'hide', 'highlight']) ids(v[k], `${path}/${k}`);
288
+ focus(v.focus, `${path}/focus`);
289
+ if (v.levels !== undefined) { if (!isMap(v.levels)) err(`${path}/levels`, 'levels is a map: an id, and how much of it'); else for (const [k, x] of Object.entries(v.levels)) num(x, `${path}/levels/${k}`, { min: 0, max: 1 }); }
290
+ if (v.variants !== undefined) { if (!isMap(v.variants)) err(`${path}/variants`, 'variants is a map: an id, and the name of its look'); else for (const [k, x] of Object.entries(v.variants)) if (typeof x !== 'string') err(`${path}/variants/${k}`, 'a look\'s name, or "" for its own'); }
291
+ if (v.enter !== undefined) { if (!isMap(v.enter)) err(`${path}/enter`, 'enter is { fade }'); else { keys(v.enter, ['fade', 'easing'], `${path}/enter`); if (v.enter.fade === undefined) err(`${path}/enter/fade`, 'how long the fade is'); time(v.enter.fade, `${path}/enter/fade`); ease(v.enter.easing, `${path}/enter/easing`); } }
292
+ if (v.ants !== undefined && typeof v.ants !== 'boolean') ids(v.ants, `${path}/ants`);
293
+ for (const k of ['children', 'layers']) if (v[k] !== undefined) nodes(v[k], `${path}/${k}`);
294
+ if (v.series !== undefined) series(v.series, `${path}/series`);
295
+ }
296
+ function series(v: unknown, path: string) {
297
+ if (!Array.isArray(v)) { err(path, 'a series is a list'); return; }
298
+ v.forEach((it, i) => {
299
+ const p = `${path}/${i}`;
300
+ if (!isMap(it) || Object.keys(it).length !== 1 || !('sequence' in it || 'transition' in it)) { err(p, 'a series holds sequences and transitions'); return; }
301
+ if ('sequence' in it) { sequence(it.sequence, `${p}/sequence`, true); return; }
302
+ const t = it.transition, q = `${p}/transition`, seqAt = (k: number) => isMap(v[k]) && 'sequence' in (v[k] as object);
303
+ if (!seqAt(i - 1) || !seqAt(i + 1)) err(q, 'a transition is between two sequences');
304
+ if (!isMap(t)) { err(q, 'a transition is { presentation, timing }'); return; }
305
+ keys(t, ['presentation', 'timing'], q);
306
+ if (t.presentation === undefined) err(`${q}/presentation`, 'a presentation'); else oneOf(t.presentation, ['fade', 'none'], `${q}/presentation`);
307
+ if (!isMap(t.timing)) err(`${q}/timing`, 'a timing is { linear } or { spring }');
308
+ else if ('linear' in t.timing) { keys(t.timing, ['linear'], `${q}/timing`); time(t.timing.linear, `${q}/timing/linear`); }
309
+ else if ('spring' in t.timing) { keys(t.timing, ['spring'], `${q}/timing`); spring(t.timing.spring, `${q}/timing/spring`, ['duration']); if (isMap(t.timing.spring)) time(t.timing.spring.duration, `${q}/timing/spring/duration`); }
310
+ else err(`${q}/timing`, 'a timing is { linear } or { spring }');
311
+ });
312
+ }
313
+ function nodes(v: unknown, path: string) {
314
+ if (!Array.isArray(v)) { err(path, 'a list'); return; }
315
+ v.forEach((n, i) => {
316
+ const p = `${path}/${i}`;
317
+ if (!isMap(n)) { err(p, 'a map'); return; }
318
+ const kinds = KINDS.filter(k => k in n);
319
+ // (focus and highlight are kinds of their own, and no other kind has a key of that name)
320
+ if (kinds.length !== 1) {
321
+ const late = Object.keys(n).find(k => k in LATER), k0 = Object.keys(n)[0], d = k0 ? didYouMean(k0, KINDS) : undefined;
322
+ err(p, late ? `${late}: ${LATER[late]}` : kinds.length ? `one of ${kinds.join(', ')}, not several` : `one of ${KINDS.join(', ')}`, !late && !kinds.length && d ? `did you mean "${d}"?` : undefined);
323
+ return;
324
+ }
325
+ const k = kinds[0], c = n[k], q = `${p}/${k}`;
326
+ keys(n, [k, ...KEYS[k]], p);
327
+ if (KEYS[k].includes('from')) time(n.from, `${p}/from`);
328
+ if (KEYS[k].includes('duration')) { time(n.duration, `${p}/duration`); ease(n.easing, `${p}/easing`); }
329
+ for (const t of ['each', 'hold', 'stagger', 'fade']) if (KEYS[k].includes(t)) time(n[t], `${p}/${t}`);
330
+ if (KEYS[k].includes('loop') && n.loop !== undefined && typeof n.loop !== 'boolean') err(`${p}/loop`, 'true or false');
331
+ if (k === 'sequence') sequence(c, q, false);
332
+ else if (k === 'series') series(c, q);
333
+ else if (k === 'layers') nodes(c, q);
334
+ else if (k === 'assemble' || k === 'disassemble') { if (!isMap(c)) err(q, `${k} is a map (its options)`); else { oneOf(c.style, k === 'assemble' ? ASSEMBLE_STYLES : DISASSEMBLE_STYLES, `${q}/style`); oneOf(c.order, MOTION_ORDERS, `${q}/order`); if (c.duration !== undefined) err(`${q}/duration`, 'its length is beside it', 'duration is the node\'s'); } }
335
+ else if (k === 'travel') { if (!Array.isArray(c) || !c.length) err(q, 'a list of connectors'); else c.forEach((x, j) => { if (Array.isArray(x)) ids(x, `${q}/${j}`); else if (typeof x !== 'string') err(`${q}/${j}`, 'a connector, or a list of them at once'); }); }
336
+ else if (k === 'ants') { if (c !== true) ids(c, q); num(n.speed, `${p}/speed`); if (n.dash !== undefined && (!Array.isArray(n.dash) || n.dash.length !== 2 || n.dash.some(x => typeof x !== 'number' || x <= 0))) err(`${p}/dash`, 'two numbers over 0: on, off'); }
337
+ else if (k === 'move') { if (typeof c !== 'string') err(q, 'an id'); for (const a of ['x', 'y', 'scale', 'opacity']) curve(n[a], `${p}/${a}`); }
338
+ else if (k === 'focus') focus(c, q);
339
+ else if (k === 'camera') {
340
+ if (c !== 'fit') {
341
+ if (!isMap(c)) err(q, '"fit", or { focus, zoom, margin }'); else {
342
+ keys(c, ['focus', 'zoom', 'margin'], q); num(c.zoom, `${q}/zoom`, { over: 0 }); num(c.margin, `${q}/margin`);
343
+ const f = c.focus; if (!(typeof f === 'string' || (Array.isArray(f) && (f.every(x => typeof x === 'string') || (f.length === 4 && f.every(x => typeof x === 'number')))))) err(`${q}/focus`, 'an id, some ids, or a box [x, y, w, h]');
344
+ }
345
+ }
346
+ } else ids(c, q);
347
+ if (k === 'draw') oneOf(n.by, ['stroke', 'grow'], `${p}/by`);
348
+ if (k === 'lift') { if (n.by === undefined) err(`${p}/by`, 'how far apart'); num(n.by, `${p}/by`); oneOf(n.to, ['up', 'down'], `${p}/to`); curve(n.opacity, `${p}/opacity`); }
349
+ });
350
+ }
351
+ if (doc.series !== undefined) series(doc.series, '/series');
352
+ if (doc.layers !== undefined) nodes(doc.layers, '/layers');
353
+ if (doc.markers !== undefined) {
354
+ if (!isMap(doc.markers)) err('/markers', 'markers is a map');
355
+ else for (const [k, v] of Object.entries(doc.markers)) {
356
+ if (typeof v === 'string' && /^[A-Za-z][\w-]*([+-].+)?$/.test(v)) { const m = /^([A-Za-z][\w-]*?)(?:([+-])(\d.*))?$/.exec(v); const at = m && (names.has(m[1]) ? m[1] : undefined); if (!at) { const whole = names.has(v); if (!whole) { const d = didYouMean(m?.[1] ?? v, names.keys()); err(`/markers/${k}`, `no sequence is named "${m?.[1] ?? v}"`, d ? `did you mean "${d}"?` : undefined); } } else if (m![3] !== undefined) time(m![3], `/markers/${k}`); }
357
+ else time(v, `/markers/${k}`);
358
+ }
359
+ }
360
+ return out;
361
+ }
@@ -0,0 +1,12 @@
1
+ // The package's /motion: plans, the motion document and its Timeline, the builder, looks and ants. All pure, no DOM but paint().
2
+
3
+ export * from './motion.ts';
4
+ export { bindMotion, motionOfSteps } from './timeline.ts';
5
+ export type { Timeline, BindOptions, BeatInfo, Cue } from './timeline.ts';
6
+ export { motion, MotionBuilder, SeriesBuilder, NodesBuilder } from './motion-builder.ts';
7
+ export type { SequenceOptions, Timing, CameraVerbs, Ids as MotionIds } from './motion-builder.ts';
8
+ export { MOTION_SCHEMA, EASING_NAMES, EXTRAPOLATE, checkMotion, msOf, readMs, easeOf, curveAt, springAt, bezier } from './motion-doc.ts';
9
+ export type { MotionDoc, MotionNode, MotionTime, MotionEase, EasingName, Extrapolate, SpringConfig, Curve, Say as MotionSay, Focus, ReadTime, Sequence, SeriesItem, Transition, MotionDiag } from './motion-doc.ts';
10
+ export { withAnts, antsOf, antDash, ANTS_CSS, ANT_SPEED, ANT_DASH, ANT_STILL } from './ants.ts';
11
+ export type { AntsOptions } from './ants.ts';
12
+ export { drawLooks } from './looks.ts';