@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.
- package/EDITOR-GUIDE.md +9 -2
- package/README.md +1 -1
- package/assets/app.css +6 -0
- package/assets/marks.css +2 -0
- package/iso-editor.js +194 -184
- package/iso-render.js +157 -148
- package/iso-systems.js +170 -159
- package/js/iso-editor/make-way.js +62 -0
- package/js/iso-editor/ui/canvas.js +477 -216
- package/js/iso-editor/ui/palette-look.js +53 -0
- package/js/iso-editor/ui/setup.js +3 -0
- 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-editor/make-way.ts +89 -0
- package/src/iso-editor/ui/app.css +4 -0
- package/src/iso-editor/ui/canvas.ts +390 -170
- package/src/iso-editor/ui/marks.css +2 -0
- package/src/iso-editor/ui/palette-look.ts +51 -0
- package/src/iso-editor/ui/setup.ts +8 -0
- 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-editor/make-way.d.ts +40 -0
- package/types/iso-editor/ui/canvas.d.ts +33 -12
- package/types/iso-editor/ui/palette-look.d.ts +5 -0
- package/types/iso-editor/ui/setup.d.ts +5 -0
- 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
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
@layer views {
|
|
8
8
|
.iso-canvas { --iso-sel: var(--accent); --iso-sel-fill: color-mix(in srgb, var(--iso-sel) 12%, transparent);
|
|
9
9
|
--iso-sel-faint: color-mix(in srgb, var(--iso-sel) 6%, transparent); --iso-bad: var(--bad); --iso-warn: var(--warn); }
|
|
10
|
+
/* a drag moves things, it doesn't select the drawing's words */
|
|
11
|
+
.iso-canvas svg.scene { user-select: none; -webkit-user-select: none; }
|
|
10
12
|
.iso-canvas svg.scene [data-id] { cursor: pointer; }
|
|
11
13
|
.iso-canvas svg.scene.mode-pan { cursor: grab; }
|
|
12
14
|
.iso-canvas svg.scene.mode-select { cursor: crosshair; }
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// The palette's entries as what they make (0.14.0): each thing drawn alone, small, in the drawing's own style. The canvas takes it as
|
|
2
|
+
// its kit's `looks`; a host that doesn't ask for it bundles none of it.
|
|
3
|
+
|
|
4
|
+
import { buildSvg } from '../../iso-core/svg-dom.ts';
|
|
5
|
+
import { boxSilhouette } from '../../iso-objects/geometry.ts';
|
|
6
|
+
import { applyStep } from '../splice.ts';
|
|
7
|
+
import type { Path } from '../path.ts';
|
|
8
|
+
import { h } from './icons.ts';
|
|
9
|
+
import type { CanvasDrawing, CanvasKit, CanvasUi, PaletteItem } from './canvas.ts';
|
|
10
|
+
|
|
11
|
+
type Kit<M, D extends CanvasDrawing> = Pick<CanvasKit<M, D>, 'kinds' | 'commands' | 'trial'>;
|
|
12
|
+
/** The kit's `looks`: the entries that show, one at a time, each once; an entry that can't be drawn alone keeps its icon. */
|
|
13
|
+
export const paletteLooks = <M, D extends CanvasDrawing>(K: Kit<M, D>): NonNullable<CanvasKit<M, D>['looks']> => (palette, data, ui: CanvasUi, theme) => {
|
|
14
|
+
palette.classList.add('looks');
|
|
15
|
+
let soon = 0;
|
|
16
|
+
return function draw() {
|
|
17
|
+
if (soon) return;
|
|
18
|
+
soon = window.setTimeout(() => {
|
|
19
|
+
soon = 0;
|
|
20
|
+
const b = [...palette.querySelectorAll<HTMLButtonElement>('.pal-item:not([data-look])')].find(q => !q.hidden && !q.closest('[hidden]')), d = data();
|
|
21
|
+
if (!b || !d) return;
|
|
22
|
+
const s = lookOf(K, ui.palette[Number(b.dataset.entry)], d, theme);
|
|
23
|
+
b.dataset.look = s ? 'object' : 'icon';
|
|
24
|
+
if (s) b.querySelector('.icon')?.replaceWith(h('span', { class: `${ui.rootClass} pal-look` }, s as unknown as HTMLElement));
|
|
25
|
+
draw();
|
|
26
|
+
}, 0);
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
/** the thing an entry makes: the file with nothing in it but its plain values, and one of it; its own elements, without labels,
|
|
30
|
+
* framed by its outline. Undefined where that can't be drawn. */
|
|
31
|
+
function lookOf<M, D extends CanvasDrawing>(K: Kit<M, D>, item: PaletteItem, data: unknown, theme: string): SVGSVGElement | undefined {
|
|
32
|
+
const k = K.kinds.find(x => x.kind === item.kind);
|
|
33
|
+
if (!k) return undefined;
|
|
34
|
+
const name = K.commands.freshName('', item.name ?? k.singular), path = [...k.section, name] as Path;
|
|
35
|
+
const bare = Object.entries((data ?? {}) as Record<string, unknown>).filter(([, v]) => v === null || typeof v !== 'object').map(([n, v]) => `${JSON.stringify(n)}: ${JSON.stringify(v)}\n`).join('');
|
|
36
|
+
let d: D | undefined;
|
|
37
|
+
try { d = K.trial(K.commands.insertCmd(path.slice(0, -1), name, (item.value?.() ?? k.template()) as Record<string, unknown>).build(bare).steps.reduce((t, st) => applyStep(t, st), bare), theme); } catch { /* it needs more around it */ }
|
|
38
|
+
const id = d?.pinId.get(name), it = id ? d!.scene.layout.byId.get(id) : undefined;
|
|
39
|
+
if (!d || d.diags.length || !id || !it) return undefined;
|
|
40
|
+
const s = buildSvg(d.svg), all = [...s.querySelectorAll<SVGElement>('[data-id]')];
|
|
41
|
+
if (!all.some(e => e.getAttribute('data-id') === id)) return undefined;
|
|
42
|
+
for (const e of all) if (e.getAttribute('data-id') !== id) e.remove();
|
|
43
|
+
for (const e of s.querySelectorAll('text, title, desc, .g-labels, .g-floor')) e.remove();
|
|
44
|
+
const pts = boxSilhouette(it.box), xs = pts.map(q => q[0]), ys = pts.map(q => q[1]), m = 3;
|
|
45
|
+
s.setAttribute('viewBox', [Math.min(...xs) - m, Math.min(...ys) - m, Math.max(...xs) - Math.min(...xs) + 2 * m, Math.max(...ys) - Math.min(...ys) + 2 * m].map(v => v.toFixed(1)).join(' '));
|
|
46
|
+
for (const a of ['width', 'height', 'role', 'tabindex', 'aria-label']) s.removeAttribute(a);
|
|
47
|
+
for (const e of s.querySelectorAll('[tabindex]')) { e.removeAttribute('tabindex'); e.removeAttribute('role'); }
|
|
48
|
+
s.setAttribute('preserveAspectRatio', 'xMidYMid meet'); s.setAttribute('aria-hidden', 'true'); s.setAttribute('focusable', 'false');
|
|
49
|
+
s.classList.add('pal-scene');
|
|
50
|
+
return s;
|
|
51
|
+
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// The text pane is given, not imported: Monaco is a big dependency, and a host without it still gets the canvas, the
|
|
8
8
|
// tree and the panel.
|
|
9
9
|
|
|
10
|
+
import { paletteLooks } from './palette-look.ts';
|
|
10
11
|
import type { DomainAdapter, FileRole, ObjectRef } from '../adapter.ts';
|
|
11
12
|
import type { Path } from '../path.ts';
|
|
12
13
|
import { toPointer } from '../path.ts';
|
|
@@ -58,6 +59,11 @@ export interface DomainView<R, D extends CanvasDrawing> {
|
|
|
58
59
|
paletteLayout?: 'floating' | 'docked';
|
|
59
60
|
/** (0.12.5) what a double click on an object of the canvas does: shows it in the text (the default), or renames it */
|
|
60
61
|
doubleClick?: 'text' | 'rename';
|
|
62
|
+
/** (0.14.0) the palette's entries with their icon (the default), or with the thing itself, drawn small in the current theme */
|
|
63
|
+
paletteLook?: 'icon' | 'object';
|
|
64
|
+
/** (0.14.0) what is in a drag's way jiggles before it gives way, and glides: 'always' (the default), or 'respect': not where
|
|
65
|
+
* the viewer prefers reduced motion */
|
|
66
|
+
dragMotion?: 'always' | 'respect';
|
|
61
67
|
/** the canvas's 2D view (0.6.0): a 3D | 2D switch; its link kinds' styles (the host's canvas CSS adds flatCss) */
|
|
62
68
|
flat?: CanvasKit<R, D>['flat'];
|
|
63
69
|
/** a layer over the canvas, and a part beside the views (A: their calls) */
|
|
@@ -115,6 +121,7 @@ export function setupFor<R, D extends CanvasDrawing>(a: DomainAdapter, v: Domain
|
|
|
115
121
|
: a.kinds.map(k => ({ kind: k.kind, icon: icon(k.kind), label: k.singular[0].toUpperCase() + k.singular.slice(1) })),
|
|
116
122
|
...(v.paletteLayout === undefined ? {} : { paletteLayout: v.paletteLayout }),
|
|
117
123
|
...(v.doubleClick === undefined ? {} : { doubleClick: v.doubleClick }),
|
|
124
|
+
...(v.dragMotion === undefined ? {} : { dragMotion: v.dragMotion }),
|
|
118
125
|
text: {
|
|
119
126
|
stage: 'The file, drawn: arrow keys and Tab move through what is drawn; + and − zoom, 0 fits', dragGroup: 'A drag on nothing',
|
|
120
127
|
dragSelectTip: 'Drag draws a selection; hold the space key to pan', unreadable: 'The canvas draws a file that reads cleanly; fix the problems listed to see it.',
|
|
@@ -128,6 +135,7 @@ export function setupFor<R, D extends CanvasDrawing>(a: DomainAdapter, v: Domain
|
|
|
128
135
|
...(a.dropInto ? { dropOnto: (t: ObjectRef, v: unknown, k: string, d: unknown) => a.dropInto!(t, v, k, d) } : {}),
|
|
129
136
|
trial: (text, theme) => { const r = read(text); return r.read === undefined || r.diags.some(d => d.severity === 'error') ? undefined : v.draw(r.read as R, (r.read as { data?: unknown }).data, theme); },
|
|
130
137
|
};
|
|
138
|
+
if (v.paletteLook === 'object') canvas.looks = paletteLooks(canvas);
|
|
131
139
|
const kit: MountKit<R, undefined> = {
|
|
132
140
|
async check(text) {
|
|
133
141
|
const r = analyse(a, text, { fileName: v.fileName });
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Marching dashes on a still drawing (docs/design/MOTION.md, 5a): a loop of the drawing's own, for a page or an image that plays
|
|
2
|
+
// nothing else. Two ways: 'css' (a class on each connector and ANTS_CSS once on the page, which a switch can stop) and 'smil' (an
|
|
3
|
+
// animation inside each connector, for an SVG that goes out alone, where a page's stylesheet doesn't reach). In motion (a
|
|
4
|
+
// timeline's `ants`) there is no loop: a frame has the offset of its moment.
|
|
5
|
+
|
|
6
|
+
import type { CompiledScene } from './compile.ts';
|
|
7
|
+
|
|
8
|
+
export const ANT_SPEED = 32 / 1.1;
|
|
9
|
+
export const ANT_DASH: Record<string, [number, number]> = { solid: [9, 7], dashed: [7, 6], dotted: [3, 6] };
|
|
10
|
+
export const ANT_STILL = ['blocked', 'planned'];
|
|
11
|
+
export interface AntsOptions {
|
|
12
|
+
/** how it moves (default: smil); 'none': the dashes, standing still */
|
|
13
|
+
by?: 'css' | 'smil' | 'none';
|
|
14
|
+
/** the connectors that march (default: every one that may) */
|
|
15
|
+
ids?: string[];
|
|
16
|
+
/** units a second, in the drawing's units (default: 32 in 1.1 s) */
|
|
17
|
+
speed?: number;
|
|
18
|
+
dash?: [number, number];
|
|
19
|
+
/** the connectors' dashes, by kind: the theme's `kinds` (a dashed kind marches with its own dash) */
|
|
20
|
+
kinds?: Record<string, { dash?: 'solid' | 'dashed' | 'dotted' }>;
|
|
21
|
+
/** kinds whose connectors stand still (default: blocked, planned) */
|
|
22
|
+
still?: string[];
|
|
23
|
+
}
|
|
24
|
+
/** for 'css': one rule and its keyframes. A page stops them, where they are, with --ants: paused. */
|
|
25
|
+
export const ANTS_CSS = `
|
|
26
|
+
.flow.iso-ants{stroke-dasharray:var(--ant-on) var(--ant-off);animation:iso-ants var(--ant-turn) linear infinite;animation-play-state:var(--ants,running)}
|
|
27
|
+
@keyframes iso-ants{to{stroke-dashoffset:var(--ant-to)}}
|
|
28
|
+
`;
|
|
29
|
+
/** the dash a connector marches with, or undefined when it stands still */
|
|
30
|
+
export function antDash(kind: string, o: Pick<AntsOptions, 'dash' | 'kinds' | 'still'> = {}): [number, number] | undefined {
|
|
31
|
+
return (o.still ?? ANT_STILL).includes(kind) ? undefined : o.dash ?? ANT_DASH[o.kinds?.[kind]?.dash ?? 'solid'];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** what a scene's `ants:` in the file asks for, as options; undefined: none */
|
|
35
|
+
export function antsOf(raw: { ants?: boolean | string[] | { ids?: string[]; speed?: number; dash?: [number, number] } } | undefined): Pick<AntsOptions, 'ids' | 'speed' | 'dash'> | undefined {
|
|
36
|
+
const a = raw?.ants;
|
|
37
|
+
return !a ? undefined : a === true ? {} : Array.isArray(a) ? { ids: a } : a;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The scene's still, as SVG text, with its connectors marching. */
|
|
41
|
+
export function withAnts(scene: Pick<CompiledScene, 'rendered' | 'routes'>, o: AntsOptions = {}): string {
|
|
42
|
+
const kindOf = new Map(scene.routes.map(r => [r.id, r.kind])), wanted = o.ids && new Set(o.ids), speed = o.speed ?? ANT_SPEED;
|
|
43
|
+
const n = (v: number) => String(Number(v.toFixed(3)));
|
|
44
|
+
return scene.rendered.svg.replace(/(<g data-route="([^"]*)" data-part="line">(?:<g[^>]*>)*)<path ([^>]*?)\s*\/>/g, (m, before: string, id: string, attrs: string) => {
|
|
45
|
+
const route = id.replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');
|
|
46
|
+
const dash = wanted && !wanted.has(route) ? undefined : antDash(kindOf.get(route) ?? '', o);
|
|
47
|
+
if (!dash || !/class="flow\b/.test(attrs)) return m;
|
|
48
|
+
const period = dash[0] + dash[1], turn = n(period / Math.max(1e-6, Math.abs(speed)) ), to = n(speed < 0 ? period : -period);
|
|
49
|
+
if (o.by === 'css') return `${before}<path ${attrs.replace(/class="flow\b/, 'class="flow iso-ants')} style="--ant-on:${n(dash[0])};--ant-off:${n(dash[1])};--ant-to:${to};--ant-turn:${turn}s"/>`;
|
|
50
|
+
if (o.by === 'none') return `${before}<path ${attrs.replace(/ ?stroke-dasharray="[^"]*"/, '')} style="stroke-dasharray:${n(dash[0])} ${n(dash[1])}"/>`;
|
|
51
|
+
return `${before}<path ${attrs.replace(/ ?stroke-dasharray="[^"]*"/, '')} style="stroke-dasharray:${n(dash[0])} ${n(dash[1])}"><animate attributeName="stroke-dashoffset" from="0" to="${to}" dur="${turn}s" repeatCount="indefinite"/></path>`;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -9,10 +9,13 @@ import { routeAll, type Routed } from './route.ts';
|
|
|
9
9
|
import { resolveSteps, type Steps } from './steps.ts';
|
|
10
10
|
import { extendTheme, loadTheme } from '../iso-core/theme.ts';
|
|
11
11
|
import { type KindCatalog, nodeKindOr } from './kinds.ts';
|
|
12
|
+
import type { MotionDoc } from './motion-doc.ts';
|
|
12
13
|
import type { GridDef, RawDiagram, RawNode, RawRegion, RawScene, Theme } from './types.ts';
|
|
13
14
|
import { facePainter, iconPainter } from './assets.ts';
|
|
14
15
|
|
|
15
|
-
export interface CompiledScene { id: string; raw: RawDiagram['scenes'][number]; layout: Layout; routes: Routed[]; steps: Steps; rendered: RenderedScene; grid: GridDef; assemble: boolean
|
|
16
|
+
export interface CompiledScene { id: string; raw: RawDiagram['scenes'][number]; layout: Layout; routes: Routed[]; steps: Steps; rendered: RenderedScene; grid: GridDef; assemble: boolean;
|
|
17
|
+
/** the file's `motion:` on this scene, checked against the drawing (a file with errors in it has none) */
|
|
18
|
+
motion?: MotionDoc }
|
|
16
19
|
export interface Compiled {
|
|
17
20
|
ok: boolean;
|
|
18
21
|
/** the parsed source: positions and spans of every value, for the proof sheet */
|
package/src/iso-objects/index.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// The layer's public API. Importers name the module a name comes from (with code splitting, a barrel puts every file
|
|
6
6
|
// it reaches into each bundle that imports it); the Node tests may import this index.
|
|
7
7
|
|
|
8
|
-
export type { Side, StepRef, RawPlacement, RawDeco, RawNode, RawGate, RawRegion, RawNote, RawScene, GridDef, RawDiagram, RegionKindName, Placed, RawRole, RawKind, Theme } from './types.ts';
|
|
8
|
+
export type { Side, StepRef, RawPlacement, RawDeco, RawNode, RawGate, RawRegion, RawNote, RawScene, GridDef, RawDiagram, RegionKindName, Placed, RawRole, RawKind, Theme, RawMotion, RawVariant } from './types.ts';
|
|
9
9
|
export { regionKind, nodeKind, nodeKindOr, wallHeight, kindsCss } from './kinds.ts';
|
|
10
10
|
export type { RegionShape, RegionKind, KindCatalog } from './kinds.ts';
|
|
11
11
|
export { drawDecos, boxAsset, plate, cloudPath, drawCloud, defaultLabel, registerMarks, markCss, iconPainter, facePainter, placeholderIcon } from './assets.ts';
|
|
@@ -18,6 +18,19 @@ export { silhouette, polyBounds, polyRectArea, rectArea, lineRectArea, Obstacles
|
|
|
18
18
|
export type { Rect, Poly, LabelText, PlacedLabel, CalloutSpot } from './labels.ts';
|
|
19
19
|
export { offscreen, assembly } from './assemble.ts';
|
|
20
20
|
export type { Edge, Piece, Assembly } from './assemble.ts';
|
|
21
|
+
export { ASSEMBLE_STYLES, DISASSEMBLE_STYLES, MOTION_ORDERS, MOTION_EASINGS, motionModel, motionOrder, planOf, assemble, disassemble, alongPath, svgAt, paint, introSvg } from './motion.ts';
|
|
22
|
+
export type { AssembleStyle, DisassembleStyle, MotionOrder, MotionEasing, Direction, PieceMotion, AssembleOptions, DisassembleOptions, PlanOptions, MotionPiece, MotionModel, MotionPlan, ScenePlan, SceneState, ItemState, LabelState, RouteState } from './motion.ts';
|
|
23
|
+
export { drawLooks } from './looks.ts';
|
|
24
|
+
export { withAnts, antsOf, antDash, ANTS_CSS, ANT_SPEED, ANT_DASH, ANT_STILL } from './ants.ts';
|
|
25
|
+
export type { AntsOptions } from './ants.ts';
|
|
26
|
+
export { motion, MotionBuilder, SeriesBuilder, NodesBuilder } from './motion-builder.ts';
|
|
27
|
+
export type { SequenceOptions, Timing, CameraVerbs, Ids as MotionIds } from './motion-builder.ts';
|
|
28
|
+
export { bindMotion, motionOfSteps } from './timeline.ts';
|
|
29
|
+
export type { Timeline, BindOptions, BeatInfo, Cue } from './timeline.ts';
|
|
30
|
+
export { MOTION_SCHEMA, EASING_NAMES, EXTRAPOLATE, checkMotion, msOf, readMs, easeOf, curveAt, springAt, bezier } from './motion-doc.ts';
|
|
31
|
+
export type { MotionDoc, MotionNode, MotionTime, MotionEase, EasingName, Extrapolate, SpringConfig, Curve, Say as MotionSay, Focus, ReadTime, Sequence, SeriesItem, Transition, MotionDiag } from './motion-doc.ts';
|
|
32
|
+
export { resolveSvg, FONT_STACKS } from './resolve.ts';
|
|
33
|
+
export type { ResolveOptions, ThemeMode } from './resolve.ts';
|
|
21
34
|
export { durationFor, resolveSteps, midpoint } from './steps.ts';
|
|
22
35
|
export type { RawStep, Token, StepData, Say, Mark, Badge, Steps } from './steps.ts';
|
|
23
36
|
export { focusView } from './focus.ts';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The looks things declare (`variants:`): the same drawing with each thing's in it, for a motion to take that thing's parts from. A format
|
|
2
|
+
// that offers looks draws them after it compiles; one that doesn't bundles none of this.
|
|
3
|
+
|
|
4
|
+
import type { CompiledScene } from './compile.ts';
|
|
5
|
+
import { renderScene } from './scene.ts';
|
|
6
|
+
import type { Theme } from './types.ts';
|
|
7
|
+
|
|
8
|
+
/** Draw a scene's looks into its `rendered` (none declared: nothing). */
|
|
9
|
+
export function drawLooks(s: CompiledScene, theme: Theme): void {
|
|
10
|
+
const looks: Record<string, string[]> = {};
|
|
11
|
+
for (const p of s.layout.items) { const v = (p.region ?? p.node)?.variants; if (v) looks[p.id] = Object.keys(v); }
|
|
12
|
+
if (!Object.keys(looks).length) return;
|
|
13
|
+
s.rendered.looks = looks;
|
|
14
|
+
s.rendered.variants = Object.fromEntries([...new Set(Object.values(looks).flat())].map(name => [name, renderScene({
|
|
15
|
+
scene: s.raw, routes: s.routes, steps: s.steps, theme, id: s.raw.id, gridStep: s.grid.step ?? s.grid.snap ?? 1,
|
|
16
|
+
layout: { ...s.layout, items: s.layout.items.map(p => {
|
|
17
|
+
const v = (p.region ?? p.node)?.variants?.[name];
|
|
18
|
+
return !v ? p : p.region ? { ...p, region: { ...p.region, ...v, deco: undefined } as typeof p.region } : { ...p, node: { ...p.node!, ...v } };
|
|
19
|
+
}) },
|
|
20
|
+
}).svg]));
|
|
21
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The motion document, written in code: motion() and its methods make plain data (toJSON() is the document) and nothing else is
|
|
2
|
+
// kept. Every word here is one of the document's; `over` is its duration. What a document can't say has no method.
|
|
3
|
+
|
|
4
|
+
import type { Curve, Focus, MotionDoc, MotionEase, MotionNode, MotionTime, ReadTime, Say, Sequence, SeriesItem, Transition } from './motion-doc.ts';
|
|
5
|
+
import type { AssembleOptions, DisassembleOptions } from './motion.ts';
|
|
6
|
+
|
|
7
|
+
export interface Timing { from?: MotionTime; over?: MotionTime; easing?: MotionEase }
|
|
8
|
+
/** a sequence's own keys; `read`: as long as its words take (true, or how) */
|
|
9
|
+
export type SequenceOptions = Omit<Sequence, 'name' | 'children' | 'layers' | 'series' | 'duration'> & { duration?: MotionTime; read?: ReadTime['read'] };
|
|
10
|
+
export type Ids = string | string[];
|
|
11
|
+
const list = (v: Ids): string[] => (typeof v === 'string' ? [v] : [...v]);
|
|
12
|
+
/** what was given, as plain data of its own (no undefined, nothing shared with the caller) */
|
|
13
|
+
const plain = <T>(v: T): T => JSON.parse(JSON.stringify(v)) as T;
|
|
14
|
+
const timing = (o: Timing = {}) => ({ ...(o.from === undefined ? {} : { from: o.from }), ...(o.over === undefined ? {} : { duration: o.over }), ...(o.easing === undefined ? {} : { easing: o.easing }) });
|
|
15
|
+
|
|
16
|
+
export interface CameraVerbs<B> { focus(on: Ids | [number, number, number, number], o?: Timing & { zoom?: number; margin?: number }): B; fit(o?: Timing): B }
|
|
17
|
+
/** what is in a sequence (or in a document's layers), in the order it is said */
|
|
18
|
+
export class NodesBuilder {
|
|
19
|
+
/** @internal */
|
|
20
|
+
readonly nodes: MotionNode[] = [];
|
|
21
|
+
private add(n: MotionNode): this { this.nodes.push(plain(n)); return this; }
|
|
22
|
+
assemble(o: Omit<AssembleOptions, 'duration'> = {}, t: Timing = {}): this { return this.add({ assemble: o, ...timing(t) }); }
|
|
23
|
+
disassemble(o: Omit<DisassembleOptions, 'duration'> = {}, t: Timing = {}): this { return this.add({ disassemble: o, ...timing(t) } as MotionNode); }
|
|
24
|
+
/** tokens along connectors, one after another; a nested list at once; "id:kind" */
|
|
25
|
+
travel(routes: (string | string[])[], o: Timing & { each?: MotionTime; hold?: MotionTime; loop?: boolean } = {}): this { const { each, hold, loop } = o; return this.add({ travel: routes, ...timing(o), each, hold, loop }); }
|
|
26
|
+
pulse(ids: Ids, o: Timing & { loop?: boolean } = {}): this { return this.add({ pulse: list(ids), ...timing(o), loop: o.loop }); }
|
|
27
|
+
ants(ids: Ids | true = true, o: Timing & { speed?: number; dash?: [number, number] } = {}): this { return this.add({ ants: ids === true ? true : list(ids), ...timing(o), speed: o.speed, dash: o.dash }); }
|
|
28
|
+
draw(routes: Ids, o: Timing & { by?: 'stroke' | 'grow'; stagger?: MotionTime } = {}): this { return this.add({ draw: list(routes), ...timing(o), by: o.by, stagger: o.stagger }); }
|
|
29
|
+
type(ids: Ids, o: Timing = {}): this { return this.add({ type: list(ids), ...timing(o) }); }
|
|
30
|
+
move(id: string, o: { from?: MotionTime; x?: Curve; y?: Curve; scale?: Curve; opacity?: Curve }): this { return this.add({ move: id, ...o }); }
|
|
31
|
+
lift(ids: Ids, by: number, o: Timing & { stagger?: MotionTime; to?: 'up' | 'down'; opacity?: Curve } = {}): this { return this.add({ lift: list(ids), by, ...timing(o), stagger: o.stagger, to: o.to, opacity: o.opacity }); }
|
|
32
|
+
focus(lit: Ids, o: Timing & Omit<Focus, 'lit'> & { fade?: MotionTime } = {}): this { const { scope, level, always } = o; return this.add({ focus: { lit: list(lit), scope, level, always }, ...timing(o), fade: o.fade }); }
|
|
33
|
+
highlight(ids: Ids, o: Timing & { fade?: MotionTime } = {}): this { return this.add({ highlight: list(ids), ...timing(o), fade: o.fade }); }
|
|
34
|
+
/** the view: on a thing, some things or a box; or the whole drawing again */
|
|
35
|
+
get camera(): CameraVerbs<this> {
|
|
36
|
+
return {
|
|
37
|
+
focus: (on, o = {}) => this.add({ camera: { focus: on as string, zoom: o.zoom, margin: o.margin }, ...timing(o) }),
|
|
38
|
+
fit: (o = {}) => this.add({ camera: 'fit', ...timing(o) }),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** a sequence in this one: a clock of its own, from `from` */
|
|
42
|
+
sequence(name: string | undefined, o: SequenceOptions = {}, fill?: (b: NodesBuilder) => unknown): this { return this.add({ sequence: sequenceOf(name, o, fill) }); }
|
|
43
|
+
/** sequences one after another */
|
|
44
|
+
series(fill: (s: SeriesBuilder) => unknown): this { const s = new SeriesBuilder(); fill(s); return this.add({ series: s.items }); }
|
|
45
|
+
/** nodes over one another */
|
|
46
|
+
layers(fill: (b: NodesBuilder) => unknown): this { const b = new NodesBuilder(); fill(b); return this.add({ layers: b.nodes }); }
|
|
47
|
+
}
|
|
48
|
+
function sequenceOf(name: string | undefined, o: SequenceOptions, fill?: (b: NodesBuilder) => unknown): Sequence {
|
|
49
|
+
const { read, duration, ...rest } = o, b = new NodesBuilder();
|
|
50
|
+
fill?.(b);
|
|
51
|
+
return plain({ ...(name === undefined ? {} : { name }), ...rest, ...(read !== undefined ? { duration: { read } } : duration !== undefined ? { duration } : {}), ...(b.nodes.length ? { children: b.nodes } : {}) });
|
|
52
|
+
}
|
|
53
|
+
export class SeriesBuilder {
|
|
54
|
+
/** @internal */
|
|
55
|
+
readonly items: SeriesItem[] = [];
|
|
56
|
+
sequence(name: string | undefined, o: SequenceOptions = {}, fill?: (b: NodesBuilder) => unknown): this { this.items.push({ sequence: sequenceOf(name, o, fill) }); return this; }
|
|
57
|
+
transition(t: Transition): this { this.items.push({ transition: plain(t) }); return this; }
|
|
58
|
+
}
|
|
59
|
+
export class MotionBuilder extends SeriesBuilder {
|
|
60
|
+
private over: MotionNode[] = [];
|
|
61
|
+
private marks: Record<string, MotionTime> = {};
|
|
62
|
+
private head: { fps?: number; seed?: number; ambient?: 'on' | 'off' };
|
|
63
|
+
constructor(head: { fps?: number; seed?: number; ambient?: 'on' | 'off' } = {}) { super(); this.head = head; }
|
|
64
|
+
/** a beat: a sequence with commentary */
|
|
65
|
+
beat(name: string, say: Say, o: SequenceOptions = {}, fill?: (b: NodesBuilder) => unknown): this { return this.sequence(name, { ...o, say }, fill); }
|
|
66
|
+
/** the whole drawing arrives (a sequence as long as that takes) */
|
|
67
|
+
assemble(o: Omit<AssembleOptions, 'duration'> = {}, t: Timing = {}): this { return this.sequence(undefined, {}, b => b.assemble(o, t)); }
|
|
68
|
+
/** the whole drawing leaves */
|
|
69
|
+
disassemble(o: Omit<DisassembleOptions, 'duration'> = {}, t: Timing = {}): this { return this.sequence(undefined, {}, b => b.disassemble(o, t)); }
|
|
70
|
+
/** a name for a moment: ms, or "beat+1200" */
|
|
71
|
+
mark(name: string, at: MotionTime): this { this.marks[name] = at; return this; }
|
|
72
|
+
/** nodes over the whole document, on its own clock */
|
|
73
|
+
layers(fill: (b: NodesBuilder) => unknown): this { const b = new NodesBuilder(); fill(b); this.over.push(...b.nodes); return this; }
|
|
74
|
+
toJSON(): MotionDoc {
|
|
75
|
+
return plain({ isomotion: 1 as const, ...this.head, ...(this.items.length ? { series: this.items } : {}), ...(this.over.length ? { layers: this.over } : {}), ...(Object.keys(this.marks).length ? { markers: this.marks } : {}) });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Start a motion document. */
|
|
79
|
+
export const motion = (head: { fps?: number; seed?: number; ambient?: 'on' | 'off' } = {}): MotionBuilder => new MotionBuilder(head);
|