@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.
Files changed (81) hide show
  1. package/README.md +1 -1
  2. package/iso-editor.js +249 -203
  3. package/iso-render.js +200 -154
  4. package/iso-systems.js +210 -162
  5. package/isometric-diagram.js +389 -0
  6. package/js/iso-core/index.js +5 -0
  7. package/js/iso-core/mono.js +242 -0
  8. package/js/iso-editor/ui/canvas.js +23 -1
  9. package/js/iso-objects/ants.js +36 -0
  10. package/js/iso-objects/index.js +55 -1
  11. package/js/iso-objects/looks.js +25 -0
  12. package/js/iso-objects/mono-paint.js +96 -0
  13. package/js/iso-objects/motion-builder.js +130 -0
  14. package/js/iso-objects/motion-doc.js +421 -0
  15. package/js/iso-objects/motion-entry.js +34 -0
  16. package/js/iso-objects/motion-schema.js +83 -0
  17. package/js/iso-objects/motion.js +640 -0
  18. package/js/iso-objects/resolve.js +238 -0
  19. package/js/iso-objects/scene.js +15 -13
  20. package/js/iso-objects/timeline.js +593 -0
  21. package/js/iso-systems/compile.js +83 -1
  22. package/js/iso-systems/index.js +4 -1
  23. package/js/iso-systems/render.js +20 -0
  24. package/js/iso-systems/schema.json +40 -0
  25. package/js/iso-viewer/index.js +6 -0
  26. package/js/iso-viewer/page.js +40 -16
  27. package/js/iso-viewer/play.js +340 -0
  28. package/js/iso-viewer/player.js +149 -175
  29. package/js/iso-viewer/transport.js +289 -0
  30. package/package.json +84 -2
  31. package/src/iso-core/index.ts +1 -0
  32. package/src/iso-core/mono.ts +306 -0
  33. package/src/iso-core/theme.ts +2 -1
  34. package/src/iso-editor/ui/canvas.ts +14 -1
  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 +16 -1
  38. package/src/iso-objects/looks.ts +21 -0
  39. package/src/iso-objects/mono-paint.ts +97 -0
  40. package/src/iso-objects/motion-builder.ts +79 -0
  41. package/src/iso-objects/motion-doc.ts +309 -0
  42. package/src/iso-objects/motion-entry.ts +13 -0
  43. package/src/iso-objects/motion-schema.ts +55 -0
  44. package/src/iso-objects/motion.ts +617 -0
  45. package/src/iso-objects/resolve.ts +196 -0
  46. package/src/iso-objects/scene.ts +32 -13
  47. package/src/iso-objects/timeline.ts +493 -0
  48. package/src/iso-objects/types.ts +22 -2
  49. package/src/iso-systems/compile.ts +86 -4
  50. package/src/iso-systems/index.ts +4 -1
  51. package/src/iso-systems/render.ts +45 -0
  52. package/src/iso-systems/schema.json +40 -0
  53. package/src/iso-viewer/index.ts +4 -0
  54. package/src/iso-viewer/page.ts +28 -11
  55. package/src/iso-viewer/play.ts +280 -0
  56. package/src/iso-viewer/player.ts +132 -150
  57. package/src/iso-viewer/transport.ts +272 -0
  58. package/types/iso-core/index.d.ts +1 -0
  59. package/types/iso-core/mono.d.ts +61 -0
  60. package/types/iso-core/theme.d.ts +2 -0
  61. package/types/iso-objects/ants.d.ts +34 -0
  62. package/types/iso-objects/compile.d.ts +3 -0
  63. package/types/iso-objects/index.d.ts +16 -1
  64. package/types/iso-objects/looks.d.ts +4 -0
  65. package/types/iso-objects/mono-paint.d.ts +20 -0
  66. package/types/iso-objects/motion-builder.d.ts +105 -0
  67. package/types/iso-objects/motion-doc.d.ts +193 -0
  68. package/types/iso-objects/motion-entry.d.ts +11 -0
  69. package/types/iso-objects/motion-schema.d.ts +594 -0
  70. package/types/iso-objects/motion.d.ts +202 -0
  71. package/types/iso-objects/resolve.d.ts +20 -0
  72. package/types/iso-objects/scene.d.ts +14 -0
  73. package/types/iso-objects/timeline.d.ts +66 -0
  74. package/types/iso-objects/types.d.ts +34 -0
  75. package/types/iso-systems/compile.d.ts +25 -3
  76. package/types/iso-systems/index.d.ts +4 -1
  77. package/types/iso-systems/render.d.ts +25 -0
  78. package/types/iso-viewer/index.d.ts +4 -0
  79. package/types/iso-viewer/play.d.ts +37 -0
  80. package/types/iso-viewer/transport.d.ts +73 -0
  81. package/types/player/isometric-diagram.d.ts +24 -0
