@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.
@@ -1,4 +1,4 @@
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 __export = (target, all) => {
@@ -20,6 +20,7 @@ __export(index_exports, {
20
20
  Playback: () => Playback,
21
21
  SUPPORTED_VERSION: () => SUPPORTED_VERSION,
22
22
  VERSION: () => VERSION,
23
+ allowedURL: () => allowedURL,
23
24
  black: () => black,
24
25
  bounds: () => bounds,
25
26
  cameraView: () => cameraView,
@@ -60,11 +61,12 @@ __export(index_exports, {
60
61
 
61
62
  // src/model.ts
62
63
  var SUPPORTED_VERSION = 2;
63
- var KINDS = ["rectangle", "ellipse", "diamond", "text", "connector", "freehand", "path", "note", "image"];
64
+ var KINDS = ["rectangle", "ellipse", "diamond", "text", "connector", "freehand", "path", "note", "image", "symbol"];
64
65
  var EASES = ["linear", "easeIn", "easeOut", "easeInOut", "hold", "spring", "bounce", "elastic"];
65
66
  var KNOWN_FIELDS = {
66
67
  file: ["version", "pages", "images"],
67
- page: ["id", "name", "layers", "elements", "theme", "guidesX", "guidesY", "guidesLocked", "timeline"],
68
+ page: ["id", "name", "layers", "elements", "theme", "guidesX", "guidesY", "guidesLocked", "timeline", "symbols"],
69
+ symbol: ["id", "name", "width", "height", "layers", "elements", "timeline", "button"],
68
70
  layer: ["id", "name", "visible", "locked"],
69
71
  rgba: ["r", "g", "b", "a"],
70
72
  style: ["stroke", "fill", "fillStyle", "strokeWidth", "dash", "roughness", "bowing", "fontSize", "gridSize", "opacity", "dashOffset"],
@@ -88,6 +90,11 @@ var KNOWN_FIELDS = {
88
90
  "rotation",
89
91
  "link",
90
92
  "anchor",
93
+ "click",
94
+ "hover",
95
+ "symbol",
96
+ "playback",
97
+ "offset",
91
98
  "image",
92
99
  "reveal"
93
100
  ],
@@ -102,7 +109,9 @@ var KNOWN_FIELDS = {
102
109
  follow: ["path", "start", "end", "ease", "reverse", "orient", "laps"],
103
110
  repeat: ["start", "end", "count"],
104
111
  cameraKey: ["t", "x", "y", "width", "height", "ease"],
105
- marker: ["t", "name"]
112
+ marker: ["t", "name"],
113
+ click: ["scene", "url"],
114
+ hover: ["stroke", "fill", "opacity"]
106
115
  };
107
116
  var MAX_DURATION = 3600;
108
117
  var MAX_MARCH = 1e4;
@@ -125,6 +134,11 @@ function clampTime(t) {
125
134
  function clampFPS(f) {
126
135
  return Number.isFinite(f) ? Math.min(Math.max(f, 1), 60) : 12;
127
136
  }
137
+ function allowedURL(url) {
138
+ const u = url.trim();
139
+ if (!/^(https?:\/\/|mailto:)/i.test(u) || [...u].length <= 7 || /[\u0000-\u001f\u007f-\u009f]/.test(u)) return null;
140
+ return u;
141
+ }
128
142
  function rgba(v) {
129
143
  if (!isObj(v)) return void 0;
130
144
  const r = num(v.r), g = num(v.g), b = num(v.b);
@@ -239,10 +253,61 @@ var Reader = class {
239
253
  rotation: num(v.rotation) ?? null,
240
254
  link: uuid(v.link) ?? null,
241
255
  anchor: str(v.anchor) ?? null,
256
+ click: this.click(v.click),
257
+ hover: this.hover(v.hover),
258
+ symbol: uuid(v.symbol) ?? null,
259
+ playback: this.playback(v.playback),
260
+ offset: num(v.offset) ?? null,
242
261
  image: this.image(v.image, images),
243
262
  reveal: num(v.reveal) ?? null
244
263
  };
245
264
  }
265
+ click(v) {
266
+ if (!isObj(v)) return null;
267
+ this.fields(v, "click");
268
+ const url = str(v.url);
269
+ const safe = url === void 0 ? null : allowedURL(url);
270
+ if (url !== void 0 && safe === null) this.warn("Skipped a click link that isn't http, https or mailto");
271
+ return { scene: str(v.scene) ?? null, url: safe };
272
+ }
273
+ playback(v) {
274
+ const s = str(v);
275
+ if (s === void 0) return null;
276
+ return ["loop", "once", "still", "click"].includes(s) ? s : "loop";
277
+ }
278
+ symbol(v, images) {
279
+ if (!isObj(v)) return null;
280
+ this.fields(v, "symbol");
281
+ const id = uuid(v.id);
282
+ if (!id) {
283
+ this.warn("Skipped a symbol without an id");
284
+ return null;
285
+ }
286
+ const size = (x) => {
287
+ const n = num(x);
288
+ return Math.max(1, n ?? 100);
289
+ };
290
+ const layers = (Array.isArray(v.layers) ? v.layers : []).filter(isObj).flatMap((l) => {
291
+ const lid = uuid(l.id);
292
+ return lid ? [{ id: lid, name: str(l.name) ?? "", visible: bool(l.visible) ?? true, locked: bool(l.locked) ?? false }] : [];
293
+ });
294
+ if (layers.length === 0) layers.push({ id: `${id}-LAYER`, name: "Layer 1", visible: true, locked: false });
295
+ return {
296
+ id,
297
+ name: str(v.name) ?? "Symbol",
298
+ width: size(v.width),
299
+ height: size(v.height),
300
+ layers,
301
+ elements: (Array.isArray(v.elements) ? v.elements : []).map((e) => this.element(e, images)).filter((e) => e !== null),
302
+ timeline: this.timeline(v.timeline),
303
+ button: bool(v.button) ?? null
304
+ };
305
+ }
306
+ hover(v) {
307
+ if (!isObj(v)) return null;
308
+ this.fields(v, "hover");
309
+ return { stroke: rgba(v.stroke) ?? null, fill: rgba(v.fill) ?? null, opacity: num(v.opacity) ?? null };
310
+ }
246
311
  ease(v) {
247
312
  const s = str(v);
248
313
  if (s === void 0) return null;
@@ -335,7 +400,8 @@ var Reader = class {
335
400
  layers,
336
401
  elements: (Array.isArray(v.elements) ? v.elements : []).map((e) => this.element(e, images)).filter((e) => e !== null),
337
402
  theme: str(v.theme) ?? null,
338
- timeline: this.timeline(v.timeline)
403
+ timeline: this.timeline(v.timeline),
404
+ symbols: Array.isArray(v.symbols) ? v.symbols.map((x) => this.symbol(x, images)).filter((x) => x !== null) : null
339
405
  };
340
406
  }
341
407
  };
@@ -977,6 +1043,190 @@ function hachureLines(polygons, angle2, gap) {
977
1043
  return out;
978
1044
  }
979
1045
 
1046
+ // src/dungeon.ts
1047
+ var SUB = 2;
1048
+ var BAND = 0.75;
1049
+ var GROW = 0.8;
1050
+ var JITTER = 0.6;
1051
+ function gridLines(polygons, cell) {
1052
+ return [...scan(polygons, cell, false), ...scan(polygons, cell, true)];
1053
+ }
1054
+ function scan(polygons, cell, transposed) {
1055
+ if (!(cell > 0)) return [];
1056
+ const flip = (p) => transposed ? { x: p.y, y: p.x } : p;
1057
+ const polys = polygons.filter((p) => p.length >= 3).map((p) => p.map(flip));
1058
+ const ys = polys.flat().map((p) => p.y);
1059
+ if (ys.length === 0) return [];
1060
+ const minY = Math.min(...ys), maxY2 = Math.max(...ys);
1061
+ const out = [];
1062
+ for (let y = Math.floor(minY / cell) * cell; y < maxY2 - 0.5; y += cell) {
1063
+ if (!(y > minY + 0.5)) continue;
1064
+ const xs = [];
1065
+ for (const poly of polys) {
1066
+ for (let i = 0; i < poly.length; i++) {
1067
+ const a = poly[i], b = poly[(i + 1) % poly.length];
1068
+ 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));
1069
+ }
1070
+ }
1071
+ xs.sort((p, q) => p - q);
1072
+ for (let k = 0; k + 1 < xs.length; k += 2) {
1073
+ if (xs[k + 1] - xs[k] > 0.5) out.push([flip({ x: xs[k], y }), flip({ x: xs[k + 1], y })]);
1074
+ }
1075
+ }
1076
+ return out;
1077
+ }
1078
+ function lineColor(floor) {
1079
+ if (0.2126 * floor.r + 0.7152 * floor.g + 0.0722 * floor.b > 0.5) {
1080
+ return { r: floor.r * 0.84, g: floor.g * 0.84, b: floor.b * 0.8, a: floor.a };
1081
+ }
1082
+ 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 };
1083
+ }
1084
+ function isFloor(e) {
1085
+ return e.style.fillStyle === "grid" && e.style.fill.a > 0 && ["rectangle", "ellipse", "diamond", "path"].includes(e.kind);
1086
+ }
1087
+ function hash01(i, j, salt) {
1088
+ let h = (Math.imul(i, 668265261) ^ Math.imul(j, 374761393) ^ Math.imul(salt, 2654435761)) >>> 0;
1089
+ h = Math.imul(h ^ h >>> 15, 2246822507) >>> 0;
1090
+ h = Math.imul(h ^ h >>> 13, 3266489909) >>> 0;
1091
+ h = (h ^ h >>> 16) >>> 0;
1092
+ return h / 4294967296;
1093
+ }
1094
+ function spot(i, j, cell) {
1095
+ const s = cell / SUB;
1096
+ 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 };
1097
+ }
1098
+ function clusterSize(i, j, at, cell) {
1099
+ let nearest2 = Infinity;
1100
+ for (let di = -1; di <= 1; di++) {
1101
+ for (let dj = -1; dj <= 1; dj++) {
1102
+ if (di === 0 && dj === 0) continue;
1103
+ const o = spot(i + di, j + dj, cell);
1104
+ nearest2 = Math.min(nearest2, Math.hypot(o.x - at.x, o.y - at.y));
1105
+ }
1106
+ }
1107
+ return nearest2 * GROW;
1108
+ }
1109
+ function clusterStrokes(c, angle2, size) {
1110
+ const dx = Math.cos(angle2), dy = Math.sin(angle2);
1111
+ const half = size * 0.8, gap = size * 0.5;
1112
+ return [-1, 0, 1].map((k) => {
1113
+ const cx = c.x - dy * gap * k, cy = c.y + dx * gap * k;
1114
+ return [{ x: cx - dx * half, y: cy - dy * half }, { x: cx + dx * half, y: cy + dy * half }];
1115
+ });
1116
+ }
1117
+ function contains(contours, p) {
1118
+ let inside = false;
1119
+ for (const poly of contours) {
1120
+ if (poly.length < 3) continue;
1121
+ let k = poly.length - 1;
1122
+ for (let i = 0; i < poly.length; i++) {
1123
+ const a = poly[i], b = poly[k];
1124
+ 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;
1125
+ k = i;
1126
+ }
1127
+ }
1128
+ return inside;
1129
+ }
1130
+ function distance(p, contours) {
1131
+ let best = Infinity;
1132
+ for (const poly of contours) {
1133
+ if (poly.length < 2) continue;
1134
+ for (let i = 0; i < poly.length; i++) {
1135
+ const a = poly[i], b = poly[(i + 1) % poly.length];
1136
+ const dx = b.x - a.x, dy = b.y - a.y;
1137
+ const len2 = dx * dx + dy * dy;
1138
+ const t = len2 > 0 ? Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2)) : 0;
1139
+ best = Math.min(best, Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy)));
1140
+ }
1141
+ }
1142
+ return best;
1143
+ }
1144
+ var containsPoint = (r, p) => p.x >= r.x && p.x < r.x + r.width && p.y >= r.y && p.y < r.y + r.height;
1145
+ var rockCache = /* @__PURE__ */ new Map();
1146
+ function rock(floors) {
1147
+ if (floors.length === 0) return [];
1148
+ const key = floors.map((e) => [
1149
+ e.kind,
1150
+ e.x,
1151
+ e.y,
1152
+ e.width,
1153
+ e.height,
1154
+ e.rotation ?? 0,
1155
+ cellSize(e),
1156
+ JSON.stringify(e.subpaths ?? null),
1157
+ e.style.stroke.r,
1158
+ e.style.stroke.g,
1159
+ e.style.stroke.b,
1160
+ e.style.stroke.a,
1161
+ e.style.roughness,
1162
+ e.style.strokeWidth
1163
+ ].join(",")).join(";");
1164
+ const hit = rockCache.get(key);
1165
+ if (hit) return hit;
1166
+ const v = computeRock(floors);
1167
+ if (rockCache.size >= 16) rockCache.clear();
1168
+ rockCache.set(key, v);
1169
+ return v;
1170
+ }
1171
+ function computeRock(elements) {
1172
+ const floors = elements.map((e) => {
1173
+ const contours = fillPolygons(e).map((c) => c.map((p) => rotated(e, p)));
1174
+ const pts = contours.flat();
1175
+ const xs = pts.map((p) => p.x), ys = pts.map((p) => p.y);
1176
+ const x0 = xs.length ? Math.min(...xs) : 0, y0 = ys.length ? Math.min(...ys) : 0;
1177
+ const box = { x: x0, y: y0, width: (xs.length ? Math.max(...xs) : 0) - x0, height: (ys.length ? Math.max(...ys) : 0) - y0 };
1178
+ return { contours, box, cell: cellSize(e), element: e };
1179
+ });
1180
+ const strokes = floors.map(() => []);
1181
+ const seen = /* @__PURE__ */ new Set();
1182
+ for (const f of floors) {
1183
+ const subSize = f.cell / SUB;
1184
+ const around = inset(f.box, -f.cell, -f.cell);
1185
+ const i0 = Math.floor(around.x / subSize), i1 = Math.ceil((around.x + around.width) / subSize);
1186
+ const j0 = Math.floor(around.y / subSize), j1 = Math.ceil((around.y + around.height) / subSize);
1187
+ const reach = BAND * f.cell;
1188
+ for (let i = i0; i < i1; i++) {
1189
+ for (let j = j0; j < j1; j++) {
1190
+ const k = `${f.cell}|${i}|${j}`;
1191
+ if (seen.has(k)) continue;
1192
+ seen.add(k);
1193
+ const at = spot(i, j, f.cell);
1194
+ let nearest2 = Infinity, owner = -1;
1195
+ for (let n = 0; n < floors.length; n++) {
1196
+ const g = floors[n];
1197
+ if (!containsPoint(inset(g.box, -reach, -reach), at)) continue;
1198
+ if (contains(g.contours, at)) {
1199
+ nearest2 = 0;
1200
+ break;
1201
+ }
1202
+ const d = distance(at, g.contours);
1203
+ if (d < nearest2) {
1204
+ nearest2 = d;
1205
+ owner = n;
1206
+ }
1207
+ }
1208
+ if (!(nearest2 > 0 && nearest2 <= reach && owner >= 0)) continue;
1209
+ const angle2 = hash01(i, j, 3) * Math.PI;
1210
+ const size = clusterSize(i, j, at, f.cell);
1211
+ const seed = Math.trunc(hash01(i, j, 4) * 4294967296) >>> 0;
1212
+ for (const [a, b] of clusterStrokes(at, angle2, size)) strokes[owner].push([a, b, seed]);
1213
+ }
1214
+ }
1215
+ }
1216
+ const out = [];
1217
+ floors.forEach((f, n) => {
1218
+ if (strokes[n].length === 0) return;
1219
+ const e = f.element;
1220
+ const path = new Path();
1221
+ for (const [a, b, seed] of strokes[n]) {
1222
+ if (e.style.roughness <= 0) path.moveTo(a).lineTo(b);
1223
+ else line(a, b, new SeededRNG(seed), e.style.roughness * 0.35, 0.2, false, path);
1224
+ }
1225
+ out.push({ ink: { ...e.style.stroke, a: e.style.stroke.a * 0.6 }, width: Math.max(0.8, e.style.strokeWidth * 0.45), path });
1226
+ });
1227
+ return out;
1228
+ }
1229
+
980
1230
  // src/shapes.ts
