@entro314labs/iso-engine 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.md +165 -2
- package/THIRD_PARTY_NOTICES.md +39 -0
- package/dist/engine/anim.js +225 -0
- package/dist/engine/behaviors.js +105 -0
- package/dist/engine/diagram.js +80 -0
- package/dist/engine/draw.js +117 -0
- package/dist/engine/export.js +81 -0
- package/dist/engine/figure.js +35 -0
- package/dist/engine/global.js +4 -0
- package/dist/engine/index.d.ts +278 -0
- package/dist/engine/index.js +164 -0
- package/dist/engine/math.js +190 -0
- package/dist/engine/node.js +290 -0
- package/dist/engine/paths.js +76 -0
- package/dist/engine/prefabs/architecture.js +136 -0
- package/dist/engine/prefabs/basics.js +131 -0
- package/dist/engine/prefabs/devices.js +295 -0
- package/dist/engine/prefabs/icons.js +49 -0
- package/dist/engine/prefabs/index.js +12 -0
- package/dist/engine/prefabs/logistics.js +156 -0
- package/dist/engine/scene.js +466 -0
- package/dist/engine/shapes.js +781 -0
- package/dist/engine/sort.js +153 -0
- package/dist/engine/spec.js +81 -0
- package/dist/engine/style.js +87 -0
- package/dist/engine.css +1 -0
- package/dist/figure.css +93 -0
- package/dist/figure.d.ts +22 -0
- package/dist/figure.js +73 -0
- package/dist/filters.d.ts +9 -0
- package/dist/filters.js +22 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/iso.global.js +7990 -0
- package/dist/kernel.d.ts +102 -0
- package/dist/kernel.js +172 -0
- package/dist/loops.d.ts +49 -0
- package/dist/loops.js +64 -0
- package/dist/patterns.d.ts +44 -0
- package/dist/patterns.js +97 -0
- package/dist/theme.css +19 -0
- package/dist/types.d.ts +86 -0
- package/dist/types.js +1 -0
- package/package.json +50 -4
- package/skills/iso-engine/SKILL.md +143 -0
- package/skills/iso-engine/examples/engine.html +42 -0
- package/skills/iso-engine/examples/figure.html +51 -0
- package/skills/iso-engine/examples/loop.html +68 -0
- package/skills/iso-engine/examples/loop.js +36 -0
- package/skills/iso-engine/examples/matcher.js +134 -0
- package/skills/iso-engine/examples/spec.html +42 -0
- package/skills/iso-engine/references/animation.md +116 -0
- package/skills/iso-engine/references/api.md +207 -0
- package/skills/iso-engine/references/composition.md +66 -0
- package/skills/iso-engine/references/diagrams.md +68 -0
- package/skills/iso-engine/references/figures.md +359 -0
- package/skills/iso-engine/references/loops.md +126 -0
- package/skills/iso-engine/references/prefabs.md +103 -0
- package/skills/iso-engine/references/spec.md +66 -0
- package/skills/iso-engine/references/style.md +76 -0
- package/skills/iso-engine/references/troubleshooting.md +49 -0
- package/skills/iso-engine/scripts/build.mjs +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dominikos Pritis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,166 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @entro314-labs/iso-engine
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Interactive isometric figures and autoplaying isometric animations in plain SVG. No framework, no canvas, no
|
|
4
|
+
dependencies.
|
|
5
|
+
|
|
6
|
+
- **Figures**: a small projection kernel (`createKernel`) that turns boxes, planes and face detail into SVG. It comes
|
|
7
|
+
with interaction patterns (tweens, zoom, signal sequences, callouts), a glow kit for lit state, and `mountFigure`,
|
|
8
|
+
which runs a figure in any element.
|
|
9
|
+
- **Engine** (`@entro314-labs/iso-engine/engine`): the Iso engine. It handles scenes, depth sorting that re-sorts moving parts,
|
|
10
|
+
seekable timelines, travel/drawOn/assemble presets, diagram routing, 35 prefabs (devices, servers, buildings,
|
|
11
|
+
trucks…) and SVG/PNG export.
|
|
12
|
+
- **Loops** (`@entro314-labs/iso-engine/loops`): helpers for autoplaying engine animations that narrate their beats, with a poster
|
|
13
|
+
frame for reduced motion.
|
|
14
|
+
|
|
15
|
+
See it in use: the [Iso Figures gallery](https://github.com/idominikosgr/iso-figures) is built on this package.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
pnpm add @entro314-labs/iso-engine # or npm i @entro314-labs/iso-engine
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Everything is ESM with TypeScript types. Importing is safe during server-side rendering, because the DOM is only
|
|
24
|
+
touched when you mount something. Mount from client code.
|
|
25
|
+
|
|
26
|
+
## Styles
|
|
27
|
+
|
|
28
|
+
Load the tokens, then the shared figure classes:
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import '@entro314-labs/iso-engine/theme.css'; // colour tokens, light and dark (optional: define them yourself)
|
|
32
|
+
import '@entro314-labs/iso-engine/figure.css'; // .face, .part, .press, .flow, glow kit, host and stage sizing
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`theme.css` defines `--page --panel --edge --body --deck --glass --line --detail --live --ink --accent --accent-hi
|
|
36
|
+
--mono` on `:root`. Dark mode follows the system unless `<html data-theme="light|dark">` sets it. To recolour one
|
|
37
|
+
figure, set `--accent` / `--accent-hi` on any of its ancestors.
|
|
38
|
+
|
|
39
|
+
## A figure
|
|
40
|
+
|
|
41
|
+
A figure is an object with a `mount(ctx)` that draws into an empty `<svg>` and reports its state through `readout`:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { createKernel, frame, mountFigure, type Figure } from '@entro314-labs/iso-engine';
|
|
45
|
+
|
|
46
|
+
const lamp: Figure = {
|
|
47
|
+
id: 'lamp',
|
|
48
|
+
name: 'Lamp',
|
|
49
|
+
hint: 'Press the switch',
|
|
50
|
+
aria: 'A desk lamp on a block. Press the switch to turn it on.',
|
|
51
|
+
css: `[data-fig="lamp"] .shade.on .face { fill:var(--accent); stroke:var(--accent-hi); }`,
|
|
52
|
+
mount({ stage, readout, flash, power }) {
|
|
53
|
+
const k = createKernel();
|
|
54
|
+
let on = false;
|
|
55
|
+
const draw = () => {
|
|
56
|
+
k.pts.length = 0;
|
|
57
|
+
stage.innerHTML =
|
|
58
|
+
`<g class="deco">${k.box(0, 0, 0, 60, 40, 10)}</g>` +
|
|
59
|
+
`<g class="part press" data-id="switch">${k.box(40, 25, 10, 12, 8, 4)}</g>` +
|
|
60
|
+
`<g class="shade${on ? ' on' : ''}">${k.box(5, 5, 10, 20, 20, 30, 6)}</g>`;
|
|
61
|
+
frame(stage, k.pts);
|
|
62
|
+
readout(on ? 'lamp · on' : 'lamp · off');
|
|
63
|
+
};
|
|
64
|
+
stage.addEventListener('click', e => {
|
|
65
|
+
const sw = (e.target as Element).closest('[data-id="switch"]');
|
|
66
|
+
if (!sw) return;
|
|
67
|
+
on = !on;
|
|
68
|
+
power(on);
|
|
69
|
+
draw();
|
|
70
|
+
flash(stage.querySelector('[data-id="switch"]'));
|
|
71
|
+
});
|
|
72
|
+
draw();
|
|
73
|
+
return {};
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const host = document.querySelector<HTMLElement>('#lamp')!; // give it a size; the stage fills it
|
|
78
|
+
const fig = mountFigure(host, lamp, { readout: t => console.log(t) });
|
|
79
|
+
// later: fig.destroy()
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`mountFigure` installs the glow filters and the figure's scoped `css` (once per document), marks the host
|
|
83
|
+
`data-fig="<id>"`, and passes in a context whose `after`, `every`, `loop`, `flash`, `replay` and `power` helpers are all
|
|
84
|
+
cleared by `destroy()`. Pass `{ demo: true }` to run the figure's `demo()` on arrival.
|
|
85
|
+
|
|
86
|
+
Kernel axes: +x runs down-right, +y down-left, +z up. `box(x, y, z, w, d, h, r?)` draws a box's three visible faces;
|
|
87
|
+
`TOP`/`FRONT`/`SIDE` give the transforms for drawing flat detail (`ln`, `rc`, `ci`, `tx`) on a face. For many parts,
|
|
88
|
+
`createUnits` collects boxes plus their detail and depth-sorts them before emitting.
|
|
89
|
+
|
|
90
|
+
## The engine
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import Iso from '@entro314-labs/iso-engine/engine';
|
|
94
|
+
|
|
95
|
+
const s = Iso.scene('#app', { theme: 'dark', label: 'A laptop talking to a server' });
|
|
96
|
+
s.add('platform', { size: [240, 180] });
|
|
97
|
+
s.add('laptop', { id: 'laptop', at: [-30, -10, 0] });
|
|
98
|
+
s.add('server', { id: 'rack', at: [60, -40, 0] });
|
|
99
|
+
s.assemble();
|
|
100
|
+
s.float('laptop', { amp: 4, period: 2.6 });
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
With `theme: 'inherit'` a scene takes `--bg` / `--fg` from its host. Inside a `[data-fig]` or `[data-loop]` element,
|
|
104
|
+
`figure.css` maps the page `--accent` onto the engine's accent and glow.
|
|
105
|
+
|
|
106
|
+
Without a bundler, use the classic script, which defines `window.Iso`:
|
|
107
|
+
|
|
108
|
+
```html
|
|
109
|
+
<script src="https://unpkg.com/@entro314-labs/iso-engine/dist/iso.global.js"></script>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Loops
|
|
113
|
+
|
|
114
|
+
A loop is an engine scene that autoplays a short story and narrates its beats. The host decides when it plays.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { beats, loopScene, narrate, type Loop } from '@entro314-labs/iso-engine/loops';
|
|
118
|
+
|
|
119
|
+
const loop: Loop = {
|
|
120
|
+
id: 'hello', project: 'Demo', kind: 'flow', name: 'Hello', aria: 'A card lifts and lands.',
|
|
121
|
+
mount(host, ctx) {
|
|
122
|
+
const { list, cycle } = beats([['lift', 1.5], ['land', 1.5], ['rest', 1]]);
|
|
123
|
+
const s = loopScene(host, 'A card lifts and lands.', 0.5);
|
|
124
|
+
s.add('platform', { size: [160, 120] });
|
|
125
|
+
s.add('card', { id: 'card', at: [0, 0, 0] });
|
|
126
|
+
const tl = s.timeline({ repeat: -1 });
|
|
127
|
+
tl.to('card', { z: 20 }, { duration: 1.5 }, 0).to('card', { z: 0 }, { duration: 1.5 }, 1.5).wait(cycle - 3);
|
|
128
|
+
narrate(tl, ctx, list, ['lift · the card rises', 'land · it settles', 'rest']);
|
|
129
|
+
return { scene: s, posterTime: 0.5 };
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const host = document.querySelector<HTMLElement>('#hello')!;
|
|
134
|
+
host.dataset.loop = loop.id; // scope for the loop's css, and the accent mapping
|
|
135
|
+
const { scene } = loop.mount(host, { say: t => {}, beat: () => {} });
|
|
136
|
+
scene.play(); // pause it when it scrolls out of view
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Agent skill
|
|
140
|
+
|
|
141
|
+
The package carries an agent skill, `skills/iso-engine/`, versioned with the code it describes: how to plan and draw
|
|
142
|
+
figures, scenes, diagrams and loops, the engine reference, the visual rules, and runnable examples. To let Claude Code
|
|
143
|
+
(or another agent that reads `SKILL.md` skills) use it in your project, link it into the project's skills folder, so it
|
|
144
|
+
follows the installed version:
|
|
145
|
+
|
|
146
|
+
```sh
|
|
147
|
+
mkdir -p .claude/skills
|
|
148
|
+
ln -s ../../node_modules/@entro314-labs/iso-engine/skills/iso-engine .claude/skills/iso-engine
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The examples load the package's own `dist/`; serve `node_modules/@entro314-labs/iso-engine` over http to open them
|
|
152
|
+
(`skills/iso-engine/examples/figure.html`, `loop.html`, `engine.html`, `spec.html`).
|
|
153
|
+
|
|
154
|
+
## Content Security Policy
|
|
155
|
+
|
|
156
|
+
The engine and `mountFigure` add `<style>` elements at runtime, which a CSP without `style-src 'unsafe-inline'`
|
|
157
|
+
blocks. Under a strict CSP:
|
|
158
|
+
|
|
159
|
+
- Link the engine's styles statically, keeping this id. The engine sees it and skips its own injection:
|
|
160
|
+
`<link id="iso-engine-css" rel="stylesheet" href="…/node_modules/@entro314-labs/iso-engine/dist/engine.css">`.
|
|
161
|
+
- Leave `css` off your figure objects and put those rules in your own stylesheet.
|
|
162
|
+
|
|
163
|
+
## Licence
|
|
164
|
+
|
|
165
|
+
MIT. The kernel and the glow kit build on MIT-licensed work by Tolga Cohce (iso-figure) and April Zhu (iso-glow); see
|
|
166
|
+
`THIRD_PARTY_NOTICES.md`.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
Parts of this package derive from the following MIT-licensed work.
|
|
4
|
+
|
|
5
|
+
## iso-glow and iso-figure
|
|
6
|
+
|
|
7
|
+
- **iso-figure** by Tolga Cohce: the isometric projection kernel, the "Fig N" plate and the rule that every figure
|
|
8
|
+
must produce real output. Our `src/kernel.ts` started from it.
|
|
9
|
+
- **iso-glow** by April Zhu, built on iso-figure: the glow lighting kit (lit glass, halos, LEDs, wires with
|
|
10
|
+
travelling pulses, latched presses, power haze), the glow filters, the accent palettes, the figure helpers
|
|
11
|
+
(`after`, `every`, `loop`, `flash`, `replay`, `power`), `demo()`, the continuous rounded box, and lessons from
|
|
12
|
+
building figures. Adapted into `src/figure.css`, `src/figure.ts` (`mountFigure`), `src/filters.ts` and `src/kernel.ts`.
|
|
13
|
+
- The agent skill in `skills/iso-engine/` adapts the iso-figure skill's method and rules and iso-glow's lighting
|
|
14
|
+
guidance.
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
MIT License
|
|
18
|
+
|
|
19
|
+
Copyright (c) 2026 April Zhu (iso-glow: lighting, palettes, gallery shell, examples)
|
|
20
|
+
Copyright (c) 2026 Tolga Cohce (iso-figure, on which iso-glow is based)
|
|
21
|
+
|
|
22
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
23
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
24
|
+
in the Software without restriction, including without limitation the rights
|
|
25
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
26
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
27
|
+
furnished to do so, subject to the following conditions:
|
|
28
|
+
|
|
29
|
+
The above copyright notice and this permission notice shall be included in all
|
|
30
|
+
copies or substantial portions of the Software.
|
|
31
|
+
|
|
32
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
33
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
34
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
35
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
36
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
37
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
38
|
+
SOFTWARE.
|
|
39
|
+
```
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/* Animation: easing, timelines and tweens.
|
|
2
|
+
Timelines are deterministic functions of scene time (seekable, exportable frame by frame).
|
|
3
|
+
Reactive tweens (node.to) start from the current value at the moment they are created. */
|
|
4
|
+
|
|
5
|
+
/* ── easing ── */
|
|
6
|
+
function bezier(x1, y1, x2, y2) {
|
|
7
|
+
const cx = 3 * x1, bx = 3 * (x2 - x1) - cx, ax = 1 - cx - bx;
|
|
8
|
+
const cy = 3 * y1, by = 3 * (y2 - y1) - cy, ay = 1 - cy - by;
|
|
9
|
+
const sx = (t) => ((ax * t + bx) * t + cx) * t;
|
|
10
|
+
const sy = (t) => ((ay * t + by) * t + cy) * t;
|
|
11
|
+
const dx = (t) => (3 * ax * t + 2 * bx) * t + cx;
|
|
12
|
+
return (x) => {
|
|
13
|
+
if (x <= 0) return 0; if (x >= 1) return 1;
|
|
14
|
+
let t = x;
|
|
15
|
+
for (let i = 0; i < 8; i++) { const e = sx(t) - x; if (Math.abs(e) < 1e-5) break; const d = dx(t); if (Math.abs(d) < 1e-6) break; t -= e / d; }
|
|
16
|
+
let lo = 0, hi = 1;
|
|
17
|
+
if (Math.abs(sx(t) - x) > 1e-4) { t = x; for (let i = 0; i < 30; i++) { const v = sx(t); if (Math.abs(v - x) < 1e-5) break; if (v < x) lo = t; else hi = t; t = (lo + hi) / 2; } }
|
|
18
|
+
return sy(t);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const outBounce = (t) => {
|
|
22
|
+
const n = 7.5625, d = 2.75;
|
|
23
|
+
if (t < 1 / d) return n * t * t;
|
|
24
|
+
if (t < 2 / d) return n * (t -= 1.5 / d) * t + 0.75;
|
|
25
|
+
if (t < 2.5 / d) return n * (t -= 2.25 / d) * t + 0.9375;
|
|
26
|
+
return n * (t -= 2.625 / d) * t + 0.984375;
|
|
27
|
+
};
|
|
28
|
+
const c1 = 1.70158, c3 = c1 + 1, c2 = c1 * 1.525;
|
|
29
|
+
export const ease = {
|
|
30
|
+
linear: (t) => t,
|
|
31
|
+
inQuad: (t) => t * t, outQuad: (t) => 1 - (1 - t) * (1 - t), inOutQuad: (t) => (t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2),
|
|
32
|
+
inCubic: (t) => t * t * t, outCubic: (t) => 1 - Math.pow(1 - t, 3), inOutCubic: (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2),
|
|
33
|
+
inQuart: (t) => t ** 4, outQuart: (t) => 1 - Math.pow(1 - t, 4), inOutQuart: (t) => (t < 0.5 ? 8 * t ** 4 : 1 - Math.pow(-2 * t + 2, 4) / 2),
|
|
34
|
+
inExpo: (t) => (t === 0 ? 0 : Math.pow(2, 10 * t - 10)), outExpo: (t) => (t === 1 ? 1 : 1 - Math.pow(2, -10 * t)),
|
|
35
|
+
inOutExpo: (t) => (t === 0 ? 0 : t === 1 ? 1 : t < 0.5 ? Math.pow(2, 20 * t - 10) / 2 : (2 - Math.pow(2, -20 * t + 10)) / 2),
|
|
36
|
+
inSine: (t) => 1 - Math.cos((t * Math.PI) / 2), outSine: (t) => Math.sin((t * Math.PI) / 2), inOutSine: (t) => -(Math.cos(Math.PI * t) - 1) / 2,
|
|
37
|
+
inBack: (t) => c3 * t * t * t - c1 * t * t, outBack: (t) => 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2),
|
|
38
|
+
inOutBack: (t) => (t < 0.5 ? (Math.pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2)) / 2 : (Math.pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2),
|
|
39
|
+
outElastic: (t) => (t === 0 ? 0 : t === 1 ? 1 : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * ((2 * Math.PI) / 3)) + 1),
|
|
40
|
+
outBounce,
|
|
41
|
+
/* design-system curves */
|
|
42
|
+
out: bezier(0.16, 1, 0.3, 1),
|
|
43
|
+
standard: bezier(0.2, 0, 0, 1),
|
|
44
|
+
smooth: bezier(0.45, 0, 0.2, 1),
|
|
45
|
+
bezier,
|
|
46
|
+
steps: (n) => (t) => Math.min(1, Math.floor(t * n) / n),
|
|
47
|
+
/* critically-damped-ish spring settling at 1 */
|
|
48
|
+
spring: (k = 1) => (t) => 1 - Math.exp(-6 * t * k) * Math.cos(9 * t * k),
|
|
49
|
+
};
|
|
50
|
+
export function getEase(e) {
|
|
51
|
+
if (typeof e === 'function') return e;
|
|
52
|
+
if (Array.isArray(e)) return bezier(...e);
|
|
53
|
+
return ease[e] || ease.inOutCubic;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* ── values ── */
|
|
57
|
+
const lerpV = (a, b, t) => (Array.isArray(a) ? a.map((v, i) => v + (b[i] - v) * t) : typeof a === 'number' ? a + (b - a) * t : t < 1 ? a : b);
|
|
58
|
+
const clone = (v) => (Array.isArray(v) ? v.slice() : v);
|
|
59
|
+
|
|
60
|
+
/* ── Timeline ── */
|
|
61
|
+
export class Timeline {
|
|
62
|
+
constructor(scene, opt = {}) {
|
|
63
|
+
this.scene = scene;
|
|
64
|
+
this.repeat = opt.repeat ?? 0; // −1 = forever
|
|
65
|
+
this.yoyo = !!opt.yoyo;
|
|
66
|
+
this.repeatDelay = opt.repeatDelay || 0;
|
|
67
|
+
this.delay = opt.delay || 0;
|
|
68
|
+
this.start = opt.start ?? (scene ? scene.time : 0);
|
|
69
|
+
this.tracks = new Map(); // key `${uid}:${prop}` → {node, prop, base, entries:[]}
|
|
70
|
+
this.calls = [];
|
|
71
|
+
this.cursor = 0;
|
|
72
|
+
this.prevStart = 0;
|
|
73
|
+
this.duration = 0;
|
|
74
|
+
this.lastT = null;
|
|
75
|
+
this.iteration = -1;
|
|
76
|
+
this.done = false;
|
|
77
|
+
this.paused = false;
|
|
78
|
+
this.reactive = !!opt.reactive;
|
|
79
|
+
this.onComplete = opt.onComplete;
|
|
80
|
+
this.label = opt.label;
|
|
81
|
+
}
|
|
82
|
+
_pos(position, d) {
|
|
83
|
+
if (position === undefined || position === null) return this.cursor;
|
|
84
|
+
if (typeof position === 'number') return position;
|
|
85
|
+
if (position === '<') return this.prevStart;
|
|
86
|
+
if (position === '>') return this.cursor;
|
|
87
|
+
const m = /^([<>]?)([+-]=)?(-?[\d.]+)$/.exec(position);
|
|
88
|
+
if (m) {
|
|
89
|
+
const base = m[1] === '<' ? this.prevStart : this.cursor;
|
|
90
|
+
const n = parseFloat(m[3]);
|
|
91
|
+
if (m[2] === '+=') return base + n;
|
|
92
|
+
if (m[2] === '-=') return base - n;
|
|
93
|
+
return m[1] ? base + n : n;
|
|
94
|
+
}
|
|
95
|
+
return this.cursor;
|
|
96
|
+
}
|
|
97
|
+
_add(target, props, opt, position, mode) {
|
|
98
|
+
const nodes = this.scene ? this.scene.resolve(target) : [].concat(target);
|
|
99
|
+
const dur = opt.duration ?? 0.6, e = getEase(opt.ease ?? 'inOutCubic');
|
|
100
|
+
const t0 = this._pos(position, dur);
|
|
101
|
+
const st = staggerFn(opt.stagger, nodes, this.scene);
|
|
102
|
+
let end = t0;
|
|
103
|
+
nodes.forEach((node, i) => {
|
|
104
|
+
const s = t0 + st(i);
|
|
105
|
+
for (const prop in props) {
|
|
106
|
+
if (prop === 'duration' || prop === 'ease') continue;
|
|
107
|
+
const key = node.uid + ':' + prop;
|
|
108
|
+
let tr = this.tracks.get(key);
|
|
109
|
+
if (!tr) this.tracks.set(key, (tr = { node, prop, base: clone(node.get(prop)), entries: [] }));
|
|
110
|
+
const prev = tr.entries[tr.entries.length - 1];
|
|
111
|
+
const cur = prev ? prev.to : tr.base;
|
|
112
|
+
let from, to;
|
|
113
|
+
if (mode === 'from') { from = props[prop]; to = cur; }
|
|
114
|
+
else if (mode === 'fromTo') { from = opt.from[prop]; to = props[prop]; }
|
|
115
|
+
else { from = cur; to = props[prop]; }
|
|
116
|
+
if (typeof to === 'string' && /^[+-]=/.test(to)) to = cur + parseFloat(to.replace('=', ''));
|
|
117
|
+
if (typeof to === 'function') to = to(i, node);
|
|
118
|
+
if (typeof from === 'function') from = from(i, node);
|
|
119
|
+
tr.entries.push({ start: s, dur: mode === 'set' ? 0 : dur, from: clone(from), to: clone(to), ease: e, explicit: mode === 'from' || mode === 'fromTo' });
|
|
120
|
+
tr.entries.sort((a, b) => a.start - b.start);
|
|
121
|
+
}
|
|
122
|
+
end = Math.max(end, s + (mode === 'set' ? 0 : dur));
|
|
123
|
+
});
|
|
124
|
+
this.prevStart = t0;
|
|
125
|
+
this.cursor = Math.max(this.cursor, end);
|
|
126
|
+
this.duration = Math.max(this.duration, end);
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
to(target, props, opt = {}, position) { return this._add(target, props, opt, position, 'to'); }
|
|
130
|
+
from(target, props, opt = {}, position) { return this._add(target, props, opt, position, 'from'); }
|
|
131
|
+
fromTo(target, from, to, opt = {}, position) { return this._add(target, to, { ...opt, from }, position, 'fromTo'); }
|
|
132
|
+
set(target, props, position) { return this._add(target, props, { duration: 0 }, position, 'set'); }
|
|
133
|
+
call(fn, position) { const t = this._pos(position); this.calls.push({ t, fn }); this.calls.sort((a, b) => a.t - b.t); this.duration = Math.max(this.duration, t); this.cursor = Math.max(this.cursor, t); return this; }
|
|
134
|
+
wait(d) { this.cursor += d; this.duration = Math.max(this.duration, this.cursor); return this; }
|
|
135
|
+
/* nest another timeline's tracks (built with a fresh Timeline) at a position */
|
|
136
|
+
add(tl, position) {
|
|
137
|
+
const off = this._pos(position);
|
|
138
|
+
for (const [k, tr] of tl.tracks) {
|
|
139
|
+
let mine = this.tracks.get(k);
|
|
140
|
+
if (!mine) this.tracks.set(k, (mine = { node: tr.node, prop: tr.prop, base: tr.base, entries: [] }));
|
|
141
|
+
for (const e of tr.entries) mine.entries.push({ ...e, start: e.start + off });
|
|
142
|
+
mine.entries.sort((a, b) => a.start - b.start);
|
|
143
|
+
}
|
|
144
|
+
for (const c of tl.calls) this.calls.push({ t: c.t + off, fn: c.fn });
|
|
145
|
+
this.calls.sort((a, b) => a.t - b.t);
|
|
146
|
+
this.prevStart = off; this.cursor = Math.max(this.cursor, off + tl.duration); this.duration = Math.max(this.duration, off + tl.duration);
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
get total() { return this.repeat < 0 ? Infinity : this.delay + this.duration * (this.repeat + 1) + this.repeatDelay * this.repeat; }
|
|
150
|
+
|
|
151
|
+
/* local time τ within the current iteration, or null before start */
|
|
152
|
+
local(time) {
|
|
153
|
+
let t = time - this.start - this.delay;
|
|
154
|
+
if (t < 0) return { tau: 0, before: true, iter: 0 };
|
|
155
|
+
const cycle = this.duration + this.repeatDelay;
|
|
156
|
+
let iter = 0;
|
|
157
|
+
if (this.repeat !== 0 && cycle > 0) {
|
|
158
|
+
iter = Math.floor(t / cycle);
|
|
159
|
+
if (this.repeat > 0 && iter > this.repeat) return { tau: this.yoyo && this.repeat % 2 === 1 ? 0 : this.duration, end: true, iter: this.repeat };
|
|
160
|
+
t -= iter * cycle;
|
|
161
|
+
if (t > this.duration) t = this.duration;
|
|
162
|
+
} else if (t > this.duration) return { tau: this.duration, end: true, iter: 0 };
|
|
163
|
+
if (this.yoyo && iter % 2 === 1) t = this.duration - t;
|
|
164
|
+
return { tau: t, iter };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
apply(time) {
|
|
168
|
+
if (this.paused || this.done) return;
|
|
169
|
+
const L = this.local(time);
|
|
170
|
+
for (const tr of this.tracks.values()) {
|
|
171
|
+
const es = tr.entries;
|
|
172
|
+
let v;
|
|
173
|
+
if (L.before) {
|
|
174
|
+
if (!es[0].explicit) continue;
|
|
175
|
+
v = es[0].from;
|
|
176
|
+
} else {
|
|
177
|
+
let e = null;
|
|
178
|
+
for (let i = es.length - 1; i >= 0; i--) if (es[i].start <= L.tau + 1e-9) { e = es[i]; break; }
|
|
179
|
+
if (!e) { if (!es[0].explicit) { v = tr.base; } else v = es[0].from; }
|
|
180
|
+
else if (e.dur <= 0 || L.tau >= e.start + e.dur) v = e.to;
|
|
181
|
+
else v = lerpV(e.from, e.to, e.ease((L.tau - e.start) / e.dur));
|
|
182
|
+
}
|
|
183
|
+
tr.node.set(tr.prop, clone(v));
|
|
184
|
+
}
|
|
185
|
+
this._fire(L);
|
|
186
|
+
if (L.end) { this.done = true; if (this.onComplete) this.onComplete(this); if (this._resolve) this._resolve(this); }
|
|
187
|
+
}
|
|
188
|
+
_fire(L) {
|
|
189
|
+
if (!this.calls.length || L.before) { this.lastT = null; return; }
|
|
190
|
+
const prevIter = this.iteration, prevT = this.lastT;
|
|
191
|
+
const fireRange = (a, b) => { for (const c of this.calls) if (c.t > a && c.t <= b + 1e-9) c.fn(this.scene, this); };
|
|
192
|
+
if (prevT === null) fireRange(-1, L.tau);
|
|
193
|
+
else if (L.iter !== prevIter) { fireRange(prevT, this.duration); for (let k = prevIter + 1; k < L.iter; k++) fireRange(-1, this.duration); fireRange(-1, L.tau); }
|
|
194
|
+
else if (L.tau >= prevT) fireRange(prevT, L.tau);
|
|
195
|
+
this.lastT = L.tau; this.iteration = L.iter;
|
|
196
|
+
}
|
|
197
|
+
/* promise-like */
|
|
198
|
+
then(res, rej) {
|
|
199
|
+
if (this.done) return Promise.resolve(this).then(res, rej);
|
|
200
|
+
return new Promise((r) => { const prev = this._resolve; this._resolve = (x) => { if (prev) prev(x); r(x); }; }).then(res, rej);
|
|
201
|
+
}
|
|
202
|
+
kill() { this.done = true; if (this.scene) this.scene._removeTimeline(this); }
|
|
203
|
+
reset() { this.done = false; this.lastT = null; this.iteration = -1; }
|
|
204
|
+
play() { this.paused = false; return this; }
|
|
205
|
+
pause() { this.paused = true; return this; }
|
|
206
|
+
restart() { this.start = this.scene.time; this.reset(); return this; }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/* stagger: number (seconds per item) | {each, from: 'start'|'end'|'center'|'random'|'depth', amount} */
|
|
210
|
+
function staggerFn(st, nodes, scene) {
|
|
211
|
+
if (!st) return () => 0;
|
|
212
|
+
const o = typeof st === 'number' ? { each: st } : st;
|
|
213
|
+
const n = nodes.length;
|
|
214
|
+
const each = o.amount !== undefined ? o.amount / Math.max(1, n - 1) : o.each || 0;
|
|
215
|
+
let rank = nodes.map((_, i) => i);
|
|
216
|
+
if (o.from === 'end') rank = rank.map((i) => n - 1 - i);
|
|
217
|
+
else if (o.from === 'center') rank = rank.map((i) => Math.abs(i - (n - 1) / 2));
|
|
218
|
+
else if (o.from === 'random') { let s = o.seed || 7; rank = rank.map(() => ((s = (s * 16807) % 2147483647) / 2147483647) * (n - 1)); }
|
|
219
|
+
else if (o.from === 'depth' || o.from === 'front') {
|
|
220
|
+
const key = (nd) => { const b = nd.sortBox ? nd.sortBox() : null; return b ? (b[0] + b[3]) / 2 + (b[1] + b[4]) / 2 + b[2] : 0; };
|
|
221
|
+
const sorted = nodes.map((nd, i) => [key(nd), i]).sort((a, b) => (o.from === 'front' ? b[0] - a[0] : a[0] - b[0]));
|
|
222
|
+
sorted.forEach(([, i], r) => { rank[i] = r; });
|
|
223
|
+
}
|
|
224
|
+
return (i) => rank[i] * each;
|
|
225
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/* Motion presets. Each one is a deterministic function of scene time, so scenes can be
|
|
2
|
+
seeked, paused and exported frame by frame. All return a handle with stop(). */
|
|
3
|
+
import { Scene } from './scene.js';
|
|
4
|
+
|
|
5
|
+
const TAU = Math.PI * 2;
|
|
6
|
+
|
|
7
|
+
function anchorOf(scene, n) {
|
|
8
|
+
if (!n.rest) scene.measure();
|
|
9
|
+
const b = n.rest;
|
|
10
|
+
if (!b) return [0, 0, 0];
|
|
11
|
+
return [(b[0] + b[3]) / 2, (b[1] + b[4]) / 2, b[2]];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Object.assign(Scene.prototype, {
|
|
15
|
+
/* gentle vertical bob (additive) */
|
|
16
|
+
float(target, o = {}) {
|
|
17
|
+
const nodes = this.resolve(target), amp = o.amp ?? 3, period = o.period ?? 3, phase = o.phase ?? 0, st = o.stagger ?? 0.17;
|
|
18
|
+
return this.loop((t) => nodes.forEach((n, i) => this.nudge(n, 'z', amp * Math.sin(TAU * (t / period + phase + i * st)))));
|
|
19
|
+
},
|
|
20
|
+
/* opacity breathing between min and max (multiplicative) */
|
|
21
|
+
pulse(target, o = {}) {
|
|
22
|
+
const nodes = this.resolve(target), min = o.min ?? 0.35, max = o.max ?? 1, period = o.period ?? 2, st = o.stagger ?? 0;
|
|
23
|
+
return this.loop((t) => nodes.forEach((n, i) => this.nudge(n, 'opacity', min + (max - min) * (0.5 + 0.5 * Math.sin(TAU * (t / period + i * st))))));
|
|
24
|
+
},
|
|
25
|
+
/* on/off (LEDs, cursors) */
|
|
26
|
+
blink(target, o = {}) {
|
|
27
|
+
const nodes = this.resolve(target), period = o.period ?? 1, duty = o.duty ?? 0.5, st = o.stagger ?? 0, low = o.low ?? 0;
|
|
28
|
+
return this.loop((t) => nodes.forEach((n, i) => {
|
|
29
|
+
const ph = (((t / period + (o.phase || 0) + i * st) % 1) + 1) % 1;
|
|
30
|
+
if (ph >= duty) this.nudge(n, 'opacity', low);
|
|
31
|
+
}));
|
|
32
|
+
},
|
|
33
|
+
/* rotate around the vertical axis (rebuilds geometry each frame: keep spinning objects small) */
|
|
34
|
+
spin(target, o = {}) {
|
|
35
|
+
const nodes = this.resolve(target), speed = o.speed ?? 30;
|
|
36
|
+
const base = nodes.map((n) => n.opts.rot || 0);
|
|
37
|
+
return this.loop((t) => nodes.forEach((n, i) => n.set('rot', base[i] + speed * t)));
|
|
38
|
+
},
|
|
39
|
+
/* move along a world path. o: duration | speed (units/s), offset (0–1), ease, yoyo, orient, spread (offsets across nodes), lift */
|
|
40
|
+
travel(target, path, o = {}) {
|
|
41
|
+
const nodes = this.resolve(target);
|
|
42
|
+
const dur = o.duration ?? (o.speed ? path.length / o.speed : 4);
|
|
43
|
+
const start = o.start ?? this.time, spread = o.spread ?? (nodes.length > 1 ? 1 / nodes.length : 0);
|
|
44
|
+
const ease = o.ease ? (typeof o.ease === 'function' ? o.ease : null) : null;
|
|
45
|
+
let anchors = null;
|
|
46
|
+
const rot0 = nodes.map((n) => n.opts.rot || 0);
|
|
47
|
+
const lift = o.lift || 0;
|
|
48
|
+
return this.loop((t) => (anchors || (anchors = nodes.map((n) => anchorOf(this, n)))) && nodes.forEach((n, i) => {
|
|
49
|
+
let u = (t - start) / dur + (o.offset || 0) + i * spread;
|
|
50
|
+
if (o.repeat === 0 || o.once) u = Math.min(1, Math.max(0, u));
|
|
51
|
+
if (o.yoyo) { const k = ((u % 2) + 2) % 2; u = k > 1 ? 2 - k : k; }
|
|
52
|
+
else if (path.closed || !(o.repeat === 0 || o.once)) u = ((u % 1) + 1) % 1;
|
|
53
|
+
if (ease) u = ease(u);
|
|
54
|
+
const p = path.at(u), a = anchors[i];
|
|
55
|
+
n.set({ x: p[0] - a[0], y: p[1] - a[1], z: p[2] - a[2] + lift });
|
|
56
|
+
if (o.orient) n.set('rot', rot0[i] + path.heading(u) + (o.orientOffset || 0));
|
|
57
|
+
}));
|
|
58
|
+
},
|
|
59
|
+
orbit(target, o = {}) {
|
|
60
|
+
const P = this.constructor.path;
|
|
61
|
+
return this.travel(target, P.circle({ center: o.center || [0, 0, 0], r: o.r || 50, start: o.start || 0, ccw: o.ccw }), o);
|
|
62
|
+
},
|
|
63
|
+
/* entrance: drop in from above and fade, staggered back-to-front */
|
|
64
|
+
assemble(target, o = {}) {
|
|
65
|
+
let nodes = target ? this.resolve(target) : this.root.items();
|
|
66
|
+
if (o.filter) nodes = nodes.filter(o.filter);
|
|
67
|
+
if (!this.measured) this.measure();
|
|
68
|
+
const tl = this.timeline({ delay: o.delay ?? 0.15 });
|
|
69
|
+
tl.from(nodes, { z: o.drop ?? 36, opacity: 0 }, { duration: o.duration ?? 0.8, ease: o.ease ?? 'out', stagger: { each: o.stagger ?? 0.05, from: o.from ?? 'depth' } }, 0);
|
|
70
|
+
return tl;
|
|
71
|
+
},
|
|
72
|
+
/* stroke draw-on for lines/connectors */
|
|
73
|
+
drawOn(target, o = {}) {
|
|
74
|
+
const nodes = this.resolve(target);
|
|
75
|
+
const tl = this.timeline({ delay: o.delay ?? 0 });
|
|
76
|
+
const dur = o.duration ?? 1, stagger = o.stagger ?? 0.1;
|
|
77
|
+
tl.fromTo(nodes, { reveal: 0 }, { reveal: 1 }, { duration: dur, ease: o.ease ?? 'inOutCubic', stagger }, 0);
|
|
78
|
+
/* a diagram edge's packets and label appear as its line arrives, not before */
|
|
79
|
+
const each = typeof stagger === 'number' ? stagger : stagger.each ?? 0;
|
|
80
|
+
nodes.forEach((n, i) => {
|
|
81
|
+
if (n.attached && n.attached.length) tl.fromTo(n.attached, { opacity: 0 }, { opacity: 1 }, { duration: 0.3 }, i * each + dur * 0.8);
|
|
82
|
+
});
|
|
83
|
+
return tl;
|
|
84
|
+
},
|
|
85
|
+
/* reveal a Text node character by character */
|
|
86
|
+
typewriter(target, o = {}) {
|
|
87
|
+
const nodes = this.resolve(target);
|
|
88
|
+
const tl = this.timeline({ delay: o.delay ?? 0 });
|
|
89
|
+
nodes.forEach((n) => {
|
|
90
|
+
const len = String(n.opts.text || '').length;
|
|
91
|
+
tl.fromTo(n, { chars: 0 }, { chars: len }, { duration: o.duration ?? len * (o.perChar ?? 0.05), ease: 'linear' }, o.stagger ? undefined : 0);
|
|
92
|
+
});
|
|
93
|
+
return tl;
|
|
94
|
+
},
|
|
95
|
+
/* hover: raise the object a little (reactive) */
|
|
96
|
+
hoverLift(target, o = {}) {
|
|
97
|
+
const nodes = this.resolve(target), z = o.z ?? 4, d = o.duration ?? 0.22;
|
|
98
|
+
nodes.forEach((n) => {
|
|
99
|
+
n.on('hover', () => this.to(n, { z }, { duration: d }));
|
|
100
|
+
n.on('leave', () => this.to(n, { z: 0 }, { duration: d }));
|
|
101
|
+
if (!n.handlers.click) n.opts.interactive = n.opts.interactive ?? false;
|
|
102
|
+
});
|
|
103
|
+
return { stop() {} };
|
|
104
|
+
},
|
|
105
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/* Diagrams: nodes on a grid, orthogonal edges with flowing dashes and travelling packets, zones.
|
|
2
|
+
s.diagram({ grid: 80, nodes: [{id, cell:[i,j], type:'block', icon, label}], edges: [{from, to, packets, label}], zones: [{from:[i,j], to:[i,j], label}] }) */
|
|
3
|
+
import { Group } from './node.js';
|
|
4
|
+
import { Line, Text, routePoints } from './shapes.js';
|
|
5
|
+
import { Path } from './paths.js';
|
|
6
|
+
|
|
7
|
+
export function buildDiagram(o, make) {
|
|
8
|
+
const grid = o.grid ?? 80, gz = o.z ?? 0, origin = o.origin || [0, 0];
|
|
9
|
+
const g = new Group({ id: o.id, flat: true });
|
|
10
|
+
const cellXY = (c) => [origin[0] + c[0] * grid, origin[1] + c[1] * grid];
|
|
11
|
+
const nodes = new Map();
|
|
12
|
+
/* zones first (they lie on the floor) */
|
|
13
|
+
for (const z of o.zones || []) {
|
|
14
|
+
const a = cellXY(z.from), b = cellXY(z.to), pad = z.pad ?? grid * 0.42;
|
|
15
|
+
const x0 = Math.min(a[0], b[0]) - pad, x1 = Math.max(a[0], b[0]) + pad, y0 = Math.min(a[1], b[1]) - pad, y1 = Math.max(a[1], b[1]) + pad;
|
|
16
|
+
g.add(make('zone', { id: z.id, at: [(x0 + x1) / 2, (y0 + y1) / 2, gz], size: [x1 - x0, y1 - y0], label: z.label, r: z.r ?? 10, fill: z.fill, dash: z.dash }));
|
|
17
|
+
}
|
|
18
|
+
for (const n of o.nodes || []) {
|
|
19
|
+
const [x, y] = n.at ? n.at : cellXY(n.cell || [0, 0]);
|
|
20
|
+
const type = n.type || 'block';
|
|
21
|
+
const size = n.size || (type === 'block' ? [o.nodeSize ?? 36, o.nodeSize ?? 36, n.h ?? 12] : undefined);
|
|
22
|
+
const opts = { ...n, at: [x, y, (n.at && n.at[2]) || gz], size };
|
|
23
|
+
delete opts.cell; delete opts.type;
|
|
24
|
+
const node = make(type, opts);
|
|
25
|
+
const h = size ? size[2] ?? 12 : type === 'database' ? (n.tiers ?? 3) * ((n.tierH ?? 8) + 1.6) : n.h ?? 24;
|
|
26
|
+
node.diagram = { x, y, half: (size ? Math.max(size[0], size[1]) : (n.r ? n.r * 2 : 36)) / 2, h };
|
|
27
|
+
nodes.set(n.id, node);
|
|
28
|
+
g.add(node);
|
|
29
|
+
}
|
|
30
|
+
const edges = [];
|
|
31
|
+
for (const e of o.edges || []) {
|
|
32
|
+
const A = nodes.get(e.from), B = nodes.get(e.to);
|
|
33
|
+
if (!A || !B) continue;
|
|
34
|
+
const a = [A.diagram.x, A.diagram.y], b = [B.diagram.x, B.diagram.y];
|
|
35
|
+
const route = e.route || o.route || 'auto';
|
|
36
|
+
const z = (e.z ?? gz) + 0.01;
|
|
37
|
+
/* first leg direction decides which side the edge leaves from */
|
|
38
|
+
let pts = routePoints(a, b, { route, z, radius: 0 });
|
|
39
|
+
const trim = (p, q, by) => { const dx = q[0] - p[0], dy = q[1] - p[1], L = Math.hypot(dx, dy) || 1; return [p[0] + (dx / L) * by, p[1] + (dy / L) * by, p[2]]; };
|
|
40
|
+
/* a ground point within `h` behind a node's back faces (−x, −y) is hidden by the node: trim further there */
|
|
41
|
+
const gap = e.gap ?? 4;
|
|
42
|
+
const dir = (p, q) => [Math.sign(Math.round(q[0] - p[0])), Math.sign(Math.round(q[1] - p[1]))];
|
|
43
|
+
const d0 = dir(pts[0], pts[1]), d1 = dir(pts[pts.length - 2], pts[pts.length - 1]);
|
|
44
|
+
const leavesBack = d0[0] < 0 || d0[1] < 0, entersBack = d1[0] > 0 || d1[1] > 0;
|
|
45
|
+
pts[0] = trim(pts[0], pts[1], A.diagram.half + gap + (leavesBack ? A.diagram.h * 0.9 : 0));
|
|
46
|
+
pts[pts.length - 1] = trim(pts[pts.length - 1], pts[pts.length - 2], B.diagram.half + gap + 2 + (entersBack ? B.diagram.h + 2 : 0));
|
|
47
|
+
const ptsR = routePoints(pts[0], pts[pts.length - 1], { route: pts.length > 2 ? pts.slice(1, -1).map((p) => [p[0], p[1]]) : 'straight', z, radius: e.radius ?? o.radius ?? 8 });
|
|
48
|
+
const line = new Line({ id: e.id, points: ptsR, arrow: e.arrow ?? 'end', line: e.line || 'ln', dash: e.dash, flow: e.flow === false ? undefined : e.flow ?? o.flow ?? 14, arrowSize: 4.5 });
|
|
49
|
+
g.add(line);
|
|
50
|
+
const path = new Path(ptsR);
|
|
51
|
+
/* things that belong to the edge: drawOn() fades them in once the line is drawn */
|
|
52
|
+
line.attached = [];
|
|
53
|
+
if (e.label) {
|
|
54
|
+
const mid = path.at(0.5);
|
|
55
|
+
const label = new Text({ text: e.label, at: mid, size: e.labelSize || 5.5, anchor: 'middle', dy: -5, cls: 'tx tx-label', layer: 1 });
|
|
56
|
+
g.add(label);
|
|
57
|
+
line.attached.push(label);
|
|
58
|
+
}
|
|
59
|
+
edges.push({ spec: e, line, path, from: A, to: B });
|
|
60
|
+
}
|
|
61
|
+
g.nodesById = nodes;
|
|
62
|
+
g.edges = edges;
|
|
63
|
+
/* packets travelling along edges */
|
|
64
|
+
g.onMount = (scene) => {
|
|
65
|
+
for (const ed of edges) {
|
|
66
|
+
const k = ed.spec.packets === true ? 1 : ed.spec.packets || 0;
|
|
67
|
+
if (!k) continue;
|
|
68
|
+
const cards = [];
|
|
69
|
+
for (let i = 0; i < k; i++) {
|
|
70
|
+
const p0 = ed.path.at(0);
|
|
71
|
+
const card = make(ed.spec.packet || 'card', { at: [p0[0], p0[1], p0[2]], w: 7, d: 5, t: 1.2, id: (ed.spec.id || ed.spec.from + '-' + ed.spec.to) + '-packet-' + i });
|
|
72
|
+
g.add(card);
|
|
73
|
+
cards.push(card);
|
|
74
|
+
ed.line.attached.push(card);
|
|
75
|
+
}
|
|
76
|
+
scene.travel(cards, ed.path, { duration: ed.spec.duration ?? ed.path.length / (ed.spec.speed ?? o.speed ?? 40), spread: 1 / k, lift: 0.4, offset: ed.spec.offset || 0 });
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
return g;
|
|
80
|
+
}
|