@phin-tech/dottie 0.3.0 → 0.5.1

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/dottie.js CHANGED
@@ -1,15 +1,16 @@
1
- /*! dottie.js 0.3.0 | Apache-2.0 | https://github.com/phin-tech/dottie */
1
+ /*! dottie.js 0.5.1 | Apache-2.0 | https://github.com/phin-tech/dottie */
2
2
  var __defProp = Object.defineProperty;
3
3
  var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2;
4
4
  var __publicField = (obj, key, value2) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value2);
5
5
 
6
6
  // src/model.ts
7
7
  var SUPPORTED_VERSION = 2;
8
- var KINDS = ["rectangle", "ellipse", "diamond", "text", "connector", "freehand", "path", "note", "image"];
8
+ var KINDS = ["rectangle", "ellipse", "diamond", "text", "connector", "freehand", "path", "note", "image", "symbol"];
9
9
  var EASES = ["linear", "easeIn", "easeOut", "easeInOut", "hold", "spring", "bounce", "elastic"];
10
10
  var KNOWN_FIELDS = {
11
11
  file: ["version", "pages", "images"],
12
- page: ["id", "name", "layers", "elements", "theme", "guidesX", "guidesY", "guidesLocked", "timeline"],
12
+ page: ["id", "name", "layers", "elements", "theme", "guidesX", "guidesY", "guidesLocked", "timeline", "symbols"],
13
+ symbol: ["id", "name", "width", "height", "layers", "elements", "timeline", "button"],
13
14
  layer: ["id", "name", "visible", "locked"],
14
15
  rgba: ["r", "g", "b", "a"],
15
16
  style: ["stroke", "fill", "fillStyle", "strokeWidth", "dash", "roughness", "bowing", "fontSize", "gridSize", "opacity", "dashOffset"],
@@ -33,6 +34,11 @@ var KNOWN_FIELDS = {
33
34
  "rotation",
34
35
  "link",
35
36
  "anchor",
37
+ "click",
38
+ "hover",
39
+ "symbol",
40
+ "playback",
41
+ "offset",
36
42
  "image",
37
43
  "reveal"
38
44
  ],
@@ -47,7 +53,9 @@ var KNOWN_FIELDS = {
47
53
  follow: ["path", "start", "end", "ease", "reverse", "orient", "laps"],
48
54
  repeat: ["start", "end", "count"],
49
55
  cameraKey: ["t", "x", "y", "width", "height", "ease"],
50
- marker: ["t", "name"]
56
+ marker: ["t", "name"],
57
+ click: ["scene", "url"],
58
+ hover: ["stroke", "fill", "opacity"]
51
59
  };
52
60
  var MAX_DURATION = 3600;
53
61
  var MAX_MARCH = 1e4;
@@ -70,6 +78,11 @@ function clampTime(t) {
70
78
  function clampFPS(f) {
71
79
  return Number.isFinite(f) ? Math.min(Math.max(f, 1), 60) : 12;
72
80
  }
81
+ function allowedURL(url) {
82
+ const u = url.trim();
83
+ if (!/^(https?:\/\/|mailto:)/i.test(u) || [...u].length <= 7 || /[\u0000-\u001f\u007f-\u009f]/.test(u)) return null;
84
+ return u;
85
+ }
73
86
  function rgba(v) {
74
87
  if (!isObj(v)) return void 0;
75
88
  const r = num(v.r), g = num(v.g), b = num(v.b);
@@ -184,10 +197,61 @@ var Reader = class {
184
197
  rotation: num(v.rotation) ?? null,
185
198
  link: uuid(v.link) ?? null,
186
199
  anchor: str(v.anchor) ?? null,
200
+ click: this.click(v.click),
201
+ hover: this.hover(v.hover),
202
+ symbol: uuid(v.symbol) ?? null,
203
+ playback: this.playback(v.playback),
204
+ offset: num(v.offset) ?? null,
187
205
  image: this.image(v.image, images),
188
206
  reveal: num(v.reveal) ?? null
189
207
  };
190
208
  }
209
+ click(v) {
210
+ if (!isObj(v)) return null;
211
+ this.fields(v, "click");
212
+ const url = str(v.url);
213
+ const safe = url === void 0 ? null : allowedURL(url);
214
+ if (url !== void 0 && safe === null) this.warn("Skipped a click link that isn't http, https or mailto");
215
+ return { scene: str(v.scene) ?? null, url: safe };
216
+ }
217
+ playback(v) {
218
+ const s = str(v);
219
+ if (s === void 0) return null;
220
+ return ["loop", "once", "still", "click"].includes(s) ? s : "loop";
221
+ }
222
+ symbol(v, images) {
223
+ if (!isObj(v)) return null;
224
+ this.fields(v, "symbol");
225
+ const id = uuid(v.id);
226
+ if (!id) {
227
+ this.warn("Skipped a symbol without an id");
228
+ return null;
229
+ }
230
+ const size = (x) => {
231
+ const n = num(x);
232
+ return Math.max(1, n ?? 100);
233
+ };
234
+ const layers = (Array.isArray(v.layers) ? v.layers : []).filter(isObj).flatMap((l) => {
235
+ const lid = uuid(l.id);
236
+ return lid ? [{ id: lid, name: str(l.name) ?? "", visible: bool(l.visible) ?? true, locked: bool(l.locked) ?? false }] : [];
237
+ });
238
+ if (layers.length === 0) layers.push({ id: `${id}-LAYER`, name: "Layer 1", visible: true, locked: false });
239
+ return {
240
+ id,
241
+ name: str(v.name) ?? "Symbol",
242
+ width: size(v.width),
243
+ height: size(v.height),
244
+ layers,
245
+ elements: (Array.isArray(v.elements) ? v.elements : []).map((e) => this.element(e, images)).filter((e) => e !== null),
246
+ timeline: this.timeline(v.timeline),
247
+ button: bool(v.button) ?? null
248
+ };
249
+ }
250
+ hover(v) {
251
+ if (!isObj(v)) return null;
252
+ this.fields(v, "hover");
253
+ return { stroke: rgba(v.stroke) ?? null, fill: rgba(v.fill) ?? null, opacity: num(v.opacity) ?? null };
254
+ }
191
255
  ease(v) {
192
256
  const s = str(v);
193
257
  if (s === void 0) return null;
@@ -280,7 +344,8 @@ var Reader = class {
280
344
  layers,
281
345
  elements: (Array.isArray(v.elements) ? v.elements : []).map((e) => this.element(e, images)).filter((e) => e !== null),
282
346
  theme: str(v.theme) ?? null,
283
- timeline: this.timeline(v.timeline)
347
+ timeline: this.timeline(v.timeline),
348
+ symbols: Array.isArray(v.symbols) ? v.symbols.map((x) => this.symbol(x, images)).filter((x) => x !== null) : null
284
349
  };
285
350
  }
286
351
  };
@@ -922,6 +987,190 @@ function hachureLines(polygons, angle2, gap) {
922
987
  return out;
923
988
  }
924
989
 
990
+ // src/dungeon.ts
991
+ var SUB = 2;
992
+ var BAND = 0.75;
993
+ var GROW = 0.8;
994
+ var JITTER = 0.6;
995
+ function gridLines(polygons, cell) {
996
+ return [...scan(polygons, cell, false), ...scan(polygons, cell, true)];
997
+ }
998
+ function scan(polygons, cell, transposed) {
999
+ if (!(cell > 0)) return [];
1000
+ const flip = (p) => transposed ? { x: p.y, y: p.x } : p;
1001
+ const polys = polygons.filter((p) => p.length >= 3).map((p) => p.map(flip));
1002
+ const ys = polys.flat().map((p) => p.y);
1003
+ if (ys.length === 0) return [];
1004
+ const minY = Math.min(...ys), maxY2 = Math.max(...ys);
1005
+ const out = [];
1006
+ for (let y = Math.floor(minY / cell) * cell; y < maxY2 - 0.5; y += cell) {
1007
+ if (!(y > minY + 0.5)) continue;
1008
+ const xs = [];
1009
+ for (const poly of polys) {
1010
+ for (let i = 0; i < poly.length; i++) {
1011
+ const a = poly[i], b = poly[(i + 1) % poly.length];
1012
+ if (a.y <= y && y < b.y || b.y <= y && y < a.y) xs.push(a.x + (y - a.y) / (b.y - a.y) * (b.x - a.x));
1013
+ }
1014
+ }
1015
+ xs.sort((p, q) => p - q);
1016
+ for (let k = 0; k + 1 < xs.length; k += 2) {
1017
+ if (xs[k + 1] - xs[k] > 0.5) out.push([flip({ x: xs[k], y }), flip({ x: xs[k + 1], y })]);
1018
+ }
1019
+ }
1020
+ return out;
1021
+ }
1022
+ function lineColor(floor) {
1023
+ if (0.2126 * floor.r + 0.7152 * floor.g + 0.0722 * floor.b > 0.5) {
1024
+ return { r: floor.r * 0.84, g: floor.g * 0.84, b: floor.b * 0.8, a: floor.a };
1025
+ }
1026
+ return { r: floor.r + (1 - floor.r) * 0.25, g: floor.g + (1 - floor.g) * 0.25, b: floor.b + (1 - floor.b) * 0.25, a: floor.a };
1027
+ }
1028
+ function isFloor(e) {
1029
+ return e.style.fillStyle === "grid" && e.style.fill.a > 0 && ["rectangle", "ellipse", "diamond", "path"].includes(e.kind);
1030
+ }
1031
+ function hash01(i, j, salt) {
1032
+ let h = (Math.imul(i, 668265261) ^ Math.imul(j, 374761393) ^ Math.imul(salt, 2654435761)) >>> 0;
1033
+ h = Math.imul(h ^ h >>> 15, 2246822507) >>> 0;
1034
+ h = Math.imul(h ^ h >>> 13, 3266489909) >>> 0;
1035
+ h = (h ^ h >>> 16) >>> 0;
1036
+ return h / 4294967296;
1037
+ }
1038
+ function spot(i, j, cell) {
1039
+ const s = cell / SUB;
1040
+ return { x: (i + 0.5 + (hash01(i, j, 1) - 0.5) * JITTER) * s, y: (j + 0.5 + (hash01(i, j, 2) - 0.5) * JITTER) * s };
1041
+ }
1042
+ function clusterSize(i, j, at, cell) {
1043
+ let nearest2 = Infinity;
1044
+ for (let di = -1; di <= 1; di++) {
1045
+ for (let dj = -1; dj <= 1; dj++) {
1046
+ if (di === 0 && dj === 0) continue;
1047
+ const o = spot(i + di, j + dj, cell);
1048
+ nearest2 = Math.min(nearest2, Math.hypot(o.x - at.x, o.y - at.y));
1049
+ }
1050
+ }
1051
+ return nearest2 * GROW;
1052
+ }
1053
+ function clusterStrokes(c, angle2, size) {
1054
+ const dx = Math.cos(angle2), dy = Math.sin(angle2);
1055
+ const half = size * 0.8, gap = size * 0.5;
1056
+ return [-1, 0, 1].map((k) => {
1057
+ const cx = c.x - dy * gap * k, cy = c.y + dx * gap * k;
1058
+ return [{ x: cx - dx * half, y: cy - dy * half }, { x: cx + dx * half, y: cy + dy * half }];
1059
+ });
1060
+ }
1061
+ function contains(contours, p) {
1062
+ let inside = false;
1063
+ for (const poly of contours) {
1064
+ if (poly.length < 3) continue;
1065
+ let k = poly.length - 1;
1066
+ for (let i = 0; i < poly.length; i++) {
1067
+ const a = poly[i], b = poly[k];
1068
+ if (a.y > p.y !== b.y > p.y && p.x < (b.x - a.x) * (p.y - a.y) / (b.y - a.y) + a.x) inside = !inside;
1069
+ k = i;
1070
+ }
1071
+ }
1072
+ return inside;
1073
+ }
1074
+ function distance(p, contours) {
1075
+ let best = Infinity;
1076
+ for (const poly of contours) {
1077
+ if (poly.length < 2) continue;
1078
+ for (let i = 0; i < poly.length; i++) {
1079
+ const a = poly[i], b = poly[(i + 1) % poly.length];
1080
+ const dx = b.x - a.x, dy = b.y - a.y;
1081
+ const len2 = dx * dx + dy * dy;
1082
+ const t = len2 > 0 ? Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2)) : 0;
1083
+ best = Math.min(best, Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy)));
1084
+ }
1085
+ }
1086
+ return best;
1087
+ }
1088
+ var containsPoint = (r, p) => p.x >= r.x && p.x < r.x + r.width && p.y >= r.y && p.y < r.y + r.height;
1089
+ var rockCache = /* @__PURE__ */ new Map();
1090
+ function rock(floors) {
1091
+ if (floors.length === 0) return [];
1092
+ const key = floors.map((e) => [
1093
+ e.kind,
1094
+ e.x,
1095
+ e.y,
1096
+ e.width,
1097
+ e.height,
1098
+ e.rotation ?? 0,
1099
+ cellSize(e),
1100
+ JSON.stringify(e.subpaths ?? null),
1101
+ e.style.stroke.r,
1102
+ e.style.stroke.g,
1103
+ e.style.stroke.b,
1104
+ e.style.stroke.a,
1105
+ e.style.roughness,
1106
+ e.style.strokeWidth
1107
+ ].join(",")).join(";");
1108
+ const hit = rockCache.get(key);
1109
+ if (hit) return hit;
1110
+ const v = computeRock(floors);
1111
+ if (rockCache.size >= 16) rockCache.clear();
1112
+ rockCache.set(key, v);
1113
+ return v;
1114
+ }
1115
+ function computeRock(elements) {
1116
+ const floors = elements.map((e) => {
1117
+ const contours = fillPolygons(e).map((c) => c.map((p) => rotated(e, p)));
1118
+ const pts = contours.flat();
1119
+ const xs = pts.map((p) => p.x), ys = pts.map((p) => p.y);
1120
+ const x0 = xs.length ? Math.min(...xs) : 0, y0 = ys.length ? Math.min(...ys) : 0;
1121
+ const box = { x: x0, y: y0, width: (xs.length ? Math.max(...xs) : 0) - x0, height: (ys.length ? Math.max(...ys) : 0) - y0 };
1122
+ return { contours, box, cell: cellSize(e), element: e };
1123
+ });
1124
+ const strokes = floors.map(() => []);
1125
+ const seen = /* @__PURE__ */ new Set();
1126
+ for (const f of floors) {
1127
+ const subSize = f.cell / SUB;
1128
+ const around = inset(f.box, -f.cell, -f.cell);
1129
+ const i0 = Math.floor(around.x / subSize), i1 = Math.ceil((around.x + around.width) / subSize);
1130
+ const j0 = Math.floor(around.y / subSize), j1 = Math.ceil((around.y + around.height) / subSize);
1131
+ const reach = BAND * f.cell;
1132
+ for (let i = i0; i < i1; i++) {
1133
+ for (let j = j0; j < j1; j++) {
1134
+ const k = `${f.cell}|${i}|${j}`;
1135
+ if (seen.has(k)) continue;
1136
+ seen.add(k);
1137
+ const at = spot(i, j, f.cell);
1138
+ let nearest2 = Infinity, owner = -1;
1139
+ for (let n = 0; n < floors.length; n++) {
1140
+ const g = floors[n];
1141
+ if (!containsPoint(inset(g.box, -reach, -reach), at)) continue;
1142
+ if (contains(g.contours, at)) {
1143
+ nearest2 = 0;
1144
+ break;
1145
+ }
1146
+ const d = distance(at, g.contours);
1147
+ if (d < nearest2) {
1148
+ nearest2 = d;
1149
+ owner = n;
1150
+ }
1151
+ }
1152
+ if (!(nearest2 > 0 && nearest2 <= reach && owner >= 0)) continue;
1153
+ const angle2 = hash01(i, j, 3) * Math.PI;
1154
+ const size = clusterSize(i, j, at, f.cell);
1155
+ const seed = Math.trunc(hash01(i, j, 4) * 4294967296) >>> 0;
1156
+ for (const [a, b] of clusterStrokes(at, angle2, size)) strokes[owner].push([a, b, seed]);
1157
+ }
1158
+ }
1159
+ }
1160
+ const out = [];
1161
+ floors.forEach((f, n) => {
1162
+ if (strokes[n].length === 0) return;
1163
+ const e = f.element;
1164
+ const path = new Path();
1165
+ for (const [a, b, seed] of strokes[n]) {
1166
+ if (e.style.roughness <= 0) path.moveTo(a).lineTo(b);
1167
+ else line(a, b, new SeededRNG(seed), e.style.roughness * 0.35, 0.2, false, path);
1168
+ }
1169
+ out.push({ ink: { ...e.style.stroke, a: e.style.stroke.a * 0.6 }, width: Math.max(0.8, e.style.strokeWidth * 0.45), path });
1170
+ });
1171
+ return out;
1172
+ }
1173
+
925
1174
  // src/shapes.ts