981
1231
  var Cache = class {
982
1232
  constructor(limit = 6e3) {
@@ -1229,196 +1479,551 @@ function computeConnector(e, index) {
1229
1479
  return [path, solid];
1230
1480
  }
1231
1481
 
1232
- // src/dungeon.ts
1233
- var SUB = 2;
1234
- var BAND = 0.75;
1235
- var GROW = 0.8;
1236
- var JITTER = 0.6;
1237
- function gridLines(polygons, cell) {
1238
- return [...scan(polygons, cell, false), ...scan(polygons, cell, true)];
1239
- }
1240
- function scan(polygons, cell, transposed) {
1241
- if (!(cell > 0)) return [];
1242
- const flip = (p) => transposed ? { x: p.y, y: p.x } : p;
1243
- const polys = polygons.filter((p) => p.length >= 3).map((p) => p.map(flip));
1244
- const ys = polys.flat().map((p) => p.y);
1245
- if (ys.length === 0) return [];
1246
- const minY = Math.min(...ys), maxY2 = Math.max(...ys);
1247
- const out = [];
1248
- for (let y = Math.floor(minY / cell) * cell; y < maxY2 - 0.5; y += cell) {
1249
- if (!(y > minY + 0.5)) continue;
1250
- const xs = [];
1251
- for (const poly of polys) {
1252
- for (let i = 0; i < poly.length; i++) {
1253
- const a = poly[i], b = poly[(i + 1) % poly.length];
1254
- 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));
1255
- }
1256
- }
1257
- xs.sort((p, q) => p - q);
1258
- for (let k = 0; k + 1 < xs.length; k += 2) {
1259
- if (xs[k + 1] - xs[k] > 0.5) out.push([flip({ x: xs[k], y }), flip({ x: xs[k + 1], y })]);
1260
- }
1482
+ // src/timeline.ts
1483
+ function applyEase(ease, u) {
1484
+ switch (ease) {
1485
+ case "linear":
1486
+ return u;
1487
+ case "easeIn":
1488
+ return u * u * u;
1489
+ case "easeOut":
1490
+ return 1 - Math.pow(1 - u, 3);
1491
+ case "easeInOut":
1492
+ return u < 0.5 ? 4 * u * u * u : 1 - Math.pow(-2 * u + 2, 3) / 2;
1493
+ case "hold":
1494
+ return 0;
1495
+ case "spring":
1496
+ return spring(u);
1497
+ case "bounce":
1498
+ return bounce(u);
1499
+ case "elastic":
1500
+ return elastic(u);
1261
1501
  }
1262
- return out;
1263
1502
  }
1264
- function lineColor(floor) {
1265
- if (0.2126 * floor.r + 0.7152 * floor.g + 0.0722 * floor.b > 0.5) {
1266
- return { r: floor.r * 0.84, g: floor.g * 0.84, b: floor.b * 0.8, a: floor.a };
1267
- }
1268
- 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 };
1503
+ function spring(u) {
1504
+ if (u <= 0) return 0;
1505
+ if (u >= 1) return 1;
1506
+ const s = (u2) => 1 - Math.exp(-6 * u2) * (Math.cos(3 * Math.PI * u2) + 6 / (3 * Math.PI) * Math.sin(3 * Math.PI * u2));
1507
+ return s(u) + u * (1 - s(1));
1269
1508
  }
1270
- function isFloor(e) {
1271
- return e.style.fillStyle === "grid" && e.style.fill.a > 0 && ["rectangle", "ellipse", "diamond", "path"].includes(e.kind);
1509
+ function bounce(u) {
1510
+ if (u <= 0) return 0;
1511
+ if (u >= 1) return 1;
1512
+ const n = 7.5625, d = 2.75;
1513
+ if (u < 1 / d) return n * u * u;
1514
+ if (u < 2 / d) {
1515
+ const v2 = u - 1.5 / d;
1516
+ return n * v2 * v2 + 0.75;
1517
+ }
1518
+ if (u < 2.5 / d) {
1519
+ const v2 = u - 2.25 / d;
1520
+ return n * v2 * v2 + 0.9375;
1521
+ }
1522
+ const v = u - 2.625 / d;
1523
+ return n * v * v + 0.984375;
1272
1524
  }
1273
- function hash01(i, j, salt) {
1274
- let h = (Math.imul(i, 668265261) ^ Math.imul(j, 374761393) ^ Math.imul(salt, 2654435761)) >>> 0;
1275
- h = Math.imul(h ^ h >>> 15, 2246822507) >>> 0;
1276
- h = Math.imul(h ^ h >>> 13, 3266489909) >>> 0;
1277
- h = (h ^ h >>> 16) >>> 0;
1278
- return h / 4294967296;
1525
+ function elastic(u) {
1526
+ if (u <= 0) return 0;
1527
+ if (u >= 1) return 1;
1528
+ return Math.pow(2, -10 * u) * Math.sin((u * 10 - 0.75) * (2 * Math.PI / 3)) + 1;
1279
1529
  }
1280
- function spot(i, j, cell) {
1281
- const s = cell / SUB;
1282
- 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 };
1530
+ var rem = (a, b) => a % b;
1531
+ function localTime(track, t) {
1532
+ const reps = (track.repeats ?? []).filter(repeatIsValid).slice().sort((a, b) => a.start - b.start);
1533
+ if (reps.length === 0) return t;
1534
+ for (const r of reps) {
1535
+ if (!(t >= r.end)) continue;
1536
+ const length = r.end - r.start;
1537
+ if (r.count === null) return r.start + rem(t - r.start, length);
1538
+ const played = length * r.count;
1539
+ if (t < r.start + played) return r.start + rem(t - r.start, length);
1540
+ t -= played - length;
1541
+ }
1542
+ return t;
1283
1543
  }
1284
- function clusterSize(i, j, at, cell) {
1285
- let nearest2 = Infinity;
1286
- for (let di = -1; di <= 1; di++) {
1287
- for (let dj = -1; dj <= 1; dj++) {
1288
- if (di === 0 && dj === 0) continue;
1289
- const o = spot(i + di, j + dj, cell);
1290
- nearest2 = Math.min(nearest2, Math.hypot(o.x - at.x, o.y - at.y));
1544
+ function value(track, t, get, base, lerp) {
1545
+ t = localTime(track, t);
1546
+ let before, after;
1547
+ for (const k of track.keys) {
1548
+ if (get(k) === void 0) continue;
1549
+ if (k.t <= t) before = k;
1550
+ else {
1551
+ after = k;
1552
+ break;
1291
1553
  }
1292
1554
  }
1293
- return nearest2 * GROW;
1555
+ if (!before && !after) return base;
1556
+ if (before && !after) return get(before);
1557
+ if (!before && after) return get(after);
1558
+ const b = before, a = after;
1559
+ const u = a.t - b.t > 0 ? (t - b.t) / (a.t - b.t) : 1;
1560
+ return lerp(get(b), get(a), applyEase(b.ease ?? "linear", u));
1294
1561
  }
1295
- function clusterStrokes(c, angle2, size) {
1296
- const dx = Math.cos(angle2), dy = Math.sin(angle2);
1297
- const half = size * 0.8, gap = size * 0.5;
1298
- return [-1, 0, 1].map((k) => {
1299
- const cx = c.x - dy * gap * k, cy = c.y + dx * gap * k;
1300
- return [{ x: cx - dx * half, y: cy - dy * half }, { x: cx + dx * half, y: cy + dy * half }];
1301
- });
1562
+ var lerpNum = (a, b, u) => a + (b - a) * u;
1563
+ var mix01 = (a, b, u) => Math.min(Math.max(a + (b - a) * u, 0), 1);
1564
+ var lerpColor = (a, b, u) => ({
1565
+ r: mix01(a.r, b.r, u),
1566
+ g: mix01(a.g, b.g, u),
1567
+ b: mix01(a.b, b.b, u),
1568
+ a: mix01(a.a, b.a, u)
1569
+ });
1570
+ function num2(track, t, p, base) {
1571
+ return value(track, t, (k) => k[p], base, lerpNum);
1302
1572
  }
1303
- function contains(contours, p) {
1304
- let inside = false;
1305
- for (const poly of contours) {
1306
- if (poly.length < 3) continue;
1307
- let k = poly.length - 1;
1308
- for (let i = 0; i < poly.length; i++) {
1309
- const a = poly[i], b = poly[k];
1310
- 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;
1311
- k = i;
1312
- }
1313
- }
1314
- return inside;
1573
+ function isChanging(track, t) {
1574
+ t = localTime(track, t);
1575
+ const changing = (get, eq) => {
1576
+ let b;
1577
+ for (const k of track.keys) if (k.t <= t && get(k) !== void 0) b = k;
1578
+ const a = track.keys.find((k) => k.t > t && get(k) !== void 0);
1579
+ if (!b || !a) return false;
1580
+ return b.ease !== "hold" && !eq(get(b), get(a));
1581
+ };
1582
+ const n = (p) => changing((k) => k[p], (a, b) => a === b);
1583
+ 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");
1315
1584
  }
1316
- function distance(p, contours) {
1317
- let best = Infinity;
1318
- for (const poly of contours) {
1319
- if (poly.length < 2) continue;
1320
- for (let i = 0; i < poly.length; i++) {
1321
- const a = poly[i], b = poly[(i + 1) % poly.length];
1322
- const dx = b.x - a.x, dy = b.y - a.y;
1323
- const len2 = dx * dx + dy * dy;
1324
- const t = len2 > 0 ? Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / len2)) : 0;
1325
- best = Math.min(best, Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy)));
1326
- }
1585
+ function isVisible(track, t) {
1586
+ t = localTime(track, t);
1587
+ const keyed = track.keys.filter((k) => k.visible !== void 0);
1588
+ if (keyed.length === 0) return true;
1589
+ let last;
1590
+ for (const k of keyed) if (k.t <= t) last = k;
1591
+ return last ? last.visible : !keyed[0].visible;
1592
+ }
1593
+ function applyTrack(track, e, t) {
1594
+ e = cloneElement(e);
1595
+ if (track.keys.some((k) => k.reveal !== void 0)) {
1596
+ const r = num2(track, t, "reveal", e.reveal ?? 1);
1597
+ e.reveal = r >= 1 ? null : Math.max(0, r);
1327
1598
  }
1328
- return best;
1599
+ if (track.keys.some((k) => k.opacity !== void 0)) {
1600
+ const o = Math.min(Math.max(num2(track, t, "opacity", e.style.opacity ?? 1), 0), 1);
1601
+ e.style.opacity = o >= 1 ? null : o;
1602
+ }
1603
+ if (e.kind === "connector") return e;
1604
+ e.x = num2(track, t, "x", e.x);
1605
+ e.y = num2(track, t, "y", e.y);
1606
+ e.width = Math.max(0, num2(track, t, "width", e.width));
1607
+ e.height = Math.max(0, num2(track, t, "height", e.height));
1608
+ if (track.keys.some((k) => k.rotation !== void 0)) e.rotation = num2(track, t, "rotation", angle(e));
1609
+ e.style.strokeWidth = Math.max(0, num2(track, t, "strokeWidth", e.style.strokeWidth));
1610
+ e.style.stroke = value(track, t, (k) => k.stroke, e.style.stroke, lerpColor);
1611
+ e.style.fill = value(track, t, (k) => k.fill, e.style.fill, lerpColor);
1612
+ return e;
1329
1613
  }
1330
- var containsPoint = (r, p) => p.x >= r.x && p.x < r.x + r.width && p.y >= r.y && p.y < r.y + r.height;
1331
- var rockCache = /* @__PURE__ */ new Map();
1332
- function rock(floors) {
1333
- if (floors.length === 0) return [];
1334
- const key = floors.map((e) => [
1335
- e.kind,
1336
- e.x,
1337
- e.y,
1338
- e.width,
1339
- e.height,
1340
- e.rotation ?? 0,
1341
- cellSize(e),
1342
- JSON.stringify(e.subpaths ?? null),
1343
- e.style.stroke.r,
1344
- e.style.stroke.g,
1345
- e.style.stroke.b,
1346
- e.style.stroke.a,
1347
- e.style.roughness,
1348
- e.style.strokeWidth
1349
- ].join(",")).join(";");
1350
- const hit = rockCache.get(key);
1351
- if (hit) return hit;
1352
- const v = computeRock(floors);
1353
- if (rockCache.size >= 16) rockCache.clear();
1354
- rockCache.set(key, v);
1355
- return v;
1614
+ function followSpan(track, t) {
1615
+ t = localTime(track, t);
1616
+ let f = null;
1617
+ for (const s of track.follow ?? []) if (s.start <= t && (f === null || s.start > f.start)) f = s;
1618
+ if (!f) return null;
1619
+ const span = f;
1620
+ if (track.keys.some((k) => k.t >= span.end && k.t <= t && (k.x !== void 0 || k.y !== void 0))) return null;
1621
+ return f;
1356
1622
  }
1357
- function computeRock(elements) {
1358
- const floors = elements.map((e) => {
1359
- const contours = fillPolygons(e).map((c) => c.map((p) => rotated(e, p)));
1360
- const pts = contours.flat();
1361
- const xs = pts.map((p) => p.x), ys = pts.map((p) => p.y);
1362
- const x0 = xs.length ? Math.min(...xs) : 0, y0 = ys.length ? Math.min(...ys) : 0;
1363
- const box = { x: x0, y: y0, width: (xs.length ? Math.max(...xs) : 0) - x0, height: (ys.length ? Math.max(...ys) : 0) - y0 };
1364
- return { contours, box, cell: cellSize(e), element: e };
1365
- });
1366
- const strokes = floors.map(() => []);
1367
- const seen = /* @__PURE__ */ new Set();
1368
- for (const f of floors) {
1369
- const subSize = f.cell / SUB;
1370
- const around = inset(f.box, -f.cell, -f.cell);
1371
- const i0 = Math.floor(around.x / subSize), i1 = Math.ceil((around.x + around.width) / subSize);
1372
- const j0 = Math.floor(around.y / subSize), j1 = Math.ceil((around.y + around.height) / subSize);
1373
- const reach = BAND * f.cell;
1374
- for (let i = i0; i < i1; i++) {
1375
- for (let j = j0; j < j1; j++) {
1376
- const k = `${f.cell}|${i}|${j}`;
1377
- if (seen.has(k)) continue;
1378
- seen.add(k);
1379
- const at = spot(i, j, f.cell);
1380
- let nearest2 = Infinity, owner = -1;
1381
- for (let n = 0; n < floors.length; n++) {
1382
- const g = floors[n];
1383
- if (!containsPoint(inset(g.box, -reach, -reach), at)) continue;
1384
- if (contains(g.contours, at)) {
1385
- nearest2 = 0;
1386
- break;
1387
- }
1388
- const d = distance(at, g.contours);
1389
- if (d < nearest2) {
1390
- nearest2 = d;
1391
- owner = n;
1392
- }
1393
- }
1394
- if (!(nearest2 > 0 && nearest2 <= reach && owner >= 0)) continue;
1395
- const angle2 = hash01(i, j, 3) * Math.PI;
1396
- const size = clusterSize(i, j, at, f.cell);
1397
- const seed = Math.trunc(hash01(i, j, 4) * 4294967296) >>> 0;
1398
- for (const [a, b] of clusterStrokes(at, angle2, size)) strokes[owner].push([a, b, seed]);
1399
- }
1400
- }
1401
- }
1402
- const out = [];
1403
- floors.forEach((f, n) => {
1404
- if (strokes[n].length === 0) return;
1405
- const e = f.element;
1406
- const path = new Path();
1407
- for (const [a, b, seed] of strokes[n]) {
1408
- if (e.style.roughness <= 0) path.moveTo(a).lineTo(b);
1409
- else line(a, b, new SeededRNG(seed), e.style.roughness * 0.35, 0.2, false, path);
1410
- }
1411
- out.push({ ink: { ...e.style.stroke, a: e.style.stroke.a * 0.6 }, width: Math.max(0.8, e.style.strokeWidth * 0.45), path });
1412
- });
1413
- return out;
1623
+ function tracing(e, page) {
1624
+ if (e.kind === "connector") return new Polyline(routePoints(route(e, indexOf(page))));
1625
+ const clean = cloneElement(e);
1626
+ clean.style.roughness = 0;
1627
+ const pts = outline(clean).contours()[0] ?? [];
1628
+ return new Polyline(angle(e) === 0 ? pts : pts.map((p) => rotated(e, p)));
1414
1629
  }
1415
-
1416
- // src/metrics.ts
1417
- var ascent = 0.951995849609375;
1418
- var descent = 0.2129974365234375;
1419
- var leading = 0.0279998779296875;
1420
- var boldAscent = 0.975006103515625;
1421
- var boldDescent = 0.2169952392578125;
1630
+ function following(track, e, t, page) {
1631
+ const f = followSpan(track, t);
1632
+ if (!f) return e;
1633
+ t = localTime(track, t);
1634
+ const pathElement = page.elements.find((x) => x.id === f.path);
1635
+ if (!pathElement || pathElement.id === e.id) return e;
1636
+ const line2 = tracing(pathElement, page);
1637
+ const span = f.end - f.start;
1638
+ 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);
1639
+ if (u > 1) u = rem(u, 1) === 0 ? 1 : rem(u, 1);
1640
+ if (f.reverse === true) u = 1 - u;
1641
+ const p = line2.point(u);
1642
+ const moved = cloneElement(e);
1643
+ moved.x = p.x - e.width / 2;
1644
+ moved.y = p.y - e.height / 2;
1645
+ if (f.orient === true) {
1646
+ const step = f.reverse === true ? -0.01 : 0.01;
1647
+ const a = line2.point(u - step / 2), b = line2.point(u + step / 2);
1648
+ if (a.x !== b.x || a.y !== b.y) moved.rotation = Math.atan2(b.y - a.y, b.x - a.x);
1649
+ }
1650
+ return moved;
1651
+ }
1652
+ function marchOffset(tl, speed, period, t) {
1653
+ if (!(period > 0)) return 0;
1654
+ const d = clampTime(tl.duration);
1655
+ if (d > 0) {
1656
+ const laps = roundHalfAway(speed * d / period);
1657
+ if (laps !== 0) speed = laps * period / d;
1658
+ }
1659
+ const r = rem(speed * t, period);
1660
+ return r < 0 ? -r : r === 0 ? 0 : period - r;
1661
+ }
1662
+ function trackMap(tl) {
1663
+ const m = /* @__PURE__ */ new Map();
1664
+ for (const t of tl.tracks) if (!m.has(t.element)) m.set(t.element, t);
1665
+ return m;
1666
+ }
1667
+ function resolved(page, t) {
1668
+ return expandingSymbols(posed(page, t), clampTime(t));
1669
+ }
1670
+ function posed(page, t) {
1671
+ const timeline = page.timeline;
1672
+ if (!timeline) return page;
1673
+ t = clampTime(t);
1674
+ const tracks = trackMap(timeline);
1675
+ const reseed = timeline.boil === null ? null : Math.trunc(Math.floor(t * clampFPS(timeline.fps) + 1e-9) / Math.max(1, timeline.boil)) % BOIL_DRAWINGS >>> 0;
1676
+ const hidden = /* @__PURE__ */ new Set();
1677
+ for (const tr of tracks.values()) if (!isVisible(tr, t)) hidden.add(tr.element);
1678
+ let moving = null;
1679
+ if (timeline.settle === true) {
1680
+ moving = /* @__PURE__ */ new Set();
1681
+ for (const tr of tracks.values()) if (isChanging(tr, t)) moving.add(tr.element);
1682
+ }
1683
+ const boils = (e) => {
1684
+ const override = tracks.get(e.id)?.boil;
1685
+ if (override !== void 0 && override !== null) return override;
1686
+ if (!moving) return true;
1687
+ if (e.connector) return [e.connector.start.elementID, e.connector.end.elementID].some((id) => id !== null && moving.has(id));
1688
+ return moving.has(e.id);
1689
+ };
1690
+ let elements = page.elements.map((e) => {
1691
+ const track = tracks.get(e.id);
1692
+ let out = track ? applyTrack(track, e, t) : e;
1693
+ if (reseed !== null && reseed > 0 && boils(e)) {
1694
+ if (out === e) out = cloneElement(e);
1695
+ out.seed = boiled(e.seed, reseed);
1696
+ }
1697
+ const pattern = track?.march != null ? dashPattern(out.style.dash, out.style.strokeWidth) : null;
1698
+ if (track?.march != null && pattern) {
1699
+ if (out === e) out = cloneElement(e);
1700
+ out.style.dashOffset = marchOffset(timeline, track.march, pattern.reduce((a, b) => a + b, 0), t);
1701
+ }
1702
+ return out;
1703
+ });
1704
+ elements = elements.map((e, i) => hidden.has(e.id) ? page.elements[i] : e);
1705
+ const posed2 = { ...page, elements, timeline: null };
1706
+ elements = elements.map((e) => {
1707
+ const track = tracks.get(e.id);
1708
+ return track ? following(track, e, t, posed2) : e;
1709
+ });
1710
+ const current = { ...page, elements, timeline: null };
1711
+ const index = indexOf(current);
1712
+ const gone = new Set(hidden);
1713
+ elements = elements.map((e) => {
1714
+ const c = e.connector;
1715
+ if (!c || ![c.start.elementID, c.end.elementID].some((id) => id !== null && hidden.has(id))) return e;
1716
+ if (!(tracks.get(e.id)?.keys.some((k) => k.visible !== void 0) ?? false)) {
1717
+ gone.add(e.id);
1718
+ return e;
1719
+ }
1720
+ const [a, b] = endpoints(e, index);
1721
+ const pinned = cloneElement(e);
1722
+ pinned.connector.start = { elementID: null, port: c.start.port, x: a.x, y: a.y };
1723
+ pinned.connector.end = { elementID: null, port: c.end.port, x: b.x, y: b.y };
1724
+ return pinned;
1725
+ });
1726
+ return { ...page, elements: elements.filter((e) => !gone.has(e.id)), timeline: null };
1727
+ }
1728
+ function posterTime(page) {
1729
+ return page.timeline ? page.timeline.poster ?? page.timeline.duration : 0;
1730
+ }
1731
+ function still(page) {
1732
+ return page.timeline ? resolved(page, posterTime(page)) : expandingSymbols(page, 0);
1733
+ }
1734
+ function cameraRect(tl, t) {
1735
+ const keys = tl.camera ?? [];
1736
+ if (keys.length === 0) return null;
1737
+ let before;
1738
+ for (const k of keys) if (k.t <= t) before = k;
1739
+ const after = keys.find((k) => k.t > t);
1740
+ if (!before) return rectOf(keys[0]);
1741
+ if (!after) return rectOf(before);
1742
+ const u = applyEase(before.ease ?? "easeInOut", (t - before.t) / Math.max(after.t - before.t, 1e-9));
1743
+ const mix3 = (p, q) => p + (q - p) * u;
1744
+ 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)) };
1745
+ }
1746
+ var rectOf = (k) => ({ x: k.x, y: k.y, width: k.width, height: k.height });
1747
+ function cameraView(tl, t) {
1748
+ const first = tl.camera?.[0];
1749
+ const r = cameraRect(tl, t);
1750
+ if (!first || !r || !(r.width > 0 && r.height > 0)) return null;
1751
+ return fit(r, first.width / first.height);
1752
+ }
1753
+ function fit(r, aspect) {
1754
+ if (!(r.width > 0 && r.height > 0 && aspect > 0)) return r;
1755
+ let w = r.width, h = r.height;
1756
+ if (r.width / r.height < aspect) w = r.height * aspect;
1757
+ else h = r.width / aspect;
1758
+ return { x: r.x + r.width / 2 - w / 2, y: r.y + r.height / 2 - h / 2, width: w, height: h };
1759
+ }
1760
+ function frameCount(tl) {
1761
+ return Math.max(1, Math.ceil(clampTime(tl.duration) * clampFPS(tl.fps)));
1762
+ }
1763
+ function frameTimes(tl) {
1764
+ const fps = clampFPS(tl.fps);
1765
+ const times = [];
1766
+ for (let i = 0; i < frameCount(tl); i++) times.push(i / fps);
1767
+ const last = times[times.length - 1];
1768
+ if (tl.duration - last > 1e-6) times.push(tl.duration);
1769
+ return times;
1770
+ }
1771
+ function sceneAt(tl, t) {
1772
+ let s = -1;
1773
+ (tl.markers ?? []).forEach((m, i) => {
1774
+ if (m.t <= t) s = i;
1775
+ });
1776
+ return s;
1777
+ }
1778
+ function transientElements(page) {
1779
+ const tl = page.timeline;
1780
+ if (!tl) return /* @__PURE__ */ new Set();
1781
+ const end = tl.poster ?? tl.duration;
1782
+ return new Set(tl.tracks.filter((tr) => !isVisible(tr, 0) && !isVisible(tr, end)).map((tr) => tr.element));
1783
+ }
1784
+
1785
+ // src/symbols.ts
1786
+ var MAX_DEPTH = 8;
1787
+ var MAX_ELEMENTS = 2e4;
1788
+ var EPSILON = 1e-9;
1789
+ function sceneTime(s, state) {
1790
+ const m = (s.timeline?.markers ?? []).find((x) => x.name.toLowerCase() === state);
1791
+ return m ? Math.min(Math.max(m.t, 0), clampTime(s.timeline?.duration ?? 0)) : null;
1792
+ }
1793
+ function buttonTime(s, state) {
1794
+ switch (state) {
1795
+ case "up":
1796
+ return sceneTime(s, "up") ?? 0;
1797
+ case "over":
1798
+ return sceneTime(s, "over") ?? buttonTime(s, "up");
1799
+ case "down":
1800
+ return sceneTime(s, "down") ?? buttonTime(s, "over");
1801
+ default:
1802
+ return sceneTime(s, "hit");
1803
+ }
1804
+ }
1805
+ function symbolTime(s, t, playback, offset2) {
1806
+ const d = clampTime(s.timeline?.duration ?? 0);
1807
+ if (s.button === true) return buttonTime(s, "up") ?? 0;
1808
+ const o = offset2 !== null && Number.isFinite(offset2) ? offset2 : 0;
1809
+ switch (playback ?? "loop") {
1810
+ case "still":
1811
+ return Math.min(s.timeline?.poster ?? d, d);
1812
+ case "click":
1813
+ return Math.min(Math.max(o, 0), d);
1814
+ case "once":
1815
+ return Math.min(Math.max(t + o, 0), d);
1816
+ default: {
1817
+ if (d <= 0) return 0;
1818
+ const x = t + o;
1819
+ let r = x % d;
1820
+ if (r < 0) r += d;
1821
+ if (x > EPSILON && (r < EPSILON || d - r < EPSILON)) return d;
1822
+ return r;
1823
+ }
1824
+ }
1825
+ }
1826
+ function symbolPage(s) {
1827
+ return { id: s.id, name: s.name, layers: s.layers, elements: s.elements, theme: null, timeline: s.timeline, symbols: null };
1828
+ }
1829
+ function hasInstances(page) {
1830
+ return page.elements.some((e) => e.kind === "symbol");
1831
+ }
1832
+ function expandingSymbols(page, t, posing = true, times = /* @__PURE__ */ new Map()) {
1833
+ if (!hasInstances(page)) return page;
1834
+ const budget = { left: MAX_ELEMENTS };
1835
+ return { ...page, elements: expand(page.elements, page, t, symbolTable(page), [], posing, budget, times) };
1836
+ }
1837
+ function symbolTable(page) {
1838
+ const table = /* @__PURE__ */ new Map();
1839
+ for (const s of page.symbols ?? []) if (!table.has(s.id)) table.set(s.id, s);
1840
+ return table;
1841
+ }
1842
+ function expand(elements, page, t, table, stack, posing, budget, times = /* @__PURE__ */ new Map()) {
1843
+ const instances = new Set(elements.filter((e) => e.kind === "symbol").map((e) => e.id));
1844
+ if (instances.size === 0) {
1845
+ budget.left -= elements.length;
1846
+ return elements;
1847
+ }
1848
+ const index = indexOf(page);
1849
+ const out = [];
1850
+ for (const e of elements) {
1851
+ if (budget.left <= 0) break;
1852
+ const c = e.connector;
1853
+ if (e.kind === "connector" && c && [c.start.elementID, c.end.elementID].some((id2) => id2 !== null && instances.has(id2))) {
1854
+ const [a, b] = endpoints(e, index);
1855
+ const pinned = cloneElement(e);
1856
+ 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 };
1857
+ 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 };
1858
+ out.push(pinned);
1859
+ budget.left -= 1;
1860
+ continue;
1861
+ }
1862
+ if (e.kind !== "symbol") {
1863
+ out.push(e);
1864
+ budget.left -= 1;
1865
+ continue;
1866
+ }
1867
+ budget.left -= 1;
1868
+ const id = e.symbol;
1869
+ const symbol = id === null ? void 0 : table.get(id);
1870
+ if (id === null || !symbol || stack.includes(id) || stack.length >= MAX_DEPTH) continue;
1871
+ const override = stack.length === 0 ? times.get(e.id) : void 0;
1872
+ const u = override !== void 0 ? Math.min(Math.max(override, 0), clampTime(symbol.timeline?.duration ?? 0)) : symbolTime(symbol, t, e.playback, e.offset);
1873
+ const inner = posing ? posed(symbolPage(symbol), u) : symbolPage(symbol);
1874
+ const visible = new Set(inner.layers.filter((l) => l.visible).map((l) => l.id));
1875
+ const drawn = {
1876
+ ...inner,
1877
+ elements: orderedElements(inner).filter((x) => visible.has(x.layerID)),
1878
+ layers: [{ id: e.layerID, name: "", visible: true, locked: false }]
1879
+ };
1880
+ for (const x of expand(drawn.elements, drawn, u, table, [...stack, id], posing, budget)) out.push(placed(x, e, symbol));
1881
+ }
1882
+ return out;
1883
+ }
1884
+ function placed(inner, instance, symbol) {
1885
+ const f = { x: instance.x, y: instance.y, width: instance.width, height: instance.height };
1886
+ const sx = f.width / symbol.width, sy = f.height / symbol.height;
1887
+ const s = Math.sqrt(Math.abs(sx * sy));
1888
+ const center2 = { x: f.x + f.width / 2, y: f.y + f.height / 2 };
1889
+ const a = angle(instance);
1890
+ const point = (p) => rotate({ x: f.x + p.x * sx, y: f.y + p.y * sy }, center2, a);
1891
+ const e = cloneElement(inner);
1892
+ e.id = derivedID(instance.id, inner.id);
1893
+ e.layerID = instance.layerID;
1894
+ e.groupID = null;
1895
+ e.link = inner.link === null ? null : derivedID(instance.id, inner.link);
1896
+ e.seed = boiled(inner.seed, instance.seed);
1897
+ e.click = null;
1898
+ e.hover = null;
1899
+ const scaled = { x: f.x + inner.x * sx, y: f.y + inner.y * sy, width: inner.width * sx, height: inner.height * sy };
1900
+ const moved = rotate({ x: scaled.x + scaled.width / 2, y: scaled.y + scaled.height / 2 }, center2, a);
1901
+ e.x = moved.x - scaled.width / 2;
1902
+ e.y = moved.y - scaled.height / 2;
1903
+ e.width = scaled.width;
1904
+ e.height = scaled.height;
1905
+ if (a !== 0 || inner.rotation !== null) e.rotation = angle(inner) + a;
1906
+ e.style.strokeWidth = inner.style.strokeWidth * s;
1907
+ e.style.fontSize = inner.style.fontSize * s;
1908
+ if (inner.style.gridSize !== null) e.style.gridSize = inner.style.gridSize * s;
1909
+ if (instance.style.opacity !== null) e.style.opacity = (inner.style.opacity ?? 1) * instance.style.opacity;
1910
+ if (inner.connector) {
1911
+ const end = (p) => {
1912
+ if (p.elementID !== null) return { elementID: derivedID(instance.id, p.elementID), port: p.port, x: p.x, y: p.y };
1913
+ const q = point(p);
1914
+ return { elementID: null, port: p.port, x: q.x, y: q.y };
1915
+ };
1916
+ e.connector = { ...inner.connector, start: end(inner.connector.start), end: end(inner.connector.end) };
1917
+ }
1918
+ return e;
1919
+ }
1920
+ var MASK = (1n << 64n) - 1n;
1921
+ function mix2(x) {
1922
+ let z = x + 0x9e3779b97f4a7c15n & MASK;
1923
+ z = (z ^ z >> 30n) * 0xbf58476d1ce4e5b9n & MASK;
1924
+ z = (z ^ z >> 27n) * 0x94d049bb133111ebn & MASK;
1925
+ return z ^ z >> 31n;
1926
+ }
1927
+ function halves(id) {
1928
+ const hex = id.replace(/-/g, "").padStart(32, "0").slice(-32);
1929
+ return [BigInt(`0x${hex.slice(0, 16)}`), BigInt(`0x${hex.slice(16)}`)];
1930
+ }
1931
+ function derivedID(instance, inner) {
1932
+ const [a, b] = halves(instance), [c, d] = halves(inner);
1933
+ const hi = mix2(mix2(mix2(mix2(a) ^ b) ^ c) ^ d);
1934
+ const lo = mix2(hi ^ 0x6a09e667f3bcc909n);
1935
+ const h = hi.toString(16).padStart(16, "0") + lo.toString(16).padStart(16, "0");
1936
+ return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`.toUpperCase();
1937
+ }
1938
+
1939
+ // src/clicks.ts
1940
+ function sceneNamed(name, markers) {
1941
+ const exact = markers.findIndex((m) => m.name === name);
1942
+ return exact >= 0 ? exact : markers.findIndex((m) => m.name.toLowerCase() === name.toLowerCase());
1943
+ }
1944
+ function clickTargets(page, file) {
1945
+ const markers = page.timeline?.markers ?? [];
1946
+ const out = /* @__PURE__ */ new Map();
1947
+ let pageOf = null;
1948
+ for (const e of page.elements) {
1949
+ const c = e.click;
1950
+ if (c?.scene) {
1951
+ const i = sceneNamed(c.scene, markers);
1952
+ if (i >= 0) {
1953
+ out.set(e.id, { scene: i });
1954
+ continue;
1955
+ }
1956
+ }
1957
+ const url = c?.url ? allowedURL(c.url) : null;
1958
+ if (url) {
1959
+ out.set(e.id, { url });
1960
+ continue;
1961
+ }
1962
+ if (e.link && file && file.pages.length > 1) {
1963
+ if (!pageOf) {
1964
+ pageOf = /* @__PURE__ */ new Map();
1965
+ file.pages.forEach((p, i) => {
1966
+ for (const x of p.elements) if (!pageOf.has(x.id)) pageOf.set(x.id, i);
1967
+ });
1968
+ }
1969
+ const target = pageOf.get(e.link);
1970
+ if (target !== void 0 && file.pages[target] !== page) out.set(e.id, { page: target });
1971
+ }
1972
+ }
1973
+ return out;
1974
+ }
1975
+ function hoveredElement(e) {
1976
+ const h = e.hover;
1977
+ if (!h) return e;
1978
+ const style = { ...e.style };
1979
+ if (h.stroke) style.stroke = h.stroke;
1980
+ if (h.fill) style.fill = h.fill;
1981
+ if (h.opacity !== null && Number.isFinite(h.opacity)) {
1982
+ const o = Math.min(Math.max(h.opacity, 0), 1);
1983
+ style.opacity = o >= 1 ? null : o;
1984
+ }
1985
+ return { ...e, style };
1986
+ }
1987
+ function hovered(page, ids) {
1988
+ return { ...page, elements: page.elements.map((e) => ids.has(e.id) ? hoveredElement(e) : e) };
1989
+ }
1990
+ function hasHover(e) {
1991
+ return !!e.hover && (e.hover.stroke !== null || e.hover.fill !== null || e.hover.opacity !== null);
1992
+ }
1993
+ function buttonLabel(e, symbolName) {
1994
+ const line2 = (e.text ?? "").split("\n").find((l) => l.length > 0);
1995
+ if (line2) return line2;
1996
+ if (e.anchor) return e.anchor;
1997
+ if (symbolName) return symbolName;
1998
+ return e.kind;
1999
+ }
2000
+ function partsOf(page, instances) {
2001
+ const out = /* @__PURE__ */ new Map();
2002
+ for (const e of page.elements) {
2003
+ if (!instances.has(e.id) || e.kind !== "symbol") continue;
2004
+ const alone = { ...page, layers: [{ id: e.layerID, name: "", visible: true, locked: false }], elements: [e] };
2005
+ for (const part of expandingSymbols(alone, 0, false).elements) out.set(part.id, e.id);
2006
+ }
2007
+ return out;
2008
+ }
2009
+ var CLICK_STYLE = `
2010
+ .dottie-a { cursor: pointer; outline: none; }
2011
+ .dottie-a .dottie-h { display: none; }
2012
+ .dottie-a:hover .dottie-h, .dottie-a.dottie-hover .dottie-h, .dottie-a:focus-visible .dottie-h { display: inline; }
2013
+ .dottie-a:hover .dottie-n, .dottie-a.dottie-hover .dottie-n, .dottie-a:focus-visible .dottie-n { display: none; }
2014
+ .dottie-a:focus-visible { filter: drop-shadow(0 0 3px var(--dottie-focus, #3b82f6)); }
2015
+ .dottie-a:not(.dottie-b):active, .dottie-a.dottie-press:not(.dottie-b) { transform: translate(1px, 1px); }
2016
+ .dottie-a .dottie-d { display: none; }
2017
+ .dottie-b:active .dottie-n, .dottie-b:active .dottie-h, .dottie-b.dottie-press .dottie-n, .dottie-b.dottie-press .dottie-h { display: none; }
2018
+ .dottie-b:active .dottie-d, .dottie-b.dottie-press .dottie-d { display: inline; }
2019
+ `;
2020
+
2021
+ // src/metrics.ts
2022
+ var ascent = 0.951995849609375;
2023
+ var descent = 0.2129974365234375;
2024
+ var leading = 0.0279998779296875;
2025
+ var boldAscent = 0.975006103515625;
2026
+ var boldDescent = 0.2169952392578125;
1422
2027
  var boldLeading = 0.0290069580078125;
1423
2028
  var italicAscent = 0.957000732421875;
1424
2029
  var italicDescent = 0.2129974365234375;
@@ -1938,476 +2543,177 @@ function measureRun(r) {
1938
2543
  return w;
1939
2544
  }
1940
2545
  var TAB_INTERVAL = 28;
1941
- function layoutLines(paras, width, height, fontSize) {
1942
- const lines = [];
1943
- let baseline = 0;
1944
- let prev = null;
1945
- paras.forEach((p, pi) => {
1946
- 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;
1947
- const chars = [];
1948
- runs.forEach((r, ri) => {
1949
- for (const ch of r.text) chars.push({ ch, run: ri });
1950
- });
1951
- let k = 0;
1952
- let firstLine = true;
1953
- while (k < chars.length || firstLine && chars.length === 0) {
1954
- const start = firstLine ? p.firstIndent : p.indent;
1955
- const maxX2 = width;
1956
- const glyphs = [];
1957
- let x = start, lastSpace = -1, end = k;
1958
- for (; end < chars.length; end++) {
1959
- const { ch, run } = chars[end];
1960
- const r = runs[run];
1961
- if (ch === SEP || ch === "\n") {
1962
- glyphs.push({ ch, run, x, w: 0 });
1963
- end++;
1964
- break;
1965
- }
1966
- if (ch === " ") {
1967
- const stop = p.tabs.find((t) => t.x > x + 0.01);
1968
- let tx;
1969
- if (stop) {
1970
- if (stop.align === "left") tx = stop.x;
1971
- else {
1972
- let seg = 0, prevCh = "";
1973
- for (let q = end + 1; q < chars.length && chars[q].ch !== " " && chars[q].ch !== "\n" && chars[q].ch !== SEP; q++) {
1974
- const rr = runs[chars[q].run];
1975
- seg += charWidth(chars[q].ch, rr) + (prevCh ? kern(prevCh, chars[q].ch, rr) : 0);
1976
- prevCh = chars[q].ch;
1977
- }
1978
- tx = Math.max(x, stop.align === "right" ? stop.x - seg : stop.x - seg / 2);
1979
- }
1980
- } else tx = (Math.floor(x / TAB_INTERVAL) + 1) * TAB_INTERVAL;
1981
- glyphs.push({ ch, run, x, w: tx - x });
1982
- x = tx;
1983
- continue;
1984
- }
1985
- const prevGlyph = glyphs[glyphs.length - 1];
1986
- const kx = prevGlyph && prevGlyph.run === run && prevGlyph.ch !== " " ? kern(prevGlyph.ch, ch, r) : 0;
1987
- if (prevGlyph) {
1988
- prevGlyph.w += kx;
1989
- x += kx;
1990
- }
1991
- const w = charWidth(ch, r);
1992
- if (ch !== " " && x + w > maxX2 + 0.01 && glyphs.some((g) => g.ch !== " ")) {
1993
- if (lastSpace >= 0) {
1994
- glyphs.length = lastSpace + 1;
1995
- end = k + lastSpace + 1;
1996
- }
1997
- if (prevGlyph) prevGlyph.w -= kx;
1998
- break;
1999
- }
2000
- glyphs.push({ ch, run, x, w });
2001
- x += w;
2002
- if (breaksAfter(ch, chars[end + 1]?.ch ?? "")) lastSpace = glyphs.length - 1;
2003
- }
2004
- if (glyphs.length === 0 && end === k && k < chars.length) {
2005
- const { ch, run } = chars[k];
2006
- glyphs.push({ ch, run, x: start, w: charWidth(ch, runs[run]) });
2007
- end = k + 1;
2008
- }
2009
- let asc = 0, desc = 0, lead = 0;
2010
- for (const g of glyphs) {
2011
- const [a, d, l] = metrics(g.ch, runs[g.run]);
2012
- asc = Math.max(asc, a);
2013
- desc = Math.max(desc, d);
2014
- lead = Math.max(lead, l);
2015
- }
2016
- 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 });
2017
- baseline = prev ? baseline + Math.round(prev.desc) + Math.round(prev.lead) + (firstLine ? p.spacing : 0) + Math.round(asc) : Math.round(asc);
2018
- if (baseline + desc > height + 0.5) return;
2019
- const line2 = { glyphs, asc, desc, lead, baseline, para: p, first: firstLine };
2020
- line2.glyphs.forEach((g) => {
2021
- g.r = runs[g.run];
2022
- });
2023
- lines.push(line2);
2024
- prev = line2;
2025
- firstLine = false;
2026
- k = end;
2027
- if (chars.length === 0) break;
2028
- }
2029
- });
2030
- return lines;
2031
- }
2032
- function noteMarkup(source, e) {
2033
- const rect2 = textRect({ x: e.x, y: e.y, width: e.width, height: e.height });
2034
- if (!(rect2.width > 1 && rect2.height > 1)) return "";
2035
- const b = new Builder(e.style.fontSize, rect2.width);
2036
- for (const block of parseBlocks(source)) b.block(block, { indent: 0, marker: null, quote: null, listDepth: 0 });
2037
- const lines = layoutLines(b.paras, rect2.width, rect2.height, e.style.fontSize);
2038
- const ink = color(e.style.stroke);
2039
- let s = "";
2040
- const marks = [];
2041
- for (const l of lines) {
2042
- const p = l.para;
2043
- 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]);
2044
- if (p.rule !== null) marks.push([rect2.x + p.rule, rect2.y + l.baseline - 3, rect2.x + rect2.width, rect2.y + l.baseline - 3]);
2045
- if (p.tableRule) {
2046
- const y = rect2.y + l.baseline + l.desc + 2;
2047
- marks.push([rect2.x + p.tableRule[0], y, Math.min(rect2.x + rect2.width, rect2.x + p.tableRule[0] + p.tableRule[1]), y]);
2048
- }
2049
- let spans = "";
2050
- let i = 0;
2051
- const gs = l.glyphs;
2052
- while (i < gs.length) {
2053
- const r = gs[i].r;
2054
- let j = i;
2055
- let text = "";
2056
- const font = fontOf(gs[i].ch);
2057
- while (j < gs.length && sameStyle(gs[j].r, r) && gs[j].ch !== " " && fontOf(gs[j].ch) === font) {
2058
- text += gs[j].ch;
2059
- j++;
2060
- }
2061
- const shown = text.replaceAll("\n", "").replaceAll(SEP, "");
2062
- if (shown.trim() !== "") {
2063
- let a = `font-size="${fmt(r.size)}"`;
2064
- if (r.mono) a += ` font-family="Menlo, Consolas, monospace"`;
2065
- if (r.bold) a += ` font-weight="bold"`;
2066
- if (r.italic) a += ` font-style="italic"`;
2067
- if (r.underline) a += ` text-decoration="underline"`;
2068
- if (r.alpha < 1) a += ` fill-opacity="${fmt(r.alpha * e.style.stroke.a)}"`;
2069
- spans += `<tspan x="${fmt(rect2.x + gs[i].x)}" ${a}>${escapeXML(shown)}</tspan>`;
2070
- }
2071
- if (r.strike && j > i) {
2072
- const y = l.baseline - metrics(gs[i].ch, r)[0] * 0.32;
2073
- marks.push([rect2.x + gs[i].x, rect2.y + y, rect2.x + gs[j - 1].x + gs[j - 1].w, rect2.y + y]);
2074
- }
2075
- if (j === i) j++;
2076
- i = j;
2077
- }
2078
- 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>
2079
- `;
2080
- }
2081
- const width = fmt(Math.max(1, e.style.fontSize / 14));
2082
- for (const [x1, y1, x2, y2] of marks) {
2083
- s += `<line x1="${fmt(x1)}" y1="${fmt(y1)}" x2="${fmt(x2)}" y2="${fmt(y2)}" stroke="${ink}" stroke-width="${width}"/>
2084
- `;
2085
- }
2086
- return s;
2087
- }
2088
- function fontOf(ch) {
2089
- return fallbackMetrics[ch] ? `fb:${ch}` : "";
2090
- }
2091
- function sameStyle(a, b) {
2092
- 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;
2093
- }
2094
-
2095
- // src/theme.ts
2096
- var rgb = (r, g, b) => ({ r, g, b, a: 1 });
2097
- var themes = [
2098
- { id: "paper", name: "Paper", background: rgb(0.984, 0.98, 0.965), ink: rgb(0.12, 0.12, 0.13) },
2099
- { id: "white", name: "White", background: rgb(1, 1, 1), ink: rgb(0.12, 0.12, 0.13) },
2100
- { id: "sepia", name: "Sepia", background: rgb(0.953, 0.914, 0.824), ink: rgb(0.23, 0.18, 0.13) },
2101
- { id: "blueprint", name: "Blueprint", background: rgb(0.106, 0.227, 0.361), ink: rgb(0.91, 0.945, 0.984) },
2102
- { id: "chalkboard", name: "Chalkboard", background: rgb(0.169, 0.227, 0.2), ink: rgb(0.945, 0.945, 0.918) },
2103
- { id: "midnight", name: "Midnight", background: rgb(0.082, 0.086, 0.102), ink: rgb(0.925, 0.925, 0.94) }
2104
- ];
2105
- function themeNamed(id) {
2106
- return themes.find((t) => t.id === id) ?? null;
2107
- }
2108
-
2109
- // src/timeline.ts
2110
- function applyEase(ease, u) {
2111
- switch (ease) {
2112
- case "linear":
2113
- return u;
2114
- case "easeIn":
2115
- return u * u * u;
2116
- case "easeOut":
2117
- return 1 - Math.pow(1 - u, 3);
2118
- case "easeInOut":
2119
- return u < 0.5 ? 4 * u * u * u : 1 - Math.pow(-2 * u + 2, 3) / 2;
2120
- case "hold":
2121
- return 0;
2122
- case "spring":
2123
- return spring(u);
2124
- case "bounce":
2125
- return bounce(u);
2126
- case "elastic":
2127
- return elastic(u);
2128
- }
2129
- }
2130
- function spring(u) {
2131
- if (u <= 0) return 0;
2132
- if (u >= 1) return 1;
2133
- const s = (u2) => 1 - Math.exp(-6 * u2) * (Math.cos(3 * Math.PI * u2) + 6 / (3 * Math.PI) * Math.sin(3 * Math.PI * u2));
2134
- return s(u) + u * (1 - s(1));
2135
- }
2136
- function bounce(u) {
2137
- if (u <= 0) return 0;
2138
- if (u >= 1) return 1;
2139
- const n = 7.5625, d = 2.75;
2140
- if (u < 1 / d) return n * u * u;
2141
- if (u < 2 / d) {
2142
- const v2 = u - 1.5 / d;
2143
- return n * v2 * v2 + 0.75;
2144
- }
2145
- if (u < 2.5 / d) {
2146
- const v2 = u - 2.25 / d;
2147
- return n * v2 * v2 + 0.9375;
2148
- }
2149
- const v = u - 2.625 / d;
2150
- return n * v * v + 0.984375;
2151
- }
2152
- function elastic(u) {
2153
- if (u <= 0) return 0;
2154
- if (u >= 1) return 1;
2155
- return Math.pow(2, -10 * u) * Math.sin((u * 10 - 0.75) * (2 * Math.PI / 3)) + 1;
2156
- }
2157
- var rem = (a, b) => a % b;
2158
- function localTime(track, t) {
2159
- const reps = (track.repeats ?? []).filter(repeatIsValid).slice().sort((a, b) => a.start - b.start);
2160
- if (reps.length === 0) return t;
2161
- for (const r of reps) {
2162
- if (!(t >= r.end)) continue;
2163
- const length = r.end - r.start;
2164
- if (r.count === null) return r.start + rem(t - r.start, length);
2165
- const played = length * r.count;
2166
- if (t < r.start + played) return r.start + rem(t - r.start, length);
2167
- t -= played - length;
2168
- }
2169
- return t;
2170
- }
2171
- function value(track, t, get, base, lerp) {
2172
- t = localTime(track, t);
2173
- let before, after;
2174
- for (const k of track.keys) {
2175
- if (get(k) === void 0) continue;
2176
- if (k.t <= t) before = k;
2177
- else {
2178
- after = k;
2179
- break;
2180
- }
2181
- }
2182
- if (!before && !after) return base;
2183
- if (before && !after) return get(before);
2184
- if (!before && after) return get(after);
2185
- const b = before, a = after;
2186
- const u = a.t - b.t > 0 ? (t - b.t) / (a.t - b.t) : 1;
2187
- return lerp(get(b), get(a), applyEase(b.ease ?? "linear", u));
2188
- }
2189
- var lerpNum = (a, b, u) => a + (b - a) * u;
2190
- var mix01 = (a, b, u) => Math.min(Math.max(a + (b - a) * u, 0), 1);
2191
- var lerpColor = (a, b, u) => ({
2192
- r: mix01(a.r, b.r, u),
2193
- g: mix01(a.g, b.g, u),
2194
- b: mix01(a.b, b.b, u),
2195
- a: mix01(a.a, b.a, u)
2196
- });
2197
- function num2(track, t, p, base) {
2198
- return value(track, t, (k) => k[p], base, lerpNum);
2199
- }
2200
- function isChanging(track, t) {
2201
- t = localTime(track, t);
2202
- const changing = (get, eq) => {
2203
- let b;
2204
- for (const k of track.keys) if (k.t <= t && get(k) !== void 0) b = k;
2205
- const a = track.keys.find((k) => k.t > t && get(k) !== void 0);
2206
- if (!b || !a) return false;
2207
- return b.ease !== "hold" && !eq(get(b), get(a));
2208
- };
2209
- const n = (p) => changing((k) => k[p], (a, b) => a === b);
2210
- 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");
2211
- }
2212
- function isVisible(track, t) {
2213
- t = localTime(track, t);
2214
- const keyed = track.keys.filter((k) => k.visible !== void 0);
2215
- if (keyed.length === 0) return true;
2216
- let last;
2217
- for (const k of keyed) if (k.t <= t) last = k;
2218
- return last ? last.visible : !keyed[0].visible;
2219
- }
2220
- function applyTrack(track, e, t) {
2221
- e = cloneElement(e);
2222
- if (track.keys.some((k) => k.reveal !== void 0)) {
2223
- const r = num2(track, t, "reveal", e.reveal ?? 1);
2224
- e.reveal = r >= 1 ? null : Math.max(0, r);
2225
- }
2226
- if (track.keys.some((k) => k.opacity !== void 0)) {
2227
- const o = Math.min(Math.max(num2(track, t, "opacity", e.style.opacity ?? 1), 0), 1);
2228
- e.style.opacity = o >= 1 ? null : o;
2229
- }
2230
- if (e.kind === "connector") return e;
2231
- e.x = num2(track, t, "x", e.x);
2232
- e.y = num2(track, t, "y", e.y);
2233
- e.width = Math.max(0, num2(track, t, "width", e.width));
2234
- e.height = Math.max(0, num2(track, t, "height", e.height));
2235
- if (track.keys.some((k) => k.rotation !== void 0)) e.rotation = num2(track, t, "rotation", angle(e));
2236
- e.style.strokeWidth = Math.max(0, num2(track, t, "strokeWidth", e.style.strokeWidth));
2237
- e.style.stroke = value(track, t, (k) => k.stroke, e.style.stroke, lerpColor);
2238
- e.style.fill = value(track, t, (k) => k.fill, e.style.fill, lerpColor);
2239
- return e;
2240
- }
2241
- function followSpan(track, t) {
2242
- t = localTime(track, t);
2243
- let f = null;
2244
- for (const s of track.follow ?? []) if (s.start <= t && (f === null || s.start > f.start)) f = s;
2245
- if (!f) return null;
2246
- const span = f;
2247
- if (track.keys.some((k) => k.t >= span.end && k.t <= t && (k.x !== void 0 || k.y !== void 0))) return null;
2248
- return f;
2249
- }
2250
- function tracing(e, page) {
2251
- if (e.kind === "connector") return new Polyline(routePoints(route(e, indexOf(page))));
2252
- const clean = cloneElement(e);
2253
- clean.style.roughness = 0;
2254
- const pts = outline(clean).contours()[0] ?? [];
2255
- return new Polyline(angle(e) === 0 ? pts : pts.map((p) => rotated(e, p)));
2256
- }
2257
- function following(track, e, t, page) {
2258
- const f = followSpan(track, t);
2259
- if (!f) return e;
2260
- t = localTime(track, t);
2261
- const pathElement = page.elements.find((x) => x.id === f.path);
2262
- if (!pathElement || pathElement.id === e.id) return e;
2263
- const line2 = tracing(pathElement, page);
2264
- const span = f.end - f.start;
2265
- 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);
2266
- if (u > 1) u = rem(u, 1) === 0 ? 1 : rem(u, 1);
2267
- if (f.reverse === true) u = 1 - u;
2268
- const p = line2.point(u);
2269
- const moved = cloneElement(e);
2270
- moved.x = p.x - e.width / 2;
2271
- moved.y = p.y - e.height / 2;
2272
- if (f.orient === true) {
2273
- const step = f.reverse === true ? -0.01 : 0.01;
2274
- const a = line2.point(u - step / 2), b = line2.point(u + step / 2);
2275
- if (a.x !== b.x || a.y !== b.y) moved.rotation = Math.atan2(b.y - a.y, b.x - a.x);
2276
- }
2277
- return moved;
2278
- }
2279
- function marchOffset(tl, speed, period, t) {
2280
- if (!(period > 0)) return 0;
2281
- const d = clampTime(tl.duration);
2282
- if (d > 0) {
2283
- const laps = roundHalfAway(speed * d / period);
2284
- if (laps !== 0) speed = laps * period / d;
2285
- }
2286
- const r = rem(speed * t, period);
2287
- return r < 0 ? -r : r === 0 ? 0 : period - r;
2288
- }
2289
- function trackMap(tl) {
2290
- const m = /* @__PURE__ */ new Map();
2291
- for (const t of tl.tracks) if (!m.has(t.element)) m.set(t.element, t);
2292
- return m;
2293
- }
2294
- function resolved(page, t) {
2295
- const timeline = page.timeline;
2296
- if (!timeline) return page;
2297
- t = clampTime(t);
2298
- const tracks = trackMap(timeline);
2299
- const reseed = timeline.boil === null ? null : Math.trunc(Math.floor(t * clampFPS(timeline.fps) + 1e-9) / Math.max(1, timeline.boil)) % BOIL_DRAWINGS >>> 0;
2300
- const hidden = /* @__PURE__ */ new Set();
2301
- for (const tr of tracks.values()) if (!isVisible(tr, t)) hidden.add(tr.element);
2302
- let moving = null;
2303
- if (timeline.settle === true) {
2304
- moving = /* @__PURE__ */ new Set();
2305
- for (const tr of tracks.values()) if (isChanging(tr, t)) moving.add(tr.element);
2306
- }
2307
- const boils = (e) => {
2308
- const override = tracks.get(e.id)?.boil;
2309
- if (override !== void 0 && override !== null) return override;
2310
- if (!moving) return true;
2311
- if (e.connector) return [e.connector.start.elementID, e.connector.end.elementID].some((id) => id !== null && moving.has(id));
2312
- return moving.has(e.id);
2313
- };
2314
- let elements = page.elements.map((e) => {
2315
- const track = tracks.get(e.id);
2316
- let out = track ? applyTrack(track, e, t) : e;
2317
- if (reseed !== null && reseed > 0 && boils(e)) {
2318
- if (out === e) out = cloneElement(e);
2319
- out.seed = boiled(e.seed, reseed);
2320
- }
2321
- const pattern = track?.march != null ? dashPattern(out.style.dash, out.style.strokeWidth) : null;
2322
- if (track?.march != null && pattern) {
2323
- if (out === e) out = cloneElement(e);
2324
- out.style.dashOffset = marchOffset(timeline, track.march, pattern.reduce((a, b) => a + b, 0), t);
2325
- }
2326
- return out;
2327
- });
2328
- elements = elements.map((e, i) => hidden.has(e.id) ? page.elements[i] : e);
2329
- const posed = { ...page, elements, timeline: null };
2330
- elements = elements.map((e) => {
2331
- const track = tracks.get(e.id);
2332
- return track ? following(track, e, t, posed) : e;
2333
- });
2334
- const current = { ...page, elements, timeline: null };
2335
- const index = indexOf(current);
2336
- const gone = new Set(hidden);
2337
- elements = elements.map((e) => {
2338
- const c = e.connector;
2339
- if (!c || ![c.start.elementID, c.end.elementID].some((id) => id !== null && hidden.has(id))) return e;
2340
- if (!(tracks.get(e.id)?.keys.some((k) => k.visible !== void 0) ?? false)) {
2341
- gone.add(e.id);
2342
- return e;
2546
+ function layoutLines(paras, width, height, fontSize) {
2547
+ const lines = [];
2548
+ let baseline = 0;
2549
+ let prev = null;
2550
+ paras.forEach((p, pi) => {
2551
+ 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;
2552
+ const chars = [];
2553
+ runs.forEach((r, ri) => {
2554
+ for (const ch of r.text) chars.push({ ch, run: ri });
2555
+ });
2556
+ let k = 0;
2557
+ let firstLine = true;
2558
+ while (k < chars.length || firstLine && chars.length === 0) {
2559
+ const start = firstLine ? p.firstIndent : p.indent;
2560
+ const maxX2 = width;
2561
+ const glyphs = [];
2562
+ let x = start, lastSpace = -1, end = k;
2563
+ for (; end < chars.length; end++) {
2564
+ const { ch, run } = chars[end];
2565
+ const r = runs[run];
2566
+ if (ch === SEP || ch === "\n") {
2567
+ glyphs.push({ ch, run, x, w: 0 });
2568
+ end++;
2569
+ break;
2570
+ }
2571
+ if (ch === " ") {
2572
+ const stop = p.tabs.find((t) => t.x > x + 0.01);
2573
+ let tx;
2574
+ if (stop) {
2575
+ if (stop.align === "left") tx = stop.x;
2576
+ else {
2577
+ let seg = 0, prevCh = "";
2578
+ for (let q = end + 1; q < chars.length && chars[q].ch !== " " && chars[q].ch !== "\n" && chars[q].ch !== SEP; q++) {
2579
+ const rr = runs[chars[q].run];
2580
+ seg += charWidth(chars[q].ch, rr) + (prevCh ? kern(prevCh, chars[q].ch, rr) : 0);
2581
+ prevCh = chars[q].ch;
2582
+ }
2583
+ tx = Math.max(x, stop.align === "right" ? stop.x - seg : stop.x - seg / 2);
2584
+ }
2585
+ } else tx = (Math.floor(x / TAB_INTERVAL) + 1) * TAB_INTERVAL;
2586
+ glyphs.push({ ch, run, x, w: tx - x });
2587
+ x = tx;
2588
+ continue;
2589
+ }
2590
+ const prevGlyph = glyphs[glyphs.length - 1];
2591
+ const kx = prevGlyph && prevGlyph.run === run && prevGlyph.ch !== " " ? kern(prevGlyph.ch, ch, r) : 0;
2592
+ if (prevGlyph) {
2593
+ prevGlyph.w += kx;
2594
+ x += kx;
2595
+ }
2596
+ const w = charWidth(ch, r);
2597
+ if (ch !== " " && x + w > maxX2 + 0.01 && glyphs.some((g) => g.ch !== " ")) {
2598
+ if (lastSpace >= 0) {
2599
+ glyphs.length = lastSpace + 1;
2600
+ end = k + lastSpace + 1;
2601
+ }
2602
+ if (prevGlyph) prevGlyph.w -= kx;
2603
+ break;
2604
+ }
2605
+ glyphs.push({ ch, run, x, w });
2606
+ x += w;
2607
+ if (breaksAfter(ch, chars[end + 1]?.ch ?? "")) lastSpace = glyphs.length - 1;
2608
+ }
2609
+ if (glyphs.length === 0 && end === k && k < chars.length) {
2610
+ const { ch, run } = chars[k];
2611
+ glyphs.push({ ch, run, x: start, w: charWidth(ch, runs[run]) });
2612
+ end = k + 1;
2613
+ }
2614
+ let asc = 0, desc = 0, lead = 0;
2615
+ for (const g of glyphs) {
2616
+ const [a, d, l] = metrics(g.ch, runs[g.run]);
2617
+ asc = Math.max(asc, a);
2618
+ desc = Math.max(desc, d);
2619
+ lead = Math.max(lead, l);
2620
+ }
2621
+ 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 });
2622
+ baseline = prev ? baseline + Math.round(prev.desc) + Math.round(prev.lead) + (firstLine ? p.spacing : 0) + Math.round(asc) : Math.round(asc);
2623
+ if (baseline + desc > height + 0.5) return;
2624
+ const line2 = { glyphs, asc, desc, lead, baseline, para: p, first: firstLine };
2625
+ line2.glyphs.forEach((g) => {
2626
+ g.r = runs[g.run];
2627
+ });
2628
+ lines.push(line2);
2629
+ prev = line2;
2630
+ firstLine = false;
2631
+ k = end;
2632
+ if (chars.length === 0) break;
2343
2633
  }
2344
- const [a, b] = endpoints(e, index);
2345
- const pinned = cloneElement(e);
2346
- pinned.connector.start = { elementID: null, port: c.start.port, x: a.x, y: a.y };
2347
- pinned.connector.end = { elementID: null, port: c.end.port, x: b.x, y: b.y };
2348
- return pinned;
2349
2634
  });
2350
- return { ...page, elements: elements.filter((e) => !gone.has(e.id)), timeline: null };
2351
- }
2352
- function posterTime(page) {
2353
- return page.timeline ? page.timeline.poster ?? page.timeline.duration : 0;
2354
- }
2355
- function still(page) {
2356
- return page.timeline ? resolved(page, posterTime(page)) : page;
2357
- }
2358
- function cameraRect(tl, t) {
2359
- const keys = tl.camera ?? [];
2360
- if (keys.length === 0) return null;
2361
- let before;
2362
- for (const k of keys) if (k.t <= t) before = k;
2363
- const after = keys.find((k) => k.t > t);
2364
- if (!before) return rectOf(keys[0]);
2365
- if (!after) return rectOf(before);
2366
- const u = applyEase(before.ease ?? "easeInOut", (t - before.t) / Math.max(after.t - before.t, 1e-9));
2367
- const mix2 = (p, q) => p + (q - p) * u;
2368
- 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)) };
2369
- }
2370
- var rectOf = (k) => ({ x: k.x, y: k.y, width: k.width, height: k.height });
2371
- function cameraView(tl, t) {
2372
- const first = tl.camera?.[0];
2373
- const r = cameraRect(tl, t);
2374
- if (!first || !r || !(r.width > 0 && r.height > 0)) return null;
2375
- return fit(r, first.width / first.height);
2376
- }
2377
- function fit(r, aspect) {
2378
- if (!(r.width > 0 && r.height > 0 && aspect > 0)) return r;
2379
- let w = r.width, h = r.height;
2380
- if (r.width / r.height < aspect) w = r.height * aspect;
2381
- else h = r.width / aspect;
2382
- return { x: r.x + r.width / 2 - w / 2, y: r.y + r.height / 2 - h / 2, width: w, height: h };
2635
+ return lines;
2383
2636
  }
2384
- function frameCount(tl) {
2385
- return Math.max(1, Math.ceil(clampTime(tl.duration) * clampFPS(tl.fps)));
2637
+ function noteMarkup(source, e) {
2638
+ const rect2 = textRect({ x: e.x, y: e.y, width: e.width, height: e.height });
2639
+ if (!(rect2.width > 1 && rect2.height > 1)) return "";
2640
+ const b = new Builder(e.style.fontSize, rect2.width);
2641
+ for (const block of parseBlocks(source)) b.block(block, { indent: 0, marker: null, quote: null, listDepth: 0 });
2642
+ const lines = layoutLines(b.paras, rect2.width, rect2.height, e.style.fontSize);
2643
+ const ink = color(e.style.stroke);
2644
+ let s = "";
2645
+ const marks = [];
2646
+ for (const l of lines) {
2647
+ const p = l.para;
2648
+ 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]);
2649
+ if (p.rule !== null) marks.push([rect2.x + p.rule, rect2.y + l.baseline - 3, rect2.x + rect2.width, rect2.y + l.baseline - 3]);
2650
+ if (p.tableRule) {
2651
+ const y = rect2.y + l.baseline + l.desc + 2;
2652
+ marks.push([rect2.x + p.tableRule[0], y, Math.min(rect2.x + rect2.width, rect2.x + p.tableRule[0] + p.tableRule[1]), y]);
2653
+ }
2654
+ let spans = "";
2655
+ let i = 0;
2656
+ const gs = l.glyphs;
2657
+ while (i < gs.length) {
2658
+ const r = gs[i].r;
2659
+ let j = i;
2660
+ let text = "";
2661
+ const font = fontOf(gs[i].ch);
2662
+ while (j < gs.length && sameStyle(gs[j].r, r) && gs[j].ch !== " " && fontOf(gs[j].ch) === font) {
2663
+ text += gs[j].ch;
2664
+ j++;
2665
+ }
2666
+ const shown = text.replaceAll("\n", "").replaceAll(SEP, "");
2667
+ if (shown.trim() !== "") {
2668
+ let a = `font-size="${fmt(r.size)}"`;
2669
+ if (r.mono) a += ` font-family="Menlo, Consolas, monospace"`;
2670
+ if (r.bold) a += ` font-weight="bold"`;
2671
+ if (r.italic) a += ` font-style="italic"`;
2672
+ if (r.underline) a += ` text-decoration="underline"`;
2673
+ if (r.alpha < 1) a += ` fill-opacity="${fmt(r.alpha * e.style.stroke.a)}"`;
2674
+ spans += `<tspan x="${fmt(rect2.x + gs[i].x)}" ${a}>${escapeXML(shown)}</tspan>`;
2675
+ }
2676
+ if (r.strike && j > i) {
2677
+ const y = l.baseline - metrics(gs[i].ch, r)[0] * 0.32;
2678
+ marks.push([rect2.x + gs[i].x, rect2.y + y, rect2.x + gs[j - 1].x + gs[j - 1].w, rect2.y + y]);
2679
+ }
2680
+ if (j === i) j++;
2681
+ i = j;
2682
+ }
2683
+ 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>
2684
+ `;
2685
+ }
2686
+ const width = fmt(Math.max(1, e.style.fontSize / 14));
2687
+ for (const [x1, y1, x2, y2] of marks) {
2688
+ s += `<line x1="${fmt(x1)}" y1="${fmt(y1)}" x2="${fmt(x2)}" y2="${fmt(y2)}" stroke="${ink}" stroke-width="${width}"/>
2689
+ `;
2690
+ }
2691
+ return s;
2386
2692
  }
2387
- function frameTimes(tl) {
2388
- const fps = clampFPS(tl.fps);
2389
- const times = [];
2390
- for (let i = 0; i < frameCount(tl); i++) times.push(i / fps);
2391
- const last = times[times.length - 1];
2392
- if (tl.duration - last > 1e-6) times.push(tl.duration);
2393
- return times;
2693
+ function fontOf(ch) {
2694
+ return fallbackMetrics[ch] ? `fb:${ch}` : "";
2394
2695
  }
2395
- function sceneAt(tl, t) {
2396
- let s = -1;
2397
- (tl.markers ?? []).forEach((m, i) => {
2398
- if (m.t <= t) s = i;
2399
- });
2400
- return s;
2696
+ function sameStyle(a, b) {
2697
+ 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;
2401
2698
  }
2402
- function transientElements(page) {
2403
- const tl = page.timeline;
2404
- if (!tl) return /* @__PURE__ */ new Set();
2405
- const end = tl.poster ?? tl.duration;
2406
- return new Set(tl.tracks.filter((tr) => !isVisible(tr, 0) && !isVisible(tr, end)).map((tr) => tr.element));
2699
+
2700
+ // src/theme.ts
2701
+ var rgb = (r, g, b) => ({ r, g, b, a: 1 });
2702
+ var themes = [
2703
+ { id: "paper", name: "Paper", background: rgb(0.984, 0.98, 0.965), ink: rgb(0.12, 0.12, 0.13) },
2704
+ { id: "white", name: "White", background: rgb(1, 1, 1), ink: rgb(0.12, 0.12, 0.13) },
2705
+ { id: "sepia", name: "Sepia", background: rgb(0.953, 0.914, 0.824), ink: rgb(0.23, 0.18, 0.13) },
2706
+ { id: "blueprint", name: "Blueprint", background: rgb(0.106, 0.227, 0.361), ink: rgb(0.91, 0.945, 0.984) },
2707
+ { id: "chalkboard", name: "Chalkboard", background: rgb(0.169, 0.227, 0.2), ink: rgb(0.945, 0.945, 0.918) },
2708
+ { id: "midnight", name: "Midnight", background: rgb(0.082, 0.086, 0.102), ink: rgb(0.925, 0.925, 0.94) }
2709
+ ];
2710
+ function themeNamed(id) {
2711
+ return themes.find((t) => t.id === id) ?? null;
2407
2712
  }
2408
2713
 
2409
2714
  // src/svg.ts
2410
2715
  function bounds(page, padding = 24) {
2716
+ if (hasInstances(page)) return bounds(expandingSymbols(page, 0), padding);
2411
2717
  const index = indexOf(page);
2412
2718
  const box = (e) => {
2413
2719
  if (e.kind === "connector") return connectorBounds(e, index);
@@ -2449,6 +2755,7 @@ function svgOpen(b, background, paint) {
2449
2755
  return out;
2450
2756
  }
2451
2757
  function svgParts(page, background, fixed) {
2758
+ if (hasInstances(page)) return svgParts(expandingSymbols(page, 0), background, fixed);
2452
2759
  const theme = themeNamed(page.theme);
2453
2760
  const bg = background ? theme?.background ?? background : null;
2454
2761
  const ink = theme?.ink ?? black;
@@ -2629,8 +2936,9 @@ input[type=range] { flex: 1; min-width: 60px; accent-color: currentColor; }
2629
2936
  output { font-variant-numeric: tabular-nums; min-width: 4.5em; text-align: right; }
2630
2937
  .scene { min-width: 0; flex: 0 1 auto; text-align: center; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
2631
2938
  .error { padding: 16px; font-family: ui-monospace, Menlo, monospace; white-space: pre-wrap; }
2632
- `;
2939
+ ${CLICK_STYLE}`;
2633
2940
  var Playback = class {
2941
+ /** `file` is the drawing `page` belongs to, so links to its other pages are clickable. */
2634
2942
  constructor(page, into, options = {}) {
2635
2943
  __publicField(this, "page");
2636
2944
  __publicField(this, "plan");
@@ -2638,6 +2946,22 @@ var Playback = class {
2638
2946
  __publicField(this, "sceneNames");
2639
2947
  __publicField(this, "background");
2640
2948
  __publicField(this, "ink");
2949
+ /** What clicking each clickable element does, by element id. */
2950
+ __publicField(this, "clicks");
2951
+ /** Copies of symbols that play through once when clicked (playback "click"), by id, with their clip's length. */
2952
+ __publicField(this, "replays", /* @__PURE__ */ new Map());
2953
+ /** When each replaying copy was clicked (performance.now() milliseconds). */
2954
+ __publicField(this, "replayStarts", /* @__PURE__ */ new Map());
2955
+ __publicField(this, "hoverIDs");
2956
+ __publicField(this, "labels", /* @__PURE__ */ new Map());
2957
+ /** Each drawn part of a clickable copy, to that copy's id; and each copy's parts in drawing order. */
2958
+ __publicField(this, "owner", /* @__PURE__ */ new Map());
2959
+ __publicField(this, "partsOfOwner", /* @__PURE__ */ new Map());
2960
+ /** The moments of the button copies' over, down and hit states. */
2961
+ __publicField(this, "overTimes", /* @__PURE__ */ new Map());
2962
+ __publicField(this, "downTimes", /* @__PURE__ */ new Map());
2963
+ __publicField(this, "hitTimes", /* @__PURE__ */ new Map());
2964
+ __publicField(this, "last", { t: 0, i: 0 });
2641
2965
  __publicField(this, "svg");
2642
2966
  __publicField(this, "groups", /* @__PURE__ */ new Map());
2643
2967
  __publicField(this, "order", []);
@@ -2645,6 +2969,30 @@ var Playback = class {
2645
2969
  __publicField(this, "onFrame", null);
2646
2970
  this.page = options.clean ? withRoughness(page, 0) : page;
2647
2971
  this.plan = plan(this.page);
2972
+ this.clicks = clickTargets(page, options.file);
2973
+ const table = symbolTable(this.page);
2974
+ for (const e of this.page.elements) {
2975
+ const s = e.kind === "symbol" && e.symbol !== null ? table.get(e.symbol) : void 0;
2976
+ if (!s) continue;
2977
+ if (s.button === true) {
2978
+ for (const [state, times] of [["over", this.overTimes], ["down", this.downTimes], ["hit", this.hitTimes]]) {
2979
+ const at = buttonTime(s, state);
2980
+ if (at !== null) times.set(e.id, at);
2981
+ }
2982
+ } else if (e.playback === "click" && (s.timeline?.duration ?? 0) > 0) {
2983
+ this.replays.set(e.id, s.timeline.duration);
2984
+ }
2985
+ }
2986
+ const clickable2 = /* @__PURE__ */ new Set([...this.clicks.keys(), ...this.replays.keys()]);
2987
+ this.hoverIDs = new Set(this.page.elements.filter((e) => clickable2.has(e.id) && e.kind !== "symbol" && hasHover(e)).map((e) => e.id));
2988
+ const instances = new Set(this.page.elements.filter((e) => clickable2.has(e.id) && e.kind === "symbol").map((e) => e.id));
2989
+ for (const e of this.page.elements) {
2990
+ if (!clickable2.has(e.id)) continue;
2991
+ this.labels.set(e.id, buttonLabel(e, e.symbol !== null ? table.get(e.symbol)?.name : void 0));
2992
+ if (e.kind !== "symbol") this.owner.set(e.id, e.id);
2993
+ }
2994
+ for (const [part, id] of partsOf(this.page, instances)) this.owner.set(part, id);
2995
+ for (const [part, id] of this.owner) this.partsOfOwner.set(id, [...this.partsOfOwner.get(id) ?? [], part]);
2648
2996
  const near = (t) => {
2649
2997
  let best = 0;
2650
2998
  this.plan.times.forEach((x, i) => {
@@ -2683,24 +3031,73 @@ var Playback = class {
2683
3031
  this.frame = i;
2684
3032
  this.onFrame?.(i);
2685
3033
  }
3034
+ /** Starts copy `id` replaying from `at` (performance.now() milliseconds; earlier to start partway). */
3035
+ replay(id, at) {
3036
+ if (this.replays.has(id)) this.replayStarts.set(id, at);
3037
+ }
3038
+ /** Whether a copy is still playing through after a click. */
3039
+ replaying(now = performance.now()) {
3040
+ for (const [id, start] of this.replayStarts) if (now - start < (this.replays.get(id) ?? 0) * 1e3) return true;
3041
+ return false;
3042
+ }
3043
+ /** Draws the last moment drawn again, for copies replaying while the page stands still. */
3044
+ redraw() {
3045
+ this.drawAt(this.last.t, this.last.i);
3046
+ }
2686
3047
  /** Draws the moment `t` (seconds) in the frame-`i` view. */
2687
3048
  drawAt(t, i = 0) {
3049
+ this.last = { t, i };
2688
3050
  const r = this.plan.rects[Math.min(i, this.plan.rects.length - 1)];
2689
- const { parts } = svgParts(resolved(this.page, t), this.background, r);
3051
+ const now = typeof performance === "undefined" ? 0 : performance.now();
3052
+ const times = /* @__PURE__ */ new Map();
3053
+ for (const [id, start] of this.replayStarts) times.set(id, Math.min((now - start) / 1e3, this.replays.get(id) ?? 0));
3054
+ const tc = clampTime(t);
3055
+ const pose = posed(this.page, tc);
3056
+ const frame2 = expandingSymbols(pose, tc, true, times);
3057
+ const { parts } = svgParts(frame2, this.background, r);
3058
+ const owned = (m, parts2) => {
3059
+ for (const p of parts2) if (this.owner.has(p.key)) m.set(p.key, p.markup);
3060
+ return m;
3061
+ };
3062
+ 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) ?? "")));
3063
+ 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)));
3064
+ const over = state(this.overTimes), down = state(this.downTimes), hit = state(this.hitTimes);
3065
+ const normal = owned(/* @__PURE__ */ new Map(), parts);
3066
+ const merged = [];
3067
+ const done = /* @__PURE__ */ new Set();
3068
+ for (const p of parts) {
3069
+ const o = this.owner.get(p.key);
3070
+ if (o === void 0) {
3071
+ merged.push(p);
3072
+ continue;
3073
+ }
3074
+ if (done.has(o)) continue;
3075
+ done.add(o);
3076
+ merged.push(this.buttonPart(o, normal, hover, over, down, hit, pose));
3077
+ }
2690
3078
  this.svg.setAttribute("viewBox", `${fmt(r.x)} ${fmt(r.y)} ${fmt(r.width)} ${fmt(r.height)}`);
2691
3079
  const ns = "http://www.w3.org/2000/svg";
2692
- const keys = parts.map((p) => p.key);
2693
- for (const p of parts) {
3080
+ const keys = merged.map((p) => p.key);
3081
+ for (const p of merged) {
3082
+ const markup = p.markup;
2694
3083
  const have = this.groups.get(p.key);
2695
3084
  if (have) {
2696
- if (have.markup !== p.markup) {
2697
- have.g.innerHTML = p.markup;
2698
- have.markup = p.markup;
3085
+ if (have.markup !== markup) {
3086
+ have.g.innerHTML = markup;
3087
+ have.markup = markup;
2699
3088
  }
3089
+ if (p.button !== void 0) have.g.classList.toggle("dottie-b", p.button);
2700
3090
  } else {
2701
3091
  const g = document.createElementNS(ns, "g");
2702
- g.innerHTML = p.markup;
2703
- this.groups.set(p.key, { g, markup: p.markup });
3092
+ g.innerHTML = markup;
3093
+ if (p.button !== void 0) {
3094
+ g.setAttribute("class", p.button ? "dottie-a dottie-b" : "dottie-a");
3095
+ g.setAttribute("data-a", p.key);
3096
+ g.setAttribute("tabindex", "0");
3097
+ g.setAttribute("role", "button");
3098
+ g.setAttribute("aria-label", this.labels.get(p.key) ?? "");
3099
+ }
3100
+ this.groups.set(p.key, { g, markup });
2704
3101
  }
2705
3102
  }
2706
3103
  const live = new Set(keys);
@@ -2709,10 +3106,66 @@ var Playback = class {
2709
3106
  this.groups.delete(k);
2710
3107
  }
2711
3108
  if (keys.length !== this.order.length || keys.some((k, j) => k !== this.order[j])) {
2712
- this.svg.replaceChildren(...keys.map((k) => this.groups.get(k).g));
3109
+ let at = this.svg.firstChild;
3110
+ for (const k of keys) {
3111
+ const g = this.groups.get(k).g;
3112
+ if (g === at) at = at.nextSibling;
3113
+ else this.svg.insertBefore(g, at);
3114
+ }
3115
+ while (at) {
3116
+ const next = at.nextSibling;
3117
+ at.remove();
3118
+ at = next;
3119
+ }
2713
3120
  this.order = keys;
2714
3121
  }
2715
3122
  }
3123
+ /** What a clickable owner draws as: its normal look, and its hover, over and down looks where they differ. A
3124
+ * symbol's copy keeps its drawing out of the pointer's way and takes clicks on a layer above: its hit state drawn
3125
+ * invisibly, or its frame. `button` is whether it has a down look (the stylesheet nudges the others). */
3126
+ buttonPart(o, normal, hover, over, down, hit, pose) {
3127
+ const keys = this.partsOfOwner.get(o) ?? [o];
3128
+ const join = (m2) => {
3129
+ const s = keys.map((k) => m2.get(k) ?? "").join("");
3130
+ return s === "" ? null : s;
3131
+ };
3132
+ const base = join(normal) ?? "";
3133
+ const instance = pose.elements.find((e) => e.id === o && e.kind === "symbol");
3134
+ const overLook = instance ? join(over) : join(hover);
3135
+ const downLook = instance ? join(down) : null;
3136
+ const shownOver = overLook !== null && overLook !== base ? overLook : null;
3137
+ const shownDown = downLook !== null && downLook !== (overLook ?? base) ? downLook : null;
3138
+ if (!instance && shownOver === null) return { key: o, markup: base, button: false };
3139
+ const still2 = instance ? ` pointer-events="none"` : "";
3140
+ let m = `<g class="dottie-n"${still2}>
3141
+ ${base}</g>
3142
+ `;
3143
+ if (shownOver !== null) m += `<g class="dottie-h"${still2}>
3144
+ ${shownOver}</g>
3145
+ `;
3146
+ if (shownDown !== null) m += `<g class="dottie-d"${still2}>
3147
+ ${shownDown}</g>
3148
+ `;
3149
+ if (instance) {
3150
+ const h = join(hit);
3151
+ if (h !== null) m += `<g class="dottie-hit" opacity="0" pointer-events="all">
3152
+ ${h}</g>
3153
+ `;
3154
+ else {
3155
+ const c = { x: instance.x + instance.width / 2, y: instance.y + instance.height / 2 }, a = instance.rotation ?? 0;
3156
+ const corners2 = [
3157
+ [instance.x, instance.y],
3158
+ [instance.x + instance.width, instance.y],
3159
+ [instance.x + instance.width, instance.y + instance.height],
3160
+ [instance.x, instance.y + instance.height]
3161
+ ].map(([x, y]) => rotate({ x, y }, c, a));
3162
+ const path = new Path().moveTo(corners2[0]).lineTo(corners2[1]).lineTo(corners2[2]).lineTo(corners2[3]).close();
3163
+ m += `<path class="dottie-hit" d="${path.data()}" fill="none" stroke="none" pointer-events="all"/>
3164
+ `;
3165
+ }
3166
+ }
3167
+ return { key: o, markup: m, button: shownDown !== null };
3168
+ }
2716
3169
  /** The scene frame `i` is in, or -1 before the first. */
2717
3170
  sceneAt(i) {
2718
3171
  let s = -1;
@@ -2742,6 +3195,7 @@ var DottiePlayer = class extends ElementBase {
2742
3195
  __publicField(this, "last", 0);
2743
3196
  __publicField(this, "acc", 0);
2744
3197
  __publicField(this, "raf", 0);
3198
+ __publicField(this, "replayRaf", 0);
2745
3199
  __publicField(this, "loadToken", 0);
2746
3200
  __publicField(this, "visible", true);
2747
3201
  __publicField(this, "observer", null);
@@ -2775,6 +3229,10 @@ var DottiePlayer = class extends ElementBase {
2775
3229
  };
2776
3230
  this.next.onclick = () => this.nextScene();
2777
3231
  this.prev.onclick = () => this.prevScene();
3232
+ this.stage.addEventListener("click", (e) => {
3233
+ const b = clickable(e.target);
3234
+ if (b) this.activate(b.getAttribute("data-a") ?? "");
3235
+ });
2778
3236
  this.addEventListener("keydown", (e) => this.key(e));
2779
3237
  }
2780
3238
  connectedCallback() {
@@ -2836,7 +3294,7 @@ var DottiePlayer = class extends ElementBase {
2836
3294
  mount() {
2837
3295
  const file = this.file;
2838
3296
  const page = file.pages[pageIndex(file, this.getAttribute("page"))];
2839
- const p = new Playback(page, this.stage, { clean: this.hasAttribute("clean") });
3297
+ const p = new Playback(page, this.stage, { clean: this.hasAttribute("clean"), file });
2840
3298
  this.playback = p;
2841
3299
  this.style.setProperty("--dottie-page", color(p.background));
2842
3300
  this.style.setProperty("--dottie-ink", color(p.ink));
@@ -2911,6 +3369,50 @@ var DottiePlayer = class extends ElementBase {
2911
3369
  this.setPlaying(false);
2912
3370
  p.show(s >= 0 ? p.scenes[s] : 0);
2913
3371
  }
3372
+ /** Does what clicking element `id` does: jumps to its scene and plays on to the next (with reduced motion, or in a
3373
+ * still, straight to where that stops), opens its link in a new tab, or shows its page. */
3374
+ activate(id) {
3375
+ const p = this.playback;
3376
+ const click = p?.clicks.get(id);
3377
+ const replay = p?.replays.has(id) ?? false;
3378
+ if (!p || !click && !replay) return;
3379
+ const detail = { element: id, ...click ?? {}, ...replay ? { replay: true } : {} };
3380
+ if (!this.dispatchEvent(new CustomEvent("action", { detail, cancelable: true }))) return;
3381
+ const reduced = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
3382
+ if (replay) {
3383
+ const now = performance.now();
3384
+ p.replay(id, reduced ? now - p.replays.get(id) * 1e3 : now);
3385
+ p.redraw();
3386
+ this.scheduleReplay();
3387
+ }
3388
+ if (!click) return;
3389
+ if ("url" in click) {
3390
+ if (/^(https?:|mailto:)/i.test(click.url)) window.open(click.url, "_blank", "noopener");
3391
+ } else if ("page" in click) {
3392
+ this.setAttribute("page", String(click.page + 1));
3393
+ } else if (click.scene >= 0 && click.scene < p.scenes.length) {
3394
+ if (reduced || this.hasAttribute("at") || p.frameCount < 2) {
3395
+ this.setPlaying(false);
3396
+ p.show(click.scene + 1 < p.scenes.length ? p.scenes[click.scene + 1] : p.frameCount - 1);
3397
+ } else {
3398
+ p.show(p.scenes[click.scene]);
3399
+ this.setPlaying(true);
3400
+ }
3401
+ }
3402
+ }
3403
+ /** Redraws, while the page itself stands still, as long as a clicked copy is playing through. */
3404
+ scheduleReplay() {
3405
+ if (this.replayRaf) return;
3406
+ const step = () => {
3407
+ this.replayRaf = 0;
3408
+ const p = this.playback;
3409
+ if (!p || !this.isConnected) return;
3410
+ if (!this.playing) p.redraw();
3411
+ if (p.replaying()) this.replayRaf = requestAnimationFrame(step);
3412
+ else if (!this.playing) p.redraw();
3413
+ };
3414
+ this.replayRaf = requestAnimationFrame(step);
3415
+ }
2914
3416
  setPlaying(on) {
2915
3417
  const p = this.playback;
2916
3418
  this.playing = on && !!p && p.frameCount > 1 && p.page.timeline !== null;
@@ -2952,8 +3454,15 @@ var DottiePlayer = class extends ElementBase {
2952
3454
  }
2953
3455
  key(e) {
2954
3456
  const p = this.playback;
3457
+ const target = e.composedPath()[0];
3458
+ const b = clickable(target);
3459
+ if (p && b && (e.key === "Enter" || e.key === " ")) {
3460
+ e.preventDefault();
3461
+ this.activate(b.getAttribute("data-a") ?? "");
3462
+ return;
3463
+ }
2955
3464
  if (!p || p.page.timeline === null || p.frameCount <= 1 || this.hasAttribute("at")) return;
2956
- if (e.target === this.scrub && e.key !== " ") return;
3465
+ if (target === this.scrub && e.key !== " ") return;
2957
3466
  const scenes = p.scenes.length > 0;
2958
3467
  let handled = true;
2959
3468
  if (e.key === " ") scenes ? this.playing ? this.setPlaying(false) : this.nextScene() : this.setPlaying(!this.playing);
@@ -2980,6 +3489,10 @@ var DottiePlayer = class extends ElementBase {
2980
3489
  }
2981
3490
  };
2982
3491
  __publicField(DottiePlayer, "observedAttributes", ["src", "page", "at", "clean", "no-controls", "label"]);
3492
+ function clickable(target) {
3493
+ const el = target;
3494
+ return el && typeof el.closest === "function" ? el.closest(".dottie-a") : null;
3495
+ }
2983
3496
  function nearest(p, t) {
2984
3497
  let best = 0;
2985
3498
  p.plan.times.forEach((x, i) => {
@@ -3079,7 +3592,7 @@ function render(source, options = {}) {
3079
3592
  function isFile(v) {
3080
3593
  return typeof v === "object" && v !== null && Array.isArray(v.pages) && Array.isArray(v.warnings);
3081
3594
  }
3082
- var VERSION = "0.3.0";
3595
+ var VERSION = "0.5.1";
3083
3596
 
3084
3597
  // src/player-entry.ts
3085
3598
  definePlayer();
@@ -3095,6 +3608,7 @@ export {
3095
3608
  Playback,
3096
3609
  SUPPORTED_VERSION,
3097
3610
  VERSION,
3611
+ allowedURL,
3098
3612
  black,
3099
3613
  bounds,
3100
3614
  cameraView,