@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
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/* Iso — an isometric illustration and animation engine. SVG, zero dependencies. */
|
|
2
|
+
import * as math from './math.js';
|
|
3
|
+
import { Node, Group } from './node.js';
|
|
4
|
+
import { Box, Prism, Cylinder, TubeSegment, Sphere, Hull, Panel, Flat, Line, Ribbon, Helix, Text, Pane, Grid, Custom, Face, slabVertices, SLAB_FACES, routePoints, filletPolyline, fitText } from './shapes.js';
|
|
5
|
+
import { Scene, clock } from './scene.js';
|
|
6
|
+
import { Timeline, ease, getEase } from './anim.js';
|
|
7
|
+
import { Path, path } from './paths.js';
|
|
8
|
+
import { css, injectCSS, THEMES, MATERIALS } from './style.js';
|
|
9
|
+
import { figure } from './figure.js';
|
|
10
|
+
import { depthSort } from './sort.js';
|
|
11
|
+
import { installPrefabs, ICONS, MARK, icon, iconPath } from './prefabs/index.js';
|
|
12
|
+
import { buildDiagram } from './diagram.js';
|
|
13
|
+
import { renderSpec, pathFrom } from './spec.js';
|
|
14
|
+
import './behaviors.js';
|
|
15
|
+
import './export.js';
|
|
16
|
+
|
|
17
|
+
export const VERSION = '0.1.0';
|
|
18
|
+
export { fitText };
|
|
19
|
+
|
|
20
|
+
/* ── primitive factories ── */
|
|
21
|
+
function wedgeHull(o) {
|
|
22
|
+
const [x, y, z] = o.at || [0, 0, 0], [w, d, h] = o.size || [20, 20, 10], up = o.up || 'x';
|
|
23
|
+
/* canonical: rises toward +x in a unit cube, then mapped */
|
|
24
|
+
const unit = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [1, 0, 1], [1, 1, 1]];
|
|
25
|
+
const map = { x: (p) => p, '-x': (p) => [1 - p[0], p[1], p[2]], y: (p) => [p[1], p[0], p[2]], '-y': (p) => [p[1], 1 - p[0], p[2]] }[up] || ((p) => p);
|
|
26
|
+
const vertices = unit.map(map).map((p) => [x + p[0] * w, y + p[1] * d, z + p[2] * h]);
|
|
27
|
+
return new Hull({ ...o, at: [0, 0, 0], vertices, faces: [[0, 1, 2, 3], [1, 4, 5, 2], [0, 3, 5, 4], [0, 1, 4], [3, 2, 5]] });
|
|
28
|
+
}
|
|
29
|
+
function pyramidHull(o) {
|
|
30
|
+
const [cx, cy, z] = o.at || [0, 0, 0], [w, d] = o.size || [20, 20], h = o.h ?? 20;
|
|
31
|
+
const v = [[cx - w / 2, cy - d / 2, z], [cx + w / 2, cy - d / 2, z], [cx + w / 2, cy + d / 2, z], [cx - w / 2, cy + d / 2, z], [cx, cy, z + h]];
|
|
32
|
+
return new Hull({ ...o, at: [0, 0, 0], vertices: v, faces: [[0, 1, 2, 3], [0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4]] });
|
|
33
|
+
}
|
|
34
|
+
function roofHull(o) {
|
|
35
|
+
/* gable roof on a footprint; ridge along `axis` */
|
|
36
|
+
const [x, y, z] = o.at || [0, 0, 0], [w, d, h] = o.size || [40, 30, 14], ax = o.axis || 'x';
|
|
37
|
+
const v = ax === 'x'
|
|
38
|
+
? [[x, y, z], [x + w, y, z], [x + w, y + d, z], [x, y + d, z], [x, y + d / 2, z + h], [x + w, y + d / 2, z + h]]
|
|
39
|
+
: [[x, y, z], [x + w, y, z], [x + w, y + d, z], [x, y + d, z], [x + w / 2, y, z + h], [x + w / 2, y + d, z + h]];
|
|
40
|
+
const f = ax === 'x' ? [[0, 1, 2, 3], [0, 1, 5, 4], [3, 2, 5, 4], [0, 3, 4], [1, 2, 5]] : [[0, 1, 2, 3], [0, 3, 5, 4], [1, 2, 5, 4], [0, 1, 4], [3, 2, 5]];
|
|
41
|
+
return new Hull({ ...o, at: [0, 0, 0], vertices: v, faces: f });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const shapes = {
|
|
45
|
+
box: (o) => new Box(o),
|
|
46
|
+
prism: (o) => new Prism(o),
|
|
47
|
+
cylinder: (o) => new Cylinder(o),
|
|
48
|
+
cone: (o) => new Cylinder({ rTop: 0, ...o }),
|
|
49
|
+
sphere: (o) => new Sphere(o),
|
|
50
|
+
hull: (o) => new Hull(o),
|
|
51
|
+
panel: (o) => new Panel(o),
|
|
52
|
+
slab: (o) => new Hull({ ...o, at: [0, 0, 0], vertices: slabVertices(o.origin || o.at || [0, 0, 0], o.u, o.v, o.w), faces: SLAB_FACES }),
|
|
53
|
+
wedge: wedgeHull,
|
|
54
|
+
pyramid: pyramidHull,
|
|
55
|
+
roof: roofHull,
|
|
56
|
+
rect: (o) => new Flat({ ...o, shape: 'rect' }),
|
|
57
|
+
circle: (o) => new Flat({ ...o, shape: 'circle' }),
|
|
58
|
+
ring: (o) => new Flat({ ...o, shape: 'ring' }),
|
|
59
|
+
poly: (o) => new Flat({ ...o, shape: 'poly' }),
|
|
60
|
+
line: (o) => new Line(o),
|
|
61
|
+
connector: (o) => new Line({ arrow: 'end', ...o, points: routePoints(o.from, o.to, o) }),
|
|
62
|
+
ribbon: (o) => new Ribbon(o),
|
|
63
|
+
helix: (o) => new Helix(o),
|
|
64
|
+
text: (o) => new Text(o),
|
|
65
|
+
pane: (o) => new Pane(o),
|
|
66
|
+
grid: (o) => new Grid(o),
|
|
67
|
+
custom: (o) => new Custom(o),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/* Factories that build small groups. */
|
|
71
|
+
const builders = {
|
|
72
|
+
/* hollow cylinder split into angular segments (ring buildings, rims, tracks) */
|
|
73
|
+
tube(o) {
|
|
74
|
+
const n = o.segments || 16, g = new Group({ id: o.id, flat: true, class: o.class, material: o.material });
|
|
75
|
+
const [cx, cy, z] = o.at || [0, 0, 0];
|
|
76
|
+
for (let i = 0; i < n; i++) {
|
|
77
|
+
const seg = new TubeSegment({ ...o, id: o.id ? o.id + '-' + i : undefined, at: [cx, cy, z], r: o.r ?? 40, ri: o.ri ?? (o.r ?? 40) * 0.7, h: o.h ?? 10, a0: (360 * i) / n, a1: (360 * (i + 1)) / n });
|
|
78
|
+
g.add(seg);
|
|
79
|
+
}
|
|
80
|
+
return g;
|
|
81
|
+
},
|
|
82
|
+
/* screen-facing label with a leader line from an anchor point */
|
|
83
|
+
label(o) {
|
|
84
|
+
const [x, y, z] = o.at || [0, 0, 0], lift = o.lift ?? 28, g = new Group({ id: o.id, layer: o.layer ?? 1, class: o.class, sort: 'none' });
|
|
85
|
+
if (lift) g.add(new Line({ points: [[x, y, z], [x, y, z + lift]], line: o.line || 'ln-soft' }));
|
|
86
|
+
if (o.dot !== false) g.add(new Flat({ shape: 'circle', at: [x, y, z], r: o.dotSize || 1.6, fill: 'f-ink', line: null }));
|
|
87
|
+
g.add(new Text({ text: o.text, at: [x, y, z + lift], size: o.size || 7, anchor: o.anchor || 'middle', dy: o.dy ?? -5, cls: o.cls || 'tx tx-label', spacing: o.spacing }));
|
|
88
|
+
return g;
|
|
89
|
+
},
|
|
90
|
+
diagram: (o) => buildDiagram(o, make),
|
|
91
|
+
};
|
|
92
|
+
/* make any object by type name: primitive, builder or prefab */
|
|
93
|
+
export function make(type, opts = {}) {
|
|
94
|
+
if (shapes[type]) return shapes[type](opts);
|
|
95
|
+
if (builders[type]) return builders[type](opts);
|
|
96
|
+
return makePrefab(type, opts);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* ── prefabs ── */
|
|
100
|
+
export const prefabs = {};
|
|
101
|
+
/* define(name, build(g, opts, Iso)) — build populates the group in local coordinates
|
|
102
|
+
(origin = footprint centre at the base); `at` and `rot` place it. */
|
|
103
|
+
export function define(name, build, meta = {}) {
|
|
104
|
+
prefabs[name] = { build, meta };
|
|
105
|
+
Group.prototype[name] = function (opts = {}) { return this.add(makePrefab(name, opts)); };
|
|
106
|
+
if (!Scene.prototype[name]) Scene.prototype[name] = function (opts) { return this.root[name](opts); };
|
|
107
|
+
return build;
|
|
108
|
+
}
|
|
109
|
+
export function makePrefab(name, opts = {}) {
|
|
110
|
+
const p = prefabs[name];
|
|
111
|
+
if (!p) throw new Error('Iso: unknown prefab "' + name + '"');
|
|
112
|
+
const rot = (opts.rot || 0) + ({ right: -90, back: 180, 'back-right': 180, 'back-left': 90 }[opts.facing] || 0);
|
|
113
|
+
const g = new Group({ ...opts, rot, sort: opts.sort || p.meta.sort || 'auto', flat: opts.flat ?? p.meta.flat });
|
|
114
|
+
g.prefab = name;
|
|
115
|
+
const api = p.build(g, opts, Iso);
|
|
116
|
+
if (api && typeof api === 'object') Object.assign(g, api);
|
|
117
|
+
return g;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
for (const k in shapes) {
|
|
121
|
+
Group.prototype[k] = function (o = {}) { return this.add(shapes[k](o)); };
|
|
122
|
+
Scene.prototype[k] = function (o) { return this.root[k](o); };
|
|
123
|
+
}
|
|
124
|
+
for (const k in builders) {
|
|
125
|
+
Group.prototype[k] = function (o = {}) { return this.add(builders[k](o)); };
|
|
126
|
+
Scene.prototype[k] = function (o) { return this.root[k](o); };
|
|
127
|
+
}
|
|
128
|
+
Group.prototype.group = function (opts = {}, fn) {
|
|
129
|
+
if (typeof opts === 'function') { fn = opts; opts = {}; }
|
|
130
|
+
const g = new Group(opts);
|
|
131
|
+
if (fn) fn(g);
|
|
132
|
+
return this.add(g);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/* Iso.scene(host, opts): host may be a selector or element. With opts.figure, the scene is
|
|
136
|
+
framed with captions and scene.status(text) updates the state caption. */
|
|
137
|
+
function scene(host, opts = {}) {
|
|
138
|
+
if (opts.figure && host) {
|
|
139
|
+
const f = figure(host, { theme: opts.theme, ...opts.figure });
|
|
140
|
+
if (opts.colors && opts.colors.bg) f.el.style.setProperty('--iso-fig-bg', 'color-mix(in oklab, ' + opts.colors.bg + ' 92%, ' + (opts.colors.fg || '#fff') + ')');
|
|
141
|
+
if (opts.colors && opts.colors.fg) { f.el.style.setProperty('--iso-fig-cap', 'color-mix(in oklab, ' + opts.colors.fg + ' 45%, transparent)'); f.el.style.setProperty('--iso-fig-cap-strong', 'color-mix(in oklab, ' + opts.colors.fg + ' 70%, transparent)'); }
|
|
142
|
+
const s = new Scene(f.stage, opts);
|
|
143
|
+
s.figure = f;
|
|
144
|
+
s.status = (t) => { f.status(t); return s; };
|
|
145
|
+
return s;
|
|
146
|
+
}
|
|
147
|
+
const s = new Scene(host, opts);
|
|
148
|
+
s.status = () => s;
|
|
149
|
+
return s;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export const Iso = {
|
|
153
|
+
VERSION, scene, Scene, Node, Group, Face, Timeline, Path, path, ease, getEase, clock,
|
|
154
|
+
shapes, prefabs, define, make, makePrefab, figure, pathFrom,
|
|
155
|
+
render: (spec, host) => renderSpec(Iso, spec, host), css, injectCSS, THEMES, MATERIALS, depthSort,
|
|
156
|
+
math, rng: math.rng, routePoints, filletPolyline, ICONS, MARK, icon, iconPath,
|
|
157
|
+
advance: (dt) => clock.advance(dt),
|
|
158
|
+
set manual(v) { clock.manual = !!v; },
|
|
159
|
+
get manual() { return clock.manual; },
|
|
160
|
+
scenes: () => Array.from(clock.scenes),
|
|
161
|
+
};
|
|
162
|
+
Scene.path = path;
|
|
163
|
+
installPrefabs(define, Iso);
|
|
164
|
+
export default Iso;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/* Isometric engine: math helpers.
|
|
2
|
+
World axes: x → screen right-down, y → screen left-down, z → up.
|
|
3
|
+
The viewer looks along (−1, −1, −2S): larger x, y or z is closer to the viewer. */
|
|
4
|
+
|
|
5
|
+
export const DEG = Math.PI / 180;
|
|
6
|
+
export const EPS = 1e-6;
|
|
7
|
+
|
|
8
|
+
export const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
|
|
9
|
+
export const lerp = (a, b, t) => a + (b - a) * t;
|
|
10
|
+
export const fmt = (n) => {
|
|
11
|
+
const v = Math.round(n * 100) / 100;
|
|
12
|
+
return Object.is(v, -0) ? '0' : String(v);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/* ── sanitising (scenes may be built from untrusted JSON) ── */
|
|
16
|
+
export const escAttr = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
17
|
+
/* class lists: letters, digits, _, - and spaces only */
|
|
18
|
+
export const cssClass = (s) => String(s ?? '').replace(/[^\w\- ]/g, '');
|
|
19
|
+
/* a CSS value without the characters that end a declaration or an attribute */
|
|
20
|
+
export const cssValue = (s) => String(s ?? '').replace(/[;"'<>{}\\]/g, '');
|
|
21
|
+
export const num = (v) => { const n = parseFloat(v); return Number.isFinite(n) ? fmt(n) : ''; };
|
|
22
|
+
export const dashArray = (s) => String(s ?? '').replace(/[^0-9.,\s]/g, '').trim();
|
|
23
|
+
export const pathData = (s) => (/^[MmLlHhVvCcSsQqTtAaZzEe0-9.,\s+-]*$/.test(String(s ?? '')) ? String(s ?? '') : '');
|
|
24
|
+
export const textAnchor = (a) => (a === 'middle' || a === 'end' ? a : 'start');
|
|
25
|
+
export const fontWeight = (w) => (/^(normal|bold|[1-9]00)$/.test(String(w)) ? String(w) : '');
|
|
26
|
+
|
|
27
|
+
/* A projection. angle 30 is true isometric; 26.565 is the 2:1 "pixel" dimetric. */
|
|
28
|
+
export function makeView(angle = 30) {
|
|
29
|
+
const C = Math.cos(angle * DEG);
|
|
30
|
+
const S = Math.sin(angle * DEG);
|
|
31
|
+
const P = (x, y, z = 0) => [(x - y) * C, (x + y) * S - z];
|
|
32
|
+
/* z scaled so that the view direction is (1, 1, 1) in (x, y, zs) space */
|
|
33
|
+
const ZS = 1 / (2 * S);
|
|
34
|
+
return { angle, C, S, ZS, P };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* ── plan polygons (x →, y ↓ in plan) ── */
|
|
38
|
+
|
|
39
|
+
/* Rounded rectangle. Returns points with a parallel `kinds` array:
|
|
40
|
+
'c' sharp corner, 't' tangent (start/end of an arc), 's' smooth (inside an arc). */
|
|
41
|
+
export function rrect(x, y, w, h, r = 0, n) {
|
|
42
|
+
r = Math.max(0, Math.min(r || 0, w / 2, h / 2));
|
|
43
|
+
let pts, kinds;
|
|
44
|
+
if (r < 1e-3) {
|
|
45
|
+
pts = [[x, y], [x + w, y], [x + w, y + h], [x, y + h]];
|
|
46
|
+
kinds = ['c', 'c', 'c', 'c'];
|
|
47
|
+
} else {
|
|
48
|
+
n = n || clamp(Math.round(r / 1.6), 3, 12);
|
|
49
|
+
pts = []; kinds = [];
|
|
50
|
+
const cs = [[x + w - r, y + r, -90], [x + w - r, y + h - r, 0], [x + r, y + h - r, 90], [x + r, y + r, 180]];
|
|
51
|
+
for (const [cx, cy, a0] of cs) {
|
|
52
|
+
for (let i = 0; i <= n; i++) {
|
|
53
|
+
const a = (a0 + (90 * i) / n) * DEG;
|
|
54
|
+
pts.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
|
|
55
|
+
kinds.push(i === 0 || i === n ? 't' : 's');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return dedupe(pts, kinds);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/* Regular polygon / circle samples in plan. */
|
|
63
|
+
export function circlePts(cx, cy, r, n = 32, a0 = 0) {
|
|
64
|
+
const pts = [];
|
|
65
|
+
for (let i = 0; i < n; i++) {
|
|
66
|
+
const a = (a0 + (360 * i) / n) * DEG;
|
|
67
|
+
pts.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]);
|
|
68
|
+
}
|
|
69
|
+
pts.kinds = pts.map(() => 's');
|
|
70
|
+
return pts;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function regularPolygon(cx, cy, r, sides, a0 = -90) {
|
|
74
|
+
const p = circlePts(cx, cy, r, sides, a0);
|
|
75
|
+
p.kinds = p.map(() => 'c');
|
|
76
|
+
return p;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function dedupe(pts, kinds) {
|
|
80
|
+
const out = [], ok = [];
|
|
81
|
+
for (let i = 0; i < pts.length; i++) {
|
|
82
|
+
const a = pts[i], b = out[out.length - 1];
|
|
83
|
+
if (b && Math.abs(a[0] - b[0]) < 1e-6 && Math.abs(a[1] - b[1]) < 1e-6) {
|
|
84
|
+
if (kinds[i] === 'c') ok[ok.length - 1] = 'c';
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
out.push(a); ok.push(kinds[i]);
|
|
88
|
+
}
|
|
89
|
+
const f = out[0], l = out[out.length - 1];
|
|
90
|
+
if (out.length > 1 && Math.abs(f[0] - l[0]) < 1e-6 && Math.abs(f[1] - l[1]) < 1e-6) { out.pop(); ok.pop(); }
|
|
91
|
+
out.kinds = ok;
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function signedArea(poly) {
|
|
96
|
+
let a = 0;
|
|
97
|
+
for (let i = 0; i < poly.length; i++) {
|
|
98
|
+
const p = poly[i], q = poly[(i + 1) % poly.length];
|
|
99
|
+
a += p[0] * q[1] - q[0] * p[1];
|
|
100
|
+
}
|
|
101
|
+
return a / 2;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function isConvex(poly) {
|
|
105
|
+
let sign = 0;
|
|
106
|
+
for (let i = 0; i < poly.length; i++) {
|
|
107
|
+
const a = poly[i], b = poly[(i + 1) % poly.length], c = poly[(i + 2) % poly.length];
|
|
108
|
+
const cr = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]);
|
|
109
|
+
if (Math.abs(cr) < 1e-9) continue;
|
|
110
|
+
const s = Math.sign(cr);
|
|
111
|
+
if (sign && s !== sign) return false;
|
|
112
|
+
sign = s;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/* Inset a convex polygon by d (positive shrinks). Keeps `kinds`. */
|
|
118
|
+
export function insetConvex(poly, d) {
|
|
119
|
+
const n = poly.length, ccw = signedArea(poly) > 0, lines = [];
|
|
120
|
+
for (let i = 0; i < n; i++) {
|
|
121
|
+
const a = poly[i], b = poly[(i + 1) % n];
|
|
122
|
+
let dx = b[0] - a[0], dy = b[1] - a[1];
|
|
123
|
+
const L = Math.hypot(dx, dy) || 1; dx /= L; dy /= L;
|
|
124
|
+
/* inward normal */
|
|
125
|
+
const nx = ccw ? -dy : dy, ny = ccw ? dx : -dx;
|
|
126
|
+
lines.push([a[0] + nx * d, a[1] + ny * d, dx, dy]);
|
|
127
|
+
}
|
|
128
|
+
const out = [];
|
|
129
|
+
for (let i = 0; i < n; i++) {
|
|
130
|
+
const l1 = lines[(i + n - 1) % n], l2 = lines[i];
|
|
131
|
+
const den = l1[2] * l2[3] - l1[3] * l2[2];
|
|
132
|
+
if (Math.abs(den) < 1e-9) { out.push([l2[0], l2[1]]); continue; }
|
|
133
|
+
const t = ((l2[0] - l1[0]) * l2[3] - (l2[1] - l1[1]) * l2[2]) / den;
|
|
134
|
+
out.push([l1[0] + l1[2] * t, l1[1] + l1[3] * t]);
|
|
135
|
+
}
|
|
136
|
+
out.kinds = poly.kinds ? poly.kinds.slice() : out.map(() => 'c');
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function rotatePts(pts, cx, cy, deg) {
|
|
141
|
+
if (!deg) return pts;
|
|
142
|
+
const c = Math.cos(deg * DEG), s = Math.sin(deg * DEG);
|
|
143
|
+
const out = pts.map(([x, y]) => [cx + (x - cx) * c - (y - cy) * s, cy + (x - cx) * s + (y - cy) * c]);
|
|
144
|
+
out.kinds = pts.kinds;
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/* ── 3D vectors ── */
|
|
149
|
+
export const v3 = {
|
|
150
|
+
add: (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]],
|
|
151
|
+
sub: (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]],
|
|
152
|
+
mul: (a, s) => [a[0] * s, a[1] * s, a[2] * s],
|
|
153
|
+
dot: (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
|
|
154
|
+
cross: (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]],
|
|
155
|
+
len: (a) => Math.hypot(a[0], a[1], a[2]),
|
|
156
|
+
norm: (a) => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; },
|
|
157
|
+
lerp: (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t],
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/* Seeded PRNG (mulberry32) so scenes with scattered objects are reproducible. */
|
|
161
|
+
export function rng(seed = 1) {
|
|
162
|
+
let a = seed >>> 0;
|
|
163
|
+
const next = () => {
|
|
164
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
165
|
+
let t = a;
|
|
166
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
167
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
168
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
169
|
+
};
|
|
170
|
+
next.range = (a0, b0) => a0 + (b0 - a0) * next();
|
|
171
|
+
next.int = (a0, b0) => Math.floor(a0 + (b0 - a0 + 1) * next());
|
|
172
|
+
next.pick = (arr) => arr[Math.floor(next() * arr.length)];
|
|
173
|
+
next.chance = (p) => next() < p;
|
|
174
|
+
return next;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/* AABB helpers: [x0, y0, z0, x1, y1, z1] */
|
|
178
|
+
export const box3 = {
|
|
179
|
+
empty: () => [Infinity, Infinity, Infinity, -Infinity, -Infinity, -Infinity],
|
|
180
|
+
addPt(b, x, y, z) {
|
|
181
|
+
if (x < b[0]) b[0] = x; if (y < b[1]) b[1] = y; if (z < b[2]) b[2] = z;
|
|
182
|
+
if (x > b[3]) b[3] = x; if (y > b[4]) b[4] = y; if (z > b[5]) b[5] = z;
|
|
183
|
+
return b;
|
|
184
|
+
},
|
|
185
|
+
union(a, b) {
|
|
186
|
+
return [Math.min(a[0], b[0]), Math.min(a[1], b[1]), Math.min(a[2], b[2]), Math.max(a[3], b[3]), Math.max(a[4], b[4]), Math.max(a[5], b[5])];
|
|
187
|
+
},
|
|
188
|
+
shift: (b, x, y, z) => [b[0] + x, b[1] + y, b[2] + z, b[3] + x, b[4] + y, b[5] + z],
|
|
189
|
+
valid: (b) => b[0] <= b[3] && b[1] <= b[4] && b[2] <= b[5],
|
|
190
|
+
};
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/* Scene graph: Node (a drawable primitive) and Group (a container).
|
|
2
|
+
A node renders to one <g data-uid>. Its geometry lives in `opts` (world units);
|
|
3
|
+
its animated state lives in `t` (absolute offsets) and `a` (additive, reset every frame). */
|
|
4
|
+
import { Draw, IDENTITY, compose, makeFrame, applyFrame } from './draw.js';
|
|
5
|
+
import { box3, fmt, cssClass, cssValue } from './math.js';
|
|
6
|
+
import { depthSort, makeItem } from './sort.js';
|
|
7
|
+
|
|
8
|
+
let UID = 0;
|
|
9
|
+
const TRANSFORM_KEYS = ['x', 'y', 'z', 'scale', 'opacity'];
|
|
10
|
+
|
|
11
|
+
export class Node {
|
|
12
|
+
constructor(opts = {}) {
|
|
13
|
+
this.uid = ++UID;
|
|
14
|
+
this.opts = { ...opts };
|
|
15
|
+
this.id = opts.id || null;
|
|
16
|
+
this.parent = null;
|
|
17
|
+
this.t = { x: 0, y: 0, z: 0, scale: 1, opacity: opts.opacity ?? 1 };
|
|
18
|
+
this.a = { x: 0, y: 0, z: 0, scale: 1, opacity: 1 };
|
|
19
|
+
this.el = null;
|
|
20
|
+
this.handlers = null;
|
|
21
|
+
this.layer = opts.layer || 0;
|
|
22
|
+
this.order = 0;
|
|
23
|
+
this.dirty = true;
|
|
24
|
+
this.moved = true;
|
|
25
|
+
this.frame = IDENTITY; // frame this node was drawn in
|
|
26
|
+
this.rest = null; // world AABB at rest
|
|
27
|
+
this.sb = null; // screen bounds at rest
|
|
28
|
+
this.hidden = !!opts.hidden;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get kind() { return 'node'; }
|
|
32
|
+
get scene() { let n = this; while (n.parent) n = n.parent; return n._scene || null; }
|
|
33
|
+
get isGroup() { return false; }
|
|
34
|
+
|
|
35
|
+
/* ── geometry (override) ── */
|
|
36
|
+
draw(_d) {}
|
|
37
|
+
/* local AABB before the frame; override */
|
|
38
|
+
lbox() { return null; }
|
|
39
|
+
/* world AABB at rest for a frame */
|
|
40
|
+
aabb(frame) {
|
|
41
|
+
const b = this.lbox();
|
|
42
|
+
if (!b) return null;
|
|
43
|
+
if (!frame.rot) return box3.shift(b, frame.x, frame.y, frame.z);
|
|
44
|
+
const out = box3.empty();
|
|
45
|
+
for (const x of [b[0], b[3]]) for (const y of [b[1], b[4]]) {
|
|
46
|
+
const w = applyFrame(frame, x, y, 0);
|
|
47
|
+
box3.addPt(out, w[0], w[1], b[2] + frame.z);
|
|
48
|
+
box3.addPt(out, w[0], w[1], b[5] + frame.z);
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* ── state ── */
|
|
54
|
+
get(key) {
|
|
55
|
+
if (TRANSFORM_KEYS.includes(key)) return this.t[key];
|
|
56
|
+
const v = this.opts[key];
|
|
57
|
+
return v;
|
|
58
|
+
}
|
|
59
|
+
set(key, value) {
|
|
60
|
+
if (typeof key === 'object') { for (const k in key) this.set(k, key[k]); return this; }
|
|
61
|
+
if (TRANSFORM_KEYS.includes(key)) {
|
|
62
|
+
if (this.t[key] !== value) { this.t[key] = value; if (key === 'opacity') this.fade = true; else this.moved = true; this._wake(); }
|
|
63
|
+
} else if (this.opts[key] !== value) {
|
|
64
|
+
this.opts[key] = value;
|
|
65
|
+
if (key === 'hidden') { this.hidden = !!value; this.restyle = true; }
|
|
66
|
+
else if (key === 'material' || key === 'class' || key === 'color' || key === 'style' || key === 'pressed' || key === 'label' || key === 'interactive' || key === 'glow') this.restyle = true;
|
|
67
|
+
else this.dirty = true;
|
|
68
|
+
this._wake();
|
|
69
|
+
}
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
/* Total offset relative to the nearest atomic container (includes flat ancestors). */
|
|
73
|
+
offset() {
|
|
74
|
+
let x = this.t.x + this.a.x, y = this.t.y + this.a.y, z = this.t.z + this.a.z;
|
|
75
|
+
let p = this.parent;
|
|
76
|
+
while (p && p.flat) { x += p.t.x + p.a.x; y += p.t.y + p.a.y; z += p.t.z + p.a.z; p = p.parent; }
|
|
77
|
+
return [x, y, z];
|
|
78
|
+
}
|
|
79
|
+
opacity() {
|
|
80
|
+
let o = this.t.opacity * this.a.opacity, p = this.parent;
|
|
81
|
+
while (p && p.flat) { o *= p.t.opacity * p.a.opacity; p = p.parent; }
|
|
82
|
+
return o;
|
|
83
|
+
}
|
|
84
|
+
/* AABB used for depth sorting within the container. */
|
|
85
|
+
sortBox() {
|
|
86
|
+
if (!this.rest) return null;
|
|
87
|
+
const o = this.offset();
|
|
88
|
+
return box3.shift(this.rest, o[0], o[1], o[2]);
|
|
89
|
+
}
|
|
90
|
+
_wake() { const s = this.scene; if (s) s._touch(this); }
|
|
91
|
+
|
|
92
|
+
/* ── animation sugar (delegates to the scene) ── */
|
|
93
|
+
to(props, opts) { return this.scene.to(this, props, opts); }
|
|
94
|
+
on(evt, fn) {
|
|
95
|
+
(this.handlers || (this.handlers = {}))[evt] = (this.handlers[evt] || []).concat(fn);
|
|
96
|
+
this.restyle = true; this._wake();
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
emit(evt, e) { const hs = this.handlers && this.handlers[evt]; if (hs) hs.forEach((h) => h.call(this, e, this)); }
|
|
100
|
+
remove() { if (this.parent) this.parent.removeChild(this); }
|
|
101
|
+
|
|
102
|
+
/* ── rendering ── */
|
|
103
|
+
classes() {
|
|
104
|
+
const o = this.opts, c = ['iso-n'];
|
|
105
|
+
if (o.material) c.push('m-' + cssClass(o.material));
|
|
106
|
+
if (o.color) c.push('m-tint');
|
|
107
|
+
if (o.class) c.push(cssClass(o.class));
|
|
108
|
+
if (o.glow) c.push('glow');
|
|
109
|
+
if (this.interactive()) c.push('hit');
|
|
110
|
+
return c.join(' ');
|
|
111
|
+
}
|
|
112
|
+
interactive() { return !!(this.handlers && (this.handlers.click || this.handlers.press)) || !!this.opts.interactive; }
|
|
113
|
+
styleAttr() {
|
|
114
|
+
const o = this.opts, s = [];
|
|
115
|
+
if (o.color) s.push('--iso-tint:' + cssValue(o.color));
|
|
116
|
+
if (o.style) for (const k in o.style) if (/^-{0,2}[a-z][\w-]*$/i.test(k)) s.push(k + ':' + cssValue(o.style[k]));
|
|
117
|
+
const op = this.opacity();
|
|
118
|
+
if (op !== 1) s.push('opacity:' + fmt(op));
|
|
119
|
+
if (this.hidden) s.push('display:none');
|
|
120
|
+
return s.length ? ' style="' + s.join(';') + '"' : '';
|
|
121
|
+
}
|
|
122
|
+
transformAttr(view) {
|
|
123
|
+
const [x, y, z] = this.offset();
|
|
124
|
+
const sc = this.t.scale * this.a.scale;
|
|
125
|
+
let tr = '';
|
|
126
|
+
if (x || y || z) { const q = view.P(x, y, z); tr = 'translate(' + fmt(q[0]) + ' ' + fmt(q[1]) + ')'; }
|
|
127
|
+
if (sc !== 1 && this.rest) {
|
|
128
|
+
const b = this.rest, an = this.opts.anchor || 'bottom';
|
|
129
|
+
const ax = (b[0] + b[3]) / 2, ay = (b[1] + b[4]) / 2;
|
|
130
|
+
const az = an === 'center' ? (b[2] + b[5]) / 2 : an === 'top' ? b[5] : b[2];
|
|
131
|
+
const q = view.P(ax, ay, az);
|
|
132
|
+
tr += ' translate(' + fmt(q[0]) + ' ' + fmt(q[1]) + ') scale(' + fmt(sc) + ') translate(' + fmt(-q[0]) + ' ' + fmt(-q[1]) + ')';
|
|
133
|
+
}
|
|
134
|
+
return tr;
|
|
135
|
+
}
|
|
136
|
+
attrs(view) {
|
|
137
|
+
let s = ' class="' + this.classes() + '" data-uid="' + this.uid + '"';
|
|
138
|
+
if (this.id) s += ' data-id="' + esc(this.id) + '"';
|
|
139
|
+
const tr = this.transformAttr(view);
|
|
140
|
+
if (tr) s += ' transform="' + tr + '"';
|
|
141
|
+
s += this.styleAttr();
|
|
142
|
+
if (this.interactive()) {
|
|
143
|
+
const o = this.opts;
|
|
144
|
+
s += ' tabindex="0" role="' + esc(o.role || 'button') + '"';
|
|
145
|
+
if (o.label) s += ' aria-label="' + esc(o.label) + '"';
|
|
146
|
+
if (o.pressed !== undefined) s += ' aria-pressed="' + !!o.pressed + '"';
|
|
147
|
+
}
|
|
148
|
+
return s;
|
|
149
|
+
}
|
|
150
|
+
/* Draw this node's content in `frame`; updates rest/sb. */
|
|
151
|
+
content(view, frame) {
|
|
152
|
+
this.frame = frame;
|
|
153
|
+
const d = new Draw(view, frame);
|
|
154
|
+
if (!this.hidden || true) this.draw(d);
|
|
155
|
+
this.rest = this.aabb(frame);
|
|
156
|
+
this.sb = d.b;
|
|
157
|
+
let s = d.toString();
|
|
158
|
+
if (this.interactive()) s += focusRing(view, this.rest);
|
|
159
|
+
this.dirty = false;
|
|
160
|
+
return s;
|
|
161
|
+
}
|
|
162
|
+
markup(view, frame) {
|
|
163
|
+
const c = this.content(view, frame);
|
|
164
|
+
this.moved = false; this.restyle = false;
|
|
165
|
+
return '<g' + this.attrs(view) + '>' + c + '</g>';
|
|
166
|
+
}
|
|
167
|
+
/* Apply transform/style changes to the live element. */
|
|
168
|
+
sync(view) {
|
|
169
|
+
if (!this.el) return;
|
|
170
|
+
const tr = this.transformAttr(view);
|
|
171
|
+
if (tr) this.el.setAttribute('transform', tr); else this.el.removeAttribute('transform');
|
|
172
|
+
const op = this.opacity();
|
|
173
|
+
this.el.style.opacity = op === 1 ? '' : fmt(op);
|
|
174
|
+
if (this.restyle) {
|
|
175
|
+
this.el.setAttribute('class', this.classes());
|
|
176
|
+
this.el.style.display = this.hidden ? 'none' : '';
|
|
177
|
+
if (this.opts.color) this.el.style.setProperty('--iso-tint', cssValue(this.opts.color));
|
|
178
|
+
if (this.opts.style) for (const k in this.opts.style) if (/^-{0,2}[a-z][\w-]*$/i.test(k)) this.el.style.setProperty(k, cssValue(this.opts.style[k]));
|
|
179
|
+
if (this.opts.pressed !== undefined) this.el.setAttribute('aria-pressed', !!this.opts.pressed);
|
|
180
|
+
if (this.opts.label) this.el.setAttribute('aria-label', this.opts.label);
|
|
181
|
+
if (this.interactive() && !this.el.hasAttribute('tabindex')) { this.el.setAttribute('tabindex', '0'); this.el.setAttribute('role', this.opts.role || 'button'); }
|
|
182
|
+
this.restyle = false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/* ── Group ── */
|
|
188
|
+
export class Group extends Node {
|
|
189
|
+
constructor(opts = {}) {
|
|
190
|
+
super(opts);
|
|
191
|
+
this.children = [];
|
|
192
|
+
this.flat = !!opts.flat;
|
|
193
|
+
this.sortMode = opts.sort || 'auto'; // 'auto' | 'none'
|
|
194
|
+
this._seq = 0;
|
|
195
|
+
}
|
|
196
|
+
get kind() { return 'group'; }
|
|
197
|
+
get isGroup() { return true; }
|
|
198
|
+
childFrame(frame) {
|
|
199
|
+
const o = this.opts, at = o.at || [0, 0, 0];
|
|
200
|
+
if (!at[0] && !at[1] && !at[2] && !o.rot) return frame;
|
|
201
|
+
return compose(frame, makeFrame(at[0] || 0, at[1] || 0, at[2] || 0, o.rot || 0));
|
|
202
|
+
}
|
|
203
|
+
add(child) {
|
|
204
|
+
if (Array.isArray(child)) { child.forEach((c) => this.add(c)); return child; }
|
|
205
|
+
if (child.parent) child.parent.removeChild(child);
|
|
206
|
+
child.parent = this;
|
|
207
|
+
child.order = this._seq++;
|
|
208
|
+
this.children.push(child);
|
|
209
|
+
const s = this.scene;
|
|
210
|
+
if (s) { s._register(child); s._structure(); }
|
|
211
|
+
return child;
|
|
212
|
+
}
|
|
213
|
+
removeChild(child) {
|
|
214
|
+
const i = this.children.indexOf(child);
|
|
215
|
+
if (i >= 0) this.children.splice(i, 1);
|
|
216
|
+
child.parent = null;
|
|
217
|
+
const s = this.scene;
|
|
218
|
+
if (s) { s._unregister(child); s._structure(); }
|
|
219
|
+
}
|
|
220
|
+
clear() { this.children.slice().forEach((c) => this.removeChild(c)); }
|
|
221
|
+
/* all descendants, depth first */
|
|
222
|
+
each(fn) { for (const c of this.children) { fn(c); if (c.isGroup) c.each(fn); } }
|
|
223
|
+
find(pred) { let r = null; this.each((c) => { if (!r && pred(c)) r = c; }); return r; }
|
|
224
|
+
findAll(pred) { const r = []; this.each((c) => { if (pred(c)) r.push(c); }); return r; }
|
|
225
|
+
get(key) { return key in this.t ? this.t[key] : this.opts[key]; }
|
|
226
|
+
/* Items that sort in this container: children, with flat groups expanded. */
|
|
227
|
+
items() {
|
|
228
|
+
const out = [];
|
|
229
|
+
const walk = (g) => { for (const c of g.children) { if (c.isGroup && c.flat) walk(c); else out.push(c); } };
|
|
230
|
+
walk(this);
|
|
231
|
+
return out;
|
|
232
|
+
}
|
|
233
|
+
aabb() {
|
|
234
|
+
let b = null;
|
|
235
|
+
for (const c of this.items()) {
|
|
236
|
+
if (c.hidden) continue;
|
|
237
|
+
const cb = c.sortBox();
|
|
238
|
+
if (cb) b = b ? box3.union(b, cb) : cb;
|
|
239
|
+
}
|
|
240
|
+
return b;
|
|
241
|
+
}
|
|
242
|
+
/* Render children (flat groups expanded, each in its own frame), order them, concatenate. */
|
|
243
|
+
content(view, frame) {
|
|
244
|
+
this.frame = frame;
|
|
245
|
+
const parts = [];
|
|
246
|
+
const walk = (g, f) => {
|
|
247
|
+
const cf = g.childFrame(f);
|
|
248
|
+
if (g !== this) g.frame = f;
|
|
249
|
+
for (const c of g.children) {
|
|
250
|
+
if (c.isGroup && c.flat) walk(c, cf);
|
|
251
|
+
else parts.push({ node: c, s: c.markup(view, cf) });
|
|
252
|
+
}
|
|
253
|
+
if (g !== this) { g.rest = g.aabb(); g.dirty = false; g.moved = false; }
|
|
254
|
+
};
|
|
255
|
+
walk(this, frame);
|
|
256
|
+
const zs = view.ZS;
|
|
257
|
+
let ordered;
|
|
258
|
+
if (this.sortMode === 'none') ordered = parts.map((p, i) => ({ ...makeItem(p.node, p.node.sortBox(), zs, i), s: p.s }));
|
|
259
|
+
else ordered = depthSort(parts.map((p, i) => ({ ...makeItem(p.node, p.node.sortBox(), zs, i), s: p.s })));
|
|
260
|
+
this.sorted = ordered.map((it) => it.node);
|
|
261
|
+
this.rest = this.aabb();
|
|
262
|
+
/* screen bounds: union of children (with their offsets) */
|
|
263
|
+
const b = [Infinity, Infinity, -Infinity, -Infinity];
|
|
264
|
+
for (const it of ordered) {
|
|
265
|
+
const n = it.node, sb = n.sb;
|
|
266
|
+
if (!sb || !isFinite(sb[0])) continue;
|
|
267
|
+
const o = n.offset(), q = view.P(o[0], o[1], o[2]);
|
|
268
|
+
b[0] = Math.min(b[0], sb[0] + q[0]); b[1] = Math.min(b[1], sb[1] + q[1]);
|
|
269
|
+
b[2] = Math.max(b[2], sb[2] + q[0]); b[3] = Math.max(b[3], sb[3] + q[1]);
|
|
270
|
+
}
|
|
271
|
+
this.sb = b;
|
|
272
|
+
this.dirty = false;
|
|
273
|
+
let s = ordered.map((it) => it.s).join('');
|
|
274
|
+
if (this.interactive()) s += focusRing(view, this.rest);
|
|
275
|
+
return s;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function esc(s) {
|
|
280
|
+
return String(s).replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/* Focus ring: the hexagonal silhouette of an AABB, drawn invisible until :focus-visible. */
|
|
284
|
+
export function focusRing(view, b) {
|
|
285
|
+
if (!b) return '';
|
|
286
|
+
const m = 3;
|
|
287
|
+
const [x0, y0, z0, x1, y1, z1] = [b[0] - m, b[1] - m, b[2] - 1, b[3] + m, b[4] + m, b[5] + m];
|
|
288
|
+
const pts = [[x0, y0, z1], [x1, y0, z1], [x1, y0, z0], [x1, y1, z0], [x0, y1, z0], [x0, y1, z1]].map((p) => view.P(p[0], p[1], p[2]));
|
|
289
|
+
return '<path class="focus-ring" d="M' + pts.map((p) => fmt(p[0]) + ' ' + fmt(p[1])).join('L') + 'Z"/>';
|
|
290
|
+
}
|