@sebastianwessel/isostate 0.1.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/dist/animation/animation-engine.d.ts +78 -0
- package/dist/animation/animation-engine.d.ts.map +1 -0
- package/dist/animation/controller.d.ts +130 -0
- package/dist/animation/controller.d.ts.map +1 -0
- package/dist/browser/isostate.runtime.js +2449 -0
- package/dist/browser/isostate.runtime.js.map +1 -0
- package/dist/chunks/errors-DyqEkrm5.js +29 -0
- package/dist/chunks/errors-DyqEkrm5.js.map +1 -0
- package/dist/chunks/index-CDQt8CfR.js +2380 -0
- package/dist/chunks/index-CDQt8CfR.js.map +1 -0
- package/dist/dsl/compiler.d.ts +13 -0
- package/dist/dsl/compiler.d.ts.map +1 -0
- package/dist/dsl/index.d.ts +7 -0
- package/dist/dsl/index.d.ts.map +1 -0
- package/dist/dsl/index.js +2191 -0
- package/dist/dsl/index.js.map +1 -0
- package/dist/dsl/scene-parser.d.ts +6 -0
- package/dist/dsl/scene-parser.d.ts.map +1 -0
- package/dist/dsl/scene-validator.d.ts +11 -0
- package/dist/dsl/scene-validator.d.ts.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/rendering/animation-css.d.ts +3 -0
- package/dist/rendering/animation-css.d.ts.map +1 -0
- package/dist/rendering/asset-node.d.ts +13 -0
- package/dist/rendering/asset-node.d.ts.map +1 -0
- package/dist/rendering/rendering-engine.d.ts +77 -0
- package/dist/rendering/rendering-engine.d.ts.map +1 -0
- package/dist/rendering/theme.d.ts +2 -0
- package/dist/rendering/theme.d.ts.map +1 -0
- package/dist/runtime/index.d.ts +3 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +3 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/mount-scene.d.ts +52 -0
- package/dist/runtime/mount-scene.d.ts.map +1 -0
- package/dist/types/asset-registry.d.ts +26 -0
- package/dist/types/asset-registry.d.ts.map +1 -0
- package/dist/types/assets.d.ts +37 -0
- package/dist/types/assets.d.ts.map +1 -0
- package/dist/types/errors.d.ts +23 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/node.d.ts +251 -0
- package/dist/types/node.d.ts.map +1 -0
- package/dist/types/runtime-bundle.d.ts +44 -0
- package/dist/types/runtime-bundle.d.ts.map +1 -0
- package/dist/types/scene.d.ts +94 -0
- package/dist/types/scene.d.ts.map +1 -0
- package/dist/types/validation.d.ts +40 -0
- package/dist/types/validation.d.ts.map +1 -0
- package/dist/utils/easing.d.ts +25 -0
- package/dist/utils/easing.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/projection.d.ts +22 -0
- package/dist/utils/projection.d.ts.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,2380 @@
|
|
|
1
|
+
import { R as RenderError, C as ControllerError } from './errors-DyqEkrm5.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Animation engine — resolves progress against compiled RuntimeBundle scene stops.
|
|
5
|
+
*
|
|
6
|
+
* The engine does not manipulate DOM. It computes interpolation data and lifecycle
|
|
7
|
+
* transitions that the controller applies to the rendering engine.
|
|
8
|
+
*/
|
|
9
|
+
class AnimationEngine {
|
|
10
|
+
_bundle = null;
|
|
11
|
+
_progress = 0;
|
|
12
|
+
_paused = false;
|
|
13
|
+
_prevFrameMap = new Map();
|
|
14
|
+
_elementFrameMap = new Map();
|
|
15
|
+
_prevConnectorFrameMap = new Map();
|
|
16
|
+
_connectorFrameMap = new Map();
|
|
17
|
+
get bundle() {
|
|
18
|
+
return this._bundle;
|
|
19
|
+
}
|
|
20
|
+
get progress() {
|
|
21
|
+
return this._progress;
|
|
22
|
+
}
|
|
23
|
+
getProgress() {
|
|
24
|
+
return this._progress;
|
|
25
|
+
}
|
|
26
|
+
get paused() {
|
|
27
|
+
return this._paused;
|
|
28
|
+
}
|
|
29
|
+
isPaused() {
|
|
30
|
+
return this._paused;
|
|
31
|
+
}
|
|
32
|
+
get elementsCount() {
|
|
33
|
+
return this._elementFrameMap.size;
|
|
34
|
+
}
|
|
35
|
+
get connectorsCount() {
|
|
36
|
+
return this._connectorFrameMap.size;
|
|
37
|
+
}
|
|
38
|
+
/** Initialize with a compiled runtime bundle. */
|
|
39
|
+
init(bundle) {
|
|
40
|
+
this._bundle = bundle;
|
|
41
|
+
this._progress = 0;
|
|
42
|
+
this._paused = false;
|
|
43
|
+
this._prevFrameMap.clear();
|
|
44
|
+
this._elementFrameMap.clear();
|
|
45
|
+
this._prevConnectorFrameMap.clear();
|
|
46
|
+
this._connectorFrameMap.clear();
|
|
47
|
+
const initial = resolveFrameMap(bundle, 0);
|
|
48
|
+
this._elementFrameMap = initial;
|
|
49
|
+
this._prevFrameMap = cloneFrameMap(initial);
|
|
50
|
+
const initialConnectors = resolveConnectorFrameMap(bundle, 0);
|
|
51
|
+
this._connectorFrameMap = initialConnectors;
|
|
52
|
+
this._prevConnectorFrameMap = cloneConnectorFrameMap(initialConnectors);
|
|
53
|
+
}
|
|
54
|
+
/** Set current scroll progress (0-1) and compute frame update. */
|
|
55
|
+
setProgress(progress) {
|
|
56
|
+
const clamped = Math.max(0, Math.min(1, progress));
|
|
57
|
+
if (this._paused)
|
|
58
|
+
return;
|
|
59
|
+
this._progress = clamped;
|
|
60
|
+
if (!this._bundle)
|
|
61
|
+
return;
|
|
62
|
+
this._prevFrameMap = cloneFrameMap(this._elementFrameMap);
|
|
63
|
+
this._elementFrameMap = resolveFrameMap(this._bundle, clamped);
|
|
64
|
+
this._prevConnectorFrameMap = cloneConnectorFrameMap(this._connectorFrameMap);
|
|
65
|
+
this._connectorFrameMap = resolveConnectorFrameMap(this._bundle, clamped);
|
|
66
|
+
}
|
|
67
|
+
/** Get interpolated FrameUpdate for an element id or runtime element. */
|
|
68
|
+
getElementUpdate(element) {
|
|
69
|
+
const id = typeof element === "string" ? element : element.id;
|
|
70
|
+
const frame = this._elementFrameMap.get(id);
|
|
71
|
+
if (!frame) {
|
|
72
|
+
return {
|
|
73
|
+
id,
|
|
74
|
+
asset: "",
|
|
75
|
+
lifecycle: "removed",
|
|
76
|
+
ambient: [],
|
|
77
|
+
pos: [0, 0],
|
|
78
|
+
size: 1,
|
|
79
|
+
layer: "",
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return frameToUpdate(frame);
|
|
83
|
+
}
|
|
84
|
+
getFrameUpdates() {
|
|
85
|
+
return [...this._elementFrameMap.values()].map(frameToUpdate);
|
|
86
|
+
}
|
|
87
|
+
/** Get interpolated ConnectorFrameUpdate for a connector id or runtime connector. */
|
|
88
|
+
getConnectorUpdate(connector) {
|
|
89
|
+
const id = typeof connector === "string" ? connector : connector.id;
|
|
90
|
+
const frame = this._connectorFrameMap.get(id);
|
|
91
|
+
if (!frame)
|
|
92
|
+
return connectorFrameToUpdate(removedConnectorFrame(id));
|
|
93
|
+
return connectorFrameToUpdate(frame);
|
|
94
|
+
}
|
|
95
|
+
getConnectorFrameUpdates() {
|
|
96
|
+
return [...this._connectorFrameMap.values()].map(connectorFrameToUpdate);
|
|
97
|
+
}
|
|
98
|
+
/** Compute lifecycle transition between previous and current frame. */
|
|
99
|
+
getLifecycleTransition(elId) {
|
|
100
|
+
const prev = this._prevFrameMap.get(elId);
|
|
101
|
+
const current = this._elementFrameMap.get(elId);
|
|
102
|
+
const from = (prev?.lifecycle ?? "removed");
|
|
103
|
+
const to = (current?.lifecycle ?? "removed");
|
|
104
|
+
if (from === to)
|
|
105
|
+
return null;
|
|
106
|
+
return {
|
|
107
|
+
from,
|
|
108
|
+
to,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** Compute lifecycle transition between previous and current connector frame. */
|
|
112
|
+
getConnectorLifecycleTransition(connectorId) {
|
|
113
|
+
const prev = this._prevConnectorFrameMap.get(connectorId);
|
|
114
|
+
const current = this._connectorFrameMap.get(connectorId);
|
|
115
|
+
const from = (prev?.lifecycle ?? "removed");
|
|
116
|
+
const to = (current?.lifecycle ?? "removed");
|
|
117
|
+
if (from === to)
|
|
118
|
+
return null;
|
|
119
|
+
return {
|
|
120
|
+
from,
|
|
121
|
+
to,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
getCurrentState() {
|
|
125
|
+
const bundle = this._bundle;
|
|
126
|
+
if (!bundle)
|
|
127
|
+
return null;
|
|
128
|
+
const pair = findSurroundingStops(bundle.scenes, this._progress);
|
|
129
|
+
return pair?.nextStop ?? bundle.scenes[0] ?? null;
|
|
130
|
+
}
|
|
131
|
+
pause() {
|
|
132
|
+
this._paused = true;
|
|
133
|
+
}
|
|
134
|
+
resume() {
|
|
135
|
+
this._paused = false;
|
|
136
|
+
}
|
|
137
|
+
destroy() {
|
|
138
|
+
this._bundle = null;
|
|
139
|
+
this._progress = 0;
|
|
140
|
+
this._paused = false;
|
|
141
|
+
this._elementFrameMap.clear();
|
|
142
|
+
this._prevFrameMap.clear();
|
|
143
|
+
this._connectorFrameMap.clear();
|
|
144
|
+
this._prevConnectorFrameMap.clear();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// ── Interpolation helpers ──────────────────────────────────────────────────
|
|
148
|
+
function resolveFrameMap(bundle, progress) {
|
|
149
|
+
const pair = findSurroundingStops(bundle.scenes, progress);
|
|
150
|
+
const result = new Map();
|
|
151
|
+
if (!pair)
|
|
152
|
+
return result;
|
|
153
|
+
const ids = new Set();
|
|
154
|
+
for (const stop of bundle.scenes) {
|
|
155
|
+
for (const element of stop.elements ?? [])
|
|
156
|
+
ids.add(element.id);
|
|
157
|
+
}
|
|
158
|
+
for (const element of pair.prevStop.elements ?? [])
|
|
159
|
+
ids.add(element.id);
|
|
160
|
+
for (const element of pair.nextStop.elements ?? [])
|
|
161
|
+
ids.add(element.id);
|
|
162
|
+
for (const id of ids) {
|
|
163
|
+
const frame = withRemovedElementGeometry(interpolateElement(id, pair.prevStop, pair.nextStop, pair.t), bundle.scenes, id, progress);
|
|
164
|
+
result.set(id, frame);
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
function resolveConnectorFrameMap(bundle, progress) {
|
|
169
|
+
const pair = findSurroundingStops(bundle.scenes, progress);
|
|
170
|
+
const result = new Map();
|
|
171
|
+
if (!pair)
|
|
172
|
+
return result;
|
|
173
|
+
const ids = new Set();
|
|
174
|
+
for (const stop of bundle.scenes) {
|
|
175
|
+
for (const connector of stop.connectors ?? [])
|
|
176
|
+
ids.add(connector.id);
|
|
177
|
+
}
|
|
178
|
+
for (const connector of pair.prevStop.connectors ?? [])
|
|
179
|
+
ids.add(connector.id);
|
|
180
|
+
for (const connector of pair.nextStop.connectors ?? [])
|
|
181
|
+
ids.add(connector.id);
|
|
182
|
+
for (const id of ids) {
|
|
183
|
+
const frame = interpolateConnector(id, pair.prevStop, pair.nextStop, pair.t);
|
|
184
|
+
result.set(id, frame);
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
function findSurroundingStops(stops, progress) {
|
|
189
|
+
if (stops.length === 0)
|
|
190
|
+
return null;
|
|
191
|
+
const sorted = [...stops].sort((a, b) => a.progress - b.progress);
|
|
192
|
+
if (sorted.length === 1 || progress <= sorted[0].progress) {
|
|
193
|
+
return { prevStop: sorted[0], nextStop: sorted[0], t: 0, nextIndex: 0 };
|
|
194
|
+
}
|
|
195
|
+
const lastIndex = sorted.length - 1;
|
|
196
|
+
if (progress >= sorted[lastIndex].progress) {
|
|
197
|
+
return {
|
|
198
|
+
prevStop: sorted[lastIndex - 1] ?? sorted[lastIndex],
|
|
199
|
+
nextStop: sorted[lastIndex],
|
|
200
|
+
t: 1,
|
|
201
|
+
nextIndex: lastIndex,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
205
|
+
const nextStop = sorted[i];
|
|
206
|
+
if (nextStop.progress >= progress) {
|
|
207
|
+
const prevStop = sorted[i - 1];
|
|
208
|
+
const range = nextStop.progress - prevStop.progress;
|
|
209
|
+
const t = range > 0 ? (progress - prevStop.progress) / range : 0;
|
|
210
|
+
return { prevStop, nextStop, t, nextIndex: i };
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
function interpolateElement(id, prevStop, nextStop, t) {
|
|
216
|
+
const prev = findElement(prevStop, id);
|
|
217
|
+
const next = findElement(nextStop, id);
|
|
218
|
+
const source = next ?? prev;
|
|
219
|
+
if (!source) {
|
|
220
|
+
return removedFrame(id);
|
|
221
|
+
}
|
|
222
|
+
if (!prev || prev.presence === "removed") {
|
|
223
|
+
return frameFromElement(source, source.presence === "removed" || t < 1 ? "removed" : source.presence);
|
|
224
|
+
}
|
|
225
|
+
if (!next || next.presence === "removed") {
|
|
226
|
+
return frameFromElement(prev, t < 1 ? prev.presence : "removed");
|
|
227
|
+
}
|
|
228
|
+
const lifecycle = t < 1 ? prev.presence : next.presence;
|
|
229
|
+
return {
|
|
230
|
+
id,
|
|
231
|
+
asset: next.asset,
|
|
232
|
+
pos: interpolatePos(prev.pos, next.pos, t),
|
|
233
|
+
size: prev.size + (next.size - prev.size) * t,
|
|
234
|
+
lifecycle,
|
|
235
|
+
ambient: cloneAmbient(next.ambient),
|
|
236
|
+
layer: t < 1 ? prev.layer : next.layer,
|
|
237
|
+
entry: next.enter ?? prev.enter,
|
|
238
|
+
exit: next.exit ?? prev.exit,
|
|
239
|
+
text: cloneText(next.text ?? prev.text),
|
|
240
|
+
primitive: clonePrimitive(next.primitive ?? prev.primitive),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function interpolateConnector(id, prevStop, nextStop, t) {
|
|
244
|
+
const prev = findConnector(prevStop, id);
|
|
245
|
+
const next = findConnector(nextStop, id);
|
|
246
|
+
const source = next ?? prev;
|
|
247
|
+
if (!source)
|
|
248
|
+
return removedConnectorFrame(id);
|
|
249
|
+
if (!prev || prev.presence === "removed") {
|
|
250
|
+
return frameFromConnector(source, source.presence === "removed" || t < 1 ? "removed" : source.presence);
|
|
251
|
+
}
|
|
252
|
+
if (!next || next.presence === "removed") {
|
|
253
|
+
return frameFromConnector(prev, t < 1 ? prev.presence : "removed");
|
|
254
|
+
}
|
|
255
|
+
const lifecycle = t < 1 ? prev.presence : next.presence;
|
|
256
|
+
return {
|
|
257
|
+
id,
|
|
258
|
+
route: interpolateRoute(prev.route, next.route, t),
|
|
259
|
+
layer: t < 1 ? prev.layer : next.layer,
|
|
260
|
+
lifecycle,
|
|
261
|
+
style: cloneConnectorStyle(next.style),
|
|
262
|
+
start: next.start,
|
|
263
|
+
end: next.end,
|
|
264
|
+
direction: next.direction,
|
|
265
|
+
ambient: cloneAmbient(next.ambient),
|
|
266
|
+
entry: next.enter ?? prev.enter,
|
|
267
|
+
exit: next.exit ?? prev.exit,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
function findElement(stop, id) {
|
|
271
|
+
return (stop.elements ?? []).find((element) => element.id === id);
|
|
272
|
+
}
|
|
273
|
+
function findConnector(stop, id) {
|
|
274
|
+
return (stop.connectors ?? []).find((connector) => connector.id === id);
|
|
275
|
+
}
|
|
276
|
+
function withRemovedElementGeometry(frame, stops, id, progress) {
|
|
277
|
+
if (frame.lifecycle !== "removed")
|
|
278
|
+
return frame;
|
|
279
|
+
const reference = findNearestElementGeometry(stops, id, progress);
|
|
280
|
+
if (!reference)
|
|
281
|
+
return frame;
|
|
282
|
+
return {
|
|
283
|
+
...frame,
|
|
284
|
+
asset: reference.asset,
|
|
285
|
+
pos: [...reference.pos],
|
|
286
|
+
size: reference.size,
|
|
287
|
+
ambient: cloneAmbient(reference.ambient),
|
|
288
|
+
layer: reference.layer,
|
|
289
|
+
entry: reference.enter,
|
|
290
|
+
exit: reference.exit,
|
|
291
|
+
text: cloneText(reference.text),
|
|
292
|
+
primitive: clonePrimitive(reference.primitive),
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
function findNearestElementGeometry(stops, id, progress) {
|
|
296
|
+
const sorted = [...stops].sort((a, b) => a.progress - b.progress);
|
|
297
|
+
const next = sorted
|
|
298
|
+
.filter((stop) => stop.progress >= progress)
|
|
299
|
+
.flatMap((stop) => stop.elements ?? [])
|
|
300
|
+
.find((element) => element.id === id && element.presence !== "removed");
|
|
301
|
+
if (next)
|
|
302
|
+
return next;
|
|
303
|
+
for (let index = sorted.length - 1; index >= 0; index -= 1) {
|
|
304
|
+
if (sorted[index].progress > progress)
|
|
305
|
+
continue;
|
|
306
|
+
const previous = (sorted[index].elements ?? []).find((element) => element.id === id && element.presence !== "removed");
|
|
307
|
+
if (previous)
|
|
308
|
+
return previous;
|
|
309
|
+
}
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
function frameFromElement(element, lifecycle = element.presence) {
|
|
313
|
+
return {
|
|
314
|
+
id: element.id,
|
|
315
|
+
asset: element.asset,
|
|
316
|
+
pos: [...element.pos],
|
|
317
|
+
size: element.size,
|
|
318
|
+
lifecycle,
|
|
319
|
+
ambient: cloneAmbient(element.ambient),
|
|
320
|
+
layer: element.layer,
|
|
321
|
+
entry: element.enter,
|
|
322
|
+
exit: element.exit,
|
|
323
|
+
text: cloneText(element.text),
|
|
324
|
+
primitive: clonePrimitive(element.primitive),
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function frameFromConnector(connector, lifecycle = connector.presence) {
|
|
328
|
+
return {
|
|
329
|
+
id: connector.id,
|
|
330
|
+
route: cloneRoute(connector.route),
|
|
331
|
+
layer: connector.layer,
|
|
332
|
+
lifecycle,
|
|
333
|
+
style: cloneConnectorStyle(connector.style),
|
|
334
|
+
start: connector.start,
|
|
335
|
+
end: connector.end,
|
|
336
|
+
direction: connector.direction,
|
|
337
|
+
ambient: cloneAmbient(connector.ambient),
|
|
338
|
+
entry: connector.enter,
|
|
339
|
+
exit: connector.exit,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
function removedFrame(id) {
|
|
343
|
+
return {
|
|
344
|
+
id,
|
|
345
|
+
asset: "",
|
|
346
|
+
pos: [0, 0],
|
|
347
|
+
size: 1,
|
|
348
|
+
lifecycle: "removed",
|
|
349
|
+
ambient: [],
|
|
350
|
+
layer: "",
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
function removedConnectorFrame(id) {
|
|
354
|
+
return {
|
|
355
|
+
id,
|
|
356
|
+
route: [],
|
|
357
|
+
layer: "",
|
|
358
|
+
lifecycle: "removed",
|
|
359
|
+
style: {
|
|
360
|
+
variant: "line",
|
|
361
|
+
pattern: "solid",
|
|
362
|
+
stroke: "#2563eb",
|
|
363
|
+
strokeWidth: 3,
|
|
364
|
+
opacity: 1,
|
|
365
|
+
outlineWidth: 0,
|
|
366
|
+
lane: "none",
|
|
367
|
+
},
|
|
368
|
+
start: "none",
|
|
369
|
+
end: "none",
|
|
370
|
+
direction: "route",
|
|
371
|
+
ambient: [],
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
function interpolatePos(prev, next, t) {
|
|
375
|
+
return [prev[0] + (next[0] - prev[0]) * t, prev[1] + (next[1] - prev[1]) * t];
|
|
376
|
+
}
|
|
377
|
+
function interpolateRoute(prev, next, t) {
|
|
378
|
+
if (prev.length !== next.length)
|
|
379
|
+
return cloneRoute(t < 1 ? prev : next);
|
|
380
|
+
return prev.map((point, index) => interpolatePos(point, next[index], t));
|
|
381
|
+
}
|
|
382
|
+
function cloneRoute(route) {
|
|
383
|
+
return route.map((point) => [point[0], point[1]]);
|
|
384
|
+
}
|
|
385
|
+
function cloneConnectorStyle(style) {
|
|
386
|
+
return {
|
|
387
|
+
...style,
|
|
388
|
+
...(style.dash ? { dash: [...style.dash] } : {}),
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
function cloneAmbient(ambient) {
|
|
392
|
+
return (ambient ?? []).map((item) => ({ ...item }));
|
|
393
|
+
}
|
|
394
|
+
function frameToUpdate(frame) {
|
|
395
|
+
return {
|
|
396
|
+
id: frame.id,
|
|
397
|
+
asset: frame.asset,
|
|
398
|
+
lifecycle: frame.lifecycle,
|
|
399
|
+
ambient: cloneAmbient(frame.ambient),
|
|
400
|
+
pos: [...frame.pos],
|
|
401
|
+
size: frame.size,
|
|
402
|
+
layer: frame.layer,
|
|
403
|
+
entry: frame.entry,
|
|
404
|
+
exit: frame.exit,
|
|
405
|
+
text: cloneText(frame.text),
|
|
406
|
+
primitive: clonePrimitive(frame.primitive),
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function connectorFrameToUpdate(frame) {
|
|
410
|
+
return {
|
|
411
|
+
id: frame.id,
|
|
412
|
+
route: cloneRoute(frame.route),
|
|
413
|
+
layer: frame.layer,
|
|
414
|
+
lifecycle: frame.lifecycle,
|
|
415
|
+
style: cloneConnectorStyle(frame.style),
|
|
416
|
+
start: frame.start,
|
|
417
|
+
end: frame.end,
|
|
418
|
+
direction: frame.direction,
|
|
419
|
+
ambient: cloneAmbient(frame.ambient),
|
|
420
|
+
entry: frame.entry,
|
|
421
|
+
exit: frame.exit,
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
function cloneFrameMap(map) {
|
|
425
|
+
const clone = new Map();
|
|
426
|
+
for (const [id, frame] of map) {
|
|
427
|
+
clone.set(id, {
|
|
428
|
+
...frame,
|
|
429
|
+
pos: [...frame.pos],
|
|
430
|
+
ambient: cloneAmbient(frame.ambient),
|
|
431
|
+
text: cloneText(frame.text),
|
|
432
|
+
primitive: clonePrimitive(frame.primitive),
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
return clone;
|
|
436
|
+
}
|
|
437
|
+
function cloneText(text) {
|
|
438
|
+
return text ? { ...text } : undefined;
|
|
439
|
+
}
|
|
440
|
+
function clonePrimitive(primitive) {
|
|
441
|
+
if (!primitive)
|
|
442
|
+
return undefined;
|
|
443
|
+
return {
|
|
444
|
+
...(primitive.rectangle
|
|
445
|
+
? {
|
|
446
|
+
rectangle: {
|
|
447
|
+
...primitive.rectangle,
|
|
448
|
+
dash: cloneDash(primitive.rectangle.dash),
|
|
449
|
+
},
|
|
450
|
+
}
|
|
451
|
+
: {}),
|
|
452
|
+
...(primitive.circle
|
|
453
|
+
? {
|
|
454
|
+
circle: {
|
|
455
|
+
...primitive.circle,
|
|
456
|
+
dash: cloneDash(primitive.circle.dash),
|
|
457
|
+
},
|
|
458
|
+
}
|
|
459
|
+
: {}),
|
|
460
|
+
...(primitive.polygon
|
|
461
|
+
? {
|
|
462
|
+
polygon: {
|
|
463
|
+
...primitive.polygon,
|
|
464
|
+
dash: cloneDash(primitive.polygon.dash),
|
|
465
|
+
points: cloneRoute(primitive.polygon.points),
|
|
466
|
+
},
|
|
467
|
+
}
|
|
468
|
+
: {}),
|
|
469
|
+
...(primitive.line
|
|
470
|
+
? {
|
|
471
|
+
line: {
|
|
472
|
+
...primitive.line,
|
|
473
|
+
dash: cloneDash(primitive.line.dash),
|
|
474
|
+
points: cloneRoute(primitive.line.points),
|
|
475
|
+
},
|
|
476
|
+
}
|
|
477
|
+
: {}),
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
function cloneDash(dash) {
|
|
481
|
+
return dash ? [dash[0], dash[1]] : undefined;
|
|
482
|
+
}
|
|
483
|
+
function cloneConnectorFrameMap(map) {
|
|
484
|
+
const clone = new Map();
|
|
485
|
+
for (const [id, frame] of map) {
|
|
486
|
+
clone.set(id, {
|
|
487
|
+
...frame,
|
|
488
|
+
route: cloneRoute(frame.route),
|
|
489
|
+
style: cloneConnectorStyle(frame.style),
|
|
490
|
+
ambient: cloneAmbient(frame.ambient),
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
return clone;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// ── Built-in themes ────────────────────────────────────────────────────────
|
|
497
|
+
const BUILTIN_THEMES = {
|
|
498
|
+
light: {
|
|
499
|
+
"--color-top": "#e2e8f0",
|
|
500
|
+
"--color-front": "#94a3b8",
|
|
501
|
+
"--color-side": "#64748b",
|
|
502
|
+
"--color-back": "#475569",
|
|
503
|
+
"--color-leaf": "#15803d",
|
|
504
|
+
"--color-trunk": "#78350f",
|
|
505
|
+
"--color-accent": "#3b82f6",
|
|
506
|
+
},
|
|
507
|
+
dark: {
|
|
508
|
+
"--color-top": "#334155",
|
|
509
|
+
"--color-front": "#1e293b",
|
|
510
|
+
"--color-side": "#0f172a",
|
|
511
|
+
"--color-back": "#020617",
|
|
512
|
+
"--color-leaf": "#166534",
|
|
513
|
+
"--color-trunk": "#451a03",
|
|
514
|
+
"--color-accent": "#60a5fa",
|
|
515
|
+
},
|
|
516
|
+
brand: {
|
|
517
|
+
"--color-top": "#c7d2fe",
|
|
518
|
+
"--color-front": "#818cf8",
|
|
519
|
+
"--color-side": "#6366f1",
|
|
520
|
+
"--color-back": "#4338ca",
|
|
521
|
+
"--color-leaf": "#22c55e",
|
|
522
|
+
"--color-trunk": "#854d0e",
|
|
523
|
+
"--color-accent": "#f59e0b",
|
|
524
|
+
},
|
|
525
|
+
};
|
|
526
|
+
/**
|
|
527
|
+
* Resolve a theme name to its CSS variable map.
|
|
528
|
+
* Returns undefined if the theme is not found.
|
|
529
|
+
*/
|
|
530
|
+
function resolveTheme(name) {
|
|
531
|
+
return BUILTIN_THEMES[name];
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Compose a new theme by extending an existing one with overrides.
|
|
535
|
+
*/
|
|
536
|
+
function composeTheme(baseName, overrides) {
|
|
537
|
+
const base = BUILTIN_THEMES[baseName];
|
|
538
|
+
if (!base) {
|
|
539
|
+
return { name: baseName, vars: { ...overrides } };
|
|
540
|
+
}
|
|
541
|
+
return { name: baseName, vars: { ...base, ...overrides } };
|
|
542
|
+
}
|
|
543
|
+
// ── Asset registry implementation ──────────────────────────────────────────
|
|
544
|
+
/**
|
|
545
|
+
* Default asset registry implementation.
|
|
546
|
+
* Maps asset ids to definitions and supports category filtering.
|
|
547
|
+
*/
|
|
548
|
+
class AssetRegistryImpl {
|
|
549
|
+
_assets = new Map();
|
|
550
|
+
register(asset) {
|
|
551
|
+
this._assets.set(asset.id, asset);
|
|
552
|
+
}
|
|
553
|
+
get(id) {
|
|
554
|
+
return this._assets.get(id);
|
|
555
|
+
}
|
|
556
|
+
getAll(category) {
|
|
557
|
+
if (category) {
|
|
558
|
+
return [...this._assets.values()].filter((a) => a.category === category);
|
|
559
|
+
}
|
|
560
|
+
return [...this._assets.values()];
|
|
561
|
+
}
|
|
562
|
+
has(id) {
|
|
563
|
+
return this._assets.has(id);
|
|
564
|
+
}
|
|
565
|
+
remove(id) {
|
|
566
|
+
this._assets.delete(id);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
function createAssetRegistry(assets = []) {
|
|
570
|
+
const registry = new AssetRegistryImpl();
|
|
571
|
+
for (const asset of assets) {
|
|
572
|
+
registry.register(asset);
|
|
573
|
+
}
|
|
574
|
+
return registry;
|
|
575
|
+
}
|
|
576
|
+
/** Create a fresh registry populated with the built-in demo assets. */
|
|
577
|
+
function createDefaultRegistry() {
|
|
578
|
+
return createAssetRegistry([
|
|
579
|
+
{
|
|
580
|
+
id: "iso-platform",
|
|
581
|
+
category: "infrastructure",
|
|
582
|
+
},
|
|
583
|
+
{
|
|
584
|
+
id: "iso-server",
|
|
585
|
+
category: "equipment",
|
|
586
|
+
},
|
|
587
|
+
{
|
|
588
|
+
id: "iso-database",
|
|
589
|
+
category: "equipment",
|
|
590
|
+
},
|
|
591
|
+
{
|
|
592
|
+
id: "iso-connector",
|
|
593
|
+
category: "decoration",
|
|
594
|
+
},
|
|
595
|
+
{
|
|
596
|
+
id: "iso-cloud",
|
|
597
|
+
category: "decoration",
|
|
598
|
+
},
|
|
599
|
+
]);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** Default cell size in pixels */
|
|
603
|
+
const DEFAULT_CELL_SIZE = 64;
|
|
604
|
+
/**
|
|
605
|
+
* Calculate raw scene-space coordinates from isometric grid position.
|
|
606
|
+
* Layout is applied separately by subtracting resolved bounds and adding padding.
|
|
607
|
+
*/
|
|
608
|
+
function projectToRaw(gridX, gridY, cellSize) {
|
|
609
|
+
return {
|
|
610
|
+
rawX: cellSize * (gridX - gridY) * 0.5,
|
|
611
|
+
rawY: cellSize * (gridX + gridY) * 0.25,
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* Calculate screen coordinates from raw isometric projection and resolved bounds.
|
|
616
|
+
*/
|
|
617
|
+
function projectToScreen(gridX, gridY, cellSize, boundsMinX = 0, boundsMinY = 0, paddingX = 0, paddingY = 0) {
|
|
618
|
+
const { rawX, rawY } = projectToRaw(gridX, gridY, cellSize);
|
|
619
|
+
return {
|
|
620
|
+
screenX: rawX - boundsMinX + paddingX,
|
|
621
|
+
screenY: rawY - boundsMinY + paddingY,
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
/** Calculate the screen size for an element based on its grid size. */
|
|
625
|
+
function calculateVisualSize(gridSize, cellSize) {
|
|
626
|
+
return cellSize * gridSize;
|
|
627
|
+
}
|
|
628
|
+
/** Calculate the element transform string for positioning and scaling. */
|
|
629
|
+
function calculateTransform(screenX, screenY, visualSize, cellSize) {
|
|
630
|
+
const scale = visualSize / cellSize;
|
|
631
|
+
return `translate(${screenX}px, ${screenY}px) scale(${scale})`;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** Build built-in entry, exit, ambient, and reduced-motion CSS keyframes. */
|
|
635
|
+
function buildKeyframeCSS() {
|
|
636
|
+
return [
|
|
637
|
+
"@keyframes iso-anim-fade-in{from{opacity:0}to{opacity:1}}",
|
|
638
|
+
"@keyframes iso-anim-fade-in-grow{from{opacity:0;scale:.01}to{opacity:1;scale:1}}",
|
|
639
|
+
"@keyframes iso-anim-fall-in{from{opacity:0;translate:0 -40px}to{opacity:1;translate:0 0}}",
|
|
640
|
+
"@keyframes iso-anim-rise-from-ground{from{opacity:0;translate:0 40px}to{opacity:1;translate:0 0}}",
|
|
641
|
+
"@keyframes iso-anim-slide-in-left{from{opacity:0;translate:-60px 0}to{opacity:1;translate:0 0}}",
|
|
642
|
+
"@keyframes iso-anim-slide-in-right{from{opacity:0;translate:60px 0}to{opacity:1;translate:0 0}}",
|
|
643
|
+
"@keyframes iso-anim-flip-in{from{opacity:1;scale:0 1}to{opacity:1;scale:1 1}}",
|
|
644
|
+
"@keyframes iso-anim-fade-out{from{opacity:1}to{opacity:0}}",
|
|
645
|
+
"@keyframes iso-anim-fade-out-shrink{from{opacity:1;scale:1}to{opacity:0;scale:.01}}",
|
|
646
|
+
"@keyframes iso-anim-fall-through-ground{from{opacity:1;translate:0 0}to{opacity:0;translate:0 40px}}",
|
|
647
|
+
"@keyframes iso-anim-rise-away{from{opacity:1;translate:0 0}to{opacity:0;translate:0 -40px}}",
|
|
648
|
+
"@keyframes iso-anim-slide-out-left{from{opacity:1;translate:0 0}to{opacity:0;translate:-60px 0}}",
|
|
649
|
+
"@keyframes iso-anim-slide-out-right{from{opacity:1;translate:0 0}to{opacity:0;translate:60px 0}}",
|
|
650
|
+
"@keyframes iso-anim-flip-out{from{opacity:1;scale:1 1}to{opacity:0;scale:0 1}}",
|
|
651
|
+
"@keyframes iso-anim-pulse{0%,100%{opacity:.7}50%{opacity:1}}",
|
|
652
|
+
"@keyframes iso-anim-float{0%,100%{translate:0 0}50%{translate:0 -6px}}",
|
|
653
|
+
"@keyframes iso-anim-shake{0%,25%,75%,100%{translate:0 0}50%{translate:3px 0}}",
|
|
654
|
+
"@keyframes iso-anim-glow{0%,100%{filter:drop-shadow(0 0 2px rgba(255,255,255,.5))}50%{filter:drop-shadow(0 0 8px rgba(255,255,255,.9))}}",
|
|
655
|
+
"@keyframes iso-anim-spin{from{rotate:0deg}to{rotate:360deg}}",
|
|
656
|
+
"@keyframes iso-anim-blink{0%,49%{opacity:1}50%,100%{opacity:0}}",
|
|
657
|
+
"@keyframes iso-connector-flow-route{from{stroke-dashoffset:0}to{stroke-dashoffset:calc(-1px * var(--iso-flow-distance,20))}}",
|
|
658
|
+
"@keyframes iso-connector-flow-reverse{from{stroke-dashoffset:0}to{stroke-dashoffset:calc(1px * var(--iso-flow-distance,20))}}",
|
|
659
|
+
".iso-connector-pattern-dashed .iso-connector-shaft,.iso-connector-pattern-dotted .iso-connector-shaft{--iso-flow-distance:20}",
|
|
660
|
+
".iso-connector-direction-route .iso-connector-shaft.iso-ambient-flow{animation:iso-connector-flow-route 900ms linear infinite}",
|
|
661
|
+
".iso-connector-direction-reverse .iso-connector-shaft.iso-ambient-flow{animation:iso-connector-flow-reverse 900ms linear infinite}",
|
|
662
|
+
"@media (prefers-reduced-motion: reduce){.iso-element,.iso-connector{animation-duration:1ms!important}.iso-ambient-pulse,.iso-ambient-float,.iso-ambient-shake,.iso-ambient-glow,.iso-ambient-spin,.iso-ambient-blink,.iso-ambient-flow{animation:none!important}}",
|
|
663
|
+
].join("\n");
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const NS$1 = "http://www.w3.org/2000/svg";
|
|
667
|
+
function createAssetResolver(bundle) {
|
|
668
|
+
const embedded = new Map();
|
|
669
|
+
const bundleAssets = bundle?.assets;
|
|
670
|
+
if (bundleAssets && typeof bundleAssets === "object") {
|
|
671
|
+
for (const [name, asset] of Object.entries(bundleAssets)) {
|
|
672
|
+
if (asset && typeof asset === "object" && typeof asset.url === "string") {
|
|
673
|
+
embedded.set(name, { url: asset.url, anchor: asset.anchor });
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
return (name) => embedded.get(name);
|
|
678
|
+
}
|
|
679
|
+
function createAssetNode(asset, assetName, cellSize) {
|
|
680
|
+
return createUrlAssetNode(asset.url, assetName, cellSize, asset.anchor);
|
|
681
|
+
}
|
|
682
|
+
function createTextAssetNode(textContent, assetName, cellSize) {
|
|
683
|
+
if (!textContent?.value) {
|
|
684
|
+
throw new RenderError("TEXT_CONTENT_MISSING", `Text content is missing for built-in asset: ${assetName}`, {
|
|
685
|
+
asset: assetName,
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
const group = document.createElementNS(NS$1, "g");
|
|
689
|
+
const text = document.createElementNS(NS$1, "text");
|
|
690
|
+
const align = textContent.align ?? "middle";
|
|
691
|
+
const fontSize = textContent.fontSize ?? 12;
|
|
692
|
+
const lineHeight = textContent.lineHeight ?? 1.2;
|
|
693
|
+
const anchorX = textAnchorX(align, cellSize);
|
|
694
|
+
text.setAttribute("x", String(anchorX));
|
|
695
|
+
text.setAttribute("y", String(-cellSize));
|
|
696
|
+
text.setAttribute("text-anchor", align);
|
|
697
|
+
text.setAttribute("dominant-baseline", "text-before-edge");
|
|
698
|
+
text.setAttribute("font-family", "Arial, Helvetica, sans-serif");
|
|
699
|
+
text.setAttribute("font-size", String(fontSize));
|
|
700
|
+
text.setAttribute("font-weight", String(textContent.fontWeight ?? 700));
|
|
701
|
+
text.setAttribute("fill", textContent.fill ?? "currentColor");
|
|
702
|
+
const lines = normalizeTextLines(textContent.value);
|
|
703
|
+
for (const [index, line] of lines.entries()) {
|
|
704
|
+
const tspan = document.createElementNS(NS$1, "tspan");
|
|
705
|
+
tspan.setAttribute("x", String(anchorX));
|
|
706
|
+
tspan.setAttribute("dy", index === 0 ? "0" : String(fontSize * lineHeight));
|
|
707
|
+
tspan.textContent = line;
|
|
708
|
+
text.appendChild(tspan);
|
|
709
|
+
}
|
|
710
|
+
group.appendChild(text);
|
|
711
|
+
return group;
|
|
712
|
+
}
|
|
713
|
+
function createPrimitiveAssetNode(assetName, primitive, cellSize) {
|
|
714
|
+
const group = document.createElementNS(NS$1, "g");
|
|
715
|
+
switch (assetName) {
|
|
716
|
+
case "rectangle":
|
|
717
|
+
appendProjectedPolygon(group, rectanglePoints(), primitive?.rectangle, cellSize);
|
|
718
|
+
return group;
|
|
719
|
+
case "polygon":
|
|
720
|
+
appendProjectedPolygon(group, primitive?.polygon?.points, primitive?.polygon, cellSize);
|
|
721
|
+
return group;
|
|
722
|
+
case "line":
|
|
723
|
+
appendProjectedPolyline(group, primitive?.line, primitive?.line, cellSize);
|
|
724
|
+
return group;
|
|
725
|
+
case "circle":
|
|
726
|
+
appendCircle(group, primitive?.circle, cellSize);
|
|
727
|
+
return group;
|
|
728
|
+
default:
|
|
729
|
+
throw new RenderError("PRIMITIVE_ASSET_UNKNOWN", `Unknown built-in primitive asset: ${assetName}`, {
|
|
730
|
+
asset: assetName,
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
function textAnchorX(align, cellSize) {
|
|
735
|
+
if (align === "start")
|
|
736
|
+
return -cellSize / 2;
|
|
737
|
+
if (align === "end")
|
|
738
|
+
return cellSize / 2;
|
|
739
|
+
return 0;
|
|
740
|
+
}
|
|
741
|
+
function normalizeTextLines(value) {
|
|
742
|
+
const normalized = value.replace(/\r\n?/g, "\n").replace(/\n$/, "");
|
|
743
|
+
return normalized.split("\n");
|
|
744
|
+
}
|
|
745
|
+
function rectanglePoints() {
|
|
746
|
+
return [
|
|
747
|
+
[0, 0],
|
|
748
|
+
[1, 0],
|
|
749
|
+
[1, 1],
|
|
750
|
+
[0, 1],
|
|
751
|
+
];
|
|
752
|
+
}
|
|
753
|
+
function appendProjectedPolygon(group, points, style, cellSize) {
|
|
754
|
+
if (!points)
|
|
755
|
+
return;
|
|
756
|
+
const polygon = document.createElementNS(NS$1, "polygon");
|
|
757
|
+
polygon.setAttribute("points", points.map((point) => projectLocalPoint(point, cellSize)).join(" "));
|
|
758
|
+
applyPrimitiveStyle(polygon, style, { fill: "currentColor", stroke: "none" });
|
|
759
|
+
group.appendChild(polygon);
|
|
760
|
+
}
|
|
761
|
+
function appendProjectedPolyline(group, line, style, cellSize) {
|
|
762
|
+
if (!line?.points)
|
|
763
|
+
return;
|
|
764
|
+
const polyline = document.createElementNS(NS$1, "polyline");
|
|
765
|
+
polyline.setAttribute("points", line.points.map((point) => projectLocalPoint(point, cellSize)).join(" "));
|
|
766
|
+
polyline.setAttribute("fill", "none");
|
|
767
|
+
polyline.setAttribute("stroke-linecap", line.lineCap ?? "round");
|
|
768
|
+
polyline.setAttribute("stroke-linejoin", line.lineJoin ?? "round");
|
|
769
|
+
applyPrimitiveStyle(polyline, style, {
|
|
770
|
+
fill: "none",
|
|
771
|
+
stroke: "currentColor",
|
|
772
|
+
});
|
|
773
|
+
group.appendChild(polyline);
|
|
774
|
+
}
|
|
775
|
+
function appendCircle(group, style, cellSize) {
|
|
776
|
+
const center = projectLocalPoint([0.5, 0.5], cellSize);
|
|
777
|
+
const circle = document.createElementNS(NS$1, "circle");
|
|
778
|
+
const [cx, cy] = center.split(",").map(Number);
|
|
779
|
+
circle.setAttribute("cx", String(cx));
|
|
780
|
+
circle.setAttribute("cy", String(cy));
|
|
781
|
+
circle.setAttribute("r", String(cellSize * 0.2));
|
|
782
|
+
applyPrimitiveStyle(circle, style, {
|
|
783
|
+
fill: "currentColor",
|
|
784
|
+
stroke: "none",
|
|
785
|
+
});
|
|
786
|
+
group.appendChild(circle);
|
|
787
|
+
}
|
|
788
|
+
function projectLocalPoint(point, cellSize) {
|
|
789
|
+
const projected = projectToRaw(point[0], point[1], cellSize);
|
|
790
|
+
const anchor = projectToRaw(1, 1, cellSize);
|
|
791
|
+
return `${projected.rawX - anchor.rawX},${projected.rawY - anchor.rawY}`;
|
|
792
|
+
}
|
|
793
|
+
function applyPrimitiveStyle(node, style, defaults) {
|
|
794
|
+
node.setAttribute("fill", style?.fill ?? defaults.fill);
|
|
795
|
+
node.setAttribute("stroke", style?.stroke ?? defaults.stroke);
|
|
796
|
+
node.setAttribute("stroke-width", String(style?.strokeWidth ?? 0));
|
|
797
|
+
node.setAttribute("opacity", String(style?.opacity ?? 1));
|
|
798
|
+
if (style?.dash)
|
|
799
|
+
node.setAttribute("stroke-dasharray", style.dash.join(" "));
|
|
800
|
+
}
|
|
801
|
+
function createUrlAssetNode(url, assetName, cellSize, anchor) {
|
|
802
|
+
if (!isSafeAssetUrl(url)) {
|
|
803
|
+
throw new RenderError("INVALID_ASSET_URL", `Asset URL is unsafe: ${assetName}`, { asset: assetName });
|
|
804
|
+
}
|
|
805
|
+
const group = document.createElementNS(NS$1, "g");
|
|
806
|
+
const image = document.createElementNS(NS$1, "image");
|
|
807
|
+
const resolvedUrl = resolveBrowserAssetUrl(url);
|
|
808
|
+
const [anchorX, anchorY] = anchor ?? [0.5, 1];
|
|
809
|
+
image.setAttribute("href", resolvedUrl);
|
|
810
|
+
image.setAttributeNS("http://www.w3.org/1999/xlink", "href", resolvedUrl);
|
|
811
|
+
image.setAttribute("x", String(-cellSize * anchorX));
|
|
812
|
+
image.setAttribute("y", String(-cellSize * anchorY));
|
|
813
|
+
image.setAttribute("width", String(cellSize));
|
|
814
|
+
image.setAttribute("height", String(cellSize));
|
|
815
|
+
image.setAttribute("preserveAspectRatio", "xMidYMax meet");
|
|
816
|
+
group.appendChild(image);
|
|
817
|
+
return group;
|
|
818
|
+
}
|
|
819
|
+
function resolveBrowserAssetUrl(url) {
|
|
820
|
+
try {
|
|
821
|
+
const baseURI = document.baseURI;
|
|
822
|
+
return typeof baseURI === "string" ? new URL(url, baseURI).href : url;
|
|
823
|
+
}
|
|
824
|
+
catch {
|
|
825
|
+
return url;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
function isSafeAssetUrl(url) {
|
|
829
|
+
const normalized = url.trim().toLowerCase();
|
|
830
|
+
return normalized.length > 0 && !normalized.startsWith("javascript:");
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
const CSS_CUSTOM_PROPERTY_PATTERN = /^--[a-zA-Z0-9-_]+$/;
|
|
834
|
+
function applyThemeToElement(element, themeVars) {
|
|
835
|
+
for (const [name, value] of Object.entries(themeVars)) {
|
|
836
|
+
if (!CSS_CUSTOM_PROPERTY_PATTERN.test(name)) {
|
|
837
|
+
throw new RenderError("INVALID_THEME_VAR", `Invalid CSS custom property name: ${name}`);
|
|
838
|
+
}
|
|
839
|
+
element.style.setProperty(name, value);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
const NS = "http://www.w3.org/2000/svg";
|
|
844
|
+
const BUILT_IN_TEXT_ASSET_ID = "text";
|
|
845
|
+
const BUILT_IN_PRIMITIVE_ASSET_IDS = new Set(["rectangle", "circle", "polygon", "line"]);
|
|
846
|
+
const DEFAULT_CONNECTOR_DASH = {
|
|
847
|
+
dashed: [12, 8],
|
|
848
|
+
dotted: [0, 8],
|
|
849
|
+
};
|
|
850
|
+
const ENDPOINT_RADIUS_GRID = 0.14;
|
|
851
|
+
const ARROW_LENGTH_GRID = 0.35;
|
|
852
|
+
const ARROW_WIDTH_GRID = 0.28;
|
|
853
|
+
const BAR_WIDTH_GRID = 0.4;
|
|
854
|
+
// ── Public API ────────────────────────────────────────────────────────────
|
|
855
|
+
/** Build the SVG DOM for a compiled runtime bundle and mount it into a container. */
|
|
856
|
+
function buildSceneDOM(container, bundle, config) {
|
|
857
|
+
const layout = resolveSceneLayout(bundle);
|
|
858
|
+
const initialStop = bundle.scenes[0];
|
|
859
|
+
const allElements = collectElementDefinitions(bundle);
|
|
860
|
+
const allConnectors = collectConnectorDefinitions(bundle);
|
|
861
|
+
const initialById = new Map((initialStop?.elements ?? []).map((element) => [element.id, element]));
|
|
862
|
+
const initialConnectorsById = new Map((initialStop?.connectors ?? []).map((connector) => [connector.id, connector]));
|
|
863
|
+
const svg = createRootSvg(layout, config?.label, bundle.className);
|
|
864
|
+
const assetResolver = createAssetResolver(bundle);
|
|
865
|
+
svg.appendChild(createCssDefs());
|
|
866
|
+
applyThemeToElement(svg, {
|
|
867
|
+
...(resolveTheme(bundle.theme) ?? {}),
|
|
868
|
+
...(bundle.themeVars ?? {}),
|
|
869
|
+
...(config?.themeVars ?? {}),
|
|
870
|
+
});
|
|
871
|
+
const sortedLayers = sortLayers(bundle.layers);
|
|
872
|
+
const layerMap = createLayerMap(sortedLayers);
|
|
873
|
+
if (bundle.floor.visible) {
|
|
874
|
+
svg.appendChild(createFloorGrid(bundle, layout));
|
|
875
|
+
}
|
|
876
|
+
const connectorMap = new Map();
|
|
877
|
+
for (const def of allConnectors) {
|
|
878
|
+
const initial = initialConnectorsById.get(def.id) ?? {
|
|
879
|
+
...def,
|
|
880
|
+
presence: "removed",
|
|
881
|
+
};
|
|
882
|
+
const state = createConnectorInstance(initial, layout);
|
|
883
|
+
if (initial.presence === "removed") {
|
|
884
|
+
state.isHidden = true;
|
|
885
|
+
hideElementAfterExit(state.node);
|
|
886
|
+
}
|
|
887
|
+
applyConnectorAmbientClasses(state, initial.ambient ?? []);
|
|
888
|
+
svg.appendChild(state.node);
|
|
889
|
+
connectorMap.set(def.id, state);
|
|
890
|
+
}
|
|
891
|
+
const depthGroup = document.createElementNS(NS, "g");
|
|
892
|
+
depthGroup.classList.add("iso-depth-layer");
|
|
893
|
+
depthGroup.setAttribute("data-layer", "depth");
|
|
894
|
+
svg.appendChild(depthGroup);
|
|
895
|
+
const labelGroup = document.createElementNS(NS, "g");
|
|
896
|
+
labelGroup.classList.add("iso-layer", "iso-layer-labels");
|
|
897
|
+
labelGroup.setAttribute("data-layer", "labels");
|
|
898
|
+
svg.appendChild(labelGroup);
|
|
899
|
+
const elementMap = new Map();
|
|
900
|
+
const sortedElements = sortElementsForPerspective(allElements);
|
|
901
|
+
for (const def of sortedElements) {
|
|
902
|
+
const declaredLayer = layerMap.get(def.layer);
|
|
903
|
+
if (!declaredLayer) {
|
|
904
|
+
throw new RenderError("MISSING_LAYER", `Unknown layer for "${def.id}": ${def.layer}`);
|
|
905
|
+
}
|
|
906
|
+
const initial = initialById.get(def.id) ?? {
|
|
907
|
+
...def,
|
|
908
|
+
presence: "removed",
|
|
909
|
+
};
|
|
910
|
+
const instance = createElementInstance(initial, layout, assetResolver);
|
|
911
|
+
instance.node.classList.add(`iso-layer-${def.layer}`);
|
|
912
|
+
instance.node.setAttribute("data-layer", def.layer);
|
|
913
|
+
if (initial.presence === "removed") {
|
|
914
|
+
instance.isHidden = true;
|
|
915
|
+
hideElementAfterExit(instance.node);
|
|
916
|
+
}
|
|
917
|
+
applyAmbientClasses(instance, initial.ambient ?? []);
|
|
918
|
+
const parent = isTextAsset(def.asset) ? labelGroup : depthGroup;
|
|
919
|
+
parent.appendChild(instance.node);
|
|
920
|
+
elementMap.set(def.id, instance);
|
|
921
|
+
}
|
|
922
|
+
svg._layerMap = layerMap;
|
|
923
|
+
svg._elementMap = elementMap;
|
|
924
|
+
svg._connectorMap = connectorMap;
|
|
925
|
+
container.appendChild(svg);
|
|
926
|
+
return svg;
|
|
927
|
+
}
|
|
928
|
+
/** Update transforms and ambient classes for a live set of interpolated runtime values. */
|
|
929
|
+
function updateElementTransforms(svg, elements, connectors = []) {
|
|
930
|
+
const layout = svg._layout;
|
|
931
|
+
if (!layout)
|
|
932
|
+
return;
|
|
933
|
+
const map = svg._elementMap;
|
|
934
|
+
if (map) {
|
|
935
|
+
for (const def of elements) {
|
|
936
|
+
const state = map.get(def.id);
|
|
937
|
+
if (!state)
|
|
938
|
+
continue;
|
|
939
|
+
updateGeneratedElementContent(state.node, def, layout);
|
|
940
|
+
applyElementTransform(state.node, def, layout);
|
|
941
|
+
applyAmbientClasses(state, def.ambient ?? []);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
const connectorMap = svg._connectorMap;
|
|
945
|
+
if (!connectorMap)
|
|
946
|
+
return;
|
|
947
|
+
for (const def of connectors) {
|
|
948
|
+
const state = connectorMap.get(def.id);
|
|
949
|
+
if (!state)
|
|
950
|
+
continue;
|
|
951
|
+
applyConnectorState(state, def, layout);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
/** Read the internal ElementState for an element by its id. */
|
|
955
|
+
function getElementState(svg, id) {
|
|
956
|
+
return svg._elementMap?.get(id);
|
|
957
|
+
}
|
|
958
|
+
/** Read the internal ConnectorState for a connector by its id. */
|
|
959
|
+
function getConnectorState(svg, id) {
|
|
960
|
+
return svg._connectorMap?.get(id);
|
|
961
|
+
}
|
|
962
|
+
function getResolvedViewBox(bundle) {
|
|
963
|
+
return resolveSceneLayout(bundle).viewBox;
|
|
964
|
+
}
|
|
965
|
+
// ── Lifecycle helpers ─────────────────────────────────────────────────────
|
|
966
|
+
/** Hide an element after its exit animation completes. */
|
|
967
|
+
function hideElementAfterExit(node) {
|
|
968
|
+
node.style.visibility = "hidden";
|
|
969
|
+
node.style.pointerEvents = "none";
|
|
970
|
+
}
|
|
971
|
+
/** Show an element on re-addition. */
|
|
972
|
+
function unhideElementOnReadd(node) {
|
|
973
|
+
node.style.visibility = "visible";
|
|
974
|
+
node.style.pointerEvents = "auto";
|
|
975
|
+
}
|
|
976
|
+
// ── Layout helpers ────────────────────────────────────────────────────────
|
|
977
|
+
function resolveSceneLayout(bundle) {
|
|
978
|
+
const cellSize = bundle.grid.cellSize;
|
|
979
|
+
const padding = bundle.layout.padding;
|
|
980
|
+
const contentBounds = calculateContentBounds(bundle, cellSize);
|
|
981
|
+
const floorBounds = calculateFloorBounds(bundle, cellSize);
|
|
982
|
+
const selectedBounds = selectBounds(bundle.layout.bounds, contentBounds, floorBounds);
|
|
983
|
+
const width = selectedBounds.maxX - selectedBounds.minX + padding.x * 2;
|
|
984
|
+
const height = selectedBounds.maxY - selectedBounds.minY + padding.y * 2;
|
|
985
|
+
const viewBox = {
|
|
986
|
+
minX: 0,
|
|
987
|
+
minY: 0,
|
|
988
|
+
width: roundDimension(width || cellSize),
|
|
989
|
+
height: roundDimension(height || cellSize),
|
|
990
|
+
};
|
|
991
|
+
return {
|
|
992
|
+
cellSize,
|
|
993
|
+
padding,
|
|
994
|
+
contentBounds,
|
|
995
|
+
floorBounds,
|
|
996
|
+
selectedBounds,
|
|
997
|
+
viewBox,
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
function calculateContentBounds(bundle, cellSize) {
|
|
1001
|
+
let bounds = emptyBounds();
|
|
1002
|
+
for (const stop of bundle.scenes) {
|
|
1003
|
+
for (const element of stop.elements ?? []) {
|
|
1004
|
+
if (element.presence === "removed")
|
|
1005
|
+
continue;
|
|
1006
|
+
const { rawX, rawY } = projectToRaw(element.pos[0] + element.size, element.pos[1] + element.size, cellSize);
|
|
1007
|
+
const visualSize = calculateVisualSize(element.size, cellSize);
|
|
1008
|
+
const [anchorX, anchorY] = assetAnchorForBounds(bundle, element);
|
|
1009
|
+
bounds = includeBounds(bounds, {
|
|
1010
|
+
minX: rawX - visualSize * anchorX,
|
|
1011
|
+
minY: rawY - visualSize * anchorY,
|
|
1012
|
+
maxX: rawX + visualSize * (1 - anchorX),
|
|
1013
|
+
maxY: rawY + visualSize * (1 - anchorY),
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
for (const connector of stop.connectors ?? []) {
|
|
1017
|
+
if (connector.presence === "removed")
|
|
1018
|
+
continue;
|
|
1019
|
+
bounds = includeBounds(bounds, calculateConnectorBounds(connector, cellSize));
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return normalizeBounds(bounds, cellSize);
|
|
1023
|
+
}
|
|
1024
|
+
function assetAnchorForBounds(bundle, element) {
|
|
1025
|
+
return bundle.assets?.[element.asset]?.anchor ?? [0.5, 1];
|
|
1026
|
+
}
|
|
1027
|
+
function calculateConnectorBounds(connector, cellSize) {
|
|
1028
|
+
let bounds = emptyBounds();
|
|
1029
|
+
for (const [x, y] of connector.route) {
|
|
1030
|
+
const { rawX, rawY } = projectToRaw(x, y, cellSize);
|
|
1031
|
+
bounds = includePoint(bounds, rawX, rawY);
|
|
1032
|
+
}
|
|
1033
|
+
const endpointPadding = Math.max(ARROW_LENGTH_GRID, BAR_WIDTH_GRID, ENDPOINT_RADIUS_GRID * 2) * cellSize;
|
|
1034
|
+
const strokePadding = connector.style.strokeWidth / 2 + (connector.style.outlineWidth ?? 0);
|
|
1035
|
+
const padding = endpointPadding + strokePadding;
|
|
1036
|
+
return {
|
|
1037
|
+
minX: bounds.minX - padding,
|
|
1038
|
+
minY: bounds.minY - padding,
|
|
1039
|
+
maxX: bounds.maxX + padding,
|
|
1040
|
+
maxY: bounds.maxY + padding,
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
1043
|
+
function calculateFloorBounds(bundle, cellSize) {
|
|
1044
|
+
const origin = bundle.floor.origin;
|
|
1045
|
+
const [width, height] = bundle.floor.size;
|
|
1046
|
+
const points = [
|
|
1047
|
+
origin,
|
|
1048
|
+
[origin[0] + width, origin[1]],
|
|
1049
|
+
[origin[0], origin[1] + height],
|
|
1050
|
+
[origin[0] + width, origin[1] + height],
|
|
1051
|
+
];
|
|
1052
|
+
let bounds = emptyBounds();
|
|
1053
|
+
for (const [x, y] of points) {
|
|
1054
|
+
const { rawX, rawY } = projectToRaw(x, y, cellSize);
|
|
1055
|
+
bounds = includePoint(bounds, rawX, rawY);
|
|
1056
|
+
}
|
|
1057
|
+
return normalizeBounds(bounds, cellSize);
|
|
1058
|
+
}
|
|
1059
|
+
function selectBounds(mode, content, floor) {
|
|
1060
|
+
if (mode === "content")
|
|
1061
|
+
return content;
|
|
1062
|
+
if (mode === "floor")
|
|
1063
|
+
return floor;
|
|
1064
|
+
return includeBounds(content, floor);
|
|
1065
|
+
}
|
|
1066
|
+
function emptyBounds() {
|
|
1067
|
+
return {
|
|
1068
|
+
minX: Number.POSITIVE_INFINITY,
|
|
1069
|
+
minY: Number.POSITIVE_INFINITY,
|
|
1070
|
+
maxX: Number.NEGATIVE_INFINITY,
|
|
1071
|
+
maxY: Number.NEGATIVE_INFINITY,
|
|
1072
|
+
};
|
|
1073
|
+
}
|
|
1074
|
+
function normalizeBounds(bounds, cellSize) {
|
|
1075
|
+
if (Number.isFinite(bounds.minX))
|
|
1076
|
+
return bounds;
|
|
1077
|
+
return { minX: 0, minY: 0, maxX: cellSize, maxY: cellSize };
|
|
1078
|
+
}
|
|
1079
|
+
function includePoint(bounds, x, y) {
|
|
1080
|
+
return {
|
|
1081
|
+
minX: Math.min(bounds.minX, x),
|
|
1082
|
+
minY: Math.min(bounds.minY, y),
|
|
1083
|
+
maxX: Math.max(bounds.maxX, x),
|
|
1084
|
+
maxY: Math.max(bounds.maxY, y),
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
function includeBounds(bounds, next) {
|
|
1088
|
+
return {
|
|
1089
|
+
minX: Math.min(bounds.minX, next.minX),
|
|
1090
|
+
minY: Math.min(bounds.minY, next.minY),
|
|
1091
|
+
maxX: Math.max(bounds.maxX, next.maxX),
|
|
1092
|
+
maxY: Math.max(bounds.maxY, next.maxY),
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
function roundDimension(value) {
|
|
1096
|
+
return Math.round(value * 1000) / 1000;
|
|
1097
|
+
}
|
|
1098
|
+
// ── Private helpers ───────────────────────────────────────────────────────
|
|
1099
|
+
function createRootSvg(layout, label, className) {
|
|
1100
|
+
const svg = document.createElementNS(NS, "svg");
|
|
1101
|
+
svg.classList.add("iso-scene");
|
|
1102
|
+
for (const token of className?.trim().split(/\s+/) ?? []) {
|
|
1103
|
+
if (token)
|
|
1104
|
+
svg.classList.add(token);
|
|
1105
|
+
}
|
|
1106
|
+
svg.setAttribute("width", "100%");
|
|
1107
|
+
svg.setAttribute("height", "100%");
|
|
1108
|
+
svg.setAttribute("viewBox", `${layout.viewBox.minX} ${layout.viewBox.minY} ${layout.viewBox.width} ${layout.viewBox.height}`);
|
|
1109
|
+
svg.setAttribute("preserveAspectRatio", "xMidYMid meet");
|
|
1110
|
+
svg.style.width = "100%";
|
|
1111
|
+
svg.style.height = "100%";
|
|
1112
|
+
svg.style.display = "block";
|
|
1113
|
+
if (label) {
|
|
1114
|
+
svg.setAttribute("role", "img");
|
|
1115
|
+
svg.setAttribute("aria-label", label);
|
|
1116
|
+
}
|
|
1117
|
+
else {
|
|
1118
|
+
svg.setAttribute("aria-hidden", "true");
|
|
1119
|
+
}
|
|
1120
|
+
svg._layout = layout;
|
|
1121
|
+
svg._viewBoxW = layout.viewBox.width;
|
|
1122
|
+
svg._viewBoxH = layout.viewBox.height;
|
|
1123
|
+
return svg;
|
|
1124
|
+
}
|
|
1125
|
+
function createCssDefs() {
|
|
1126
|
+
const styleEl = document.createElementNS(NS, "style");
|
|
1127
|
+
styleEl.textContent = buildKeyframeCSS();
|
|
1128
|
+
return styleEl;
|
|
1129
|
+
}
|
|
1130
|
+
function createFloorGrid(bundle, layout) {
|
|
1131
|
+
const group = document.createElementNS(NS, "g");
|
|
1132
|
+
group.classList.add("iso-floor-grid", `iso-layer-${bundle.floor.layer}`);
|
|
1133
|
+
group.setAttribute("data-layer", bundle.floor.layer);
|
|
1134
|
+
const [originX, originY] = bundle.floor.origin;
|
|
1135
|
+
const [columns, rows] = bundle.floor.size;
|
|
1136
|
+
const corners = [
|
|
1137
|
+
projectGridPoint(originX, originY, layout),
|
|
1138
|
+
projectGridPoint(originX + columns, originY, layout),
|
|
1139
|
+
projectGridPoint(originX + columns, originY + rows, layout),
|
|
1140
|
+
projectGridPoint(originX, originY + rows, layout),
|
|
1141
|
+
];
|
|
1142
|
+
const slab = document.createElementNS(NS, "polygon");
|
|
1143
|
+
slab.classList.add("iso-floor-slab");
|
|
1144
|
+
slab.setAttribute("points", corners.map(pointToString).join(" "));
|
|
1145
|
+
slab.setAttribute("fill", "#dbe6f4");
|
|
1146
|
+
slab.setAttribute("fill-opacity", "0.22");
|
|
1147
|
+
slab.setAttribute("stroke", "#b9c9df");
|
|
1148
|
+
slab.setAttribute("stroke-width", "1");
|
|
1149
|
+
group.appendChild(slab);
|
|
1150
|
+
for (let x = 0; x <= columns; x++) {
|
|
1151
|
+
group.appendChild(createFloorLine(projectGridPoint(originX + x, originY, layout), projectGridPoint(originX + x, originY + rows, layout)));
|
|
1152
|
+
}
|
|
1153
|
+
for (let y = 0; y <= rows; y++) {
|
|
1154
|
+
group.appendChild(createFloorLine(projectGridPoint(originX, originY + y, layout), projectGridPoint(originX + columns, originY + y, layout)));
|
|
1155
|
+
}
|
|
1156
|
+
return group;
|
|
1157
|
+
}
|
|
1158
|
+
function createFloorLine(start, end) {
|
|
1159
|
+
const line = document.createElementNS(NS, "line");
|
|
1160
|
+
line.setAttribute("x1", String(start.x));
|
|
1161
|
+
line.setAttribute("y1", String(start.y));
|
|
1162
|
+
line.setAttribute("x2", String(end.x));
|
|
1163
|
+
line.setAttribute("y2", String(end.y));
|
|
1164
|
+
line.setAttribute("stroke", "#2563eb");
|
|
1165
|
+
line.setAttribute("stroke-width", "1");
|
|
1166
|
+
line.setAttribute("stroke-dasharray", "5 5");
|
|
1167
|
+
line.setAttribute("stroke-opacity", "0.2");
|
|
1168
|
+
return line;
|
|
1169
|
+
}
|
|
1170
|
+
function projectGridPoint(x, y, layout) {
|
|
1171
|
+
const screen = projectToScreen(x, y, layout.cellSize, layout.selectedBounds.minX, layout.selectedBounds.minY, layout.padding.x, layout.padding.y);
|
|
1172
|
+
return { x: screen.screenX, y: screen.screenY };
|
|
1173
|
+
}
|
|
1174
|
+
function pointToString(point) {
|
|
1175
|
+
return `${point.x},${point.y}`;
|
|
1176
|
+
}
|
|
1177
|
+
function createLayerMap(layers) {
|
|
1178
|
+
return new Map(layers.map((layer) => [layer.name, layer]));
|
|
1179
|
+
}
|
|
1180
|
+
function sortLayers(layers) {
|
|
1181
|
+
return [...layers]
|
|
1182
|
+
.map((layer, index) => ({ name: layer.name, order: layer.order ?? index }))
|
|
1183
|
+
.sort((a, b) => a.order - b.order || a.name.localeCompare(b.name));
|
|
1184
|
+
}
|
|
1185
|
+
function sortElementsForPerspective(elements) {
|
|
1186
|
+
return elements.slice().sort((a, b) => {
|
|
1187
|
+
const bucket = renderBucket(a) - renderBucket(b);
|
|
1188
|
+
if (bucket !== 0)
|
|
1189
|
+
return bucket;
|
|
1190
|
+
const depth = a.pos[0] + a.pos[1] - (b.pos[0] + b.pos[1]);
|
|
1191
|
+
if (depth !== 0)
|
|
1192
|
+
return depth;
|
|
1193
|
+
return a.id.localeCompare(b.id);
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1196
|
+
function renderBucket(element) {
|
|
1197
|
+
if (isPrimitiveAsset(element.asset))
|
|
1198
|
+
return 0;
|
|
1199
|
+
if (isTextAsset(element.asset))
|
|
1200
|
+
return 2;
|
|
1201
|
+
return 1;
|
|
1202
|
+
}
|
|
1203
|
+
function collectElementDefinitions(bundle) {
|
|
1204
|
+
const byId = new Map();
|
|
1205
|
+
for (const stop of bundle.scenes) {
|
|
1206
|
+
for (const element of stop.elements ?? []) {
|
|
1207
|
+
if (!byId.has(element.id) || byId.get(element.id)?.presence === "removed") {
|
|
1208
|
+
byId.set(element.id, element);
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
return [...byId.values()];
|
|
1213
|
+
}
|
|
1214
|
+
function collectConnectorDefinitions(bundle) {
|
|
1215
|
+
const byId = new Map();
|
|
1216
|
+
for (const stop of bundle.scenes) {
|
|
1217
|
+
for (const connector of stop.connectors ?? []) {
|
|
1218
|
+
if (!byId.has(connector.id) || byId.get(connector.id)?.presence === "removed") {
|
|
1219
|
+
byId.set(connector.id, connector);
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
return [...byId.values()];
|
|
1224
|
+
}
|
|
1225
|
+
/** Create a single element SVG instance applying entry animation. */
|
|
1226
|
+
function createElementInstance(def, layout, resolveAsset) {
|
|
1227
|
+
const node = def.asset === BUILT_IN_TEXT_ASSET_ID
|
|
1228
|
+
? createTextAssetNode(def.text, def.asset, layout.cellSize)
|
|
1229
|
+
: isPrimitiveAsset(def.asset)
|
|
1230
|
+
? createPrimitiveAssetNode(def.asset, def.primitive, layout.cellSize)
|
|
1231
|
+
: createResolvedAssetNode(def, resolveAsset, layout.cellSize);
|
|
1232
|
+
node.classList.add("iso-element", `iso-element-${def.id}`);
|
|
1233
|
+
node.setAttribute("data-id", def.id);
|
|
1234
|
+
node.setAttribute("data-asset", def.asset);
|
|
1235
|
+
node.style.overflow = "visible";
|
|
1236
|
+
node.style.pointerEvents = "auto";
|
|
1237
|
+
applyElementTransform(node, def, layout);
|
|
1238
|
+
const entryAnim = def.enter;
|
|
1239
|
+
if (entryAnim && entryAnim !== "none" && def.presence !== "removed") {
|
|
1240
|
+
const keyName = `iso-anim-${entryAnim}`;
|
|
1241
|
+
animateElement(node, keyName, "enter");
|
|
1242
|
+
node.addEventListener("animationend", () => {
|
|
1243
|
+
node.style.animation = "";
|
|
1244
|
+
}, { once: true });
|
|
1245
|
+
return { node, isHidden: false, entryKey: entryAnim, ambient: new Set() };
|
|
1246
|
+
}
|
|
1247
|
+
return { node, isHidden: false, ambient: new Set() };
|
|
1248
|
+
}
|
|
1249
|
+
function isTextAsset(assetId) {
|
|
1250
|
+
return assetId === BUILT_IN_TEXT_ASSET_ID;
|
|
1251
|
+
}
|
|
1252
|
+
function isPrimitiveAsset(assetId) {
|
|
1253
|
+
return BUILT_IN_PRIMITIVE_ASSET_IDS.has(assetId);
|
|
1254
|
+
}
|
|
1255
|
+
function createResolvedAssetNode(def, resolveAsset, cellSize) {
|
|
1256
|
+
const asset = resolveAsset(def.asset);
|
|
1257
|
+
if (!asset) {
|
|
1258
|
+
throw new RenderError("ASSET_NOT_FOUND", `Asset not found: ${def.asset}`, {
|
|
1259
|
+
asset: def.asset,
|
|
1260
|
+
elementId: def.id,
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
return createAssetNode(asset, def.asset, cellSize);
|
|
1264
|
+
}
|
|
1265
|
+
function updateGeneratedElementContent(node, def, layout) {
|
|
1266
|
+
if (!isTextAsset(def.asset) && !isPrimitiveAsset(def.asset))
|
|
1267
|
+
return;
|
|
1268
|
+
const replacement = isTextAsset(def.asset)
|
|
1269
|
+
? createTextAssetNode(def.text, def.asset, layout.cellSize)
|
|
1270
|
+
: createPrimitiveAssetNode(def.asset, def.primitive, layout.cellSize);
|
|
1271
|
+
clearChildren(node);
|
|
1272
|
+
while (replacement.firstChild) {
|
|
1273
|
+
const child = replacement.firstChild;
|
|
1274
|
+
replacement.removeChild(child);
|
|
1275
|
+
node.appendChild(child);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
function createConnectorInstance(def, layout) {
|
|
1279
|
+
const node = document.createElementNS(NS, "g");
|
|
1280
|
+
const shaft = document.createElementNS(NS, "path");
|
|
1281
|
+
const state = { node, shaft, isHidden: false, ambient: new Set() };
|
|
1282
|
+
applyConnectorState(state, def, layout);
|
|
1283
|
+
const entryAnim = def.enter;
|
|
1284
|
+
if (entryAnim && entryAnim !== "none" && def.presence !== "removed") {
|
|
1285
|
+
animateElement(node, `iso-anim-${entryAnim}`, "enter");
|
|
1286
|
+
node.addEventListener("animationend", () => {
|
|
1287
|
+
node.style.animation = "";
|
|
1288
|
+
}, { once: true });
|
|
1289
|
+
}
|
|
1290
|
+
return state;
|
|
1291
|
+
}
|
|
1292
|
+
function applyConnectorState(state, def, layout) {
|
|
1293
|
+
applyConnectorGroupAttrs(state.node, def);
|
|
1294
|
+
clearChildren(state.node);
|
|
1295
|
+
const d = routePath(def.route, layout);
|
|
1296
|
+
if (shouldRenderOutline(def)) {
|
|
1297
|
+
const outline = document.createElementNS(NS, "path");
|
|
1298
|
+
outline.classList.add("iso-connector-outline");
|
|
1299
|
+
applyConnectorPathAttrs(outline, def, d, {
|
|
1300
|
+
stroke: def.style.outline ?? def.style.stroke,
|
|
1301
|
+
strokeWidth: def.style.strokeWidth + def.style.outlineWidth * 2,
|
|
1302
|
+
includeDash: false,
|
|
1303
|
+
});
|
|
1304
|
+
state.node.appendChild(outline);
|
|
1305
|
+
}
|
|
1306
|
+
state.shaft = document.createElementNS(NS, "path");
|
|
1307
|
+
state.shaft.classList.add("iso-connector-shaft");
|
|
1308
|
+
applyConnectorPathAttrs(state.shaft, def, d, {
|
|
1309
|
+
stroke: def.style.stroke,
|
|
1310
|
+
strokeWidth: def.style.strokeWidth,
|
|
1311
|
+
includeDash: true,
|
|
1312
|
+
});
|
|
1313
|
+
state.node.appendChild(state.shaft);
|
|
1314
|
+
if (def.style.variant === "road" && def.style.lane === "center-dashed") {
|
|
1315
|
+
const lane = document.createElementNS(NS, "path");
|
|
1316
|
+
lane.classList.add("iso-connector-lane");
|
|
1317
|
+
applyConnectorPathAttrs(lane, def, d, {
|
|
1318
|
+
stroke: "#ffffff",
|
|
1319
|
+
strokeWidth: Math.max(1, def.style.strokeWidth * 0.12),
|
|
1320
|
+
includeDash: false,
|
|
1321
|
+
});
|
|
1322
|
+
lane.setAttribute("stroke-dasharray", "8 8");
|
|
1323
|
+
state.node.appendChild(lane);
|
|
1324
|
+
}
|
|
1325
|
+
appendEndpoint(state.node, def, "start", layout);
|
|
1326
|
+
appendEndpoint(state.node, def, "end", layout);
|
|
1327
|
+
applyConnectorAmbientClasses(state, def.ambient ?? []);
|
|
1328
|
+
}
|
|
1329
|
+
function clearChildren(node) {
|
|
1330
|
+
while (node.firstChild)
|
|
1331
|
+
node.removeChild(node.firstChild);
|
|
1332
|
+
}
|
|
1333
|
+
function applyConnectorGroupAttrs(node, def) {
|
|
1334
|
+
node.setAttribute("class", [
|
|
1335
|
+
"iso-connector",
|
|
1336
|
+
`iso-connector-${def.id}`,
|
|
1337
|
+
`iso-connector-variant-${def.style.variant}`,
|
|
1338
|
+
`iso-connector-pattern-${def.style.pattern}`,
|
|
1339
|
+
`iso-connector-direction-${def.direction}`,
|
|
1340
|
+
`iso-layer-${def.layer}`,
|
|
1341
|
+
].join(" "));
|
|
1342
|
+
node.setAttribute("data-id", def.id);
|
|
1343
|
+
node.setAttribute("data-layer", def.layer);
|
|
1344
|
+
node.style.overflow = "visible";
|
|
1345
|
+
node.style.pointerEvents = "auto";
|
|
1346
|
+
}
|
|
1347
|
+
function shouldRenderOutline(def) {
|
|
1348
|
+
return Boolean(def.style.outline && def.style.outlineWidth > 0);
|
|
1349
|
+
}
|
|
1350
|
+
function applyConnectorPathAttrs(path, def, d, options) {
|
|
1351
|
+
path.setAttribute("d", d);
|
|
1352
|
+
path.setAttribute("fill", "none");
|
|
1353
|
+
path.setAttribute("stroke", options.stroke);
|
|
1354
|
+
path.setAttribute("stroke-width", String(options.strokeWidth));
|
|
1355
|
+
path.setAttribute("stroke-linecap", "round");
|
|
1356
|
+
path.setAttribute("stroke-linejoin", "round");
|
|
1357
|
+
path.setAttribute("opacity", String(def.style.opacity));
|
|
1358
|
+
if (options.includeDash && def.style.pattern !== "solid") {
|
|
1359
|
+
const dash = def.style.dash ?? DEFAULT_CONNECTOR_DASH[def.style.pattern];
|
|
1360
|
+
path.setAttribute("stroke-dasharray", dash.join(" "));
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
function routePath(route, layout) {
|
|
1364
|
+
return route
|
|
1365
|
+
.map((point, index) => {
|
|
1366
|
+
const projected = projectGridPoint(point[0], point[1], layout);
|
|
1367
|
+
return `${index === 0 ? "M" : "L"} ${projected.x} ${projected.y}`;
|
|
1368
|
+
})
|
|
1369
|
+
.join(" ");
|
|
1370
|
+
}
|
|
1371
|
+
function appendEndpoint(group, def, kind, layout) {
|
|
1372
|
+
const endpoint = def[kind];
|
|
1373
|
+
if (endpoint === "none" || def.route.length < 2)
|
|
1374
|
+
return;
|
|
1375
|
+
const node = createEndpointNode(endpoint, def, kind, layout);
|
|
1376
|
+
node.classList.add(`iso-connector-${kind}`);
|
|
1377
|
+
group.appendChild(node);
|
|
1378
|
+
}
|
|
1379
|
+
function createEndpointNode(endpoint, def, kind, layout) {
|
|
1380
|
+
switch (endpoint) {
|
|
1381
|
+
case "arrow":
|
|
1382
|
+
return createArrowEndpoint(def, kind, layout);
|
|
1383
|
+
case "dot":
|
|
1384
|
+
return createCircleEndpoint(def, kind, layout, true);
|
|
1385
|
+
case "circle":
|
|
1386
|
+
return createCircleEndpoint(def, kind, layout, false);
|
|
1387
|
+
case "diamond":
|
|
1388
|
+
return createDiamondEndpoint(def, kind, layout);
|
|
1389
|
+
case "bar":
|
|
1390
|
+
return createBarEndpoint(def, kind, layout);
|
|
1391
|
+
case "none":
|
|
1392
|
+
throw new RenderError("CONNECTOR_ENDPOINT_NONE", "Cannot create geometry for endpoint none");
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
function createArrowEndpoint(def, kind, layout) {
|
|
1396
|
+
const tip = endpointPoint(def, kind);
|
|
1397
|
+
const direction = endpointDirection(def, kind);
|
|
1398
|
+
const perpendicular = [-direction[1], direction[0]];
|
|
1399
|
+
const base = [tip[0] - direction[0] * ARROW_LENGTH_GRID, tip[1] - direction[1] * ARROW_LENGTH_GRID];
|
|
1400
|
+
const halfWidth = ARROW_WIDTH_GRID / 2;
|
|
1401
|
+
const points = [
|
|
1402
|
+
tip,
|
|
1403
|
+
[base[0] + perpendicular[0] * halfWidth, base[1] + perpendicular[1] * halfWidth],
|
|
1404
|
+
[base[0] - perpendicular[0] * halfWidth, base[1] - perpendicular[1] * halfWidth],
|
|
1405
|
+
].map((point) => projectGridPoint(point[0], point[1], layout));
|
|
1406
|
+
const polygon = document.createElementNS(NS, "polygon");
|
|
1407
|
+
polygon.setAttribute("points", points.map(pointToString).join(" "));
|
|
1408
|
+
polygon.setAttribute("fill", def.style.stroke);
|
|
1409
|
+
polygon.setAttribute("opacity", String(def.style.opacity));
|
|
1410
|
+
return polygon;
|
|
1411
|
+
}
|
|
1412
|
+
function createCircleEndpoint(def, kind, layout, filled) {
|
|
1413
|
+
const point = endpointPoint(def, kind);
|
|
1414
|
+
const projected = projectGridPoint(point[0], point[1], layout);
|
|
1415
|
+
const circle = document.createElementNS(NS, "circle");
|
|
1416
|
+
circle.setAttribute("cx", String(projected.x));
|
|
1417
|
+
circle.setAttribute("cy", String(projected.y));
|
|
1418
|
+
circle.setAttribute("r", String(ENDPOINT_RADIUS_GRID * layout.cellSize));
|
|
1419
|
+
circle.setAttribute("stroke", def.style.stroke);
|
|
1420
|
+
circle.setAttribute("stroke-width", String(Math.max(1, def.style.strokeWidth)));
|
|
1421
|
+
circle.setAttribute("opacity", String(def.style.opacity));
|
|
1422
|
+
circle.setAttribute("fill", filled ? def.style.stroke : "none");
|
|
1423
|
+
return circle;
|
|
1424
|
+
}
|
|
1425
|
+
function createDiamondEndpoint(def, kind, layout) {
|
|
1426
|
+
const center = endpointPoint(def, kind);
|
|
1427
|
+
const radius = ENDPOINT_RADIUS_GRID;
|
|
1428
|
+
const points = [
|
|
1429
|
+
[center[0], center[1] - radius],
|
|
1430
|
+
[center[0] + radius, center[1]],
|
|
1431
|
+
[center[0], center[1] + radius],
|
|
1432
|
+
[center[0] - radius, center[1]],
|
|
1433
|
+
];
|
|
1434
|
+
const polygon = document.createElementNS(NS, "polygon");
|
|
1435
|
+
polygon.setAttribute("points", points
|
|
1436
|
+
.map((point) => projectGridPoint(point[0], point[1], layout))
|
|
1437
|
+
.map(pointToString)
|
|
1438
|
+
.join(" "));
|
|
1439
|
+
polygon.setAttribute("fill", def.style.stroke);
|
|
1440
|
+
polygon.setAttribute("opacity", String(def.style.opacity));
|
|
1441
|
+
return polygon;
|
|
1442
|
+
}
|
|
1443
|
+
function createBarEndpoint(def, kind, layout) {
|
|
1444
|
+
const center = endpointPoint(def, kind);
|
|
1445
|
+
const direction = endpointDirection(def, kind);
|
|
1446
|
+
const perpendicular = [-direction[1], direction[0]];
|
|
1447
|
+
const half = BAR_WIDTH_GRID / 2;
|
|
1448
|
+
const a = projectGridPoint(center[0] + perpendicular[0] * half, center[1] + perpendicular[1] * half, layout);
|
|
1449
|
+
const b = projectGridPoint(center[0] - perpendicular[0] * half, center[1] - perpendicular[1] * half, layout);
|
|
1450
|
+
const line = document.createElementNS(NS, "line");
|
|
1451
|
+
line.setAttribute("x1", String(a.x));
|
|
1452
|
+
line.setAttribute("y1", String(a.y));
|
|
1453
|
+
line.setAttribute("x2", String(b.x));
|
|
1454
|
+
line.setAttribute("y2", String(b.y));
|
|
1455
|
+
line.setAttribute("stroke", def.style.stroke);
|
|
1456
|
+
line.setAttribute("stroke-width", String(Math.max(1, def.style.strokeWidth)));
|
|
1457
|
+
line.setAttribute("stroke-linecap", "round");
|
|
1458
|
+
line.setAttribute("opacity", String(def.style.opacity));
|
|
1459
|
+
return line;
|
|
1460
|
+
}
|
|
1461
|
+
function endpointPoint(def, kind) {
|
|
1462
|
+
return kind === "start" ? def.route[0] : def.route[def.route.length - 1];
|
|
1463
|
+
}
|
|
1464
|
+
function endpointDirection(def, kind) {
|
|
1465
|
+
const point = kind === "start"
|
|
1466
|
+
? vectorBetween(def.route[0], def.route[1])
|
|
1467
|
+
: vectorBetween(def.route[def.route.length - 2], def.route[def.route.length - 1]);
|
|
1468
|
+
const effective = def.direction === "reverse" ? [-point[0], -point[1]] : point;
|
|
1469
|
+
return normalizeVector(effective);
|
|
1470
|
+
}
|
|
1471
|
+
function vectorBetween(start, end) {
|
|
1472
|
+
return [end[0] - start[0], end[1] - start[1]];
|
|
1473
|
+
}
|
|
1474
|
+
function normalizeVector(vector) {
|
|
1475
|
+
const length = Math.hypot(vector[0], vector[1]);
|
|
1476
|
+
if (length === 0)
|
|
1477
|
+
return [1, 0];
|
|
1478
|
+
return [vector[0] / length, vector[1] / length];
|
|
1479
|
+
}
|
|
1480
|
+
function applyElementTransform(node, def, layout) {
|
|
1481
|
+
const screen = projectToScreen(def.pos[0] + def.size, def.pos[1] + def.size, layout.cellSize, layout.selectedBounds.minX, layout.selectedBounds.minY, layout.padding.x, layout.padding.y);
|
|
1482
|
+
const visualSize = calculateVisualSize(def.size, layout.cellSize);
|
|
1483
|
+
const scale = visualSize / layout.cellSize;
|
|
1484
|
+
node.setAttribute("transform", `translate(${screen.screenX} ${screen.screenY}) scale(${scale})`);
|
|
1485
|
+
}
|
|
1486
|
+
function applyAmbientClasses(state, ambient) {
|
|
1487
|
+
const next = new Set((ambient ?? []).map((item) => item.name));
|
|
1488
|
+
for (const name of state.ambient) {
|
|
1489
|
+
if (!next.has(name))
|
|
1490
|
+
state.node.classList.remove(`iso-ambient-${name}`);
|
|
1491
|
+
}
|
|
1492
|
+
for (const name of next) {
|
|
1493
|
+
if (!state.ambient.has(name))
|
|
1494
|
+
state.node.classList.add(`iso-ambient-${name}`);
|
|
1495
|
+
}
|
|
1496
|
+
state.ambient = next;
|
|
1497
|
+
}
|
|
1498
|
+
function applyConnectorAmbientClasses(state, ambient) {
|
|
1499
|
+
const next = new Set((ambient ?? []).map((item) => item.name));
|
|
1500
|
+
for (const name of state.ambient) {
|
|
1501
|
+
if (!next.has(name))
|
|
1502
|
+
state.shaft.classList.remove(`iso-ambient-${name}`);
|
|
1503
|
+
}
|
|
1504
|
+
for (const name of next) {
|
|
1505
|
+
if (!state.ambient.has(name))
|
|
1506
|
+
state.shaft.classList.add(`iso-ambient-${name}`);
|
|
1507
|
+
}
|
|
1508
|
+
state.ambient = next;
|
|
1509
|
+
}
|
|
1510
|
+
/** Apply a CSS keyframe animation to an element. */
|
|
1511
|
+
function animateElement(node, keyframeName, type = "enter") {
|
|
1512
|
+
node.style.opacity = "1";
|
|
1513
|
+
node.style.animation = "none";
|
|
1514
|
+
node.getBoundingClientRect();
|
|
1515
|
+
const duration = type === "exit" ? "var(--iso-anim-duration-exit, 300ms)" : "var(--iso-anim-duration-enter, 400ms)";
|
|
1516
|
+
const easing = type === "exit" ? "var(--iso-anim-easing-exit, ease-in)" : "var(--iso-anim-easing-enter, ease-out)";
|
|
1517
|
+
node.style.animation = `${keyframeName} ${duration} ${easing} forwards`;
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
/**
|
|
1521
|
+
* Linear easing (no interpolation).
|
|
1522
|
+
*/
|
|
1523
|
+
function linear(t) {
|
|
1524
|
+
return t;
|
|
1525
|
+
}
|
|
1526
|
+
/**
|
|
1527
|
+
* Cubic ease-in: starts slowly, accelerates.
|
|
1528
|
+
*/
|
|
1529
|
+
function easeInCubic(t) {
|
|
1530
|
+
return t * t * t;
|
|
1531
|
+
}
|
|
1532
|
+
/**
|
|
1533
|
+
* Cubic ease-out: starts fast, decelerates.
|
|
1534
|
+
*/
|
|
1535
|
+
function easeOutCubic(t) {
|
|
1536
|
+
return 1 - (1 - t) ** 3;
|
|
1537
|
+
}
|
|
1538
|
+
/**
|
|
1539
|
+
* Cubic ease-in-out: slow start, fast middle, slow end.
|
|
1540
|
+
*/
|
|
1541
|
+
function easeInOutCubic(t) {
|
|
1542
|
+
return t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* Resolve an easing type string to a function.
|
|
1546
|
+
*/
|
|
1547
|
+
function resolveEasing(type) {
|
|
1548
|
+
switch (type) {
|
|
1549
|
+
case "linear":
|
|
1550
|
+
return linear;
|
|
1551
|
+
case "easeInCubic":
|
|
1552
|
+
return easeInCubic;
|
|
1553
|
+
case "easeOutCubic":
|
|
1554
|
+
return easeOutCubic;
|
|
1555
|
+
case "easeInOutCubic":
|
|
1556
|
+
return easeInOutCubic;
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
const DEFAULT_CONFIG = {
|
|
1561
|
+
scrollDirection: "vertical",
|
|
1562
|
+
scrollOffset: {},
|
|
1563
|
+
minProgress: 0,
|
|
1564
|
+
maxProgress: 1,
|
|
1565
|
+
keyboardControls: false,
|
|
1566
|
+
touchControls: false,
|
|
1567
|
+
scrollSensitivity: 1.0,
|
|
1568
|
+
transitionDuration: 600,
|
|
1569
|
+
transitionEasing: "ease-in-out",
|
|
1570
|
+
};
|
|
1571
|
+
// ── Controller implementation ──────────────────────────────────────────────
|
|
1572
|
+
/**
|
|
1573
|
+
* Animation controller — manages scroll progress, scene navigation,
|
|
1574
|
+
* and delegates to the animation engine for frame updates.
|
|
1575
|
+
*/
|
|
1576
|
+
class AnimationController {
|
|
1577
|
+
_engine = new AnimationEngine();
|
|
1578
|
+
_bundle = null;
|
|
1579
|
+
_sceneIndex = 0;
|
|
1580
|
+
_progress = 0;
|
|
1581
|
+
_paused = false;
|
|
1582
|
+
_config = DEFAULT_CONFIG;
|
|
1583
|
+
_container = null;
|
|
1584
|
+
_sceneElement = null;
|
|
1585
|
+
_ownsEngine = true;
|
|
1586
|
+
_rafId = null;
|
|
1587
|
+
_pendingProgress = null;
|
|
1588
|
+
_destroyed = false;
|
|
1589
|
+
_listeners = new Map();
|
|
1590
|
+
// Scroll tracking state
|
|
1591
|
+
_minScroll = 0;
|
|
1592
|
+
_maxScroll = 0;
|
|
1593
|
+
_touchStartY = 0;
|
|
1594
|
+
_touchStartX = 0;
|
|
1595
|
+
_isDragging = false;
|
|
1596
|
+
// Transition animation state
|
|
1597
|
+
_transitionAnim = null;
|
|
1598
|
+
get engine() {
|
|
1599
|
+
return this._engine;
|
|
1600
|
+
}
|
|
1601
|
+
get progress() {
|
|
1602
|
+
return this._progress;
|
|
1603
|
+
}
|
|
1604
|
+
getProgress() {
|
|
1605
|
+
this._assertNotDestroyed();
|
|
1606
|
+
return this._progress;
|
|
1607
|
+
}
|
|
1608
|
+
get sceneIndex() {
|
|
1609
|
+
return this._sceneIndex;
|
|
1610
|
+
}
|
|
1611
|
+
getSceneIndex() {
|
|
1612
|
+
this._assertNotDestroyed();
|
|
1613
|
+
return this._sceneIndex;
|
|
1614
|
+
}
|
|
1615
|
+
get scenes() {
|
|
1616
|
+
return this._bundle?.scenes ?? [];
|
|
1617
|
+
}
|
|
1618
|
+
get paused() {
|
|
1619
|
+
return this._paused;
|
|
1620
|
+
}
|
|
1621
|
+
get currentScene() {
|
|
1622
|
+
return this.scenes[this._sceneIndex];
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Initialize the controller with a compiled bundle and optional runtime resources.
|
|
1626
|
+
*/
|
|
1627
|
+
init(bundle, config = {}, runtime = {}) {
|
|
1628
|
+
this._assertNotDestroyed();
|
|
1629
|
+
if (!bundle.scenes || bundle.scenes.length === 0) {
|
|
1630
|
+
throw new ControllerError("CONTROLLER_NO_SCENES", "init() requires at least one compiled scene stop");
|
|
1631
|
+
}
|
|
1632
|
+
this._cancelFrame();
|
|
1633
|
+
this._bundle = bundle;
|
|
1634
|
+
this._engine = runtime.engine ?? new AnimationEngine();
|
|
1635
|
+
this._ownsEngine = !runtime.engine;
|
|
1636
|
+
this._sceneIndex = 0;
|
|
1637
|
+
this._progress = 0;
|
|
1638
|
+
this._paused = false;
|
|
1639
|
+
this._config = { ...DEFAULT_CONFIG, ...config };
|
|
1640
|
+
this._sceneElement = runtime.sceneElement ?? config.sceneElement ?? null;
|
|
1641
|
+
this._engine.init(bundle);
|
|
1642
|
+
this._bindScroll();
|
|
1643
|
+
}
|
|
1644
|
+
/**
|
|
1645
|
+
* Set scroll progress (0–1, clamped) and trigger frame update.
|
|
1646
|
+
*/
|
|
1647
|
+
setProgress(progress) {
|
|
1648
|
+
this._assertNotDestroyed();
|
|
1649
|
+
if (!Number.isFinite(progress)) {
|
|
1650
|
+
throw new ControllerError("CONTROLLER_PROGRESS_OUT_OF_RANGE", "setProgress() requires a finite progress value");
|
|
1651
|
+
}
|
|
1652
|
+
const clamped = Math.max(0, Math.min(1, progress));
|
|
1653
|
+
if (clamped === this._progress && !this._paused && this._rafId !== null) {
|
|
1654
|
+
return;
|
|
1655
|
+
}
|
|
1656
|
+
this._progress = clamped;
|
|
1657
|
+
if (this._paused)
|
|
1658
|
+
return;
|
|
1659
|
+
this._scheduleProgressForward(clamped);
|
|
1660
|
+
}
|
|
1661
|
+
/**
|
|
1662
|
+
* Navigate to next scene (wraps to 0 if at end).
|
|
1663
|
+
*/
|
|
1664
|
+
nextScene() {
|
|
1665
|
+
this._assertNotDestroyed();
|
|
1666
|
+
if (this.scenes.length <= 1)
|
|
1667
|
+
return;
|
|
1668
|
+
const nextIndex = (this._sceneIndex + 1) % this.scenes.length;
|
|
1669
|
+
this._transitionToScene(nextIndex);
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1672
|
+
* Navigate to previous scene (wraps to last if at beginning).
|
|
1673
|
+
*/
|
|
1674
|
+
prevScene() {
|
|
1675
|
+
this._assertNotDestroyed();
|
|
1676
|
+
if (this.scenes.length <= 1)
|
|
1677
|
+
return;
|
|
1678
|
+
const prevIndex = (this._sceneIndex - 1 + this.scenes.length) % this.scenes.length;
|
|
1679
|
+
this._transitionToScene(prevIndex);
|
|
1680
|
+
}
|
|
1681
|
+
/**
|
|
1682
|
+
* Set scene index directly.
|
|
1683
|
+
*/
|
|
1684
|
+
setSceneIndex(index) {
|
|
1685
|
+
this._assertNotDestroyed();
|
|
1686
|
+
if (index < 0 || index >= this.scenes.length) {
|
|
1687
|
+
throw new ControllerError("CONTROLLER_SCENE_INDEX_OUT_OF_RANGE", `Scene index ${index} is out of bounds [0, ${this.scenes.length - 1}]`);
|
|
1688
|
+
}
|
|
1689
|
+
this._transitionToScene(index);
|
|
1690
|
+
}
|
|
1691
|
+
/**
|
|
1692
|
+
* Pause all animations.
|
|
1693
|
+
*/
|
|
1694
|
+
pause() {
|
|
1695
|
+
this._assertNotDestroyed();
|
|
1696
|
+
if (this._paused)
|
|
1697
|
+
return;
|
|
1698
|
+
this._paused = true;
|
|
1699
|
+
this._cancelFrame();
|
|
1700
|
+
this._engine.pause();
|
|
1701
|
+
this._applyPauseState(true);
|
|
1702
|
+
this._emit("paused");
|
|
1703
|
+
}
|
|
1704
|
+
/**
|
|
1705
|
+
* Resume from paused state.
|
|
1706
|
+
*/
|
|
1707
|
+
resume() {
|
|
1708
|
+
this._assertNotDestroyed();
|
|
1709
|
+
if (!this._paused)
|
|
1710
|
+
return;
|
|
1711
|
+
this._paused = false;
|
|
1712
|
+
this._engine.resume();
|
|
1713
|
+
this._applyPauseState(false);
|
|
1714
|
+
this._scheduleProgressForward(this._progress);
|
|
1715
|
+
this._emit("resumed");
|
|
1716
|
+
}
|
|
1717
|
+
/**
|
|
1718
|
+
* Check if controller is paused.
|
|
1719
|
+
*/
|
|
1720
|
+
isPaused() {
|
|
1721
|
+
this._assertNotDestroyed();
|
|
1722
|
+
return this._paused;
|
|
1723
|
+
}
|
|
1724
|
+
/**
|
|
1725
|
+
* Destroy controller and clean up all listeners and resources.
|
|
1726
|
+
*/
|
|
1727
|
+
destroy() {
|
|
1728
|
+
this._assertNotDestroyed();
|
|
1729
|
+
this._unbindScroll();
|
|
1730
|
+
this._cancelFrame();
|
|
1731
|
+
this._cancelTransition();
|
|
1732
|
+
if (this._ownsEngine)
|
|
1733
|
+
this._engine.destroy();
|
|
1734
|
+
this._listeners.clear();
|
|
1735
|
+
this._bundle = null;
|
|
1736
|
+
this._container = null;
|
|
1737
|
+
this._pendingProgress = null;
|
|
1738
|
+
this._destroyed = true;
|
|
1739
|
+
}
|
|
1740
|
+
// ── Event system ───────────────────────────────────────────────────────
|
|
1741
|
+
on(event, listener) {
|
|
1742
|
+
this._assertNotDestroyed();
|
|
1743
|
+
const set = this._listeners.get(event) ?? new Set();
|
|
1744
|
+
set.add(listener);
|
|
1745
|
+
this._listeners.set(event, set);
|
|
1746
|
+
}
|
|
1747
|
+
off(event, listener) {
|
|
1748
|
+
this._assertNotDestroyed();
|
|
1749
|
+
const set = this._listeners.get(event);
|
|
1750
|
+
if (set) {
|
|
1751
|
+
set.delete(listener);
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
_emit(event, ...args) {
|
|
1755
|
+
const set = this._listeners.get(event);
|
|
1756
|
+
if (set) {
|
|
1757
|
+
for (const listener of set) {
|
|
1758
|
+
try {
|
|
1759
|
+
listener(...args);
|
|
1760
|
+
}
|
|
1761
|
+
catch (error) {
|
|
1762
|
+
queueMicrotask(() => {
|
|
1763
|
+
throw error;
|
|
1764
|
+
});
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
// ── Frame update ───────────────────────────────────────────────────────
|
|
1770
|
+
_scheduleProgressForward(progress) {
|
|
1771
|
+
this._pendingProgress = progress;
|
|
1772
|
+
if (this._rafId !== null)
|
|
1773
|
+
return;
|
|
1774
|
+
this._rafId = requestAnimationFrame(() => {
|
|
1775
|
+
this._rafId = null;
|
|
1776
|
+
const pending = this._pendingProgress;
|
|
1777
|
+
this._pendingProgress = null;
|
|
1778
|
+
if (pending === null || this._paused || this._destroyed)
|
|
1779
|
+
return;
|
|
1780
|
+
this._engine.setProgress(pending);
|
|
1781
|
+
this._applyFrameUpdate();
|
|
1782
|
+
this._emit("progress-change", pending);
|
|
1783
|
+
});
|
|
1784
|
+
}
|
|
1785
|
+
_cancelFrame() {
|
|
1786
|
+
if (this._rafId !== null) {
|
|
1787
|
+
cancelAnimationFrame(this._rafId);
|
|
1788
|
+
this._rafId = null;
|
|
1789
|
+
}
|
|
1790
|
+
this._pendingProgress = null;
|
|
1791
|
+
}
|
|
1792
|
+
_applyFrameUpdate() {
|
|
1793
|
+
if (!this._bundle)
|
|
1794
|
+
return;
|
|
1795
|
+
const svg = (this._sceneElement ?? this._container?.querySelector("svg") ?? null);
|
|
1796
|
+
if (!svg)
|
|
1797
|
+
return;
|
|
1798
|
+
const updates = this._engine.getFrameUpdates().map((update) => ({
|
|
1799
|
+
id: update.id,
|
|
1800
|
+
asset: update.asset,
|
|
1801
|
+
pos: update.pos,
|
|
1802
|
+
size: update.size,
|
|
1803
|
+
layer: update.layer,
|
|
1804
|
+
presence: update.lifecycle,
|
|
1805
|
+
enter: update.entry,
|
|
1806
|
+
exit: update.exit,
|
|
1807
|
+
ambient: update.ambient,
|
|
1808
|
+
text: update.text,
|
|
1809
|
+
primitive: update.primitive,
|
|
1810
|
+
}));
|
|
1811
|
+
const connectors = this._engine.getConnectorFrameUpdates().map((update) => ({
|
|
1812
|
+
id: update.id,
|
|
1813
|
+
route: update.route,
|
|
1814
|
+
layer: update.layer,
|
|
1815
|
+
presence: update.lifecycle,
|
|
1816
|
+
style: update.style,
|
|
1817
|
+
start: update.start,
|
|
1818
|
+
end: update.end,
|
|
1819
|
+
direction: update.direction,
|
|
1820
|
+
enter: update.entry,
|
|
1821
|
+
exit: update.exit,
|
|
1822
|
+
ambient: update.ambient,
|
|
1823
|
+
}));
|
|
1824
|
+
updateElementTransforms(svg, updates, connectors);
|
|
1825
|
+
this._applyLifecycleChanges(updates);
|
|
1826
|
+
this._applyConnectorLifecycleChanges(connectors);
|
|
1827
|
+
}
|
|
1828
|
+
_applyLifecycleChanges(elements) {
|
|
1829
|
+
for (const elDef of elements) {
|
|
1830
|
+
const transition = this._engine.getLifecycleTransition(elDef.id);
|
|
1831
|
+
if (!transition)
|
|
1832
|
+
continue;
|
|
1833
|
+
const svgForState = (this._sceneElement ?? this._container?.querySelector("svg"));
|
|
1834
|
+
if (!svgForState)
|
|
1835
|
+
continue;
|
|
1836
|
+
const state = getElementState(svgForState, elDef.id);
|
|
1837
|
+
if (!state)
|
|
1838
|
+
continue;
|
|
1839
|
+
if (transition.to === "entering" || transition.to === "present") {
|
|
1840
|
+
state.isHidden = false;
|
|
1841
|
+
unhideElementOnReadd(state.node);
|
|
1842
|
+
}
|
|
1843
|
+
if (isForwardEntryTransition(transition)) {
|
|
1844
|
+
this._applyEntryAnimation(elDef, state);
|
|
1845
|
+
}
|
|
1846
|
+
if (isReverseExitTransition(transition)) {
|
|
1847
|
+
this._applyExitAnimation({ ...elDef, exit: oppositeExitAnimation(elDef.enter ?? "fade-in") }, state);
|
|
1848
|
+
continue;
|
|
1849
|
+
}
|
|
1850
|
+
if (isForwardExitTransition(transition)) {
|
|
1851
|
+
this._applyExitAnimation(elDef, state);
|
|
1852
|
+
}
|
|
1853
|
+
if (isReverseEntryTransition(transition)) {
|
|
1854
|
+
state.isHidden = false;
|
|
1855
|
+
unhideElementOnReadd(state.node);
|
|
1856
|
+
this._applyEntryAnimation({ ...elDef, enter: oppositeEntryAnimation(elDef.exit ?? "fade-out") }, state);
|
|
1857
|
+
continue;
|
|
1858
|
+
}
|
|
1859
|
+
if (transition.to === "removed") {
|
|
1860
|
+
state.isHidden = true;
|
|
1861
|
+
hideElementAfterExit(state.node);
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
_applyConnectorLifecycleChanges(connectors) {
|
|
1866
|
+
for (const connectorDef of connectors) {
|
|
1867
|
+
const transition = this._engine.getConnectorLifecycleTransition(connectorDef.id);
|
|
1868
|
+
if (!transition)
|
|
1869
|
+
continue;
|
|
1870
|
+
const svgForState = (this._sceneElement ?? this._container?.querySelector("svg"));
|
|
1871
|
+
if (!svgForState)
|
|
1872
|
+
continue;
|
|
1873
|
+
const state = getConnectorState(svgForState, connectorDef.id);
|
|
1874
|
+
if (!state)
|
|
1875
|
+
continue;
|
|
1876
|
+
if (transition.to === "entering" || transition.to === "present") {
|
|
1877
|
+
state.isHidden = false;
|
|
1878
|
+
unhideElementOnReadd(state.node);
|
|
1879
|
+
}
|
|
1880
|
+
if (isForwardEntryTransition(transition)) {
|
|
1881
|
+
this._applyConnectorEntryAnimation(connectorDef, state);
|
|
1882
|
+
}
|
|
1883
|
+
if (isReverseExitTransition(transition)) {
|
|
1884
|
+
this._applyConnectorExitAnimation({
|
|
1885
|
+
...connectorDef,
|
|
1886
|
+
exit: oppositeExitAnimation(connectorDef.enter ?? "fade-in"),
|
|
1887
|
+
}, state);
|
|
1888
|
+
continue;
|
|
1889
|
+
}
|
|
1890
|
+
if (isForwardExitTransition(transition)) {
|
|
1891
|
+
this._applyConnectorExitAnimation(connectorDef, state);
|
|
1892
|
+
}
|
|
1893
|
+
if (isReverseEntryTransition(transition)) {
|
|
1894
|
+
state.isHidden = false;
|
|
1895
|
+
unhideElementOnReadd(state.node);
|
|
1896
|
+
this._applyConnectorEntryAnimation({
|
|
1897
|
+
...connectorDef,
|
|
1898
|
+
enter: oppositeEntryAnimation(connectorDef.exit ?? "fade-out"),
|
|
1899
|
+
}, state);
|
|
1900
|
+
continue;
|
|
1901
|
+
}
|
|
1902
|
+
if (transition.to === "removed") {
|
|
1903
|
+
state.isHidden = true;
|
|
1904
|
+
hideElementAfterExit(state.node);
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
_applyEntryAnimation(elDef, state) {
|
|
1909
|
+
const entryAnim = elDef.enter ?? "fade-in";
|
|
1910
|
+
if (entryAnim === "none")
|
|
1911
|
+
return;
|
|
1912
|
+
animateElement(state.node, `iso-anim-${entryAnim}`, "enter");
|
|
1913
|
+
state.node.addEventListener("animationend", () => {
|
|
1914
|
+
state.node.style.animation = "";
|
|
1915
|
+
}, { once: true });
|
|
1916
|
+
}
|
|
1917
|
+
_applyExitAnimation(elDef, state) {
|
|
1918
|
+
const exitAnim = elDef.exit ?? "fade-out";
|
|
1919
|
+
if (exitAnim === "none") {
|
|
1920
|
+
hideElementAfterExit(state.node);
|
|
1921
|
+
return;
|
|
1922
|
+
}
|
|
1923
|
+
animateElement(state.node, `iso-anim-${exitAnim}`, "exit");
|
|
1924
|
+
state.node.addEventListener("animationend", () => {
|
|
1925
|
+
hideElementAfterExit(state.node);
|
|
1926
|
+
}, { once: true });
|
|
1927
|
+
}
|
|
1928
|
+
_applyConnectorEntryAnimation(connectorDef, state) {
|
|
1929
|
+
const entryAnim = connectorDef.enter ?? "fade-in";
|
|
1930
|
+
if (entryAnim === "none")
|
|
1931
|
+
return;
|
|
1932
|
+
animateElement(state.node, `iso-anim-${entryAnim}`, "enter");
|
|
1933
|
+
state.node.addEventListener("animationend", () => {
|
|
1934
|
+
state.node.style.animation = "";
|
|
1935
|
+
}, { once: true });
|
|
1936
|
+
}
|
|
1937
|
+
_applyConnectorExitAnimation(connectorDef, state) {
|
|
1938
|
+
const exitAnim = connectorDef.exit ?? "fade-out";
|
|
1939
|
+
if (exitAnim === "none") {
|
|
1940
|
+
hideElementAfterExit(state.node);
|
|
1941
|
+
return;
|
|
1942
|
+
}
|
|
1943
|
+
animateElement(state.node, `iso-anim-${exitAnim}`, "exit");
|
|
1944
|
+
state.node.addEventListener("animationend", () => {
|
|
1945
|
+
hideElementAfterExit(state.node);
|
|
1946
|
+
}, { once: true });
|
|
1947
|
+
}
|
|
1948
|
+
// ── Scene transitions ──────────────────────────────────────────────────
|
|
1949
|
+
_transitionToScene(index) {
|
|
1950
|
+
this._cancelTransition();
|
|
1951
|
+
if (index === this._sceneIndex)
|
|
1952
|
+
return;
|
|
1953
|
+
this._sceneIndex = index;
|
|
1954
|
+
this._emit("scene-change", index);
|
|
1955
|
+
const stop = this.scenes[index];
|
|
1956
|
+
const from = this._progress;
|
|
1957
|
+
const to = stop.progress;
|
|
1958
|
+
const duration = this._config.transitionDuration;
|
|
1959
|
+
if (duration > 0 && from !== to) {
|
|
1960
|
+
this._animateProgress(from, to, duration);
|
|
1961
|
+
return;
|
|
1962
|
+
}
|
|
1963
|
+
this._progress = to;
|
|
1964
|
+
this._scheduleProgressForward(to);
|
|
1965
|
+
}
|
|
1966
|
+
_animateProgress(from, to, duration) {
|
|
1967
|
+
const easing = resolveEasing((this._config.transitionEasing === "ease-in-out"
|
|
1968
|
+
? "easeInOutCubic"
|
|
1969
|
+
: this._config.transitionEasing === "ease-out"
|
|
1970
|
+
? "easeOutCubic"
|
|
1971
|
+
: "linear"));
|
|
1972
|
+
const start = performance.now();
|
|
1973
|
+
const step = (now) => {
|
|
1974
|
+
const elapsed = now - start;
|
|
1975
|
+
const t = Math.min(elapsed / duration, 1);
|
|
1976
|
+
const easedT = easing(t);
|
|
1977
|
+
const currentProgress = from + (to - from) * easedT;
|
|
1978
|
+
this.setProgress(currentProgress);
|
|
1979
|
+
if (t < 1) {
|
|
1980
|
+
this._transitionAnim = requestAnimationFrame(step);
|
|
1981
|
+
}
|
|
1982
|
+
else {
|
|
1983
|
+
this._transitionAnim = null;
|
|
1984
|
+
}
|
|
1985
|
+
};
|
|
1986
|
+
this._transitionAnim = requestAnimationFrame(step);
|
|
1987
|
+
}
|
|
1988
|
+
_cancelTransition() {
|
|
1989
|
+
if (this._transitionAnim !== null) {
|
|
1990
|
+
cancelAnimationFrame(this._transitionAnim);
|
|
1991
|
+
this._transitionAnim = null;
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
// ── Scroll binding ─────────────────────────────────────────────────────
|
|
1995
|
+
_bindScroll() {
|
|
1996
|
+
const container = this._config.container;
|
|
1997
|
+
if (!container)
|
|
1998
|
+
return;
|
|
1999
|
+
this._container = container;
|
|
2000
|
+
this._calculateScrollBounds();
|
|
2001
|
+
container.addEventListener("scroll", this._onScroll, { passive: true });
|
|
2002
|
+
window.addEventListener("resize", this._onResize, { passive: true });
|
|
2003
|
+
if (this._config.keyboardControls) {
|
|
2004
|
+
document.addEventListener("keydown", this._onKeyDown);
|
|
2005
|
+
}
|
|
2006
|
+
if (this._config.touchControls) {
|
|
2007
|
+
container.addEventListener("touchstart", this._onTouchStart, {
|
|
2008
|
+
passive: true,
|
|
2009
|
+
});
|
|
2010
|
+
container.addEventListener("touchmove", this._onTouchMove, {
|
|
2011
|
+
passive: false,
|
|
2012
|
+
});
|
|
2013
|
+
container.addEventListener("touchend", this._onTouchEnd);
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
_unbindScroll() {
|
|
2017
|
+
const container = this._config.container;
|
|
2018
|
+
if (!container)
|
|
2019
|
+
return;
|
|
2020
|
+
container.removeEventListener("scroll", this._onScroll);
|
|
2021
|
+
window.removeEventListener("resize", this._onResize);
|
|
2022
|
+
document.removeEventListener("keydown", this._onKeyDown);
|
|
2023
|
+
container.removeEventListener("touchstart", this._onTouchStart);
|
|
2024
|
+
container.removeEventListener("touchmove", this._onTouchMove);
|
|
2025
|
+
container.removeEventListener("touchend", this._onTouchEnd);
|
|
2026
|
+
}
|
|
2027
|
+
_calculateScrollBounds() {
|
|
2028
|
+
const container = this._config.container;
|
|
2029
|
+
if (!container)
|
|
2030
|
+
return;
|
|
2031
|
+
const offset = this._config.scrollOffset ?? {};
|
|
2032
|
+
if (this._config.scrollDirection === "horizontal") {
|
|
2033
|
+
this._minScroll = offset.left ?? 0;
|
|
2034
|
+
this._maxScroll = container.scrollWidth - container.clientWidth - (offset.right ?? 0);
|
|
2035
|
+
return;
|
|
2036
|
+
}
|
|
2037
|
+
this._minScroll = offset.top ?? 0;
|
|
2038
|
+
this._maxScroll = container.scrollHeight - container.clientHeight - (offset.bottom ?? 0);
|
|
2039
|
+
}
|
|
2040
|
+
_onScroll = () => {
|
|
2041
|
+
if (this._paused || this._destroyed)
|
|
2042
|
+
return;
|
|
2043
|
+
const container = this._config.container;
|
|
2044
|
+
if (!container)
|
|
2045
|
+
return;
|
|
2046
|
+
const currentScroll = this._config.scrollDirection === "horizontal" ? container.scrollLeft : container.scrollTop;
|
|
2047
|
+
const range = this._maxScroll - this._minScroll;
|
|
2048
|
+
if (range <= 0)
|
|
2049
|
+
return;
|
|
2050
|
+
const rawProgress = (currentScroll - this._minScroll) / range;
|
|
2051
|
+
const clampedProgress = Math.max(this._config.minProgress ?? 0, Math.min(this._config.maxProgress ?? 1, rawProgress));
|
|
2052
|
+
const sensitivity = this._config.scrollSensitivity ?? 1;
|
|
2053
|
+
if (clampedProgress !== this._progress) {
|
|
2054
|
+
this.setProgress(clampedProgress * sensitivity);
|
|
2055
|
+
}
|
|
2056
|
+
};
|
|
2057
|
+
_onResize = () => {
|
|
2058
|
+
if (this._destroyed)
|
|
2059
|
+
return;
|
|
2060
|
+
this._calculateScrollBounds();
|
|
2061
|
+
};
|
|
2062
|
+
_onKeyDown = (e) => {
|
|
2063
|
+
if (this._destroyed)
|
|
2064
|
+
return;
|
|
2065
|
+
if (e.key === "ArrowRight" || e.key === "ArrowDown" || e.key === " ") {
|
|
2066
|
+
e.preventDefault();
|
|
2067
|
+
this.nextScene();
|
|
2068
|
+
}
|
|
2069
|
+
else if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
|
|
2070
|
+
e.preventDefault();
|
|
2071
|
+
this.prevScene();
|
|
2072
|
+
}
|
|
2073
|
+
};
|
|
2074
|
+
_onTouchStart = (e) => {
|
|
2075
|
+
if (this._destroyed)
|
|
2076
|
+
return;
|
|
2077
|
+
this._isDragging = true;
|
|
2078
|
+
const touch = e.touches[0];
|
|
2079
|
+
if (this._config.scrollDirection === "horizontal") {
|
|
2080
|
+
this._touchStartX = touch.clientX;
|
|
2081
|
+
}
|
|
2082
|
+
else {
|
|
2083
|
+
this._touchStartY = touch.clientY;
|
|
2084
|
+
}
|
|
2085
|
+
};
|
|
2086
|
+
_onTouchMove = (e) => {
|
|
2087
|
+
if (this._destroyed)
|
|
2088
|
+
return;
|
|
2089
|
+
if (!this._isDragging)
|
|
2090
|
+
return;
|
|
2091
|
+
const touch = e.touches[0];
|
|
2092
|
+
const delta = this._config.scrollDirection === "horizontal"
|
|
2093
|
+
? this._touchStartX - touch.clientX
|
|
2094
|
+
: this._touchStartY - touch.clientY;
|
|
2095
|
+
const sensitivity = this._config.scrollSensitivity ?? 1.0;
|
|
2096
|
+
const progressDelta = (delta / 300) * sensitivity;
|
|
2097
|
+
const newProgress = Math.max(0, Math.min(1, this._progress + progressDelta));
|
|
2098
|
+
this.setProgress(newProgress);
|
|
2099
|
+
};
|
|
2100
|
+
_onTouchEnd = () => {
|
|
2101
|
+
if (this._destroyed)
|
|
2102
|
+
return;
|
|
2103
|
+
this._isDragging = false;
|
|
2104
|
+
};
|
|
2105
|
+
// ── Pause state ────────────────────────────────────────────────────────
|
|
2106
|
+
_applyPauseState(pause) {
|
|
2107
|
+
const container = this._config.container;
|
|
2108
|
+
if (!container)
|
|
2109
|
+
return;
|
|
2110
|
+
const svg = container.querySelector("svg");
|
|
2111
|
+
if (!svg)
|
|
2112
|
+
return;
|
|
2113
|
+
const playState = pause ? "paused" : "running";
|
|
2114
|
+
const ambientElements = svg.querySelectorAll('[class*="iso-ambient-"]');
|
|
2115
|
+
for (let i = 0; i < ambientElements.length; i++) {
|
|
2116
|
+
const el = ambientElements[i];
|
|
2117
|
+
el.style.animationPlayState = playState;
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
_assertNotDestroyed() {
|
|
2121
|
+
if (!this._destroyed)
|
|
2122
|
+
return;
|
|
2123
|
+
throw new ControllerError("CONTROLLER_DESTROYED", "AnimationController has been destroyed");
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
function isForwardEntryTransition(transition) {
|
|
2127
|
+
return transition.from === "removed" && transition.to === "entering";
|
|
2128
|
+
}
|
|
2129
|
+
function isForwardExitTransition(transition) {
|
|
2130
|
+
return transition.to === "exiting";
|
|
2131
|
+
}
|
|
2132
|
+
function isReverseExitTransition(transition) {
|
|
2133
|
+
return transition.to === "removed" && transition.from !== "exiting";
|
|
2134
|
+
}
|
|
2135
|
+
function isReverseEntryTransition(transition) {
|
|
2136
|
+
return transition.from === "exiting" && transition.to !== "removed";
|
|
2137
|
+
}
|
|
2138
|
+
function oppositeExitAnimation(entry) {
|
|
2139
|
+
switch (entry) {
|
|
2140
|
+
case "fade-in":
|
|
2141
|
+
return "fade-out";
|
|
2142
|
+
case "fade-in-grow":
|
|
2143
|
+
return "fade-out-shrink";
|
|
2144
|
+
case "fall-in":
|
|
2145
|
+
return "rise-away";
|
|
2146
|
+
case "rise-from-ground":
|
|
2147
|
+
return "fall-through-ground";
|
|
2148
|
+
case "slide-in-left":
|
|
2149
|
+
return "slide-out-left";
|
|
2150
|
+
case "slide-in-right":
|
|
2151
|
+
return "slide-out-right";
|
|
2152
|
+
case "flip-in":
|
|
2153
|
+
return "flip-out";
|
|
2154
|
+
case "none":
|
|
2155
|
+
return "none";
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
function oppositeEntryAnimation(exit) {
|
|
2159
|
+
switch (exit) {
|
|
2160
|
+
case "fade-out":
|
|
2161
|
+
return "fade-in";
|
|
2162
|
+
case "fade-out-shrink":
|
|
2163
|
+
return "fade-in-grow";
|
|
2164
|
+
case "fall-through-ground":
|
|
2165
|
+
return "rise-from-ground";
|
|
2166
|
+
case "rise-away":
|
|
2167
|
+
return "fall-in";
|
|
2168
|
+
case "slide-out-left":
|
|
2169
|
+
return "slide-in-left";
|
|
2170
|
+
case "slide-out-right":
|
|
2171
|
+
return "slide-in-right";
|
|
2172
|
+
case "flip-out":
|
|
2173
|
+
return "flip-in";
|
|
2174
|
+
case "none":
|
|
2175
|
+
return "none";
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
const RUNTIME_BUNDLE_FORMAT = "isostate-runtime-bundle";
|
|
2180
|
+
const RUNTIME_VERSION = "0.1.0";
|
|
2181
|
+
const HEX_DIGEST_PATTERN = /^[a-f0-9]{64}$/;
|
|
2182
|
+
/** Mount a compiled runtime bundle into an HTML element. */
|
|
2183
|
+
function mountScene(target, bundle, options = {}) {
|
|
2184
|
+
assertMountTarget(target);
|
|
2185
|
+
validateRuntimeBundle(bundle);
|
|
2186
|
+
const engine = new AnimationEngine();
|
|
2187
|
+
engine.init(bundle);
|
|
2188
|
+
const svg = buildSceneDOM(target, bundle, {
|
|
2189
|
+
label: options.label,
|
|
2190
|
+
themeVars: options.themeVars,
|
|
2191
|
+
});
|
|
2192
|
+
let controller;
|
|
2193
|
+
if (options.controller !== undefined && options.controller !== false) {
|
|
2194
|
+
controller = new AnimationController();
|
|
2195
|
+
controller.init(bundle, {
|
|
2196
|
+
...options.controller,
|
|
2197
|
+
container: options.controller.container ?? target,
|
|
2198
|
+
sceneElement: svg,
|
|
2199
|
+
}, {
|
|
2200
|
+
engine,
|
|
2201
|
+
sceneElement: svg,
|
|
2202
|
+
});
|
|
2203
|
+
}
|
|
2204
|
+
let destroyed = false;
|
|
2205
|
+
return {
|
|
2206
|
+
svg,
|
|
2207
|
+
engine,
|
|
2208
|
+
controller,
|
|
2209
|
+
getResolvedConfig: () => getResolvedConfig(bundle, options),
|
|
2210
|
+
destroy: () => {
|
|
2211
|
+
if (destroyed)
|
|
2212
|
+
return;
|
|
2213
|
+
destroyed = true;
|
|
2214
|
+
controller?.destroy();
|
|
2215
|
+
engine.destroy();
|
|
2216
|
+
if (svg.parentNode === target) {
|
|
2217
|
+
target.removeChild(svg);
|
|
2218
|
+
}
|
|
2219
|
+
else {
|
|
2220
|
+
svg.parentNode?.removeChild(svg);
|
|
2221
|
+
}
|
|
2222
|
+
},
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
function assertMountTarget(target) {
|
|
2226
|
+
if (!target || typeof target.appendChild !== "function" || typeof target.removeChild !== "function") {
|
|
2227
|
+
throw new RenderError("INVALID_MOUNT_TARGET", "mountScene() requires a DOM HTMLElement target");
|
|
2228
|
+
}
|
|
2229
|
+
}
|
|
2230
|
+
function validateRuntimeBundle(bundle) {
|
|
2231
|
+
if (!bundle || typeof bundle !== "object") {
|
|
2232
|
+
throw new RenderError("BUNDLE_FORMAT_MISSING", "Runtime bundle must be a plain object");
|
|
2233
|
+
}
|
|
2234
|
+
if (bundle._format !== RUNTIME_BUNDLE_FORMAT) {
|
|
2235
|
+
throw new RenderError("BUNDLE_FORMAT_MISSING", "Runtime bundle format is missing or unsupported", {
|
|
2236
|
+
expected: RUNTIME_BUNDLE_FORMAT,
|
|
2237
|
+
});
|
|
2238
|
+
}
|
|
2239
|
+
if (!Array.isArray(bundle.scenes) || bundle.scenes.length === 0) {
|
|
2240
|
+
throw new RenderError("BUNDLE_FORMAT_MISSING", "Runtime bundle must include compiled scenes[]");
|
|
2241
|
+
}
|
|
2242
|
+
if (majorVersion(bundle._version) !== majorVersion(RUNTIME_VERSION)) {
|
|
2243
|
+
throw new RenderError("BUNDLE_VERSION_MISMATCH", `Runtime bundle version ${bundle._version} is not compatible with runtime ${RUNTIME_VERSION}`, { bundleVersion: bundle._version, runtimeVersion: RUNTIME_VERSION });
|
|
2244
|
+
}
|
|
2245
|
+
if (!bundle._digest) {
|
|
2246
|
+
throw new RenderError("BUNDLE_DIGEST_MISSING", "Runtime bundle digest is missing");
|
|
2247
|
+
}
|
|
2248
|
+
if (typeof bundle._digest !== "string" || !HEX_DIGEST_PATTERN.test(bundle._digest)) {
|
|
2249
|
+
throw new RenderError("BUNDLE_DIGEST_MISMATCH", "Runtime bundle digest is malformed");
|
|
2250
|
+
}
|
|
2251
|
+
const { _digest, ...bundleWithoutDigest } = bundle;
|
|
2252
|
+
const actualDigest = sha256(canonicalStringify(bundleWithoutDigest));
|
|
2253
|
+
if (actualDigest !== _digest) {
|
|
2254
|
+
throw new RenderError("BUNDLE_DIGEST_MISMATCH", "Runtime bundle digest does not match bundle content", {
|
|
2255
|
+
expected: _digest,
|
|
2256
|
+
actual: actualDigest,
|
|
2257
|
+
});
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
function getResolvedConfig(bundle, options = {}) {
|
|
2261
|
+
return {
|
|
2262
|
+
grid: { cellSize: bundle.grid.cellSize },
|
|
2263
|
+
floor: { ...bundle.floor },
|
|
2264
|
+
layout: {
|
|
2265
|
+
...bundle.layout,
|
|
2266
|
+
padding: { ...bundle.layout.padding },
|
|
2267
|
+
align: [...bundle.layout.align],
|
|
2268
|
+
},
|
|
2269
|
+
viewBox: getResolvedViewBox(bundle),
|
|
2270
|
+
theme: bundle.theme,
|
|
2271
|
+
themeVars: getResolvedThemeVars(bundle, options.themeVars),
|
|
2272
|
+
scenes: bundle.scenes.map((scene) => ({
|
|
2273
|
+
id: scene.id,
|
|
2274
|
+
progress: scene.progress,
|
|
2275
|
+
})),
|
|
2276
|
+
layerOrder: bundle.layers
|
|
2277
|
+
.map((layer) => ({ name: layer.name, order: layer.order }))
|
|
2278
|
+
.sort((a, b) => a.order - b.order || a.name.localeCompare(b.name)),
|
|
2279
|
+
};
|
|
2280
|
+
}
|
|
2281
|
+
function getResolvedThemeVars(bundle, overrides = {}) {
|
|
2282
|
+
return {
|
|
2283
|
+
...(resolveTheme(bundle.theme) ?? {}),
|
|
2284
|
+
...(bundle.themeVars ?? {}),
|
|
2285
|
+
...overrides,
|
|
2286
|
+
};
|
|
2287
|
+
}
|
|
2288
|
+
function majorVersion(version) {
|
|
2289
|
+
const major = Number.parseInt(String(version).split(".")[0] ?? "", 10);
|
|
2290
|
+
return Number.isFinite(major) ? major : Number.NaN;
|
|
2291
|
+
}
|
|
2292
|
+
function canonicalStringify(value) {
|
|
2293
|
+
return JSON.stringify(normalizeValue(value));
|
|
2294
|
+
}
|
|
2295
|
+
function normalizeValue(value) {
|
|
2296
|
+
if (Array.isArray(value)) {
|
|
2297
|
+
return value.map((item) => (item === undefined ? null : normalizeValue(item)));
|
|
2298
|
+
}
|
|
2299
|
+
if (!isPlainObject(value))
|
|
2300
|
+
return value;
|
|
2301
|
+
const normalized = {};
|
|
2302
|
+
for (const key of Object.keys(value).sort()) {
|
|
2303
|
+
const child = value[key];
|
|
2304
|
+
if (child !== undefined) {
|
|
2305
|
+
normalized[key] = normalizeValue(child);
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
return normalized;
|
|
2309
|
+
}
|
|
2310
|
+
function isPlainObject(value) {
|
|
2311
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2312
|
+
}
|
|
2313
|
+
function sha256(input) {
|
|
2314
|
+
const bytes = utf8Bytes(input);
|
|
2315
|
+
const bitLength = bytes.length * 8;
|
|
2316
|
+
bytes.push(0x80);
|
|
2317
|
+
while (bytes.length % 64 !== 56)
|
|
2318
|
+
bytes.push(0);
|
|
2319
|
+
for (let i = 7; i >= 0; i--) {
|
|
2320
|
+
bytes.push(Math.floor(bitLength / 2 ** (i * 8)) & 0xff);
|
|
2321
|
+
}
|
|
2322
|
+
const h = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
2323
|
+
const k = [
|
|
2324
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98,
|
|
2325
|
+
0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
|
2326
|
+
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8,
|
|
2327
|
+
0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
2328
|
+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
|
|
2329
|
+
0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
|
2330
|
+
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
|
|
2331
|
+
0xc67178f2,
|
|
2332
|
+
];
|
|
2333
|
+
const w = new Array(64);
|
|
2334
|
+
for (let offset = 0; offset < bytes.length; offset += 64) {
|
|
2335
|
+
for (let i = 0; i < 16; i++) {
|
|
2336
|
+
const j = offset + i * 4;
|
|
2337
|
+
w[i] = ((bytes[j] << 24) | (bytes[j + 1] << 16) | (bytes[j + 2] << 8) | bytes[j + 3]) >>> 0;
|
|
2338
|
+
}
|
|
2339
|
+
for (let i = 16; i < 64; i++) {
|
|
2340
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
2341
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
2342
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
2343
|
+
}
|
|
2344
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
2345
|
+
for (let i = 0; i < 64; i++) {
|
|
2346
|
+
const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
2347
|
+
const ch = (e & f) ^ (~e & g);
|
|
2348
|
+
const temp1 = (hh + s1 + ch + k[i] + w[i]) >>> 0;
|
|
2349
|
+
const s0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
2350
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
2351
|
+
const temp2 = (s0 + maj) >>> 0;
|
|
2352
|
+
hh = g;
|
|
2353
|
+
g = f;
|
|
2354
|
+
f = e;
|
|
2355
|
+
e = (d + temp1) >>> 0;
|
|
2356
|
+
d = c;
|
|
2357
|
+
c = b;
|
|
2358
|
+
b = a;
|
|
2359
|
+
a = (temp1 + temp2) >>> 0;
|
|
2360
|
+
}
|
|
2361
|
+
h[0] = (h[0] + a) >>> 0;
|
|
2362
|
+
h[1] = (h[1] + b) >>> 0;
|
|
2363
|
+
h[2] = (h[2] + c) >>> 0;
|
|
2364
|
+
h[3] = (h[3] + d) >>> 0;
|
|
2365
|
+
h[4] = (h[4] + e) >>> 0;
|
|
2366
|
+
h[5] = (h[5] + f) >>> 0;
|
|
2367
|
+
h[6] = (h[6] + g) >>> 0;
|
|
2368
|
+
h[7] = (h[7] + hh) >>> 0;
|
|
2369
|
+
}
|
|
2370
|
+
return h.map((value) => value.toString(16).padStart(8, "0")).join("");
|
|
2371
|
+
}
|
|
2372
|
+
function utf8Bytes(input) {
|
|
2373
|
+
return Array.from(new TextEncoder().encode(input));
|
|
2374
|
+
}
|
|
2375
|
+
function rotr(value, bits) {
|
|
2376
|
+
return (value >>> bits) | (value << (32 - bits));
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2379
|
+
export { AnimationController as A, DEFAULT_CELL_SIZE as D, AnimationEngine as a, AssetRegistryImpl as b, applyThemeToElement as c, buildSceneDOM as d, calculateTransform as e, calculateVisualSize as f, composeTheme as g, createAssetRegistry as h, createDefaultRegistry as i, easeInCubic as j, easeInOutCubic as k, easeOutCubic as l, linear as m, mountScene as n, resolveTheme as o, projectToScreen as p, resolveEasing as r };
|
|
2380
|
+
//# sourceMappingURL=index-CDQt8CfR.js.map
|