@@ -0,0 +1,130 @@
1
+ const list = (v) => typeof v === "string" ? [v] : [...v];
2
+ const plain = (v) => JSON.parse(JSON.stringify(v));
3
+ const timing = (o = {}) => ({ ...o.from === void 0 ? {} : { from: o.from }, ...o.over === void 0 ? {} : { duration: o.over }, ...o.easing === void 0 ? {} : { easing: o.easing } });
4
+ class NodesBuilder {
5
+ /** @internal */
6
+ nodes = [];
7
+ add(n) {
8
+ this.nodes.push(plain(n));
9
+ return this;
10
+ }
11
+ assemble(o = {}, t = {}) {
12
+ return this.add({ assemble: o, ...timing(t) });
13
+ }
14
+ disassemble(o = {}, t = {}) {
15
+ return this.add({ disassemble: o, ...timing(t) });
16
+ }
17
+ /** tokens along connectors, one after another; a nested list at once; "id:kind" */
18
+ travel(routes, o = {}) {
19
+ const { each, hold, loop } = o;
20
+ return this.add({ travel: routes, ...timing(o), each, hold, loop });
21
+ }
22
+ pulse(ids, o = {}) {
23
+ return this.add({ pulse: list(ids), ...timing(o), loop: o.loop });
24
+ }
25
+ ants(ids = true, o = {}) {
26
+ return this.add({ ants: ids === true ? true : list(ids), ...timing(o), speed: o.speed, dash: o.dash });
27
+ }
28
+ draw(routes, o = {}) {
29
+ return this.add({ draw: list(routes), ...timing(o), by: o.by, stagger: o.stagger });
30
+ }
31
+ type(ids, o = {}) {
32
+ return this.add({ type: list(ids), ...timing(o) });
33
+ }
34
+ move(id, o) {
35
+ return this.add({ move: id, ...o });
36
+ }
37
+ lift(ids, by, o = {}) {
38
+ return this.add({ lift: list(ids), by, ...timing(o), stagger: o.stagger, to: o.to, opacity: o.opacity });
39
+ }
40
+ focus(lit, o = {}) {
41
+ const { scope, level, always } = o;
42
+ return this.add({ focus: { lit: list(lit), scope, level, always }, ...timing(o), fade: o.fade });
43
+ }
44
+ highlight(ids, o = {}) {
45
+ return this.add({ highlight: list(ids), ...timing(o), fade: o.fade });
46
+ }
47
+ /** the view: on a thing, some things or a box; or the whole drawing again */
48
+ get camera() {
49
+ return {
50
+ focus: (on, o = {}) => this.add({ camera: { focus: on, zoom: o.zoom, margin: o.margin }, ...timing(o) }),
51
+ fit: (o = {}) => this.add({ camera: "fit", ...timing(o) })
52
+ };
53
+ }
54
+ /** a sequence in this one: a clock of its own, from `from` */
55
+ sequence(name, o = {}, fill) {
56
+ return this.add({ sequence: sequenceOf(name, o, fill) });
57
+ }
58
+ /** sequences one after another */
59
+ series(fill) {
60
+ const s = new SeriesBuilder();
61
+ fill(s);
62
+ return this.add({ series: s.items });
63
+ }
64
+ /** nodes over one another */
65
+ layers(fill) {
66
+ const b = new NodesBuilder();
67
+ fill(b);
68
+ return this.add({ layers: b.nodes });
69
+ }
70
+ }
71
+ function sequenceOf(name, o, fill) {
72
+ const { read, duration, ...rest } = o, b = new NodesBuilder();
73
+ fill?.(b);
74
+ return plain({ ...name === void 0 ? {} : { name }, ...rest, ...read !== void 0 ? { duration: { read } } : duration !== void 0 ? { duration } : {}, ...b.nodes.length ? { children: b.nodes } : {} });
75
+ }
76
+ class SeriesBuilder {
77
+ /** @internal */
78
+ items = [];
79
+ sequence(name, o = {}, fill) {
80
+ this.items.push({ sequence: sequenceOf(name, o, fill) });
81
+ return this;
82
+ }
83
+ transition(t) {
84
+ this.items.push({ transition: plain(t) });
85
+ return this;
86
+ }
87
+ }
88
+ class MotionBuilder extends SeriesBuilder {
89
+ over = [];
90
+ marks = {};
91
+ head;
92
+ constructor(head = {}) {
93
+ super();
94
+ this.head = head;
95
+ }
96
+ /** a beat: a sequence with commentary */
97
+ beat(name, say, o = {}, fill) {
98
+ return this.sequence(name, { ...o, say }, fill);
99
+ }
100
+ /** the whole drawing arrives (a sequence as long as that takes) */
101
+ assemble(o = {}, t = {}) {
102
+ return this.sequence(void 0, {}, (b) => b.assemble(o, t));
103
+ }
104
+ /** the whole drawing leaves */
105
+ disassemble(o = {}, t = {}) {
106
+ return this.sequence(void 0, {}, (b) => b.disassemble(o, t));
107
+ }
108
+ /** a name for a moment: ms, or "beat+1200" */
109
+ mark(name, at) {
110
+ this.marks[name] = at;
111
+ return this;
112
+ }
113
+ /** nodes over the whole document, on its own clock */
114
+ layers(fill) {
115
+ const b = new NodesBuilder();
116
+ fill(b);
117
+ this.over.push(...b.nodes);
118
+ return this;
119
+ }
120
+ toJSON() {
121
+ return plain({ isomotion: 1, ...this.head, ...this.items.length ? { series: this.items } : {}, ...this.over.length ? { layers: this.over } : {}, ...Object.keys(this.marks).length ? { markers: this.marks } : {} });
122
+ }
123
+ }
124
+ const motion = (head = {}) => new MotionBuilder(head);
125
+ export {
126
+ MotionBuilder,
127
+ NodesBuilder,
128
+ SeriesBuilder,
129
+ motion
130
+ };
@@ -0,0 +1,421 @@
1
+ import { didYouMean } from "../iso-core/errors.js";
2
+ import { ASSEMBLE_STYLES, DISASSEMBLE_STYLES, MOTION_ORDERS } from "./motion.js";
3
+ const EASING_NAMES = ["linear", "ease", "ease-in", "ease-out", "ease-in-out", "settle", "bounce"];
4
+ const EXTRAPOLATE = ["extend", "clamp", "wrap", "identity"];
5
+ function msOf(t, fps) {
6
+ if (typeof t === "number") return Number.isFinite(t) ? t : void 0;
7
+ if (typeof t !== "string") return void 0;
8
+ const m = /^(-?\d+(?:\.\d+)?)(ms|s|f)?$/.exec(t.trim());
9
+ if (!m) return void 0;
10
+ const v = Number(m[1]);
11
+ return m[2] === "s" ? v * 1e3 : m[2] === "f" ? fps ? v / fps * 1e3 : void 0 : v;
12
+ }
13
+ function readMs(say, r) {
14
+ const o = r === true ? {} : r, base = o.base ?? 2600, per = o.perChar ?? 40, min = o.min ?? 5600, max = o.max ?? 12500;
15
+ return Math.max(min, Math.min(max, base + (say?.text ?? "").length * per));
16
+ }
17
+ const bezier = (x1, y1, x2, y2) => (x) => {
18
+ if (x <= 0) return 0;
19
+ if (x >= 1) return 1;
20
+ const at = (a, b, t) => 3 * a * t * (1 - t) ** 2 + 3 * b * t * t * (1 - t) + t ** 3;
21
+ let lo = 0, hi = 1;
22
+ for (let i = 0; i < 50; i++) {
23
+ const m = (lo + hi) / 2;
24
+ if (at(x1, x2, m) < x) lo = m;
25
+ else hi = m;
26
+ }
27
+ return at(y1, y2, (lo + hi) / 2);
28
+ };
29
+ const bounce = (f) => {
30
+ if (f <= 0) return 0;
31
+ if (f >= 1) return 1;
32
+ const n = 7.5625, d = 2.75;
33
+ if (f < 1 / d) return n * f * f;
34
+ if (f < 2 / d) return n * (f - 1.5 / d) ** 2 + 0.75;
35
+ if (f < 2.5 / d) return n * (f - 2.25 / d) ** 2 + 0.9375;
36
+ return n * (f - 2.625 / d) ** 2 + 0.984375;
37
+ };
38
+ const NAMED = {
39
+ linear: (f) => f,
40
+ ease: bezier(0.25, 0.1, 0.25, 1),
41
+ "ease-in": bezier(0.42, 0, 1, 1),
42
+ "ease-out": bezier(0, 0, 0.58, 1),
43
+ "ease-in-out": bezier(0.42, 0, 0.58, 1),
44
+ settle: bezier(0.16, 0.84, 0.3, 1),
45
+ bounce
46
+ };
47
+ function springAt(t, c = {}) {
48
+ const m = c.mass ?? 1, k = c.stiffness ?? 100, d = c.damping ?? 10, s = Math.max(0, t) / 1e3;
49
+ const w0 = Math.sqrt(k / m), z = d / (2 * Math.sqrt(k * m));
50
+ let x;
51
+ if (z < 1) {
52
+ const w = w0 * Math.sqrt(1 - z * z);
53
+ x = 1 - Math.exp(-z * w0 * s) * (Math.cos(w * s) + z * w0 / w * Math.sin(w * s));
54
+ } else x = 1 - Math.exp(-w0 * s) * (1 + w0 * s);
55
+ return c.overshootClamping ? Math.min(1, x) : x;
56
+ }
57
+ function easeOf(e, duration, otherwise = "ease-in-out") {
58
+ const v = e ?? otherwise;
59
+ if (typeof v === "string") return NAMED[v];
60
+ if ("bezier" in v) return bezier(...v.bezier);
61
+ return (f) => f <= 0 ? 0 : f >= 1 ? 1 : springAt(f * duration, v.spring);
62
+ }
63
+ function curveAt(c, t, fps) {
64
+ const ts = c.times.map((x) => msOf(x, fps)), vs = c.values, n = ts.length;
65
+ if (n === 1) return vs[0];
66
+ const edge = (how, low) => {
67
+ if (how === "clamp") return low ? vs[0] : vs[n - 1];
68
+ if (how === "identity") return t;
69
+ if (how === "wrap") {
70
+ const span2 = ts[n - 1] - ts[0];
71
+ return curveAt({ ...c, extrapolateLeft: "clamp", extrapolateRight: "clamp" }, ts[0] + ((t - ts[0]) % span2 + span2) % span2, fps);
72
+ }
73
+ return void 0;
74
+ };
75
+ if (t < ts[0]) {
76
+ const v = edge(c.extrapolateLeft ?? "extend", true);
77
+ if (v !== void 0) return v;
78
+ }
79
+ if (t > ts[n - 1]) {
80
+ const v = edge(c.extrapolateRight ?? "extend", false);
81
+ if (v !== void 0) return v;
82
+ }
83
+ let i = 0;
84
+ while (i < n - 2 && t >= ts[i + 1]) i++;
85
+ const span = ts[i + 1] - ts[i], f = (t - ts[i]) / span;
86
+ const g = f < 0 || f > 1 ? f : easeOf(c.easing, span, "linear")(f);
87
+ return vs[i] + (vs[i + 1] - vs[i]) * g;
88
+ }
89
+ const KINDS = ["sequence", "series", "layers", "assemble", "disassemble", "travel", "pulse", "ants", "draw", "type", "move", "lift", "focus", "highlight", "camera"];
90
+ const LATER = { 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" };
91
+ const TIMING = ["from", "duration", "easing"];
92
+ const KEYS = {
93
+ sequence: [],
94
+ series: [],
95
+ layers: [],
96
+ assemble: TIMING,
97
+ disassemble: TIMING,
98
+ travel: [...TIMING, "each", "hold", "loop"],
99
+ pulse: [...TIMING, "loop"],
100
+ ants: [...TIMING, "speed", "dash"],
101
+ draw: [...TIMING, "by", "stagger"],
102
+ type: TIMING,
103
+ move: ["from", "x", "y", "scale", "opacity"],
104
+ lift: [...TIMING, "by", "stagger", "to", "opacity"],
105
+ focus: [...TIMING, "fade"],
106
+ highlight: [...TIMING, "fade"],
107
+ camera: TIMING
108
+ };
109
+ const SEQ_KEYS = ["name", "from", "offset", "duration", "loop", "playbackRate", "trimBefore", "say", "show", "hide", "focus", "highlight", "levels", "variants", "enter", "ants", "children", "layers", "series"];
110
+ const isMap = (v) => !!v && typeof v === "object" && !Array.isArray(v);
111
+ function checkMotion(doc) {
112
+ const out = [];
113
+ const err = (path, message, hint) => {
114
+ out.push({ severity: "error", path, message, ...hint ? { hint } : {} });
115
+ };
116
+ const keys = (v, allowed, path) => {
117
+ for (const k of Object.keys(v)) if (!allowed.includes(k)) {
118
+ const d = didYouMean(k, allowed);
119
+ err(`${path}/${k}`, `unknown key "${k}"`, d ? `did you mean "${d}"?` : void 0);
120
+ }
121
+ };
122
+ if (!isMap(doc)) {
123
+ err("", "a motion document is a map");
124
+ return out;
125
+ }
126
+ keys(doc, ["isomotion", "fps", "seed", "ambient", "series", "layers", "markers"], "");
127
+ if (doc.isomotion !== 1) err("/isomotion", "isomotion is 1");
128
+ const fps = typeof doc.fps === "number" && doc.fps > 0 ? doc.fps : void 0;
129
+ if (doc.fps !== void 0 && fps === void 0) err("/fps", "fps is a number over 0");
130
+ if (doc.seed !== void 0 && !Number.isInteger(doc.seed)) err("/seed", "seed is a whole number");
131
+ if (doc.ambient !== void 0 && doc.ambient !== "on" && doc.ambient !== "off") err("/ambient", 'ambient is "on" or "off"');
132
+ const time = (v, path, o = {}) => {
133
+ if (v === void 0) return;
134
+ if (typeof v === "string" && /f$/.test(v) && fps === void 0) {
135
+ err(path, `"${v}" is in frames: the document needs its fps`);
136
+ return;
137
+ }
138
+ const t = msOf(v, fps);
139
+ if (t === void 0) err(path, `${JSON.stringify(v)} isn't a time (ms, "1.2s", "450ms", "30f")`);
140
+ else if (t < 0 && !o.negative) err(path, "a time can't be negative");
141
+ };
142
+ const oneOf = (v, list, path) => {
143
+ if (v !== void 0 && !list.includes(v)) {
144
+ const d = typeof v === "string" ? didYouMean(v, list) : void 0;
145
+ err(path, `${JSON.stringify(v)} isn't one of ${list.join(", ")}`, d ? `did you mean "${d}"?` : void 0);
146
+ }
147
+ };
148
+ const num = (v, path, o = {}) => {
149
+ if (v === void 0) return;
150
+ if (typeof v !== "number" || !Number.isFinite(v) || o.min !== void 0 && v < o.min || o.max !== void 0 && v > o.max || o.over !== void 0 && v <= o.over) err(path, `${JSON.stringify(v)} isn't a number${o.over !== void 0 ? ` over ${o.over}` : o.min !== void 0 ? ` from ${o.min}${o.max !== void 0 ? ` to ${o.max}` : ""}` : ""}`);
151
+ };
152
+ const ids = (v, path) => {
153
+ if (v === void 0) return;
154
+ if (!Array.isArray(v)) {
155
+ err(path, "a list of ids");
156
+ return;
157
+ }
158
+ v.forEach((x, i) => {
159
+ if (typeof x !== "string") err(`${path}/${i}`, "an id");
160
+ });
161
+ };
162
+ const spring = (v, path, more = []) => {
163
+ if (!isMap(v)) {
164
+ err(path, "a spring is a map");
165
+ return;
166
+ }
167
+ keys(v, ["mass", "damping", "stiffness", "overshootClamping", ...more], path);
168
+ for (const k of ["mass", "damping", "stiffness"]) num(v[k], `${path}/${k}`, { over: 0 });
169
+ if (v.overshootClamping !== void 0 && typeof v.overshootClamping !== "boolean") err(`${path}/overshootClamping`, "true or false");
170
+ };
171
+ const ease = (v, path) => {
172
+ if (v === void 0) return;
173
+ if (typeof v === "string") {
174
+ oneOf(v, EASING_NAMES, path);
175
+ return;
176
+ }
177
+ if (isMap(v) && "bezier" in v) {
178
+ keys(v, ["bezier"], path);
179
+ if (!Array.isArray(v.bezier) || v.bezier.length !== 4 || v.bezier.some((x) => typeof x !== "number")) err(`${path}/bezier`, "four numbers");
180
+ return;
181
+ }
182
+ if (isMap(v) && "spring" in v) {
183
+ keys(v, ["spring"], path);
184
+ spring(v.spring, `${path}/spring`);
185
+ return;
186
+ }
187
+ err(path, "an easing is a name, { bezier: [a, b, c, d] } or { spring: { \u2026 } }");
188
+ };
189
+ const curve = (v, path) => {
190
+ if (v === void 0) return;
191
+ if (!isMap(v)) {
192
+ err(path, "a curve is { times, values }");
193
+ return;
194
+ }
195
+ keys(v, ["times", "values", "easing", "extrapolateLeft", "extrapolateRight"], path);
196
+ const ts = v.times, vs = v.values;
197
+ if (!Array.isArray(ts) || !ts.length) err(`${path}/times`, "a list of times");
198
+ else {
199
+ ts.forEach((t, i) => time(t, `${path}/times/${i}`, { negative: true }));
200
+ const n = ts.map((t) => msOf(t, fps));
201
+ for (let i = 1; i < n.length; i++) if (n[i] !== void 0 && n[i - 1] !== void 0 && n[i] <= n[i - 1]) err(`${path}/times/${i}`, "times go up");
202
+ }
203
+ if (!Array.isArray(vs) || vs.some((x) => typeof x !== "number")) err(`${path}/values`, "a list of numbers");
204
+ else if (Array.isArray(ts) && vs.length !== ts.length) err(`${path}/values`, `${vs.length} values for ${ts.length} times`);
205
+ ease(v.easing, `${path}/easing`);
206
+ oneOf(v.extrapolateLeft, EXTRAPOLATE, `${path}/extrapolateLeft`);
207
+ oneOf(v.extrapolateRight, EXTRAPOLATE, `${path}/extrapolateRight`);
208
+ };
209
+ const focus = (v, path) => {
210
+ if (v === void 0) return;
211
+ if (!isMap(v)) {
212
+ err(path, "focus is { lit: [ids] }");
213
+ return;
214
+ }
215
+ keys(v, ["lit", "scope", "level", "always"], path);
216
+ if (v.lit === void 0) err(`${path}/lit`, "what is lit");
217
+ ids(v.lit, `${path}/lit`);
218
+ ids(v.always, `${path}/always`);
219
+ oneOf(v.scope, ["all", "links"], `${path}/scope`);
220
+ num(v.level, `${path}/level`, { min: 0, max: 1 });
221
+ };
222
+ const text = (v, path) => {
223
+ if (v !== void 0 && typeof v !== "string") err(path, "text");
224
+ };
225
+ const names = /* @__PURE__ */ new Map();
226
+ function sequence(v, path, inSeries) {
227
+ if (!isMap(v)) {
228
+ err(path, "a sequence is a map");
229
+ return;
230
+ }
231
+ keys(v, SEQ_KEYS, path);
232
+ if (v.name !== void 0) {
233
+ if (typeof v.name !== "string" || !/^[A-Za-z][\w-]*$/.test(v.name)) err(`${path}/name`, "a name is letters, digits, - and _, from a letter");
234
+ else if (names.has(v.name)) err(`${path}/name`, `"${v.name}" is already a name (${names.get(v.name)})`);
235
+ else names.set(v.name, path);
236
+ }
237
+ if (inSeries && v.from !== void 0) err(`${path}/from`, "in a series a sequence follows the one before", "offset moves it");
238
+ if (!inSeries && v.offset !== void 0) err(`${path}/offset`, "offset is for a series", "use from");
239
+ time(v.from, `${path}/from`);
240
+ time(v.offset, `${path}/offset`, { negative: true });
241
+ time(v.trimBefore, `${path}/trimBefore`);
242
+ if (isMap(v.duration)) {
243
+ keys(v.duration, ["read"], `${path}/duration`);
244
+ const r = v.duration.read;
245
+ if (r !== true && !isMap(r)) err(`${path}/duration/read`, "true, or { base, perChar, min, max }");
246
+ else if (isMap(r)) {
247
+ keys(r, ["base", "perChar", "min", "max"], `${path}/duration/read`);
248
+ for (const k of ["base", "perChar", "min", "max"]) num(r[k], `${path}/duration/read/${k}`, { min: 0 });
249
+ }
250
+ } else time(v.duration, `${path}/duration`);
251
+ if (v.loop !== void 0 && typeof v.loop !== "boolean" && !(Number.isInteger(v.loop) && v.loop >= 1)) err(`${path}/loop`, "true, false, or how many times");
252
+ num(v.playbackRate, `${path}/playbackRate`, { over: 0 });
253
+ if (v.say !== void 0) {
254
+ if (!isMap(v.say)) err(`${path}/say`, "say is a map");
255
+ else {
256
+ const s = v.say;
257
+ keys(s, ["chapter", "title", "text", "code", "facts", "color"], `${path}/say`);
258
+ for (const k of ["chapter", "title", "text", "code", "color"]) text(s[k], `${path}/say/${k}`);
259
+ if (s.facts !== void 0 && (!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]");
260
+ }
261
+ }
262
+ for (const k of ["show", "hide", "highlight"]) ids(v[k], `${path}/${k}`);
263
+ focus(v.focus, `${path}/focus`);
264
+ if (v.levels !== void 0) {
265
+ if (!isMap(v.levels)) err(`${path}/levels`, "levels is a map: an id, and how much of it");
266
+ else for (const [k, x] of Object.entries(v.levels)) num(x, `${path}/levels/${k}`, { min: 0, max: 1 });
267
+ }
268
+ if (v.variants !== void 0) {
269
+ if (!isMap(v.variants)) err(`${path}/variants`, "variants is a map: an id, and the name of its look");
270
+ 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`);
271
+ }
272
+ if (v.enter !== void 0) {
273
+ if (!isMap(v.enter)) err(`${path}/enter`, "enter is { fade }");
274
+ else {
275
+ keys(v.enter, ["fade", "easing"], `${path}/enter`);
276
+ if (v.enter.fade === void 0) err(`${path}/enter/fade`, "how long the fade is");
277
+ time(v.enter.fade, `${path}/enter/fade`);
278
+ ease(v.enter.easing, `${path}/enter/easing`);
279
+ }
280
+ }
281
+ if (v.ants !== void 0 && typeof v.ants !== "boolean") ids(v.ants, `${path}/ants`);
282
+ for (const k of ["children", "layers"]) if (v[k] !== void 0) nodes(v[k], `${path}/${k}`);
283
+ if (v.series !== void 0) series(v.series, `${path}/series`);
284
+ }
285
+ function series(v, path) {
286
+ if (!Array.isArray(v)) {
287
+ err(path, "a series is a list");
288
+ return;
289
+ }
290
+ v.forEach((it, i) => {
291
+ const p = `${path}/${i}`;
292
+ if (!isMap(it) || Object.keys(it).length !== 1 || !("sequence" in it || "transition" in it)) {
293
+ err(p, "a series holds sequences and transitions");
294
+ return;
295
+ }
296
+ if ("sequence" in it) {
297
+ sequence(it.sequence, `${p}/sequence`, true);
298
+ return;
299
+ }
300
+ const t = it.transition, q = `${p}/transition`, seqAt = (k) => isMap(v[k]) && "sequence" in v[k];
301
+ if (!seqAt(i - 1) || !seqAt(i + 1)) err(q, "a transition is between two sequences");
302
+ if (!isMap(t)) {
303
+ err(q, "a transition is { presentation, timing }");
304
+ return;
305
+ }
306
+ keys(t, ["presentation", "timing"], q);
307
+ if (t.presentation === void 0) err(`${q}/presentation`, "a presentation");
308
+ else oneOf(t.presentation, ["fade", "none"], `${q}/presentation`);
309
+ if (!isMap(t.timing)) err(`${q}/timing`, "a timing is { linear } or { spring }");
310
+ else if ("linear" in t.timing) {
311
+ keys(t.timing, ["linear"], `${q}/timing`);
312
+ time(t.timing.linear, `${q}/timing/linear`);
313
+ } else if ("spring" in t.timing) {
314
+ keys(t.timing, ["spring"], `${q}/timing`);
315
+ spring(t.timing.spring, `${q}/timing/spring`, ["duration"]);
316
+ if (isMap(t.timing.spring)) time(t.timing.spring.duration, `${q}/timing/spring/duration`);
317
+ } else err(`${q}/timing`, "a timing is { linear } or { spring }");
318
+ });
319
+ }
320
+ function nodes(v, path) {
321
+ if (!Array.isArray(v)) {
322
+ err(path, "a list");
323
+ return;
324
+ }
325
+ v.forEach((n, i) => {
326
+ const p = `${path}/${i}`;
327
+ if (!isMap(n)) {
328
+ err(p, "a map");
329
+ return;
330
+ }
331
+ const kinds = KINDS.filter((k2) => k2 in n);
332
+ if (kinds.length !== 1) {
333
+ const late = Object.keys(n).find((k2) => k2 in LATER), k0 = Object.keys(n)[0], d = k0 ? didYouMean(k0, KINDS) : void 0;
334
+ 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}"?` : void 0);
335
+ return;
336
+ }
337
+ const k = kinds[0], c = n[k], q = `${p}/${k}`;
338
+ keys(n, [k, ...KEYS[k]], p);
339
+ if (KEYS[k].includes("from")) time(n.from, `${p}/from`);
340
+ if (KEYS[k].includes("duration")) {
341
+ time(n.duration, `${p}/duration`);
342
+ ease(n.easing, `${p}/easing`);
343
+ }
344
+ for (const t of ["each", "hold", "stagger", "fade"]) if (KEYS[k].includes(t)) time(n[t], `${p}/${t}`);
345
+ if (KEYS[k].includes("loop") && n.loop !== void 0 && typeof n.loop !== "boolean") err(`${p}/loop`, "true or false");
346
+ if (k === "sequence") sequence(c, q, false);
347
+ else if (k === "series") series(c, q);
348
+ else if (k === "layers") nodes(c, q);
349
+ else if (k === "assemble" || k === "disassemble") {
350
+ if (!isMap(c)) err(q, `${k} is a map (its options)`);
351
+ else {
352
+ oneOf(c.style, k === "assemble" ? ASSEMBLE_STYLES : DISASSEMBLE_STYLES, `${q}/style`);
353
+ oneOf(c.order, MOTION_ORDERS, `${q}/order`);
354
+ if (c.duration !== void 0) err(`${q}/duration`, "its length is beside it", "duration is the node's");
355
+ }
356
+ } else if (k === "travel") {
357
+ if (!Array.isArray(c) || !c.length) err(q, "a list of connectors");
358
+ else c.forEach((x, j) => {
359
+ if (Array.isArray(x)) ids(x, `${q}/${j}`);
360
+ else if (typeof x !== "string") err(`${q}/${j}`, "a connector, or a list of them at once");
361
+ });
362
+ } else if (k === "ants") {
363
+ if (c !== true) ids(c, q);
364
+ num(n.speed, `${p}/speed`);
365
+ if (n.dash !== void 0 && (!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");
366
+ } else if (k === "move") {
367
+ if (typeof c !== "string") err(q, "an id");
368
+ for (const a of ["x", "y", "scale", "opacity"]) curve(n[a], `${p}/${a}`);
369
+ } else if (k === "focus") focus(c, q);
370
+ else if (k === "camera") {
371
+ if (c !== "fit") {
372
+ if (!isMap(c)) err(q, '"fit", or { focus, zoom, margin }');
373
+ else {
374
+ keys(c, ["focus", "zoom", "margin"], q);
375
+ num(c.zoom, `${q}/zoom`, { over: 0 });
376
+ num(c.margin, `${q}/margin`);
377
+ const f = c.focus;
378
+ 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]");
379
+ }
380
+ }
381
+ } else ids(c, q);
382
+ if (k === "draw") oneOf(n.by, ["stroke", "grow"], `${p}/by`);
383
+ if (k === "lift") {
384
+ if (n.by === void 0) err(`${p}/by`, "how far apart");
385
+ num(n.by, `${p}/by`);
386
+ oneOf(n.to, ["up", "down"], `${p}/to`);
387
+ curve(n.opacity, `${p}/opacity`);
388
+ }
389
+ });
390
+ }
391
+ if (doc.series !== void 0) series(doc.series, "/series");
392
+ if (doc.layers !== void 0) nodes(doc.layers, "/layers");
393
+ if (doc.markers !== void 0) {
394
+ if (!isMap(doc.markers)) err("/markers", "markers is a map");
395
+ else for (const [k, v] of Object.entries(doc.markers)) {
396
+ if (typeof v === "string" && /^[A-Za-z][\w-]*([+-].+)?$/.test(v)) {
397
+ const m = /^([A-Za-z][\w-]*?)(?:([+-])(\d.*))?$/.exec(v);
398
+ const at = m && (names.has(m[1]) ? m[1] : void 0);
399
+ if (!at) {
400
+ const whole = names.has(v);
401
+ if (!whole) {
402
+ const d = didYouMean(m?.[1] ?? v, names.keys());
403
+ err(`/markers/${k}`, `no sequence is named "${m?.[1] ?? v}"`, d ? `did you mean "${d}"?` : void 0);
404
+ }
405
+ } else if (m[3] !== void 0) time(m[3], `/markers/${k}`);
406
+ } else time(v, `/markers/${k}`);
407
+ }
408
+ }
409
+ return out;
410
+ }
411
+ export {
412
+ EASING_NAMES,
413
+ EXTRAPOLATE,
414
+ bezier,
415
+ checkMotion,
416
+ curveAt,
417
+ easeOf,
418
+ msOf,
419
+ readMs,
420
+ springAt
421
+ };
@@ -0,0 +1,34 @@
1
+ export * from "./motion.js";
2
+ import { bindMotion, motionOfSteps, motionSource } from "./timeline.js";
3
+ import { motion, MotionBuilder, SeriesBuilder, NodesBuilder } from "./motion-builder.js";
4
+ import { MOTION_SCHEMA } from "./motion-schema.js";
5
+ import { EASING_NAMES, EXTRAPOLATE, checkMotion, msOf, readMs, easeOf, curveAt, springAt, bezier } from "./motion-doc.js";
6
+ import { withAnts, antsOf, antDash, ANTS_CSS, ANT_SPEED, ANT_DASH, ANT_STILL } from "./ants.js";
7
+ import { drawLooks } from "./looks.js";
8
+ export {
9
+ ANTS_CSS,
10
+ ANT_DASH,
11
+ ANT_SPEED,
12
+ ANT_STILL,
13
+ EASING_NAMES,
14
+ EXTRAPOLATE,
15
+ MOTION_SCHEMA,
16
+ MotionBuilder,
17
+ NodesBuilder,
18
+ SeriesBuilder,
19
+ antDash,
20
+ antsOf,
21
+ bezier,
22
+ bindMotion,
23
+ checkMotion,
24
+ curveAt,
25
+ drawLooks,
26
+ easeOf,
27
+ motion,
28
+ motionOfSteps,
29
+ motionSource,
30
+ msOf,
31
+ readMs,
32
+ springAt,
33
+ withAnts
34
+ };
@@ -0,0 +1,83 @@
1
+ import { ASSEMBLE_STYLES, DISASSEMBLE_STYLES, MOTION_ORDERS } from "./motion.js";
2
+ import { EASING_NAMES, EXTRAPOLATE } from "./motion-doc.js";
3
+ const TIME = { anyOf: [{ type: "number" }, { type: "string", pattern: "^-?\\d+(\\.\\d+)?(ms|s|f)?$" }] };
4
+ const IDS = { type: "array", items: { type: "string" } };
5
+ 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" } } }] };
6
+ const timing = { from: { $ref: "#/$defs/time" }, duration: { $ref: "#/$defs/time" }, easing: { $ref: "#/$defs/ease" } };
7
+ const node = (key, value, more = {}, withTiming = true) => ({ type: "object", required: [key], additionalProperties: false, properties: { [key]: value, ...withTiming ? timing : {}, ...more } });
8
+ const MOTION_SCHEMA = {
9
+ $schema: "https://json-schema.org/draft/2020-12/schema",
10
+ title: "isomotion/1",
11
+ type: "object",
12
+ required: ["isomotion"],
13
+ additionalProperties: false,
14
+ properties: {
15
+ isomotion: { const: 1 },
16
+ fps: { type: "number", exclusiveMinimum: 0, description: 'Frames a second, for lengths written "30f".' },
17
+ seed: { type: "integer" },
18
+ ambient: { enum: ["on", "off"], description: "Off: the ants and the looping tokens and pulses stand still, each at its start." },
19
+ series: { $ref: "#/$defs/series" },
20
+ layers: { $ref: "#/$defs/nodes" },
21
+ markers: { type: "object", additionalProperties: { anyOf: [{ type: "number" }, { type: "string" }] } }
22
+ },
23
+ $defs: {
24
+ time: TIME,
25
+ ids: IDS,
26
+ ease: EASE,
27
+ spring: { type: "object", additionalProperties: false, properties: { mass: { type: "number", exclusiveMinimum: 0 }, damping: { type: "number", exclusiveMinimum: 0 }, stiffness: { type: "number", exclusiveMinimum: 0 }, overshootClamping: { type: "boolean" } } },
28
+ 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 } } },
29
+ 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 } } } },
30
+ 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" } } },
31
+ sequence: {
32
+ type: "object",
33
+ additionalProperties: false,
34
+ properties: {
35
+ name: { type: "string", pattern: "^[A-Za-z][\\w-]*$" },
36
+ from: { $ref: "#/$defs/time" },
37
+ offset: { $ref: "#/$defs/time" },
38
+ trimBefore: { $ref: "#/$defs/time" },
39
+ 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" } } }] } } }] },
40
+ loop: { anyOf: [{ type: "boolean" }, { type: "integer", minimum: 1 }] },
41
+ playbackRate: { type: "number", exclusiveMinimum: 0 },
42
+ say: { $ref: "#/$defs/say" },
43
+ show: { $ref: "#/$defs/ids" },
44
+ hide: { $ref: "#/$defs/ids" },
45
+ focus: { $ref: "#/$defs/focus" },
46
+ highlight: { $ref: "#/$defs/ids" },
47
+ levels: { type: "object", additionalProperties: { type: "number", minimum: 0, maximum: 1 } },
48
+ variants: { type: "object", additionalProperties: { type: "string" } },
49
+ enter: { type: "object", required: ["fade"], additionalProperties: false, properties: { fade: { $ref: "#/$defs/time" }, easing: { $ref: "#/$defs/ease" } } },
50
+ ants: { anyOf: [{ type: "boolean" }, { $ref: "#/$defs/ids" }] },
51
+ children: { $ref: "#/$defs/nodes" },
52
+ layers: { $ref: "#/$defs/nodes" },
53
+ series: { $ref: "#/$defs/series" }
54
+ }
55
+ },
56
+ 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)] } },
57
+ seq: node("sequence", { $ref: "#/$defs/sequence" }, {}, false),
58
+ plan: { type: "object" },
59
+ nodes: {
60
+ type: "array",
61
+ items: { anyOf: [
62
+ { $ref: "#/$defs/seq" },
63
+ node("series", { $ref: "#/$defs/series" }, {}, false),
64
+ node("layers", { $ref: "#/$defs/nodes" }, {}, false),
65
+ node("assemble", { type: "object", properties: { style: { enum: ASSEMBLE_STYLES }, order: { enum: MOTION_ORDERS } } }),
66
+ node("disassemble", { type: "object", properties: { style: { enum: DISASSEMBLE_STYLES }, order: { enum: MOTION_ORDERS } } }),
67
+ node("travel", { type: "array", items: { anyOf: [{ type: "string" }, { $ref: "#/$defs/ids" }] }, minItems: 1 }, { each: { $ref: "#/$defs/time" }, hold: { $ref: "#/$defs/time" }, loop: { type: "boolean" } }),
68
+ node("pulse", { $ref: "#/$defs/ids" }, { loop: { type: "boolean" } }),
69
+ node("ants", { anyOf: [{ const: true }, { $ref: "#/$defs/ids" }] }, { speed: { type: "number" }, dash: { type: "array", items: { type: "number", exclusiveMinimum: 0 }, minItems: 2, maxItems: 2 } }),
70
+ node("draw", { $ref: "#/$defs/ids" }, { by: { enum: ["stroke", "grow"] }, stagger: { $ref: "#/$defs/time" } }),
71
+ node("type", { $ref: "#/$defs/ids" }),
72
+ { 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" } } },
73
+ { 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 } },
74
+ node("focus", { $ref: "#/$defs/focus" }, { fade: { $ref: "#/$defs/time" } }),
75
+ node("highlight", { $ref: "#/$defs/ids" }, { fade: { $ref: "#/$defs/time" } }),
76
+ 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" } } }] })
77
+ ] }
78
+ }
79
+ }
80
+ };
81
+ export {
82
+ MOTION_SCHEMA
83
+ };