926
1175
  var Cache = class {
927
1176
  constructor(limit = 6e3) {
@@ -1174,196 +1423,551 @@ function computeConnector(e, index) {
1174
1423
  return [path, solid];
1175
1424
  }
1176
1425
 
1177
- // src/dungeon.ts
1178
- var SUB = 2;
1179
- var BAND = 0.75;
1180
- var GROW = 0.8;
1181
- var JITTER = 0.6;
1182
- function gridLines(polygons, cell) {
1183
- return [...scan(polygons, cell, false), ...scan(polygons, cell, true)];
1184
- }
1185
- function scan(polygons, cell, transposed) {
1186
- if (!(cell > 0)) return [];
1187
- const flip = (p) => transposed ? { x: p.y, y: p.x } : p;
1188
- const polys = polygons.filter((p) => p.length >= 3).map((p) => p.map(flip));
1189
- const ys = polys.flat().map((p) => p.y);
1190
- if (ys.length === 0) return [];
1191
- const minY = Math.min(...ys), maxY2 = Math.max(...ys);
1192
- const out = [];
1193
- for (let y = Math.floor(minY / cell) * cell; y < maxY2 - 0.5; y += cell) {
1194
- if (!(y > minY + 0.5)) continue;
1195
- const xs = [];
1196
- for (const poly of polys) {
1197
- for (let i = 0; i < poly.length; i++) {
1198
- const a = poly[i], b = poly[(i + 1) % poly.length];
1199
- if (a.y <= y && y < b.y || b.y <= y && y < a.y) xs.push(a.x + (y - a.y) / (b.y - a.y) * (b.x - a.x));
1200
- }
1201
- }
1202
- xs.sort((p, q) => p - q);
1203
- for (let k = 0; k + 1 < xs.length; k += 2) {
1204
- if (xs[k + 1] - xs[k] > 0.5) out.push([flip({ x: xs[k], y }), flip({ x: xs[k + 1], y })]);
1205
- }
1426
+ // src/timeline.ts
1427
+ function applyEase(ease, u) {
1428
+ switch (ease) {
1429
+ case "linear":
1430
+ return u;
1431
+ case "easeIn":
1432
+ return u * u * u;
1433
+ case "easeOut":
1434
+ return 1 - Math.pow(1 - u, 3);
1435
+ case "easeInOut":
1436
+ return u < 0.5 ? 4 * u * u * u : 1 - Math.pow(-2 * u + 2, 3) / 2;
1437
+ case "hold":
1438
+ return 0;
1439
+ case "spring":
1440
+ return spring(u);
1441
+ case "bounce":
1442
+ return bounce(u);
1443
+ case "elastic":
1444
+ return elastic(u);
1206
1445
  }
1207
- return out;
1208
1446
  }
1209
- function lineColor(floor) {
1210
- if (0.2126 * floor.r + 0.7152 * floor.g + 0.0722 * floor.b > 0.5) {
1211
- return { r: floor.r * 0.84, g: floor.g * 0.84, b: floor.b * 0.8, a: floor.a };
1212
- }
1213
- return { r: floor.r + (1 - floor.r) * 0.25, g: floor.g + (1 - floor.g) * 0.25, b: floor.b + (1 - floor.b) * 0.25, a: floor.a };
1447
+ function spring(u) {
1448
+ if (u <= 0) return 0;
1449
+ if (u >= 1) return 1;
1450
+ const s = (u2) => 1 - Math.exp(-6 * u2) * (Math.cos(3 * Math.PI * u2) + 6 / (3 * Math.PI) * Math.sin(3 * Math.PI * u2));
1451
+ return s(u) + u * (1 - s(1));
1214
1452
  }
1215
- function isFloor(e) {
1216
- return e.style.fillStyle === "grid" && e.style.fill.a > 0 && ["rectangle", "ellipse", "diamond", "path"].includes(e.kind);
1453
+ function bounce(u) {
1454
+ if (u <= 0) return 0;
1455
+ if (u >= 1) return 1;
1456
+ const n = 7.5625, d = 2.75;
1457
+ if (u < 1 / d) return n * u * u;
1458
+ if (u < 2 / d) {
1459
+ const v2 = u - 1.5 / d;
1460
+ return n * v2 * v2 + 0.75;
1461
+ }
1462
+ if (u < 2.5 / d) {
1463
+ const v2 = u - 2.25 / d;
1464
+ return n * v2 * v2 + 0.9375;
1465
+ }
1466
+ const v = u - 2.625 / d;
1467
+ return n * v * v + 0.984375;
1217
1468
  }
1218
- function hash01(i, j, salt) {
1219
- let h = (Math.imul(i, 668265261) ^ Math.imul(j, 374761393) ^ Math.imul(salt, 2654435761)) >>> 0;
1220
- h = Math.imul(h ^ h >>> 15, 2246822507) >>> 0;
1221
- h = Math.imul(h ^ h >>> 13, 3266489909) >>> 0;
1222
- h = (h ^ h >>> 16) >>> 0;
1223
- return h / 4294967296;
1469
+ function elastic(u) {
1470
+ if (u <= 0) return 0;
1471
+ if (u >= 1) return 1;
1472
+ return Math.pow(2, -10 * u) * Math.sin((u * 10 - 0.75) * (2 * Math.PI / 3)) + 1;
1224
1473
  }
1225
- function spot(i, j, cell) {
1226
- const s = cell / SUB;
1227
- return { x: (i + 0.5 + (hash01(i, j, 1) - 0.5) * JITTER) * s, y: (j + 0.5 + (hash01(i, j, 2) - 0.5) * JITTER) * s };
1474
+ var rem = (a, b) => a % b;
1475
+ function localTime(track, t) {
1476
+ const reps = (track.repeats ?? []).filter(repeatIsValid).slice().sort((a, b) => a.start - b.start);
1477
+ if (reps.length === 0) return t;
1478
+ for (const r of reps) {
1479
+ if (!(t >= r.end)) continue;
1480
+ const length = r.end - r.start;
1481
+ if (r.count === null) return r.start + rem(t - r.start, length);
1482
+ const played = length * r.count;
1483
+ if (t < r.start + played) return r.start + rem(t - r.start, length);
1484
+ t -= played - length;
1485
+ }
1486
+ return t;
1228
1487
  }
1229
- function clusterSize(i, j, at, cell) {
1230
- let nearest2 = Infinity;
1231
- for (let di = -1; di <= 1; di++) {
1232
- for (let dj = -1; dj <= 1; dj++) {
1233
- if (di === 0 && dj === 0) continue;
1234
- const o = spot(i + di, j + dj, cell);
1235
- nearest2 = Math.min(nearest2, Math.hypot(o.x - at.x, o.y - at.y));
1488
+ function value(track, t, get, base, lerp) {
1489
+ t = localTime(track, t);
1490
+ let before, after;
1491
+ for (const k of track.keys) {
1492
+ if (get(k) === void 0) continue;
1493
+ if (k.t <= t) before = k;
1494
+ else {
1495
+ after = k;
1496
+ break;
1236
1497
  }
1237
1498
  }
1238
- return nearest2 * GROW;
1499
+ if (!before && !after) return base;
1500
+ if (before && !after) return get(before);
1501
+ if (!before && after) return get(after);
1502
+ const b = before, a = after;
1503
+ const u = a.t - b.t > 0 ? (t - b.t) / (a.t - b.t) : 1;
1504
+ return lerp(get(b), get(a), applyEase(b.ease ?? "linear", u));
1239
1505
  }
1240
- function clusterStrokes(c, angle2, size) {
1241
- const dx = Math.cos(angle2), dy = Math.sin(angle2);
1242
- const half = size * 0.8, gap = size * 0.5;
1243
- return [-1, 0, 1].map((k) => {
1244
- const cx = c.x - dy * gap * k, cy = c.y + dx * gap * k;
1245
- return [{ x: cx - dx * half, y: cy - dy * half }, { x: cx + dx * half, y: cy + dy * half }];
1246
- });
1506
+ var lerpNum = (a, b, u) => a + (b - a) * u;
1507
+ var mix01 = (a, b, u) => Math.min(Math.max(a + (b - a) * u, 0), 1);
1508
+ var lerpColor = (a, b, u) => ({
1509
+ r: mix01(a.r, b.r, u),
1510
+ g: mix01(a.g, b.g, u),
1511
+ b: mix01(a.b, b.b, u),
1512
+ a: mix01(a.a, b.a, u)
1513
+ });
1514
+ function num2(track, t, p, base) {
1515
+ return value(track, t, (k) => k[p], base, lerpNum);
1247
1516
  }
1248
- function contains(contours, p) {
1249
- let inside = false;
1250
- for (const poly of contours) {
1251
- if (poly.length < 3) continue;
1252
- let k = poly.length - 1;
1253
- for (let i = 0; i < poly.length; i++) {
1254
- const a = poly[i], b = poly[k];
1255
- if (a.y > p.y !== b.y > p.y && p.x < (b.x - a.x) * (p.y - a.y) / (b.y - a.y) + a.x) inside = !inside;
1256
- k = i;
1257
- }
1258
- }
1259
- return inside;
1517
+ function isChanging(track, t) {
1518
+ t = localTime(track, t);
1519
+ const changing = (get, eq) => {
1520
+ let b;
1521
+ for (const k of track.keys) if (k.t <= t && get(k) !== void 0) b = k;
1522
+ const a = track.keys.find((k) => k.t > t && get(k) !== void 0);
1523
+ if (!b || !a) return false;
1524
+ return b.ease !== "hold" && !eq(get(b), get(a));
1525
+ };
1526
+ const n = (p) => changing((k) => k[p], (a, b) => a === b);
1527
+ return n("x") || n("y") || n("width") || n("height") || n("rotation") || n("strokeWidth") || changing((k) => k.stroke, sameColor) || changing((k) => k.fill, sameColor) || n("reveal") || n("opacity");
1260
1528
  }
1261
- function distance(p, contours) {
1262
- let best = Infinity;
1263
- for (const poly of contours) {
1264
- if (poly.length < 2) continue;
1265
- for (let i = 0; i < poly.length; i++) {
1266
- const a = poly[i], b = poly[(i + 1) % poly.length];
1267
- const dx = b.x - a.x, dy = b.y - a.y;
1268
- const len2 = dx * dx + dy * dy;
1269
- const t = len2 > 0 ? Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2)) : 0;
1270
- best = Math.min(best, Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy)));
1271
- }
1529
+ function isVisible(track, t) {
1530
+ t = localTime(track, t);
1531
+ const keyed = track.keys.filter((k) => k.visible !== void 0);
1532
+ if (keyed.length === 0) return true;
1533
+ let last;
1534
+ for (const k of keyed) if (k.t <= t) last = k;
1535
+ return last ? last.visible : !keyed[0].visible;
1536
+ }
1537
+ function applyTrack(track, e, t) {
1538
+ e = cloneElement(e);
1539
+ if (track.keys.some((k) => k.reveal !== void 0)) {
1540
+ const r = num2(track, t, "reveal", e.reveal ?? 1);
1541
+ e.reveal = r >= 1 ? null : Math.max(0, r);
1272
1542
  }
1273
- return best;
1543
+ if (track.keys.some((k) => k.opacity !== void 0)) {
1544
+ const o = Math.min(Math.max(num2(track, t, "opacity", e.style.opacity ?? 1), 0), 1);
1545
+ e.style.opacity = o >= 1 ? null : o;
1546
+ }
1547
+ if (e.kind === "connector") return e;
1548
+ e.x = num2(track, t, "x", e.x);
1549
+ e.y = num2(track, t, "y", e.y);
1550
+ e.width = Math.max(0, num2(track, t, "width", e.width));
1551
+ e.height = Math.max(0, num2(track, t, "height", e.height));
1552
+ if (track.keys.some((k) => k.rotation !== void 0)) e.rotation = num2(track, t, "rotation", angle(e));
1553
+ e.style.strokeWidth = Math.max(0, num2(track, t, "strokeWidth", e.style.strokeWidth));
1554
+ e.style.stroke = value(track, t, (k) => k.stroke, e.style.stroke, lerpColor);
1555
+ e.style.fill = value(track, t, (k) => k.fill, e.style.fill, lerpColor);
1556
+ return e;
1274
1557
  }
1275
- var containsPoint = (r, p) => p.x >= r.x && p.x < r.x + r.width && p.y >= r.y && p.y < r.y + r.height;
1276
- var rockCache = /* @__PURE__ */ new Map();
1277
- function rock(floors) {
1278
- if (floors.length === 0) return [];
1279
- const key = floors.map((e) => [
1280
- e.kind,
1281
- e.x,
1282
- e.y,
1283
- e.width,
1284
- e.height,
1285
- e.rotation ?? 0,
1286
- cellSize(e),
1287
- JSON.stringify(e.subpaths ?? null),
1288
- e.style.stroke.r,
1289
- e.style.stroke.g,
1290
- e.style.stroke.b,
1291
- e.style.stroke.a,
1292
- e.style.roughness,
1293
- e.style.strokeWidth
1294
- ].join(",")).join(";");
1295
- const hit = rockCache.get(key);
1296
- if (hit) return hit;
1297
- const v = computeRock(floors);
1298
- if (rockCache.size >= 16) rockCache.clear();
1299
- rockCache.set(key, v);
1300
- return v;
1558
+ function followSpan(track, t) {
1559
+ t = localTime(track, t);
1560
+ let f = null;
1561
+ for (const s of track.follow ?? []) if (s.start <= t && (f === null || s.start > f.start)) f = s;
1562
+ if (!f) return null;
1563
+ const span = f;
1564
+ if (track.keys.some((k) => k.t >= span.end && k.t <= t && (k.x !== void 0 || k.y !== void 0))) return null;
1565
+ return f;
1301
1566
  }
1302
- function computeRock(elements) {
1303
- const floors = elements.map((e) => {
1304
- const contours = fillPolygons(e).map((c) => c.map((p) => rotated(e, p)));
1305
- const pts = contours.flat();
1306
- const xs = pts.map((p) => p.x), ys = pts.map((p) => p.y);
1307
- const x0 = xs.length ? Math.min(...xs) : 0, y0 = ys.length ? Math.min(...ys) : 0;
1308
- const box = { x: x0, y: y0, width: (xs.length ? Math.max(...xs) : 0) - x0, height: (ys.length ? Math.max(...ys) : 0) - y0 };
1309
- return { contours, box, cell: cellSize(e), element: e };
1310
- });
1311
- const strokes = floors.map(() => []);
1312
- const seen = /* @__PURE__ */ new Set();
1313
- for (const f of floors) {
1314
- const subSize = f.cell / SUB;
1315
- const around = inset(f.box, -f.cell, -f.cell);
1316
- const i0 = Math.floor(around.x / subSize), i1 = Math.ceil((around.x + around.width) / subSize);
1317
- const j0 = Math.floor(around.y / subSize), j1 = Math.ceil((around.y + around.height) / subSize);
1318
- const reach = BAND * f.cell;
1319
- for (let i = i0; i < i1; i++) {
1320
- for (let j = j0; j < j1; j++) {
1321
- const k = `${f.cell}|${i}|${j}`;
1322
- if (seen.has(k)) continue;
1323
- seen.add(k);
1324
- const at = spot(i, j, f.cell);
1325
- let nearest2 = Infinity, owner = -1;
1326
- for (let n = 0; n < floors.length; n++) {
1327
- const g = floors[n];
1328
- if (!containsPoint(inset(g.box, -reach, -reach), at)) continue;
1329
- if (contains(g.contours, at)) {
1330
- nearest2 = 0;
1331
- break;
1332
- }
1333
- const d = distance(at, g.contours);
1334
- if (d < nearest2) {
1335
- nearest2 = d;
1336
- owner = n;
1337
- }
1338
- }
1339
- if (!(nearest2 > 0 && nearest2 <= reach && owner >= 0)) continue;
1340
- const angle2 = hash01(i, j, 3) * Math.PI;
1341
- const size = clusterSize(i, j, at, f.cell);
1342
- const seed = Math.trunc(hash01(i, j, 4) * 4294967296) >>> 0;
1343
- for (const [a, b] of clusterStrokes(at, angle2, size)) strokes[owner].push([a, b, seed]);
1344
- }
1345
- }
1346
- }
1347
- const out = [];
1348
- floors.forEach((f, n) => {
1349
- if (strokes[n].length === 0) return;
1350
- const e = f.element;
1351
- const path = new Path();
1352
- for (const [a, b, seed] of strokes[n]) {
1353
- if (e.style.roughness <= 0) path.moveTo(a).lineTo(b);
1354
- else line(a, b, new SeededRNG(seed), e.style.roughness * 0.35, 0.2, false, path);
1355
- }
1356
- out.push({ ink: { ...e.style.stroke, a: e.style.stroke.a * 0.6 }, width: Math.max(0.8, e.style.strokeWidth * 0.45), path });
1357
- });
1358
- return out;
1567
+ function tracing(e, page) {
1568
+ if (e.kind === "connector") return new Polyline(routePoints(route(e, indexOf(page))));
1569
+ const clean = cloneElement(e);
1570
+ clean.style.roughness = 0;
1571
+ const pts = outline(clean).contours()[0] ?? [];
1572
+ return new Polyline(angle(e) === 0 ? pts : pts.map((p) => rotated(e, p)));
1359
1573
  }
1360
-
1361
- // src/metrics.ts
1362
- var ascent = 0.951995849609375;
1363
- var descent = 0.2129974365234375;
1364
- var leading = 0.0279998779296875;
1365
- var boldAscent = 0.975006103515625;
1366
- var boldDescent = 0.2169952392578125;
1574
+ function following(track, e, t, page) {
1575
+ const f = followSpan(track, t);
1576
+ if (!f) return e;
1577
+ t = localTime(track, t);
1578
+ const pathElement = page.elements.find((x) => x.id === f.path);
1579
+ if (!pathElement || pathElement.id === e.id) return e;
1580
+ const line2 = tracing(pathElement, page);
1581
+ const span = f.end - f.start;
1582
+ let u = Math.min(Math.max(applyEase(f.ease ?? "easeInOut", span > 0 ? Math.min(1, (t - f.start) / span) : 1), 0), 1) * (f.laps ?? 1);
1583
+ if (u > 1) u = rem(u, 1) === 0 ? 1 : rem(u, 1);
1584
+ if (f.reverse === true) u = 1 - u;
1585
+ const p = line2.point(u);
1586
+ const moved = cloneElement(e);
1587
+ moved.x = p.x - e.width / 2;
1588
+ moved.y = p.y - e.height / 2;
1589
+ if (f.orient === true) {
1590
+ const step = f.reverse === true ? -0.01 : 0.01;
1591
+ const a = line2.point(u - step / 2), b = line2.point(u + step / 2);
1592
+ if (a.x !== b.x || a.y !== b.y) moved.rotation = Math.atan2(b.y - a.y, b.x - a.x);
1593
+ }
1594
+ return moved;
1595
+ }
1596
+ function marchOffset(tl, speed, period, t) {
1597
+ if (!(period > 0)) return 0;
1598
+ const d = clampTime(tl.duration);
1599
+ if (d > 0) {
1600
+ const laps = roundHalfAway(speed * d / period);
1601
+ if (laps !== 0) speed = laps * period / d;
1602
+ }
1603
+ const r = rem(speed * t, period);
1604
+ return r < 0 ? -r : r === 0 ? 0 : period - r;
1605
+ }
1606
+ function trackMap(tl) {
1607
+ const m = /* @__PURE__ */ new Map();
1608
+ for (const t of tl.tracks) if (!m.has(t.element)) m.set(t.element, t);
1609
+ return m;
1610
+ }
1611
+ function resolved(page, t) {
1612
+ return expandingSymbols(posed(page, t), clampTime(t));
1613
+ }
1614
+ function posed(page, t) {
1615
+ const timeline = page.timeline;
1616
+ if (!timeline) return page;
1617
+ t = clampTime(t);
1618
+ const tracks = trackMap(timeline);
1619
+ const reseed = timeline.boil === null ? null : Math.trunc(Math.floor(t * clampFPS(timeline.fps) + 1e-9) / Math.max(1, timeline.boil)) % BOIL_DRAWINGS >>> 0;
1620
+ const hidden = /* @__PURE__ */ new Set();
1621
+ for (const tr of tracks.values()) if (!isVisible(tr, t)) hidden.add(tr.element);
1622
+ let moving = null;
1623
+ if (timeline.settle === true) {
1624
+ moving = /* @__PURE__ */ new Set();
1625
+ for (const tr of tracks.values()) if (isChanging(tr, t)) moving.add(tr.element);
1626
+ }
1627
+ const boils = (e) => {
1628
+ const override = tracks.get(e.id)?.boil;
1629
+ if (override !== void 0 && override !== null) return override;
1630
+ if (!moving) return true;
1631
+ if (e.connector) return [e.connector.start.elementID, e.connector.end.elementID].some((id) => id !== null && moving.has(id));
1632
+ return moving.has(e.id);
1633
+ };
1634
+ let elements = page.elements.map((e) => {
1635
+ const track = tracks.get(e.id);
1636
+ let out = track ? applyTrack(track, e, t) : e;
1637
+ if (reseed !== null && reseed > 0 && boils(e)) {
1638
+ if (out === e) out = cloneElement(e);
1639
+ out.seed = boiled(e.seed, reseed);
1640
+ }
1641
+ const pattern = track?.march != null ? dashPattern(out.style.dash, out.style.strokeWidth) : null;
1642
+ if (track?.march != null && pattern) {
1643
+ if (out === e) out = cloneElement(e);
1644
+ out.style.dashOffset = marchOffset(timeline, track.march, pattern.reduce((a, b) => a + b, 0), t);
1645
+ }
1646
+ return out;
1647
+ });
1648
+ elements = elements.map((e, i) => hidden.has(e.id) ? page.elements[i] : e);
1649
+ const posed2 = { ...page, elements, timeline: null };
1650
+ elements = elements.map((e) => {
1651
+ const track = tracks.get(e.id);
1652
+ return track ? following(track, e, t, posed2) : e;
1653
+ });
1654
+ const current = { ...page, elements, timeline: null };
1655
+ const index = indexOf(current);
1656
+ const gone = new Set(hidden);
1657
+ elements = elements.map((e) => {
1658
+ const c = e.connector;
1659
+ if (!c || ![c.start.elementID, c.end.elementID].some((id) => id !== null && hidden.has(id))) return e;
1660
+ if (!(tracks.get(e.id)?.keys.some((k) => k.visible !== void 0) ?? false)) {
1661
+ gone.add(e.id);
1662
+ return e;
1663
+ }
1664
+ const [a, b] = endpoints(e, index);
1665
+ const pinned = cloneElement(e);
1666
+ pinned.connector.start = { elementID: null, port: c.start.port, x: a.x, y: a.y };
1667
+ pinned.connector.end = { elementID: null, port: c.end.port, x: b.x, y: b.y };
1668
+ return pinned;
1669
+ });
1670
+ return { ...page, elements: elements.filter((e) => !gone.has(e.id)), timeline: null };
1671
+ }
1672
+ function posterTime(page) {
1673
+ return page.timeline ? page.timeline.poster ?? page.timeline.duration : 0;
1674
+ }
1675
+ function still(page) {
1676
+ return page.timeline ? resolved(page, posterTime(page)) : expandingSymbols(page, 0);
1677
+ }
1678
+ function cameraRect(tl, t) {
1679
+ const keys = tl.camera ?? [];
1680
+ if (keys.length === 0) return null;
1681
+ let before;
1682
+ for (const k of keys) if (k.t <= t) before = k;
1683
+ const after = keys.find((k) => k.t > t);
1684
+ if (!before) return rectOf(keys[0]);
1685
+ if (!after) return rectOf(before);
1686
+ const u = applyEase(before.ease ?? "easeInOut", (t - before.t) / Math.max(after.t - before.t, 1e-9));
1687
+ const mix3 = (p, q) => p + (q - p) * u;
1688
+ return { x: mix3(before.x, after.x), y: mix3(before.y, after.y), width: Math.max(1, mix3(before.width, after.width)), height: Math.max(1, mix3(before.height, after.height)) };
1689
+ }
1690
+ var rectOf = (k) => ({ x: k.x, y: k.y, width: k.width, height: k.height });
1691
+ function cameraView(tl, t) {
1692
+ const first = tl.camera?.[0];
1693
+ const r = cameraRect(tl, t);
1694
+ if (!first || !r || !(r.width > 0 && r.height > 0)) return null;
1695
+ return fit(r, first.width / first.height);
1696
+ }
1697
+ function fit(r, aspect) {
1698
+ if (!(r.width > 0 && r.height > 0 && aspect > 0)) return r;
1699
+ let w = r.width, h = r.height;
1700
+ if (r.width / r.height < aspect) w = r.height * aspect;
1701
+ else h = r.width / aspect;
1702
+ return { x: r.x + r.width / 2 - w / 2, y: r.y + r.height / 2 - h / 2, width: w, height: h };
1703
+ }
1704
+ function frameCount(tl) {
1705
+ return Math.max(1, Math.ceil(clampTime(tl.duration) * clampFPS(tl.fps)));
1706
+ }
1707
+ function frameTimes(tl) {
1708
+ const fps = clampFPS(tl.fps);
1709
+ const times = [];
1710
+ for (let i = 0; i < frameCount(tl); i++) times.push(i / fps);
1711
+ const last = times[times.length - 1];
1712
+ if (tl.duration - last > 1e-6) times.push(tl.duration);
1713
+ return times;
1714
+ }
1715
+ function sceneAt(tl, t) {
1716
+ let s = -1;
1717
+ (tl.markers ?? []).forEach((m, i) => {
1718
+ if (m.t <= t) s = i;
1719
+ });
1720
+ return s;
1721
+ }
1722
+ function transientElements(page) {
1723
+ const tl = page.timeline;
1724
+ if (!tl) return /* @__PURE__ */ new Set();
1725
+ const end = tl.poster ?? tl.duration;
1726
+ return new Set(tl.tracks.filter((tr) => !isVisible(tr, 0) && !isVisible(tr, end)).map((tr) => tr.element));
1727
+ }
1728
+
1729
+ // src/symbols.ts
1730
+ var MAX_DEPTH = 8;
1731
+ var MAX_ELEMENTS = 2e4;
1732
+ var EPSILON = 1e-9;
1733
+ function sceneTime(s, state) {
1734
+ const m = (s.timeline?.markers ?? []).find((x) => x.name.toLowerCase() === state);
1735
+ return m ? Math.min(Math.max(m.t, 0), clampTime(s.timeline?.duration ?? 0)) : null;
1736
+ }
1737
+ function buttonTime(s, state) {
1738
+ switch (state) {
1739
+ case "up":
1740
+ return sceneTime(s, "up") ?? 0;
1741
+ case "over":
1742
+ return sceneTime(s, "over") ?? buttonTime(s, "up");
1743
+ case "down":
1744
+ return sceneTime(s, "down") ?? buttonTime(s, "over");
1745
+ default:
1746
+ return sceneTime(s, "hit");
1747
+ }
1748
+ }
1749
+ function symbolTime(s, t, playback, offset2) {
1750
+ const d = clampTime(s.timeline?.duration ?? 0);
1751
+ if (s.button === true) return buttonTime(s, "up") ?? 0;
1752
+ const o = offset2 !== null && Number.isFinite(offset2) ? offset2 : 0;
1753
+ switch (playback ?? "loop") {
1754
+ case "still":
1755
+ return Math.min(s.timeline?.poster ?? d, d);
1756
+ case "click":
1757
+ return Math.min(Math.max(o, 0), d);
1758
+ case "once":
1759
+ return Math.min(Math.max(t + o, 0), d);
1760
+ default: {
1761
+ if (d <= 0) return 0;
1762
+ const x = t + o;
1763
+ let r = x % d;
1764
+ if (r < 0) r += d;
1765
+ if (x > EPSILON && (r < EPSILON || d - r < EPSILON)) return d;
1766
+ return r;
1767
+ }
1768
+ }
1769
+ }
1770
+ function symbolPage(s) {
1771
+ return { id: s.id, name: s.name, layers: s.layers, elements: s.elements, theme: null, timeline: s.timeline, symbols: null };
1772
+ }
1773
+ function hasInstances(page) {
1774
+ return page.elements.some((e) => e.kind === "symbol");
1775
+ }
1776
+ function expandingSymbols(page, t, posing = true, times = /* @__PURE__ */ new Map()) {
1777
+ if (!hasInstances(page)) return page;
1778
+ const budget = { left: MAX_ELEMENTS };
1779
+ return { ...page, elements: expand(page.elements, page, t, symbolTable(page), [], posing, budget, times) };
1780
+ }
1781
+ function symbolTable(page) {
1782
+ const table = /* @__PURE__ */ new Map();
1783
+ for (const s of page.symbols ?? []) if (!table.has(s.id)) table.set(s.id, s);
1784
+ return table;
1785
+ }
1786
+ function expand(elements, page, t, table, stack, posing, budget, times = /* @__PURE__ */ new Map()) {
1787
+ const instances = new Set(elements.filter((e) => e.kind === "symbol").map((e) => e.id));
1788
+ if (instances.size === 0) {
1789
+ budget.left -= elements.length;
1790
+ return elements;
1791
+ }
1792
+ const index = indexOf(page);
1793
+ const out = [];
1794
+ for (const e of elements) {
1795
+ if (budget.left <= 0) break;
1796
+ const c = e.connector;
1797
+ if (e.kind === "connector" && c && [c.start.elementID, c.end.elementID].some((id2) => id2 !== null && instances.has(id2))) {
1798
+ const [a, b] = endpoints(e, index);
1799
+ const pinned = cloneElement(e);
1800
+ if (c.start.elementID !== null && instances.has(c.start.elementID)) pinned.connector.start = { elementID: null, port: c.start.port, x: a.x, y: a.y };
1801
+ if (c.end.elementID !== null && instances.has(c.end.elementID)) pinned.connector.end = { elementID: null, port: c.end.port, x: b.x, y: b.y };
1802
+ out.push(pinned);
1803
+ budget.left -= 1;
1804
+ continue;
1805
+ }
1806
+ if (e.kind !== "symbol") {
1807
+ out.push(e);
1808
+ budget.left -= 1;
1809
+ continue;
1810
+ }
1811
+ budget.left -= 1;
1812
+ const id = e.symbol;
1813
+ const symbol = id === null ? void 0 : table.get(id);
1814
+ if (id === null || !symbol || stack.includes(id) || stack.length >= MAX_DEPTH) continue;
1815
+ const override = stack.length === 0 ? times.get(e.id) : void 0;
1816
+ const u = override !== void 0 ? Math.min(Math.max(override, 0), clampTime(symbol.timeline?.duration ?? 0)) : symbolTime(symbol, t, e.playback, e.offset);
1817
+ const inner = posing ? posed(symbolPage(symbol), u) : symbolPage(symbol);
1818
+ const visible = new Set(inner.layers.filter((l) => l.visible).map((l) => l.id));
1819
+ const drawn = {
1820
+ ...inner,
1821
+ elements: orderedElements(inner).filter((x) => visible.has(x.layerID)),
1822
+ layers: [{ id: e.layerID, name: "", visible: true, locked: false }]
1823
+ };
1824
+ for (const x of expand(drawn.elements, drawn, u, table, [...stack, id], posing, budget)) out.push(placed(x, e, symbol));
1825
+ }
1826
+ return out;
1827
+ }
1828
+ function placed(inner, instance, symbol) {
1829
+ const f = { x: instance.x, y: instance.y, width: instance.width, height: instance.height };
1830
+ const sx = f.width / symbol.width, sy = f.height / symbol.height;
1831
+ const s = Math.sqrt(Math.abs(sx * sy));
1832
+ const center2 = { x: f.x + f.width / 2, y: f.y + f.height / 2 };
1833
+ const a = angle(instance);
1834
+ const point = (p) => rotate({ x: f.x + p.x * sx, y: f.y + p.y * sy }, center2, a);
1835
+ const e = cloneElement(inner);
1836
+ e.id = derivedID(instance.id, inner.id);
1837
+ e.layerID = instance.layerID;
1838
+ e.groupID = null;
1839
+ e.link = inner.link === null ? null : derivedID(instance.id, inner.link);
1840
+ e.seed = boiled(inner.seed, instance.seed);
1841
+ e.click = null;
1842
+ e.hover = null;
1843
+ const scaled = { x: f.x + inner.x * sx, y: f.y + inner.y * sy, width: inner.width * sx, height: inner.height * sy };
1844
+ const moved = rotate({ x: scaled.x + scaled.width / 2, y: scaled.y + scaled.height / 2 }, center2, a);
1845
+ e.x = moved.x - scaled.width / 2;
1846
+ e.y = moved.y - scaled.height / 2;
1847
+ e.width = scaled.width;
1848
+ e.height = scaled.height;
1849
+ if (a !== 0 || inner.rotation !== null) e.rotation = angle(inner) + a;
1850
+ e.style.strokeWidth = inner.style.strokeWidth * s;
1851
+ e.style.fontSize = inner.style.fontSize * s;
1852
+ if (inner.style.gridSize !== null) e.style.gridSize = inner.style.gridSize * s;
1853
+ if (instance.style.opacity !== null) e.style.opacity = (inner.style.opacity ?? 1) * instance.style.opacity;
1854
+ if (inner.connector) {
1855
+ const end = (p) => {
1856
+ if (p.elementID !== null) return { elementID: derivedID(instance.id, p.elementID), port: p.port, x: p.x, y: p.y };
1857
+ const q = point(p);
1858
+ return { elementID: null, port: p.port, x: q.x, y: q.y };
1859
+ };
1860
+ e.connector = { ...inner.connector, start: end(inner.connector.start), end: end(inner.connector.end) };
1861
+ }
1862
+ return e;
1863
+ }
1864
+ var MASK = (1n << 64n) - 1n;
1865
+ function mix2(x) {
1866
+ let z = x + 0x9e3779b97f4a7c15n & MASK;
1867
+ z = (z ^ z >> 30n) * 0xbf58476d1ce4e5b9n & MASK;
1868
+ z = (z ^ z >> 27n) * 0x94d049bb133111ebn & MASK;
1869
+ return z ^ z >> 31n;
1870
+ }
1871
+ function halves(id) {
1872
+ const hex = id.replace(/-/g, "").padStart(32, "0").slice(-32);
1873
+ return [BigInt(`0x${hex.slice(0, 16)}`), BigInt(`0x${hex.slice(16)}`)];
1874
+ }
1875
+ function derivedID(instance, inner) {
1876
+ const [a, b] = halves(instance), [c, d] = halves(inner);
1877
+ const hi = mix2(mix2(mix2(mix2(a) ^ b) ^ c) ^ d);
1878
+ const lo = mix2(hi ^ 0x6a09e667f3bcc909n);
1879
+ const h = hi.toString(16).padStart(16, "0") + lo.toString(16).padStart(16, "0");
1880
+ return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`.toUpperCase();
1881
+ }
1882
+
1883
+ // src/clicks.ts
1884
+ function sceneNamed(name, markers) {
1885
+ const exact = markers.findIndex((m) => m.name === name);
1886
+ return exact >= 0 ? exact : markers.findIndex((m) => m.name.toLowerCase() === name.toLowerCase());
1887
+ }
1888
+ function clickTargets(page, file) {
1889
+ const markers = page.timeline?.markers ?? [];
1890
+ const out = /* @__PURE__ */ new Map();
1891
+ let pageOf = null;
1892
+ for (const e of page.elements) {
1893
+ const c = e.click;
1894
+ if (c?.scene) {
1895
+ const i = sceneNamed(c.scene, markers);
1896
+ if (i >= 0) {
1897
+ out.set(e.id, { scene: i });
1898
+ continue;
1899
+ }
1900
+ }
1901
+ const url = c?.url ? allowedURL(c.url) : null;
1902
+ if (url) {
1903
+ out.set(e.id, { url });
1904
+ continue;
1905
+ }
1906
+ if (e.link && file && file.pages.length > 1) {
1907
+ if (!pageOf) {
1908
+ pageOf = /* @__PURE__ */ new Map();
1909
+ file.pages.forEach((p, i) => {
1910
+ for (const x of p.elements) if (!pageOf.has(x.id)) pageOf.set(x.id, i);
1911
+ });
1912
+ }
1913
+ const target = pageOf.get(e.link);
1914
+ if (target !== void 0 && file.pages[target] !== page) out.set(e.id, { page: target });
1915
+ }
1916
+ }
1917
+ return out;
1918
+ }
1919
+ function hoveredElement(e) {
1920
+ const h = e.hover;
1921
+ if (!h) return e;
1922
+ const style = { ...e.style };
1923
+ if (h.stroke) style.stroke = h.stroke;
1924
+ if (h.fill) style.fill = h.fill;
1925
+ if (h.opacity !== null && Number.isFinite(h.opacity)) {
1926
+ const o = Math.min(Math.max(h.opacity, 0), 1);
1927
+ style.opacity = o >= 1 ? null : o;
1928
+ }
1929
+ return { ...e, style };
1930
+ }
1931
+ function hovered(page, ids) {
1932
+ return { ...page, elements: page.elements.map((e) => ids.has(e.id) ? hoveredElement(e) : e) };
1933
+ }
1934
+ function hasHover(e) {
1935
+ return !!e.hover && (e.hover.stroke !== null || e.hover.fill !== null || e.hover.opacity !== null);
1936
+ }
1937
+ function buttonLabel(e, symbolName) {
1938
+ const line2 = (e.text ?? "").split("\n").find((l) => l.length > 0);
1939
+ if (line2) return line2;
1940
+ if (e.anchor) return e.anchor;
1941
+ if (symbolName) return symbolName;
1942
+ return e.kind;
1943
+ }
1944
+ function partsOf(page, instances) {
1945
+ const out = /* @__PURE__ */ new Map();
1946
+ for (const e of page.elements) {
1947
+ if (!instances.has(e.id) || e.kind !== "symbol") continue;
1948
+ const alone = { ...page, layers: [{ id: e.layerID, name: "", visible: true, locked: false }], elements: [e] };
1949
+ for (const part of expandingSymbols(alone, 0, false).elements) out.set(part.id, e.id);
1950
+ }
1951
+ return out;
1952
+ }
1953
+ var CLICK_STYLE = `
1954
+ .dottie-a { cursor: pointer; outline: none; }
1955
+ .dottie-a .dottie-h { display: none; }
1956
+ .dottie-a:hover .dottie-h, .dottie-a.dottie-hover .dottie-h, .dottie-a:focus-visible .dottie-h { display: inline; }
1957
+ .dottie-a:hover .dottie-n, .dottie-a.dottie-hover .dottie-n, .dottie-a:focus-visible .dottie-n { display: none; }
1958
+ .dottie-a:focus-visible { filter: drop-shadow(0 0 3px var(--dottie-focus, #3b82f6)); }
1959
+ .dottie-a:not(.dottie-b):active, .dottie-a.dottie-press:not(.dottie-b) { transform: translate(1px, 1px); }
1960
+ .dottie-a .dottie-d { display: none; }
1961
+ .dottie-b:active .dottie-n, .dottie-b:active .dottie-h, .dottie-b.dottie-press .dottie-n, .dottie-b.dottie-press .dottie-h { display: none; }
1962
+ .dottie-b:active .dottie-d, .dottie-b.dottie-press .dottie-d { display: inline; }
1963
+ `;
1964
+
1965
+ // src/metrics.ts
1966
+ var ascent = 0.951995849609375;
1967
+ var descent = 0.2129974365234375;
1968
+ var leading = 0.0279998779296875;
1969
+ var boldAscent = 0.975006103515625;
1970
+ var boldDescent = 0.2169952392578125;
1367
1971
  var boldLeading = 0.0290069580078125;
1368
1972
  var italicAscent = 0.957000732421875;
1369
1973
  var italicDescent = 0.2129974365234375;
@@ -1883,476 +2487,177 @@ function measureRun(r) {
1883
2487
  return w;
1884
2488
  }
1885
2489
  var TAB_INTERVAL = 28;
1886
- function layoutLines(paras, width, height, fontSize) {
1887
- const lines = [];
1888
- let baseline = 0;
1889
- let prev = null;
1890
- paras.forEach((p, pi) => {
1891
- const runs = pi < paras.length - 1 ? [...p.runs, { text: "\n", size: fontSize, bold: false, italic: false, mono: false, strike: false, underline: false, alpha: 1 }] : p.runs;
1892
- const chars = [];
1893
- runs.forEach((r, ri) => {
1894
- for (const ch of r.text) chars.push({ ch, run: ri });
1895
- });
1896
- let k = 0;
1897
- let firstLine = true;
1898
- while (k < chars.length || firstLine && chars.length === 0) {
1899
- const start = firstLine ? p.firstIndent : p.indent;
1900
- const maxX2 = width;
1901
- const glyphs = [];
1902
- let x = start, lastSpace = -1, end = k;
1903
- for (; end < chars.length; end++) {
1904
- const { ch, run } = chars[end];
1905
- const r = runs[run];
1906
- if (ch === SEP || ch === "\n") {
1907
- glyphs.push({ ch, run, x, w: 0 });
1908
- end++;
1909
- break;
1910
- }
1911
- if (ch === " ") {
1912
- const stop = p.tabs.find((t) => t.x > x + 0.01);
1913
- let tx;
1914
- if (stop) {
1915
- if (stop.align === "left") tx = stop.x;
1916
- else {
1917
- let seg = 0, prevCh = "";
1918
- for (let q = end + 1; q < chars.length && chars[q].ch !== " " && chars[q].ch !== "\n" && chars[q].ch !== SEP; q++) {
1919
- const rr = runs[chars[q].run];
1920
- seg += charWidth(chars[q].ch, rr) + (prevCh ? kern(prevCh, chars[q].ch, rr) : 0);
1921
- prevCh = chars[q].ch;
1922
- }
1923
- tx = Math.max(x, stop.align === "right" ? stop.x - seg : stop.x - seg / 2);
1924
- }
1925
- } else tx = (Math.floor(x / TAB_INTERVAL) + 1) * TAB_INTERVAL;
1926
- glyphs.push({ ch, run, x, w: tx - x });
1927
- x = tx;
1928
- continue;
1929
- }
1930
- const prevGlyph = glyphs[glyphs.length - 1];
1931
- const kx = prevGlyph && prevGlyph.run === run && prevGlyph.ch !== " " ? kern(prevGlyph.ch, ch, r) : 0;
1932
- if (prevGlyph) {
1933
- prevGlyph.w += kx;
1934
- x += kx;
1935
- }
1936
- const w = charWidth(ch, r);
1937
- if (ch !== " " && x + w > maxX2 + 0.01 && glyphs.some((g) => g.ch !== " ")) {
1938
- if (lastSpace >= 0) {
1939
- glyphs.length = lastSpace + 1;
1940
- end = k + lastSpace + 1;
1941
- }
1942
- if (prevGlyph) prevGlyph.w -= kx;
1943
- break;
1944
- }
1945
- glyphs.push({ ch, run, x, w });
1946
- x += w;
1947
- if (breaksAfter(ch, chars[end + 1]?.ch ?? "")) lastSpace = glyphs.length - 1;
1948
- }
1949
- if (glyphs.length === 0 && end === k && k < chars.length) {
1950
- const { ch, run } = chars[k];
1951
- glyphs.push({ ch, run, x: start, w: charWidth(ch, runs[run]) });
1952
- end = k + 1;
1953
- }
1954
- let asc = 0, desc = 0, lead = 0;
1955
- for (const g of glyphs) {
1956
- const [a, d, l] = metrics(g.ch, runs[g.run]);
1957
- asc = Math.max(asc, a);
1958
- desc = Math.max(desc, d);
1959
- lead = Math.max(lead, l);
1960
- }
1961
- if (glyphs.length === 0) [asc, desc, lead] = metrics(" ", runs[0] ?? { text: " ", size: fontSize, bold: false, italic: false, mono: false, strike: false, underline: false, alpha: 1 });
1962
- baseline = prev ? baseline + Math.round(prev.desc) + Math.round(prev.lead) + (firstLine ? p.spacing : 0) + Math.round(asc) : Math.round(asc);
1963
- if (baseline + desc > height + 0.5) return;
1964
- const line2 = { glyphs, asc, desc, lead, baseline, para: p, first: firstLine };
1965
- line2.glyphs.forEach((g) => {
1966
- g.r = runs[g.run];
1967
- });
1968
- lines.push(line2);
1969
- prev = line2;
1970
- firstLine = false;
1971
- k = end;
1972
- if (chars.length === 0) break;
1973
- }
1974
- });
1975
- return lines;
1976
- }
1977
- function noteMarkup(source, e) {
1978
- const rect2 = textRect({ x: e.x, y: e.y, width: e.width, height: e.height });
1979
- if (!(rect2.width > 1 && rect2.height > 1)) return "";
1980
- const b = new Builder(e.style.fontSize, rect2.width);
1981
- for (const block of parseBlocks(source)) b.block(block, { indent: 0, marker: null, quote: null, listDepth: 0 });
1982
- const lines = layoutLines(b.paras, rect2.width, rect2.height, e.style.fontSize);
1983
- const ink = color(e.style.stroke);
1984
- let s = "";
1985
- const marks = [];
1986
- for (const l of lines) {
1987
- const p = l.para;
1988
- if (p.quote !== null) marks.push([rect2.x + p.quote, rect2.y + l.baseline - l.asc, rect2.x + p.quote, rect2.y + l.baseline + l.desc]);
1989
- if (p.rule !== null) marks.push([rect2.x + p.rule, rect2.y + l.baseline - 3, rect2.x + rect2.width, rect2.y + l.baseline - 3]);
1990
- if (p.tableRule) {
1991
- const y = rect2.y + l.baseline + l.desc + 2;
1992
- marks.push([rect2.x + p.tableRule[0], y, Math.min(rect2.x + rect2.width, rect2.x + p.tableRule[0] + p.tableRule[1]), y]);
1993
- }
1994
- let spans = "";
1995
- let i = 0;
1996
- const gs = l.glyphs;
1997
- while (i < gs.length) {
1998
- const r = gs[i].r;
1999
- let j = i;
2000
- let text = "";
2001
- const font = fontOf(gs[i].ch);
2002
- while (j < gs.length && sameStyle(gs[j].r, r) && gs[j].ch !== " " && fontOf(gs[j].ch) === font) {
2003
- text += gs[j].ch;
2004
- j++;
2005
- }
2006
- const shown = text.replaceAll("\n", "").replaceAll(SEP, "");
2007
- if (shown.trim() !== "") {
2008
- let a = `font-size="${fmt(r.size)}"`;
2009
- if (r.mono) a += ` font-family="Menlo, Consolas, monospace"`;
2010
- if (r.bold) a += ` font-weight="bold"`;
2011
- if (r.italic) a += ` font-style="italic"`;
2012
- if (r.underline) a += ` text-decoration="underline"`;
2013
- if (r.alpha < 1) a += ` fill-opacity="${fmt(r.alpha * e.style.stroke.a)}"`;
2014
- spans += `<tspan x="${fmt(rect2.x + gs[i].x)}" ${a}>${escapeXML(shown)}</tspan>`;
2015
- }
2016
- if (r.strike && j > i) {
2017
- const y = l.baseline - metrics(gs[i].ch, r)[0] * 0.32;
2018
- marks.push([rect2.x + gs[i].x, rect2.y + y, rect2.x + gs[j - 1].x + gs[j - 1].w, rect2.y + y]);
2019
- }
2020
- if (j === i) j++;
2021
- i = j;
2022
- }
2023
- if (spans) s += `<text y="${fmt(rect2.y + l.baseline)}" font-family="Helvetica Neue, Helvetica, Arial, sans-serif" fill="${ink}" xml:space="preserve">${spans}</text>
2024
- `;
2025
- }
2026
- const width = fmt(Math.max(1, e.style.fontSize / 14));
2027
- for (const [x1, y1, x2, y2] of marks) {
2028
- s += `<line x1="${fmt(x1)}" y1="${fmt(y1)}" x2="${fmt(x2)}" y2="${fmt(y2)}" stroke="${ink}" stroke-width="${width}"/>
2029
- `;
2030
- }
2031
- return s;
2032
- }
2033
- function fontOf(ch) {
2034
- return fallbackMetrics[ch] ? `fb:${ch}` : "";
2035
- }
2036
- function sameStyle(a, b) {
2037
- return a === b || a.size === b.size && a.bold === b.bold && a.italic === b.italic && a.mono === b.mono && a.strike === b.strike && a.underline === b.underline && a.alpha === b.alpha;
2038
- }
2039
-
2040
- // src/theme.ts
2041
- var rgb = (r, g, b) => ({ r, g, b, a: 1 });
2042
- var themes = [
2043
- { id: "paper", name: "Paper", background: rgb(0.984, 0.98, 0.965), ink: rgb(0.12, 0.12, 0.13) },
2044
- { id: "white", name: "White", background: rgb(1, 1, 1), ink: rgb(0.12, 0.12, 0.13) },
2045
- { id: "sepia", name: "Sepia", background: rgb(0.953, 0.914, 0.824), ink: rgb(0.23, 0.18, 0.13) },
2046
- { id: "blueprint", name: "Blueprint", background: rgb(0.106, 0.227, 0.361), ink: rgb(0.91, 0.945, 0.984) },
2047
- { id: "chalkboard", name: "Chalkboard", background: rgb(0.169, 0.227, 0.2), ink: rgb(0.945, 0.945, 0.918) },
2048
- { id: "midnight", name: "Midnight", background: rgb(0.082, 0.086, 0.102), ink: rgb(0.925, 0.925, 0.94) }
2049
- ];
2050
- function themeNamed(id) {
2051
- return themes.find((t) => t.id === id) ?? null;
2052
- }
2053
-
2054
- // src/timeline.ts
2055
- function applyEase(ease, u) {
2056
- switch (ease) {
2057
- case "linear":
2058
- return u;
2059
- case "easeIn":
2060
- return u * u * u;
2061
- case "easeOut":
2062
- return 1 - Math.pow(1 - u, 3);
2063
- case "easeInOut":
2064
- return u < 0.5 ? 4 * u * u * u : 1 - Math.pow(-2 * u + 2, 3) / 2;
2065
- case "hold":
2066
- return 0;
2067
- case "spring":
2068
- return spring(u);
2069
- case "bounce":
2070
- return bounce(u);
2071
- case "elastic":
2072
- return elastic(u);
2073
- }
2074
- }
2075
- function spring(u) {
2076
- if (u <= 0) return 0;
2077
- if (u >= 1) return 1;
2078
- const s = (u2) => 1 - Math.exp(-6 * u2) * (Math.cos(3 * Math.PI * u2) + 6 / (3 * Math.PI) * Math.sin(3 * Math.PI * u2));
2079
- return s(u) + u * (1 - s(1));
2080
- }
2081
- function bounce(u) {
2082
- if (u <= 0) return 0;
2083
- if (u >= 1) return 1;
2084
- const n = 7.5625, d = 2.75;
2085
- if (u < 1 / d) return n * u * u;
2086
- if (u < 2 / d) {
2087
- const v2 = u - 1.5 / d;
2088
- return n * v2 * v2 + 0.75;
2089
- }
2090
- if (u < 2.5 / d) {
2091
- const v2 = u - 2.25 / d;
2092
- return n * v2 * v2 + 0.9375;
2093
- }
2094
- const v = u - 2.625 / d;
2095
- return n * v * v + 0.984375;
2096
- }
2097
- function elastic(u) {
2098
- if (u <= 0) return 0;
2099
- if (u >= 1) return 1;
2100
- return Math.pow(2, -10 * u) * Math.sin((u * 10 - 0.75) * (2 * Math.PI / 3)) + 1;
2101
- }
2102
- var rem = (a, b) => a % b;
2103
- function localTime(track, t) {
2104
- const reps = (track.repeats ?? []).filter(repeatIsValid).slice().sort((a, b) => a.start - b.start);
2105
- if (reps.length === 0) return t;
2106
- for (const r of reps) {
2107
- if (!(t >= r.end)) continue;
2108
- const length = r.end - r.start;
2109
- if (r.count === null) return r.start + rem(t - r.start, length);
2110
- const played = length * r.count;
2111
- if (t < r.start + played) return r.start + rem(t - r.start, length);
2112
- t -= played - length;
2113
- }
2114
- return t;
2115
- }
2116
- function value(track, t, get, base, lerp) {
2117
- t = localTime(track, t);
2118
- let before, after;
2119
- for (const k of track.keys) {
2120
- if (get(k) === void 0) continue;
2121
- if (k.t <= t) before = k;
2122
- else {
2123
- after = k;
2124
- break;
2125
- }
2126
- }
2127
- if (!before && !after) return base;
2128
- if (before && !after) return get(before);
2129
- if (!before && after) return get(after);
2130
- const b = before, a = after;
2131
- const u = a.t - b.t > 0 ? (t - b.t) / (a.t - b.t) : 1;
2132
- return lerp(get(b), get(a), applyEase(b.ease ?? "linear", u));
2133
- }
2134
- var lerpNum = (a, b, u) => a + (b - a) * u;
2135
- var mix01 = (a, b, u) => Math.min(Math.max(a + (b - a) * u, 0), 1);
2136
- var lerpColor = (a, b, u) => ({
2137
- r: mix01(a.r, b.r, u),
2138
- g: mix01(a.g, b.g, u),
2139
- b: mix01(a.b, b.b, u),
2140
- a: mix01(a.a, b.a, u)
2141
- });
2142
- function num2(track, t, p, base) {
2143
- return value(track, t, (k) => k[p], base, lerpNum);
2144
- }
2145
- function isChanging(track, t) {
2146
- t = localTime(track, t);
2147
- const changing = (get, eq) => {
2148
- let b;
2149
- for (const k of track.keys) if (k.t <= t && get(k) !== void 0) b = k;
2150
- const a = track.keys.find((k) => k.t > t && get(k) !== void 0);
2151
- if (!b || !a) return false;
2152
- return b.ease !== "hold" && !eq(get(b), get(a));
2153
- };
2154
- const n = (p) => changing((k) => k[p], (a, b) => a === b);
2155
- return n("x") || n("y") || n("width") || n("height") || n("rotation") || n("strokeWidth") || changing((k) => k.stroke, sameColor) || changing((k) => k.fill, sameColor) || n("reveal") || n("opacity");
2156
- }
2157
- function isVisible(track, t) {
2158
- t = localTime(track, t);
2159
- const keyed = track.keys.filter((k) => k.visible !== void 0);
2160
- if (keyed.length === 0) return true;
2161
- let last;
2162
- for (const k of keyed) if (k.t <= t) last = k;
2163
- return last ? last.visible : !keyed[0].visible;
2164
- }
2165
- function applyTrack(track, e, t) {
2166
- e = cloneElement(e);
2167
- if (track.keys.some((k) => k.reveal !== void 0)) {
2168
- const r = num2(track, t, "reveal", e.reveal ?? 1);
2169
- e.reveal = r >= 1 ? null : Math.max(0, r);
2170
- }
2171
- if (track.keys.some((k) => k.opacity !== void 0)) {
2172
- const o = Math.min(Math.max(num2(track, t, "opacity", e.style.opacity ?? 1), 0), 1);
2173
- e.style.opacity = o >= 1 ? null : o;
2174
- }
2175
- if (e.kind === "connector") return e;
2176
- e.x = num2(track, t, "x", e.x);
2177
- e.y = num2(track, t, "y", e.y);
2178
- e.width = Math.max(0, num2(track, t, "width", e.width));
2179
- e.height = Math.max(0, num2(track, t, "height", e.height));
2180
- if (track.keys.some((k) => k.rotation !== void 0)) e.rotation = num2(track, t, "rotation", angle(e));
2181
- e.style.strokeWidth = Math.max(0, num2(track, t, "strokeWidth", e.style.strokeWidth));
2182
- e.style.stroke = value(track, t, (k) => k.stroke, e.style.stroke, lerpColor);
2183
- e.style.fill = value(track, t, (k) => k.fill, e.style.fill, lerpColor);
2184
- return e;
2185
- }
2186
- function followSpan(track, t) {
2187
- t = localTime(track, t);
2188
- let f = null;
2189
- for (const s of track.follow ?? []) if (s.start <= t && (f === null || s.start > f.start)) f = s;
2190
- if (!f) return null;
2191
- const span = f;
2192
- if (track.keys.some((k) => k.t >= span.end && k.t <= t && (k.x !== void 0 || k.y !== void 0))) return null;
2193
- return f;
2194
- }
2195
- function tracing(e, page) {
2196
- if (e.kind === "connector") return new Polyline(routePoints(route(e, indexOf(page))));
2197
- const clean = cloneElement(e);
2198
- clean.style.roughness = 0;
2199
- const pts = outline(clean).contours()[0] ?? [];
2200
- return new Polyline(angle(e) === 0 ? pts : pts.map((p) => rotated(e, p)));
2201
- }
2202
- function following(track, e, t, page) {
2203
- const f = followSpan(track, t);
2204
- if (!f) return e;
2205
- t = localTime(track, t);
2206
- const pathElement = page.elements.find((x) => x.id === f.path);
2207
- if (!pathElement || pathElement.id === e.id) return e;
2208
- const line2 = tracing(pathElement, page);
2209
- const span = f.end - f.start;
2210
- let u = Math.min(Math.max(applyEase(f.ease ?? "easeInOut", span > 0 ? Math.min(1, (t - f.start) / span) : 1), 0), 1) * (f.laps ?? 1);
2211
- if (u > 1) u = rem(u, 1) === 0 ? 1 : rem(u, 1);
2212
- if (f.reverse === true) u = 1 - u;
2213
- const p = line2.point(u);
2214
- const moved = cloneElement(e);
2215
- moved.x = p.x - e.width / 2;
2216
- moved.y = p.y - e.height / 2;
2217
- if (f.orient === true) {
2218
- const step = f.reverse === true ? -0.01 : 0.01;
2219
- const a = line2.point(u - step / 2), b = line2.point(u + step / 2);
2220
- if (a.x !== b.x || a.y !== b.y) moved.rotation = Math.atan2(b.y - a.y, b.x - a.x);
2221
- }
2222
- return moved;
2223
- }
2224
- function marchOffset(tl, speed, period, t) {
2225
- if (!(period > 0)) return 0;
2226
- const d = clampTime(tl.duration);
2227
- if (d > 0) {
2228
- const laps = roundHalfAway(speed * d / period);
2229
- if (laps !== 0) speed = laps * period / d;
2230
- }
2231
- const r = rem(speed * t, period);
2232
- return r < 0 ? -r : r === 0 ? 0 : period - r;
2233
- }
2234
- function trackMap(tl) {
2235
- const m = /* @__PURE__ */ new Map();
2236
- for (const t of tl.tracks) if (!m.has(t.element)) m.set(t.element, t);
2237
- return m;
2238
- }
2239
- function resolved(page, t) {
2240
- const timeline = page.timeline;
2241
- if (!timeline) return page;
2242
- t = clampTime(t);
2243
- const tracks = trackMap(timeline);
2244
- const reseed = timeline.boil === null ? null : Math.trunc(Math.floor(t * clampFPS(timeline.fps) + 1e-9) / Math.max(1, timeline.boil)) % BOIL_DRAWINGS >>> 0;
2245
- const hidden = /* @__PURE__ */ new Set();
2246
- for (const tr of tracks.values()) if (!isVisible(tr, t)) hidden.add(tr.element);
2247
- let moving = null;
2248
- if (timeline.settle === true) {
2249
- moving = /* @__PURE__ */ new Set();
2250
- for (const tr of tracks.values()) if (isChanging(tr, t)) moving.add(tr.element);
2251
- }
2252
- const boils = (e) => {
2253
- const override = tracks.get(e.id)?.boil;
2254
- if (override !== void 0 && override !== null) return override;
2255
- if (!moving) return true;
2256
- if (e.connector) return [e.connector.start.elementID, e.connector.end.elementID].some((id) => id !== null && moving.has(id));
2257
- return moving.has(e.id);
2258
- };
2259
- let elements = page.elements.map((e) => {
2260
- const track = tracks.get(e.id);
2261
- let out = track ? applyTrack(track, e, t) : e;
2262
- if (reseed !== null && reseed > 0 && boils(e)) {
2263
- if (out === e) out = cloneElement(e);
2264
- out.seed = boiled(e.seed, reseed);
2265
- }
2266
- const pattern = track?.march != null ? dashPattern(out.style.dash, out.style.strokeWidth) : null;
2267
- if (track?.march != null && pattern) {
2268
- if (out === e) out = cloneElement(e);
2269
- out.style.dashOffset = marchOffset(timeline, track.march, pattern.reduce((a, b) => a + b, 0), t);
2270
- }
2271
- return out;
2272
- });
2273
- elements = elements.map((e, i) => hidden.has(e.id) ? page.elements[i] : e);
2274
- const posed = { ...page, elements, timeline: null };
2275
- elements = elements.map((e) => {
2276
- const track = tracks.get(e.id);
2277
- return track ? following(track, e, t, posed) : e;
2278
- });
2279
- const current = { ...page, elements, timeline: null };
2280
- const index = indexOf(current);
2281
- const gone = new Set(hidden);
2282
- elements = elements.map((e) => {
2283
- const c = e.connector;
2284
- if (!c || ![c.start.elementID, c.end.elementID].some((id) => id !== null && hidden.has(id))) return e;
2285
- if (!(tracks.get(e.id)?.keys.some((k) => k.visible !== void 0) ?? false)) {
2286
- gone.add(e.id);
2287
- return e;
2490
+ function layoutLines(paras, width, height, fontSize) {
2491
+ const lines = [];
2492
+ let baseline = 0;
2493
+ let prev = null;
2494
+ paras.forEach((p, pi) => {
2495
+ const runs = pi < paras.length - 1 ? [...p.runs, { text: "\n", size: fontSize, bold: false, italic: false, mono: false, strike: false, underline: false, alpha: 1 }] : p.runs;
2496
+ const chars = [];
2497
+ runs.forEach((r, ri) => {
2498
+ for (const ch of r.text) chars.push({ ch, run: ri });
2499
+ });
2500
+ let k = 0;
2501
+ let firstLine = true;
2502
+ while (k < chars.length || firstLine && chars.length === 0) {
2503
+ const start = firstLine ? p.firstIndent : p.indent;
2504
+ const maxX2 = width;
2505
+ const glyphs = [];
2506
+ let x = start, lastSpace = -1, end = k;
2507
+ for (; end < chars.length; end++) {
2508
+ const { ch, run } = chars[end];
2509
+ const r = runs[run];
2510
+ if (ch === SEP || ch === "\n") {
2511
+ glyphs.push({ ch, run, x, w: 0 });
2512
+ end++;
2513
+ break;
2514
+ }
2515
+ if (ch === " ") {
2516
+ const stop = p.tabs.find((t) => t.x > x + 0.01);
2517
+ let tx;
2518
+ if (stop) {
2519
+ if (stop.align === "left") tx = stop.x;
2520
+ else {
2521
+ let seg = 0, prevCh = "";
2522
+ for (let q = end + 1; q < chars.length && chars[q].ch !== " " && chars[q].ch !== "\n" && chars[q].ch !== SEP; q++) {
2523
+ const rr = runs[chars[q].run];
2524
+ seg += charWidth(chars[q].ch, rr) + (prevCh ? kern(prevCh, chars[q].ch, rr) : 0);
2525
+ prevCh = chars[q].ch;
2526
+ }
2527
+ tx = Math.max(x, stop.align === "right" ? stop.x - seg : stop.x - seg / 2);
2528
+ }
2529
+ } else tx = (Math.floor(x / TAB_INTERVAL) + 1) * TAB_INTERVAL;
2530
+ glyphs.push({ ch, run, x, w: tx - x });
2531
+ x = tx;
2532
+ continue;
2533
+ }
2534
+ const prevGlyph = glyphs[glyphs.length - 1];
2535
+ const kx = prevGlyph && prevGlyph.run === run && prevGlyph.ch !== " " ? kern(prevGlyph.ch, ch, r) : 0;
2536
+ if (prevGlyph) {
2537
+ prevGlyph.w += kx;
2538
+ x += kx;
2539
+ }
2540
+ const w = charWidth(ch, r);
2541
+ if (ch !== " " && x + w > maxX2 + 0.01 && glyphs.some((g) => g.ch !== " ")) {
2542
+ if (lastSpace >= 0) {
2543
+ glyphs.length = lastSpace + 1;
2544
+ end = k + lastSpace + 1;
2545
+ }
2546
+ if (prevGlyph) prevGlyph.w -= kx;
2547
+ break;
2548
+ }
2549
+ glyphs.push({ ch, run, x, w });
2550
+ x += w;
2551
+ if (breaksAfter(ch, chars[end + 1]?.ch ?? "")) lastSpace = glyphs.length - 1;
2552
+ }
2553
+ if (glyphs.length === 0 && end === k && k < chars.length) {
2554
+ const { ch, run } = chars[k];
2555
+ glyphs.push({ ch, run, x: start, w: charWidth(ch, runs[run]) });
2556
+ end = k + 1;
2557
+ }
2558
+ let asc = 0, desc = 0, lead = 0;
2559
+ for (const g of glyphs) {
2560
+ const [a, d, l] = metrics(g.ch, runs[g.run]);
2561
+ asc = Math.max(asc, a);
2562
+ desc = Math.max(desc, d);
2563
+ lead = Math.max(lead, l);
2564
+ }
2565
+ if (glyphs.length === 0) [asc, desc, lead] = metrics(" ", runs[0] ?? { text: " ", size: fontSize, bold: false, italic: false, mono: false, strike: false, underline: false, alpha: 1 });
2566
+ baseline = prev ? baseline + Math.round(prev.desc) + Math.round(prev.lead) + (firstLine ? p.spacing : 0) + Math.round(asc) : Math.round(asc);
2567
+ if (baseline + desc > height + 0.5) return;
2568
+ const line2 = { glyphs, asc, desc, lead, baseline, para: p, first: firstLine };
2569
+ line2.glyphs.forEach((g) => {
2570
+ g.r = runs[g.run];
2571
+ });
2572
+ lines.push(line2);
2573
+ prev = line2;
2574
+ firstLine = false;
2575
+ k = end;
2576
+ if (chars.length === 0) break;
2288
2577
  }
2289
- const [a, b] = endpoints(e, index);
2290
- const pinned = cloneElement(e);
2291
- pinned.connector.start = { elementID: null, port: c.start.port, x: a.x, y: a.y };
2292
- pinned.connector.end = { elementID: null, port: c.end.port, x: b.x, y: b.y };
2293
- return pinned;
2294
2578
  });
2295
- return { ...page, elements: elements.filter((e) => !gone.has(e.id)), timeline: null };
2296
- }
2297
- function posterTime(page) {
2298
- return page.timeline ? page.timeline.poster ?? page.timeline.duration : 0;
2299
- }
2300
- function still(page) {
2301
- return page.timeline ? resolved(page, posterTime(page)) : page;
2302
- }
2303
- function cameraRect(tl, t) {
2304
- const keys = tl.camera ?? [];
2305
- if (keys.length === 0) return null;
2306
- let before;
2307
- for (const k of keys) if (k.t <= t) before = k;
2308
- const after = keys.find((k) => k.t > t);
2309
- if (!before) return rectOf(keys[0]);
2310
- if (!after) return rectOf(before);
2311
- const u = applyEase(before.ease ?? "easeInOut", (t - before.t) / Math.max(after.t - before.t, 1e-9));
2312
- const mix2 = (p, q) => p + (q - p) * u;
2313
- return { x: mix2(before.x, after.x), y: mix2(before.y, after.y), width: Math.max(1, mix2(before.width, after.width)), height: Math.max(1, mix2(before.height, after.height)) };
2314
- }
2315
- var rectOf = (k) => ({ x: k.x, y: k.y, width: k.width, height: k.height });
2316
- function cameraView(tl, t) {
2317
- const first = tl.camera?.[0];
2318
- const r = cameraRect(tl, t);
2319
- if (!first || !r || !(r.width > 0 && r.height > 0)) return null;
2320
- return fit(r, first.width / first.height);
2321
- }
2322
- function fit(r, aspect) {
2323
- if (!(r.width > 0 && r.height > 0 && aspect > 0)) return r;
2324
- let w = r.width, h = r.height;
2325
- if (r.width / r.height < aspect) w = r.height * aspect;
2326
- else h = r.width / aspect;
2327
- return { x: r.x + r.width / 2 - w / 2, y: r.y + r.height / 2 - h / 2, width: w, height: h };
2579
+ return lines;
2328
2580
  }
2329
- function frameCount(tl) {
2330
- return Math.max(1, Math.ceil(clampTime(tl.duration) * clampFPS(tl.fps)));
2581
+ function noteMarkup(source, e) {
2582
+ const rect2 = textRect({ x: e.x, y: e.y, width: e.width, height: e.height });
2583
+ if (!(rect2.width > 1 && rect2.height > 1)) return "";
2584
+ const b = new Builder(e.style.fontSize, rect2.width);
2585
+ for (const block of parseBlocks(source)) b.block(block, { indent: 0, marker: null, quote: null, listDepth: 0 });
2586
+ const lines = layoutLines(b.paras, rect2.width, rect2.height, e.style.fontSize);
2587
+ const ink = color(e.style.stroke);
2588
+ let s = "";
2589
+ const marks = [];
2590
+ for (const l of lines) {
2591
+ const p = l.para;
2592
+ if (p.quote !== null) marks.push([rect2.x + p.quote, rect2.y + l.baseline - l.asc, rect2.x + p.quote, rect2.y + l.baseline + l.desc]);
2593
+ if (p.rule !== null) marks.push([rect2.x + p.rule, rect2.y + l.baseline - 3, rect2.x + rect2.width, rect2.y + l.baseline - 3]);
2594
+ if (p.tableRule) {
2595
+ const y = rect2.y + l.baseline + l.desc + 2;
2596
+ marks.push([rect2.x + p.tableRule[0], y, Math.min(rect2.x + rect2.width, rect2.x + p.tableRule[0] + p.tableRule[1]), y]);
2597
+ }
2598
+ let spans = "";
2599
+ let i = 0;
2600
+ const gs = l.glyphs;
2601
+ while (i < gs.length) {
2602
+ const r = gs[i].r;
2603
+ let j = i;
2604
+ let text = "";
2605
+ const font = fontOf(gs[i].ch);
2606
+ while (j < gs.length && sameStyle(gs[j].r, r) && gs[j].ch !== " " && fontOf(gs[j].ch) === font) {
2607
+ text += gs[j].ch;
2608
+ j++;
2609
+ }
2610
+ const shown = text.replaceAll("\n", "").replaceAll(SEP, "");
2611
+ if (shown.trim() !== "") {
2612
+ let a = `font-size="${fmt(r.size)}"`;
2613
+ if (r.mono) a += ` font-family="Menlo, Consolas, monospace"`;
2614
+ if (r.bold) a += ` font-weight="bold"`;
2615
+ if (r.italic) a += ` font-style="italic"`;
2616
+ if (r.underline) a += ` text-decoration="underline"`;
2617
+ if (r.alpha < 1) a += ` fill-opacity="${fmt(r.alpha * e.style.stroke.a)}"`;
2618
+ spans += `<tspan x="${fmt(rect2.x + gs[i].x)}" ${a}>${escapeXML(shown)}</tspan>`;
2619
+ }
2620
+ if (r.strike && j > i) {
2621
+ const y = l.baseline - metrics(gs[i].ch, r)[0] * 0.32;
2622
+ marks.push([rect2.x + gs[i].x, rect2.y + y, rect2.x + gs[j - 1].x + gs[j - 1].w, rect2.y + y]);
2623
+ }
2624
+ if (j === i) j++;
2625
+ i = j;
2626
+ }
2627
+ if (spans) s += `<text y="${fmt(rect2.y + l.baseline)}" font-family="Helvetica Neue, Helvetica, Arial, sans-serif" fill="${ink}" xml:space="preserve">${spans}</text>
2628
+ `;
2629
+ }
2630
+ const width = fmt(Math.max(1, e.style.fontSize / 14));
2631
+ for (const [x1, y1, x2, y2] of marks) {
2632
+ s += `<line x1="${fmt(x1)}" y1="${fmt(y1)}" x2="${fmt(x2)}" y2="${fmt(y2)}" stroke="${ink}" stroke-width="${width}"/>
2633
+ `;
2634
+ }
2635
+ return s;
2331
2636
  }
2332
- function frameTimes(tl) {
2333
- const fps = clampFPS(tl.fps);
2334
- const times = [];
2335
- for (let i = 0; i < frameCount(tl); i++) times.push(i / fps);
2336
- const last = times[times.length - 1];
2337
- if (tl.duration - last > 1e-6) times.push(tl.duration);
2338
- return times;
2637
+ function fontOf(ch) {
2638
+ return fallbackMetrics[ch] ? `fb:${ch}` : "";
2339
2639
  }
2340
- function sceneAt(tl, t) {
2341
- let s = -1;
2342
- (tl.markers ?? []).forEach((m, i) => {
2343
- if (m.t <= t) s = i;
2344
- });
2345
- return s;
2640
+ function sameStyle(a, b) {
2641
+ return a === b || a.size === b.size && a.bold === b.bold && a.italic === b.italic && a.mono === b.mono && a.strike === b.strike && a.underline === b.underline && a.alpha === b.alpha;
2346
2642
  }
2347
- function transientElements(page) {
2348
- const tl = page.timeline;
2349
- if (!tl) return /* @__PURE__ */ new Set();
2350
- const end = tl.poster ?? tl.duration;
2351
- return new Set(tl.tracks.filter((tr) => !isVisible(tr, 0) && !isVisible(tr, end)).map((tr) => tr.element));
2643
+
2644
+ // src/theme.ts
2645
+ var rgb = (r, g, b) => ({ r, g, b, a: 1 });
2646
+ var themes = [
2647
+ { id: "paper", name: "Paper", background: rgb(0.984, 0.98, 0.965), ink: rgb(0.12, 0.12, 0.13) },
2648
+ { id: "white", name: "White", background: rgb(1, 1, 1), ink: rgb(0.12, 0.12, 0.13) },
2649
+ { id: "sepia", name: "Sepia", background: rgb(0.953, 0.914, 0.824), ink: rgb(0.23, 0.18, 0.13) },
2650
+ { id: "blueprint", name: "Blueprint", background: rgb(0.106, 0.227, 0.361), ink: rgb(0.91, 0.945, 0.984) },
2651
+ { id: "chalkboard", name: "Chalkboard", background: rgb(0.169, 0.227, 0.2), ink: rgb(0.945, 0.945, 0.918) },
2652
+ { id: "midnight", name: "Midnight", background: rgb(0.082, 0.086, 0.102), ink: rgb(0.925, 0.925, 0.94) }
2653
+ ];
2654
+ function themeNamed(id) {
2655
+ return themes.find((t) => t.id === id) ?? null;
2352
2656
  }
2353
2657
 
2354
2658
  // src/svg.ts
2355
2659
  function bounds(page, padding = 24) {
2660
+ if (hasInstances(page)) return bounds(expandingSymbols(page, 0), padding);
2356
2661
  const index = indexOf(page);
2357
2662
  const box = (e) => {
2358
2663
  if (e.kind === "connector") return connectorBounds(e, index);
@@ -2394,6 +2699,7 @@ function svgOpen(b, background, paint) {
2394
2699
  return out;
2395
2700
  }
2396
2701
  function svgParts(page, background, fixed) {
2702
+ if (hasInstances(page)) return svgParts(expandingSymbols(page, 0), background, fixed);
2397
2703
  const theme = themeNamed(page.theme);
2398
2704
  const bg = background ? theme?.background ?? background : null;
2399
2705
  const ink = theme?.ink ?? black;
@@ -2574,8 +2880,9 @@ input[type=range] { flex: 1; min-width: 60px; accent-color: currentColor; }
2574
2880
  output { font-variant-numeric: tabular-nums; min-width: 4.5em; text-align: right; }
2575
2881
  .scene { min-width: 0; flex: 0 1 auto; text-align: center; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
2576
2882
  .error { padding: 16px; font-family: ui-monospace, Menlo, monospace; white-space: pre-wrap; }
2577
- `;
2883
+ ${CLICK_STYLE}`;
2578
2884
  var Playback = class {
2885
+ /** `file` is the drawing `page` belongs to, so links to its other pages are clickable. */
2579
2886
  constructor(page, into, options = {}) {
2580
2887
  __publicField(this, "page");
2581
2888
  __publicField(this, "plan");
@@ -2583,6 +2890,22 @@ var Playback = class {
2583
2890
  __publicField(this, "sceneNames");
2584
2891
  __publicField(this, "background");
2585
2892
  __publicField(this, "ink");
2893
+ /** What clicking each clickable element does, by element id. */
2894
+ __publicField(this, "clicks");
2895
+ /** Copies of symbols that play through once when clicked (playback "click"), by id, with their clip's length. */
2896
+ __publicField(this, "replays", /* @__PURE__ */ new Map());
2897
+ /** When each replaying copy was clicked (performance.now() milliseconds). */
2898
+ __publicField(this, "replayStarts", /* @__PURE__ */ new Map());
2899
+ __publicField(this, "hoverIDs");
2900
+ __publicField(this, "labels", /* @__PURE__ */ new Map());
2901
+ /** Each drawn part of a clickable copy, to that copy's id; and each copy's parts in drawing order. */
2902
+ __publicField(this, "owner", /* @__PURE__ */ new Map());
2903
+ __publicField(this, "partsOfOwner", /* @__PURE__ */ new Map());
2904
+ /** The moments of the button copies' over, down and hit states. */
2905
+ __publicField(this, "overTimes", /* @__PURE__ */ new Map());
2906
+ __publicField(this, "downTimes", /* @__PURE__ */ new Map());
2907
+ __publicField(this, "hitTimes", /* @__PURE__ */ new Map());
2908
+ __publicField(this, "last", { t: 0, i: 0 });
2586
2909
  __publicField(this, "svg");
2587
2910
  __publicField(this, "groups", /* @__PURE__ */ new Map());
2588
2911
  __publicField(this, "order", []);
@@ -2590,6 +2913,30 @@ var Playback = class {
2590
2913
  __publicField(this, "onFrame", null);
2591
2914
  this.page = options.clean ? withRoughness(page, 0) : page;
2592
2915
  this.plan = plan(this.page);
2916
+ this.clicks = clickTargets(page, options.file);
2917
+ const table = symbolTable(this.page);
2918
+ for (const e of this.page.elements) {
2919
+ const s = e.kind === "symbol" && e.symbol !== null ? table.get(e.symbol) : void 0;
2920
+ if (!s) continue;
2921
+ if (s.button === true) {
2922
+ for (const [state, times] of [["over", this.overTimes], ["down", this.downTimes], ["hit", this.hitTimes]]) {
2923
+ const at = buttonTime(s, state);
2924
+ if (at !== null) times.set(e.id, at);
2925
+ }
2926
+ } else if (e.playback === "click" && (s.timeline?.duration ?? 0) > 0) {
2927
+ this.replays.set(e.id, s.timeline.duration);
2928
+ }
2929
+ }
2930
+ const clickable2 = /* @__PURE__ */ new Set([...this.clicks.keys(), ...this.replays.keys()]);
2931
+ this.hoverIDs = new Set(this.page.elements.filter((e) => clickable2.has(e.id) && e.kind !== "symbol" && hasHover(e)).map((e) => e.id));
2932
+ const instances = new Set(this.page.elements.filter((e) => clickable2.has(e.id) && e.kind === "symbol").map((e) => e.id));
2933
+ for (const e of this.page.elements) {
2934
+ if (!clickable2.has(e.id)) continue;
2935
+ this.labels.set(e.id, buttonLabel(e, e.symbol !== null ? table.get(e.symbol)?.name : void 0));
2936
+ if (e.kind !== "symbol") this.owner.set(e.id, e.id);
2937
+ }
2938
+ for (const [part, id] of partsOf(this.page, instances)) this.owner.set(part, id);
2939
+ for (const [part, id] of this.owner) this.partsOfOwner.set(id, [...this.partsOfOwner.get(id) ?? [], part]);
2593
2940
  const near = (t) => {
2594
2941
  let best = 0;
2595
2942
  this.plan.times.forEach((x, i) => {
@@ -2628,24 +2975,73 @@ var Playback = class {
2628
2975
  this.frame = i;
2629
2976
  this.onFrame?.(i);
2630
2977
  }
2978
+ /** Starts copy `id` replaying from `at` (performance.now() milliseconds; earlier to start partway). */
2979
+ replay(id, at) {
2980
+ if (this.replays.has(id)) this.replayStarts.set(id, at);
2981
+ }
2982
+ /** Whether a copy is still playing through after a click. */
2983
+ replaying(now = performance.now()) {
2984
+ for (const [id, start] of this.replayStarts) if (now - start < (this.replays.get(id) ?? 0) * 1e3) return true;
2985
+ return false;
2986
+ }
2987
+ /** Draws the last moment drawn again, for copies replaying while the page stands still. */
2988
+ redraw() {
2989
+ this.drawAt(this.last.t, this.last.i);
2990
+ }
2631
2991
  /** Draws the moment `t` (seconds) in the frame-`i` view. */
2632
2992
  drawAt(t, i = 0) {
2993
+ this.last = { t, i };
2633
2994
  const r = this.plan.rects[Math.min(i, this.plan.rects.length - 1)];
2634
- const { parts } = svgParts(resolved(this.page, t), this.background, r);
2995
+ const now = typeof performance === "undefined" ? 0 : performance.now();
2996
+ const times = /* @__PURE__ */ new Map();
2997
+ for (const [id, start] of this.replayStarts) times.set(id, Math.min((now - start) / 1e3, this.replays.get(id) ?? 0));
2998
+ const tc = clampTime(t);
2999
+ const pose = posed(this.page, tc);
3000
+ const frame2 = expandingSymbols(pose, tc, true, times);
3001
+ const { parts } = svgParts(frame2, this.background, r);
3002
+ const owned = (m, parts2) => {
3003
+ for (const p of parts2) if (this.owner.has(p.key)) m.set(p.key, p.markup);
3004
+ return m;
3005
+ };
3006
+ const state = (st) => st.size === 0 ? /* @__PURE__ */ new Map() : owned(/* @__PURE__ */ new Map(), svgParts(expandingSymbols(pose, tc, true, new Map([...times, ...st])), this.background, r).parts.filter((p) => st.has(this.owner.get(p.key) ?? "")));
3007
+ const hover = this.hoverIDs.size === 0 ? /* @__PURE__ */ new Map() : owned(/* @__PURE__ */ new Map(), svgParts(hovered(frame2, this.hoverIDs), this.background, r).parts.filter((p) => this.hoverIDs.has(p.key)));
3008
+ const over = state(this.overTimes), down = state(this.downTimes), hit = state(this.hitTimes);
3009
+ const normal = owned(/* @__PURE__ */ new Map(), parts);
3010
+ const merged = [];
3011
+ const done = /* @__PURE__ */ new Set();
3012
+ for (const p of parts) {
3013
+ const o = this.owner.get(p.key);
3014
+ if (o === void 0) {
3015
+ merged.push(p);
3016
+ continue;
3017
+ }
3018
+ if (done.has(o)) continue;
3019
+ done.add(o);
3020
+ merged.push(this.buttonPart(o, normal, hover, over, down, hit, pose));
3021
+ }
2635
3022
  this.svg.setAttribute("viewBox", `${fmt(r.x)} ${fmt(r.y)} ${fmt(r.width)} ${fmt(r.height)}`);
2636
3023
  const ns = "http://www.w3.org/2000/svg";
2637
- const keys = parts.map((p) => p.key);
2638
- for (const p of parts) {
3024
+ const keys = merged.map((p) => p.key);
3025
+ for (const p of merged) {
3026
+ const markup = p.markup;
2639
3027
  const have = this.groups.get(p.key);
2640
3028
  if (have) {
2641
- if (have.markup !== p.markup) {
2642
- have.g.innerHTML = p.markup;
2643
- have.markup = p.markup;
3029
+ if (have.markup !== markup) {
3030
+ have.g.innerHTML = markup;
3031
+ have.markup = markup;
2644
3032
  }
3033
+ if (p.button !== void 0) have.g.classList.toggle("dottie-b", p.button);
2645
3034
  } else {
2646
3035
  const g = document.createElementNS(ns, "g");
2647
- g.innerHTML = p.markup;
2648
- this.groups.set(p.key, { g, markup: p.markup });
3036
+ g.innerHTML = markup;
3037
+ if (p.button !== void 0) {
3038
+ g.setAttribute("class", p.button ? "dottie-a dottie-b" : "dottie-a");
3039
+ g.setAttribute("data-a", p.key);
3040
+ g.setAttribute("tabindex", "0");
3041
+ g.setAttribute("role", "button");
3042
+ g.setAttribute("aria-label", this.labels.get(p.key) ?? "");
3043
+ }
3044
+ this.groups.set(p.key, { g, markup });
2649
3045
  }
2650
3046
  }
2651
3047
  const live = new Set(keys);
@@ -2654,10 +3050,66 @@ var Playback = class {
2654
3050
  this.groups.delete(k);
2655
3051
  }
2656
3052
  if (keys.length !== this.order.length || keys.some((k, j) => k !== this.order[j])) {
2657
- this.svg.replaceChildren(...keys.map((k) => this.groups.get(k).g));
3053
+ let at = this.svg.firstChild;
3054
+ for (const k of keys) {
3055
+ const g = this.groups.get(k).g;
3056
+ if (g === at) at = at.nextSibling;
3057
+ else this.svg.insertBefore(g, at);
3058
+ }
3059
+ while (at) {
3060
+ const next = at.nextSibling;
3061
+ at.remove();
3062
+ at = next;
3063
+ }
2658
3064
  this.order = keys;
2659
3065
  }
2660
3066
  }
3067
+ /** What a clickable owner draws as: its normal look, and its hover, over and down looks where they differ. A
3068
+ * symbol's copy keeps its drawing out of the pointer's way and takes clicks on a layer above: its hit state drawn
3069
+ * invisibly, or its frame. `button` is whether it has a down look (the stylesheet nudges the others). */
3070
+ buttonPart(o, normal, hover, over, down, hit, pose) {
3071
+ const keys = this.partsOfOwner.get(o) ?? [o];
3072
+ const join = (m2) => {
3073
+ const s = keys.map((k) => m2.get(k) ?? "").join("");
3074
+ return s === "" ? null : s;
3075
+ };
3076
+ const base = join(normal) ?? "";
3077
+ const instance = pose.elements.find((e) => e.id === o && e.kind === "symbol");
3078
+ const overLook = instance ? join(over) : join(hover);
3079
+ const downLook = instance ? join(down) : null;
3080
+ const shownOver = overLook !== null && overLook !== base ? overLook : null;
3081
+ const shownDown = downLook !== null && downLook !== (overLook ?? base) ? downLook : null;
3082
+ if (!instance && shownOver === null) return { key: o, markup: base, button: false };
3083
+ const still2 = instance ? ` pointer-events="none"` : "";
3084
+ let m = `<g class="dottie-n"${still2}>
3085
+ ${base}</g>
3086
+ `;
3087
+ if (shownOver !== null) m += `<g class="dottie-h"${still2}>
3088
+ ${shownOver}</g>
3089
+ `;
3090
+ if (shownDown !== null) m += `<g class="dottie-d"${still2}>
3091
+ ${shownDown}</g>
3092
+ `;
3093
+ if (instance) {
3094
+ const h = join(hit);
3095
+ if (h !== null) m += `<g class="dottie-hit" opacity="0" pointer-events="all">
3096
+ ${h}</g>
3097
+ `;
3098
+ else {
3099
+ const c = { x: instance.x + instance.width / 2, y: instance.y + instance.height / 2 }, a = instance.rotation ?? 0;
3100
+ const corners2 = [
3101
+ [instance.x, instance.y],
3102
+ [instance.x + instance.width, instance.y],
3103
+ [instance.x + instance.width, instance.y + instance.height],
3104
+ [instance.x, instance.y + instance.height]
3105
+ ].map(([x, y]) => rotate({ x, y }, c, a));
3106
+ const path = new Path().moveTo(corners2[0]).lineTo(corners2[1]).lineTo(corners2[2]).lineTo(corners2[3]).close();
3107
+ m += `<path class="dottie-hit" d="${path.data()}" fill="none" stroke="none" pointer-events="all"/>
3108
+ `;
3109
+ }
3110
+ }
3111
+ return { key: o, markup: m, button: shownDown !== null };
3112
+ }
2661
3113
  /** The scene frame `i` is in, or -1 before the first. */
2662
3114
  sceneAt(i) {
2663
3115
  let s = -1;
@@ -2687,6 +3139,7 @@ var DottiePlayer = class extends ElementBase {
2687
3139
  __publicField(this, "last", 0);
2688
3140
  __publicField(this, "acc", 0);
2689
3141
  __publicField(this, "raf", 0);
3142
+ __publicField(this, "replayRaf", 0);
2690
3143
  __publicField(this, "loadToken", 0);
2691
3144
  __publicField(this, "visible", true);
2692
3145
  __publicField(this, "observer", null);
@@ -2720,6 +3173,10 @@ var DottiePlayer = class extends ElementBase {
2720
3173
  };
2721
3174
  this.next.onclick = () => this.nextScene();
2722
3175
  this.prev.onclick = () => this.prevScene();
3176
+ this.stage.addEventListener("click", (e) => {
3177
+ const b = clickable(e.target);
3178
+ if (b) this.activate(b.getAttribute("data-a") ?? "");
3179
+ });
2723
3180
  this.addEventListener("keydown", (e) => this.key(e));
2724
3181
  }
2725
3182
  connectedCallback() {
@@ -2781,7 +3238,7 @@ var DottiePlayer = class extends ElementBase {
2781
3238
  mount() {
2782
3239
  const file = this.file;
2783
3240
  const page = file.pages[pageIndex(file, this.getAttribute("page"))];
2784
- const p = new Playback(page, this.stage, { clean: this.hasAttribute("clean") });
3241
+ const p = new Playback(page, this.stage, { clean: this.hasAttribute("clean"), file });
2785
3242
  this.playback = p;
2786
3243
  this.style.setProperty("--dottie-page", color(p.background));
2787
3244
  this.style.setProperty("--dottie-ink", color(p.ink));
@@ -2856,6 +3313,50 @@ var DottiePlayer = class extends ElementBase {
2856
3313
  this.setPlaying(false);
2857
3314
  p.show(s >= 0 ? p.scenes[s] : 0);
2858
3315
  }
3316
+ /** Does what clicking element `id` does: jumps to its scene and plays on to the next (with reduced motion, or in a
3317
+ * still, straight to where that stops), opens its link in a new tab, or shows its page. */
3318
+ activate(id) {
3319
+ const p = this.playback;
3320
+ const click = p?.clicks.get(id);
3321
+ const replay = p?.replays.has(id) ?? false;
3322
+ if (!p || !click && !replay) return;
3323
+ const detail = { element: id, ...click ?? {}, ...replay ? { replay: true } : {} };
3324
+ if (!this.dispatchEvent(new CustomEvent("action", { detail, cancelable: true }))) return;
3325
+ const reduced = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
3326
+ if (replay) {
3327
+ const now = performance.now();
3328
+ p.replay(id, reduced ? now - p.replays.get(id) * 1e3 : now);
3329
+ p.redraw();
3330
+ this.scheduleReplay();
3331
+ }
3332
+ if (!click) return;
3333
+ if ("url" in click) {
3334
+ if (/^(https?:|mailto:)/i.test(click.url)) window.open(click.url, "_blank", "noopener");
3335
+ } else if ("page" in click) {
3336
+ this.setAttribute("page", String(click.page + 1));
3337
+ } else if (click.scene >= 0 && click.scene < p.scenes.length) {
3338
+ if (reduced || this.hasAttribute("at") || p.frameCount < 2) {
3339
+ this.setPlaying(false);
3340
+ p.show(click.scene + 1 < p.scenes.length ? p.scenes[click.scene + 1] : p.frameCount - 1);
3341
+ } else {
3342
+ p.show(p.scenes[click.scene]);
3343
+ this.setPlaying(true);
3344
+ }
3345
+ }
3346
+ }
3347
+ /** Redraws, while the page itself stands still, as long as a clicked copy is playing through. */
3348
+ scheduleReplay() {
3349
+ if (this.replayRaf) return;
3350
+ const step = () => {
3351
+ this.replayRaf = 0;
3352
+ const p = this.playback;
3353
+ if (!p || !this.isConnected) return;
3354
+ if (!this.playing) p.redraw();
3355
+ if (p.replaying()) this.replayRaf = requestAnimationFrame(step);
3356
+ else if (!this.playing) p.redraw();
3357
+ };
3358
+ this.replayRaf = requestAnimationFrame(step);
3359
+ }
2859
3360
  setPlaying(on) {
2860
3361
  const p = this.playback;
2861
3362
  this.playing = on && !!p && p.frameCount > 1 && p.page.timeline !== null;
@@ -2897,8 +3398,15 @@ var DottiePlayer = class extends ElementBase {
2897
3398
  }
2898
3399
  key(e) {
2899
3400
  const p = this.playback;
3401
+ const target = e.composedPath()[0];
3402
+ const b = clickable(target);
3403
+ if (p && b && (e.key === "Enter" || e.key === " ")) {
3404
+ e.preventDefault();
3405
+ this.activate(b.getAttribute("data-a") ?? "");
3406
+ return;
3407
+ }
2900
3408
  if (!p || p.page.timeline === null || p.frameCount <= 1 || this.hasAttribute("at")) return;
2901
- if (e.target === this.scrub && e.key !== " ") return;
3409
+ if (target === this.scrub && e.key !== " ") return;
2902
3410
  const scenes = p.scenes.length > 0;
2903
3411
  let handled = true;
2904
3412
  if (e.key === " ") scenes ? this.playing ? this.setPlaying(false) : this.nextScene() : this.setPlaying(!this.playing);
@@ -2925,6 +3433,10 @@ var DottiePlayer = class extends ElementBase {
2925
3433
  }
2926
3434
  };
2927
3435
  __publicField(DottiePlayer, "observedAttributes", ["src", "page", "at", "clean", "no-controls", "label"]);
3436
+ function clickable(target) {
3437
+ const el = target;
3438
+ return el && typeof el.closest === "function" ? el.closest(".dottie-a") : null;
3439
+ }
2928
3440
  function nearest(p, t) {
2929
3441
  let best = 0;
2930
3442
  p.plan.times.forEach((x, i) => {
@@ -3024,7 +3536,7 @@ function render(source, options = {}) {
3024
3536
  function isFile(v) {
3025
3537
  return typeof v === "object" && v !== null && Array.isArray(v.pages) && Array.isArray(v.warnings);
3026
3538
  }
3027
- var VERSION = "0.3.0";
3539
+ var VERSION = "0.5.1";
3028
3540
  export {
3029
3541
  BOIL_DRAWINGS,
3030
3542
  DottieFormatError,
@@ -3036,6 +3548,7 @@ export {
3036
3548
  Playback,
3037
3549
  SUPPORTED_VERSION,
3038
3550
  VERSION,
3551
+ allowedURL,
3039
3552
  black,
3040
3553
  bounds,
3041
3554
  cameraView,