vizmatic 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/PROMPT.md +75 -525
- package/README.md +87 -578
- package/ROADMAP.md +49 -67
- package/SCIENCE.md +250 -0
- package/assets/THIRD_PARTY_LICENSES.md +40 -1
- package/assets/fonts/InstrumentSerif-Italic.ttf +0 -0
- package/assets/fonts/InstrumentSerif-Regular.ttf +0 -0
- package/assets/licenses/raster-codecs-MIT.txt +74 -0
- package/dist/chunk-3F2VQ7NW.js +1 -0
- package/dist/chunk-4XE64I2U.js +755 -0
- package/dist/chunk-JHHPLALU.js +79 -0
- package/dist/chunk-LTX44BAE.js +112 -0
- package/dist/chunk-MLKGABMK.js +9 -0
- package/dist/chunk-PSBF4TEK.js +1 -0
- package/dist/chunk-RPAHMKAR.js +605 -0
- package/dist/chunk-RWF4VBUL.js +3617 -0
- package/dist/chunk-WHT6HRMX.js +4201 -0
- package/dist/cli.js +371 -10
- package/dist/index.cjs +11129 -2922
- package/dist/index.d.cts +1225 -369
- package/dist/index.d.ts +1225 -369
- package/dist/index.js +6220 -3015
- package/dist/math-LXB4FH4E.js +7 -0
- package/dist/math-QVCETQDQ.js +1 -0
- package/dist/scene-browser-assets.d.ts +38 -0
- package/dist/scene-browser-assets.js +53 -0
- package/dist/scene-browser-chunks/chunk-5MVLYGXM.js +70 -0
- package/dist/scene-browser-chunks/chunk-ASXKN3NU.js +1 -0
- package/dist/scene-browser-chunks/chunk-CM5Y5LZP.js +1 -0
- package/dist/scene-browser-chunks/chunk-DP7NB4EW.js +6 -0
- package/dist/scene-browser-chunks/emscripten-module.browser-M5AEKJGE.js +1 -0
- package/dist/scene-browser-chunks/ffi-FL6ORJU3.js +1 -0
- package/dist/scene-browser-chunks/module-ES6BEMUI-JD5S5VKR.js +1 -0
- package/dist/scene-browser-chunks/module-asyncify-2EFITU5U-HWPFJFJC.js +1 -0
- package/dist/scene-browser-chunks/scene-browser-layout-M3XSXMV4.js +27 -0
- package/dist/scene-browser.d.ts +117 -0
- package/dist/scene-browser.js +220 -0
- package/dist/scene-cli.d.ts +1 -0
- package/dist/scene-cli.js +4 -0
- package/dist/scene-runtime.cjs +995 -0
- package/dist/scene-runtime.d.cts +81 -0
- package/dist/scene-runtime.d.ts +81 -0
- package/dist/scene-runtime.js +22 -0
- package/dist/server-2FYHZ4UO.js +331 -0
- package/dist/stage-browser/browser.d.ts +2482 -0
- package/dist/stage-browser/chunk-SQE76S5B.js +1 -0
- package/dist/stage-browser/chunk-VZNL6JY2.js +59 -0
- package/dist/stage-browser/dagre.esm-N2NIVIAZ.js +6 -0
- package/dist/stage-browser/math-4WFNHVV2.js +18 -0
- package/dist/stage-browser/stage.js +19 -0
- package/dist/stage-export.cjs +4950 -0
- package/dist/stage-export.d.cts +112 -0
- package/dist/stage-export.d.ts +112 -0
- package/dist/stage-export.js +544 -0
- package/dist/stage.cjs +9745 -0
- package/dist/stage.d.cts +1678 -0
- package/dist/stage.d.ts +1678 -0
- package/dist/stage.js +5404 -0
- package/dist/svg-import-Djj2q7Pc.d.cts +742 -0
- package/dist/svg-import-Djj2q7Pc.d.ts +742 -0
- package/dist/video-CcHW1qhV.d.cts +532 -0
- package/dist/video-CcHW1qhV.d.ts +532 -0
- package/examples/README.md +18 -0
- package/examples/agent-pipeline.tsx +4 -4
- package/examples/animated-pipeline.tsx +1 -5
- package/examples/attention-head.tsx +12 -12
- package/examples/aurora-incident.tsx +4 -3
- package/examples/building-blocks.tsx +8 -9
- package/examples/catalog-charts.tsx +4 -4
- package/examples/catalog-content.tsx +7 -9
- package/examples/catalog-convolution-kernel.tsx +116 -0
- package/examples/catalog-data.tsx +2 -8
- package/examples/catalog-dataflow.tsx +5 -9
- package/examples/catalog-dense.tsx +80 -0
- package/examples/catalog-deployment.tsx +5 -11
- package/examples/catalog-diagrams.tsx +13 -16
- package/examples/catalog-drawing.tsx +7 -7
- package/examples/catalog-flows.tsx +4 -4
- package/examples/catalog-foundations.tsx +11 -11
- package/examples/catalog-gaussian-blur.tsx +146 -0
- package/examples/catalog-image-tensors.tsx +115 -0
- package/examples/catalog-ml.tsx +36 -31
- package/examples/catalog-numeric-matrices.tsx +93 -0
- package/examples/catalog-photo-blur.tsx +209 -0
- package/examples/catalog-science-ai.tsx +130 -0
- package/examples/catalog-science-chemistry.tsx +103 -0
- package/examples/catalog-science-math.tsx +93 -0
- package/examples/catalog-science-physics.tsx +84 -0
- package/examples/catalog-sequence.tsx +59 -38
- package/examples/data-pipeline-lineage-presentation.tsx +5 -0
- package/examples/data-pipeline-lineage.tsx +85 -0
- package/examples/daybreak-launch.tsx +1 -0
- package/examples/desired-quadrant.tsx +7 -8
- package/examples/engineering-dashboard-presentation.tsx +13 -0
- package/examples/engineering-dashboard.tsx +160 -0
- package/examples/engineering-flow.tsx +2 -4
- package/examples/eval-dashboard.tsx +6 -6
- package/examples/films/attention.ts +278 -0
- package/examples/films/cache-miss.ts +431 -0
- package/examples/films/catalog.json +132 -0
- package/examples/films/central-limit.ts +269 -0
- package/examples/films/complex-maps.ts +273 -0
- package/examples/films/deck-example.ts +224 -0
- package/examples/films/euler-circle.ts +179 -0
- package/examples/films/fourier-epicycles.ts +265 -0
- package/examples/films/gradient-descent.ts +345 -0
- package/examples/films/linear-maps.ts +175 -0
- package/examples/films/lorenz.ts +207 -0
- package/examples/films/pythagoras.ts +185 -0
- package/examples/films/taylor-series.ts +159 -0
- package/examples/films/vizmatic-title.ts +117 -0
- package/examples/llm-serving-stack-presentation.tsx +5 -0
- package/examples/llm-serving-stack.tsx +94 -0
- package/examples/model-garden.tsx +1 -0
- package/examples/model-router.tsx +6 -6
- package/examples/nccl-all-gather.tsx +3 -3
- package/examples/nccl-all-reduce.tsx +3 -3
- package/examples/nccl-all-to-all.tsx +1 -1
- package/examples/nccl-broadcast.tsx +1 -1
- package/examples/nccl-reduce-scatter.tsx +5 -5
- package/examples/orthogonal-routing.tsx +4 -4
- package/examples/pareto-frontier.tsx +9 -10
- package/examples/presentation-frame.tsx +6 -6
- package/examples/pressure-mechanism.scene.json +14 -0
- package/examples/production-system-map-presentation.tsx +5 -0
- package/examples/production-system-map.tsx +130 -0
- package/examples/rag-graph.tsx +7 -7
- package/examples/release-card.tsx +5 -5
- package/examples/spec-anatomy.tsx +5 -5
- package/examples/system-architecture.tsx +19 -32
- package/examples/theme-system.tsx +1 -1
- package/examples/token-matrix.tsx +4 -2
- package/examples/transparent-badge.tsx +1 -2
- package/examples/videos/README.md +62 -0
- package/examples/videos/calculus-tangent.tsx +101 -0
- package/examples/videos/catalyst-pathway.tsx +56 -0
- package/examples/videos/causal-attention.tsx +91 -0
- package/examples/videos/convolution-kernel.tsx +34 -0
- package/examples/videos/gaussian-blur.tsx +32 -0
- package/examples/videos/image-to-matrix.tsx +22 -0
- package/examples/videos/matrix-multiplication.tsx +128 -0
- package/examples/videos/photo-gaussian-blur.tsx +39 -0
- package/examples/videos/request-lifecycle.tsx +99 -0
- package/examples/videos/signal-to-story.tsx +126 -0
- package/examples/videos/wave-interference.tsx +80 -0
- package/examples/vizmatic-bare.d.ts +1 -0
- package/examples/vizmatic-hero.tsx +9 -18
- package/examples/website-workflow.tsx +7 -7
- package/examples/weekend-field-notes.tsx +3 -2
- package/package.json +77 -12
|
@@ -0,0 +1,4201 @@
|
|
|
1
|
+
// src/stage/look.ts
|
|
2
|
+
var midnight = {
|
|
3
|
+
name: "midnight",
|
|
4
|
+
dark: true,
|
|
5
|
+
background: "#0b0c10",
|
|
6
|
+
bloom: 0.2,
|
|
7
|
+
colors: {
|
|
8
|
+
bg: "#0b0c10",
|
|
9
|
+
surface: "#15171d",
|
|
10
|
+
ink: "#f2efe6",
|
|
11
|
+
dim: "#a5a7ad",
|
|
12
|
+
faint: "#5c5f68",
|
|
13
|
+
grid: "#23303d",
|
|
14
|
+
gridStrong: "#3d5266",
|
|
15
|
+
blue: "#5cc4f0",
|
|
16
|
+
teal: "#4fd8c4",
|
|
17
|
+
green: "#8fe08a",
|
|
18
|
+
yellow: "#ffd75e",
|
|
19
|
+
gold: "#f2b84b",
|
|
20
|
+
orange: "#ff9d57",
|
|
21
|
+
red: "#ff6b6b",
|
|
22
|
+
pink: "#ff8fc0",
|
|
23
|
+
purple: "#b49cff"
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var paper = {
|
|
27
|
+
name: "paper",
|
|
28
|
+
dark: false,
|
|
29
|
+
background: "#f7f3ea",
|
|
30
|
+
bloom: 0,
|
|
31
|
+
colors: {
|
|
32
|
+
bg: "#f7f3ea",
|
|
33
|
+
surface: "#efe9db",
|
|
34
|
+
ink: "#1d1b16",
|
|
35
|
+
dim: "#5e5a50",
|
|
36
|
+
faint: "#a29c8d",
|
|
37
|
+
grid: "#e2dbca",
|
|
38
|
+
gridStrong: "#c4bba6",
|
|
39
|
+
blue: "#1f6fb2",
|
|
40
|
+
teal: "#0d8577",
|
|
41
|
+
green: "#3f8a35",
|
|
42
|
+
yellow: "#b98500",
|
|
43
|
+
gold: "#a8740a",
|
|
44
|
+
orange: "#cf5d12",
|
|
45
|
+
red: "#c62f2f",
|
|
46
|
+
pink: "#c2387e",
|
|
47
|
+
purple: "#6a4bc4"
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var blueprint = {
|
|
51
|
+
name: "blueprint",
|
|
52
|
+
dark: true,
|
|
53
|
+
background: "#0b2340",
|
|
54
|
+
bloom: 0.16,
|
|
55
|
+
colors: {
|
|
56
|
+
bg: "#0b2340",
|
|
57
|
+
surface: "#123054",
|
|
58
|
+
ink: "#eaf6ff",
|
|
59
|
+
dim: "#9cc0dc",
|
|
60
|
+
faint: "#5d86a8",
|
|
61
|
+
grid: "#1a3f66",
|
|
62
|
+
gridStrong: "#2c5f8e",
|
|
63
|
+
blue: "#7fd4ff",
|
|
64
|
+
teal: "#6fe6d6",
|
|
65
|
+
green: "#9de8a4",
|
|
66
|
+
yellow: "#ffe27a",
|
|
67
|
+
gold: "#f5c566",
|
|
68
|
+
orange: "#ffb27a",
|
|
69
|
+
red: "#ff8d8d",
|
|
70
|
+
pink: "#ffa8cf",
|
|
71
|
+
purple: "#c5b4ff"
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var chalk = {
|
|
75
|
+
name: "chalk",
|
|
76
|
+
dark: true,
|
|
77
|
+
background: "#10201b",
|
|
78
|
+
bloom: 0.1,
|
|
79
|
+
colors: {
|
|
80
|
+
bg: "#10201b",
|
|
81
|
+
surface: "#172c25",
|
|
82
|
+
ink: "#f4f1e4",
|
|
83
|
+
dim: "#b1b9a9",
|
|
84
|
+
faint: "#67786d",
|
|
85
|
+
grid: "#20392f",
|
|
86
|
+
gridStrong: "#355547",
|
|
87
|
+
blue: "#8ccbe8",
|
|
88
|
+
teal: "#7dd9c1",
|
|
89
|
+
green: "#a9dc8f",
|
|
90
|
+
yellow: "#f6dd7c",
|
|
91
|
+
gold: "#e6bf62",
|
|
92
|
+
orange: "#f2a874",
|
|
93
|
+
red: "#ee8a80",
|
|
94
|
+
pink: "#efa2c2",
|
|
95
|
+
purple: "#bfa9e6"
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var neon = {
|
|
99
|
+
name: "neon",
|
|
100
|
+
dark: true,
|
|
101
|
+
background: "#050507",
|
|
102
|
+
bloom: 0.4,
|
|
103
|
+
colors: {
|
|
104
|
+
bg: "#050507",
|
|
105
|
+
surface: "#101016",
|
|
106
|
+
ink: "#ffffff",
|
|
107
|
+
dim: "#a9a9b8",
|
|
108
|
+
faint: "#55555f",
|
|
109
|
+
grid: "#1a1f2e",
|
|
110
|
+
gridStrong: "#2f3a55",
|
|
111
|
+
blue: "#3fb2ff",
|
|
112
|
+
teal: "#21f0d0",
|
|
113
|
+
green: "#6dff8e",
|
|
114
|
+
yellow: "#fff04d",
|
|
115
|
+
gold: "#ffc22e",
|
|
116
|
+
orange: "#ff8a2e",
|
|
117
|
+
red: "#ff4560",
|
|
118
|
+
pink: "#ff5fb0",
|
|
119
|
+
purple: "#a56bff"
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
var LOOKS = { midnight, paper, blueprint, chalk, neon };
|
|
123
|
+
function resolveLook(look) {
|
|
124
|
+
if (look === void 0) return midnight;
|
|
125
|
+
if (typeof look !== "string") return look;
|
|
126
|
+
const found = LOOKS[look];
|
|
127
|
+
if (!found) throw new Error(`Unknown look: ${JSON.stringify(look)}. Use one of: ${Object.keys(LOOKS).join(", ")}.`);
|
|
128
|
+
return found;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/stage/color.ts
|
|
132
|
+
import parseCssColor from "parse-css-color";
|
|
133
|
+
var TRANSPARENT = [0, 0, 0, 0];
|
|
134
|
+
var cache = /* @__PURE__ */ new Map();
|
|
135
|
+
var tokens = midnight.colors;
|
|
136
|
+
function setColorTokens(next) {
|
|
137
|
+
tokens = next;
|
|
138
|
+
}
|
|
139
|
+
function parseColor(input) {
|
|
140
|
+
const token = tokens[input];
|
|
141
|
+
const value = token ?? input;
|
|
142
|
+
const cached = cache.get(value);
|
|
143
|
+
if (cached) return cached;
|
|
144
|
+
const parsed = parseCssColor(value);
|
|
145
|
+
if (!parsed) throw new Error(`Unknown color: ${JSON.stringify(input)}. Use a look token such as "blue" or a CSS color.`);
|
|
146
|
+
let [r, g, b] = parsed.values;
|
|
147
|
+
if (parsed.type === "hsl") [r, g, b] = hslToRgb(r, g, b);
|
|
148
|
+
const result = [r, g, b, parsed.alpha];
|
|
149
|
+
cache.set(value, result);
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
function hslToRgb(h, s, l) {
|
|
153
|
+
const a = s / 100 * Math.min(l / 100, 1 - l / 100);
|
|
154
|
+
const f = (n) => {
|
|
155
|
+
const k = (n + h / 30) % 12;
|
|
156
|
+
return 255 * (l / 100 - a * Math.max(-1, Math.min(k - 3, 9 - k, 1)));
|
|
157
|
+
};
|
|
158
|
+
return [f(0), f(8), f(4)];
|
|
159
|
+
}
|
|
160
|
+
function toPaint(input) {
|
|
161
|
+
if (typeof input === "string") return parseColor(input);
|
|
162
|
+
return input;
|
|
163
|
+
}
|
|
164
|
+
function isGradient(paint) {
|
|
165
|
+
return !Array.isArray(paint);
|
|
166
|
+
}
|
|
167
|
+
function css(color, alpha = 1) {
|
|
168
|
+
const a = color[3] * alpha;
|
|
169
|
+
const r = Math.round(color[0]), g = Math.round(color[1]), b = Math.round(color[2]);
|
|
170
|
+
if (a >= 1) return `rgb(${r},${g},${b})`;
|
|
171
|
+
return `rgba(${r},${g},${b},${Math.round(a * 1e3) / 1e3})`;
|
|
172
|
+
}
|
|
173
|
+
function srgbToLinear(v) {
|
|
174
|
+
const x = v / 255;
|
|
175
|
+
return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
|
|
176
|
+
}
|
|
177
|
+
function linearToSrgb(v) {
|
|
178
|
+
const x = v <= 31308e-7 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
|
|
179
|
+
return Math.max(0, Math.min(255, x * 255));
|
|
180
|
+
}
|
|
181
|
+
function toOklab(c) {
|
|
182
|
+
const r = srgbToLinear(c[0]), g = srgbToLinear(c[1]), b = srgbToLinear(c[2]);
|
|
183
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
184
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
185
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
186
|
+
return [
|
|
187
|
+
0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
|
|
188
|
+
1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
|
|
189
|
+
0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
function fromOklab(lab, alpha) {
|
|
193
|
+
const l = lab[0] + 0.3963377774 * lab[1] + 0.2158037573 * lab[2];
|
|
194
|
+
const m = lab[0] - 0.1055613458 * lab[1] - 0.0638541728 * lab[2];
|
|
195
|
+
const s = lab[0] - 0.0894841775 * lab[1] - 1.291485548 * lab[2];
|
|
196
|
+
const ll = l * l * l, mm = m * m * m, ss = s * s * s;
|
|
197
|
+
return [
|
|
198
|
+
linearToSrgb(4.0767416621 * ll - 3.3077115913 * mm + 0.2309699292 * ss),
|
|
199
|
+
linearToSrgb(-1.2684380046 * ll + 2.6097574011 * mm - 0.3413193965 * ss),
|
|
200
|
+
linearToSrgb(-0.0041960863 * ll - 0.7034186147 * mm + 1.707614701 * ss),
|
|
201
|
+
alpha
|
|
202
|
+
];
|
|
203
|
+
}
|
|
204
|
+
function mixColor(a, b, t) {
|
|
205
|
+
if (t <= 0) return a;
|
|
206
|
+
if (t >= 1) return b;
|
|
207
|
+
const x = toOklab(a), y = toOklab(b);
|
|
208
|
+
return fromOklab([x[0] + (y[0] - x[0]) * t, x[1] + (y[1] - x[1]) * t, x[2] + (y[2] - x[2]) * t], a[3] + (b[3] - a[3]) * t);
|
|
209
|
+
}
|
|
210
|
+
function mixPaint(a, b, t) {
|
|
211
|
+
if (t <= 0) return a;
|
|
212
|
+
if (t >= 1) return b;
|
|
213
|
+
const ga = isGradient(a), gb = isGradient(b);
|
|
214
|
+
if (!ga && !gb) return mixColor(a, b, t);
|
|
215
|
+
if (ga && gb && a.type === b.type && a.stops.length === b.stops.length) {
|
|
216
|
+
const stops = a.stops.map((stop, i) => [
|
|
217
|
+
stop[0] + (b.stops[i][0] - stop[0]) * t,
|
|
218
|
+
mixColor(stop[1], b.stops[i][1], t)
|
|
219
|
+
]);
|
|
220
|
+
if (a.type === "linear" && b.type === "linear") {
|
|
221
|
+
return {
|
|
222
|
+
type: "linear",
|
|
223
|
+
stops,
|
|
224
|
+
from: [a.from[0] + (b.from[0] - a.from[0]) * t, a.from[1] + (b.from[1] - a.from[1]) * t],
|
|
225
|
+
to: [a.to[0] + (b.to[0] - a.to[0]) * t, a.to[1] + (b.to[1] - a.to[1]) * t]
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (a.type === "radial" && b.type === "radial") {
|
|
229
|
+
return {
|
|
230
|
+
type: "radial",
|
|
231
|
+
stops,
|
|
232
|
+
center: [a.center[0] + (b.center[0] - a.center[0]) * t, a.center[1] + (b.center[1] - a.center[1]) * t],
|
|
233
|
+
radius: a.radius + (b.radius - a.radius) * t
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
return { type: "along", stops };
|
|
237
|
+
}
|
|
238
|
+
const solidA = ga ? sampleStops(a.stops, 0.5) : a;
|
|
239
|
+
const solidB = gb ? sampleStops(b.stops, 0.5) : b;
|
|
240
|
+
return mixColor(solidA, solidB, t);
|
|
241
|
+
}
|
|
242
|
+
function sampleStops(stops, at) {
|
|
243
|
+
if (stops.length === 0) return TRANSPARENT;
|
|
244
|
+
if (at <= stops[0][0]) return stops[0][1];
|
|
245
|
+
for (let i = 1; i < stops.length; i++) {
|
|
246
|
+
if (at <= stops[i][0]) {
|
|
247
|
+
const [p0, c0] = stops[i - 1], [p1, c1] = stops[i];
|
|
248
|
+
return mixColor(c0, c1, p1 > p0 ? (at - p0) / (p1 - p0) : 0);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return stops[stops.length - 1][1];
|
|
252
|
+
}
|
|
253
|
+
function normalizeStops(colors) {
|
|
254
|
+
const count = colors.length;
|
|
255
|
+
return colors.map((entry, i) => {
|
|
256
|
+
if (Array.isArray(entry) && entry.length === 2 && typeof entry[0] === "number") {
|
|
257
|
+
return [entry[0], solid(entry[1])];
|
|
258
|
+
}
|
|
259
|
+
return [count === 1 ? 0 : i / (count - 1), solid(entry)];
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
function solid(input) {
|
|
263
|
+
const paint = toPaint(input);
|
|
264
|
+
return isGradient(paint) ? sampleStops(paint.stops, 0.5) : paint;
|
|
265
|
+
}
|
|
266
|
+
function linearGradient(from, to2, colors) {
|
|
267
|
+
return { type: "linear", from, to: to2, stops: normalizeStops(colors) };
|
|
268
|
+
}
|
|
269
|
+
function radialGradient(center, radius, colors) {
|
|
270
|
+
return { type: "radial", center, radius, stops: normalizeStops(colors) };
|
|
271
|
+
}
|
|
272
|
+
function along(...colors) {
|
|
273
|
+
return { type: "along", stops: normalizeStops(colors) };
|
|
274
|
+
}
|
|
275
|
+
function ramp(count, ...colors) {
|
|
276
|
+
const stops = normalizeStops(colors);
|
|
277
|
+
return Array.from({ length: count }, (_, i) => sampleStops(stops, count === 1 ? 0 : i / (count - 1)));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/stage/geometry.ts
|
|
281
|
+
var IDENTITY = [1, 0, 0, 1, 0, 0];
|
|
282
|
+
var EMPTY_BOUNDS = { x: 0, y: 0, width: 0, height: 0 };
|
|
283
|
+
var KAPPA = 0.5522847498307936;
|
|
284
|
+
var LUT_STEPS = 10;
|
|
285
|
+
function mul(m, n) {
|
|
286
|
+
return [
|
|
287
|
+
m[0] * n[0] + m[2] * n[1],
|
|
288
|
+
m[1] * n[0] + m[3] * n[1],
|
|
289
|
+
m[0] * n[2] + m[2] * n[3],
|
|
290
|
+
m[1] * n[2] + m[3] * n[3],
|
|
291
|
+
m[0] * n[4] + m[2] * n[5] + m[4],
|
|
292
|
+
m[1] * n[4] + m[3] * n[5] + m[5]
|
|
293
|
+
];
|
|
294
|
+
}
|
|
295
|
+
function invert(m) {
|
|
296
|
+
const det = m[0] * m[3] - m[1] * m[2];
|
|
297
|
+
if (Math.abs(det) < 1e-12) return IDENTITY;
|
|
298
|
+
const i = 1 / det;
|
|
299
|
+
return [
|
|
300
|
+
m[3] * i,
|
|
301
|
+
-m[1] * i,
|
|
302
|
+
-m[2] * i,
|
|
303
|
+
m[0] * i,
|
|
304
|
+
(m[2] * m[5] - m[3] * m[4]) * i,
|
|
305
|
+
(m[1] * m[4] - m[0] * m[5]) * i
|
|
306
|
+
];
|
|
307
|
+
}
|
|
308
|
+
function apply(m, x, y) {
|
|
309
|
+
return [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
|
|
310
|
+
}
|
|
311
|
+
function compose(x, y, rotation, sx, sy, px, py) {
|
|
312
|
+
const cos = rotation === 0 ? 1 : Math.cos(rotation);
|
|
313
|
+
const sin = rotation === 0 ? 0 : Math.sin(rotation);
|
|
314
|
+
const a = cos * sx;
|
|
315
|
+
const b = sin * sx;
|
|
316
|
+
const c = -sin * sy;
|
|
317
|
+
const d = cos * sy;
|
|
318
|
+
return [a, b, c, d, x - a * px - c * py, y - b * px - d * py];
|
|
319
|
+
}
|
|
320
|
+
function matScale(m) {
|
|
321
|
+
return Math.sqrt(Math.abs(m[0] * m[3] - m[1] * m[2]));
|
|
322
|
+
}
|
|
323
|
+
function ring(points, closed = false) {
|
|
324
|
+
return { p: points instanceof Float64Array ? points : Float64Array.from(points), closed };
|
|
325
|
+
}
|
|
326
|
+
function segmentCount(r) {
|
|
327
|
+
return Math.max(0, (r.p.length - 2) / 6);
|
|
328
|
+
}
|
|
329
|
+
function lineInto(out, x0, y0, x1, y1) {
|
|
330
|
+
out.push(x0 + (x1 - x0) / 3, y0 + (y1 - y0) / 3, x0 + (x1 - x0) * 2 / 3, y0 + (y1 - y0) * 2 / 3, x1, y1);
|
|
331
|
+
}
|
|
332
|
+
function linePath(a, b) {
|
|
333
|
+
const out = [a[0], a[1]];
|
|
334
|
+
lineInto(out, a[0], a[1], b[0], b[1]);
|
|
335
|
+
return [ring(out)];
|
|
336
|
+
}
|
|
337
|
+
function polyPath(points, closed = false) {
|
|
338
|
+
if (points.length === 0) return [];
|
|
339
|
+
const first = points[0];
|
|
340
|
+
const out = [first[0], first[1]];
|
|
341
|
+
for (let i = 1; i < points.length; i++) lineInto(out, points[i - 1][0], points[i - 1][1], points[i][0], points[i][1]);
|
|
342
|
+
if (closed) {
|
|
343
|
+
const last = points[points.length - 1];
|
|
344
|
+
if (last[0] !== first[0] || last[1] !== first[1]) lineInto(out, last[0], last[1], first[0], first[1]);
|
|
345
|
+
}
|
|
346
|
+
return [ring(out, closed)];
|
|
347
|
+
}
|
|
348
|
+
function smoothPath(points, closed = false, tension = 1 / 6) {
|
|
349
|
+
const n = points.length;
|
|
350
|
+
if (n === 0) return [];
|
|
351
|
+
if (n < 3) return polyPath(points, closed);
|
|
352
|
+
const at = (i) => closed ? points[(i + n) % n] : points[Math.max(0, Math.min(n - 1, i))];
|
|
353
|
+
const out = [points[0][0], points[0][1]];
|
|
354
|
+
const count = closed ? n : n - 1;
|
|
355
|
+
for (let i = 0; i < count; i++) {
|
|
356
|
+
const p0 = at(i - 1), p1 = at(i), p2 = at(i + 1), p3 = at(i + 2);
|
|
357
|
+
out.push(
|
|
358
|
+
p1[0] + (p2[0] - p0[0]) * tension,
|
|
359
|
+
p1[1] + (p2[1] - p0[1]) * tension,
|
|
360
|
+
p2[0] - (p3[0] - p1[0]) * tension,
|
|
361
|
+
p2[1] - (p3[1] - p1[1]) * tension,
|
|
362
|
+
p2[0],
|
|
363
|
+
p2[1]
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
return [ring(out, closed)];
|
|
367
|
+
}
|
|
368
|
+
function arcPath(cx, cy, rx, ry, from, to2) {
|
|
369
|
+
const sweep = to2 - from;
|
|
370
|
+
if (sweep === 0) return [];
|
|
371
|
+
const closed = Math.abs(Math.abs(sweep) - Math.PI * 2) < 1e-9;
|
|
372
|
+
const count = Math.max(1, Math.ceil(Math.abs(sweep) / (Math.PI / 4) - 1e-9));
|
|
373
|
+
const step = sweep / count;
|
|
374
|
+
const k = 4 / 3 * Math.tan(step / 4);
|
|
375
|
+
const out = [cx + Math.cos(from) * rx, cy + Math.sin(from) * ry];
|
|
376
|
+
for (let i = 0; i < count; i++) {
|
|
377
|
+
const a0 = from + i * step, a1 = a0 + step;
|
|
378
|
+
const c0 = Math.cos(a0), s0 = Math.sin(a0), c1 = Math.cos(a1), s1 = Math.sin(a1);
|
|
379
|
+
out.push(cx + (c0 - k * s0) * rx, cy + (s0 + k * c0) * ry, cx + (c1 + k * s1) * rx, cy + (s1 - k * c1) * ry, cx + c1 * rx, cy + s1 * ry);
|
|
380
|
+
}
|
|
381
|
+
if (closed) {
|
|
382
|
+
out[out.length - 2] = out[0];
|
|
383
|
+
out[out.length - 1] = out[1];
|
|
384
|
+
}
|
|
385
|
+
return [ring(out, closed)];
|
|
386
|
+
}
|
|
387
|
+
function circlePath(cx, cy, r) {
|
|
388
|
+
return arcPath(cx, cy, r, r, 0, Math.PI * 2);
|
|
389
|
+
}
|
|
390
|
+
function rectPath(x, y, width, height, radius = 0) {
|
|
391
|
+
const r = Math.max(0, Math.min(radius, Math.abs(width) / 2, Math.abs(height) / 2));
|
|
392
|
+
if (r === 0) return polyPath([[x, y], [x + width, y], [x + width, y + height], [x, y + height]], true);
|
|
393
|
+
const k = r * (1 - KAPPA);
|
|
394
|
+
const right = x + width, bottom = y + height;
|
|
395
|
+
const out = [x + r, y];
|
|
396
|
+
lineInto(out, x + r, y, right - r, y);
|
|
397
|
+
out.push(right - k, y, right, y + k, right, y + r);
|
|
398
|
+
lineInto(out, right, y + r, right, bottom - r);
|
|
399
|
+
out.push(right, bottom - k, right - k, bottom, right - r, bottom);
|
|
400
|
+
lineInto(out, right - r, bottom, x + r, bottom);
|
|
401
|
+
out.push(x + k, bottom, x, bottom - k, x, bottom - r);
|
|
402
|
+
lineInto(out, x, bottom - r, x, y + r);
|
|
403
|
+
out.push(x, y + k, x + k, y, x + r, y);
|
|
404
|
+
return [ring(out, true)];
|
|
405
|
+
}
|
|
406
|
+
function parametricPath(f, from, to2, samples = 64) {
|
|
407
|
+
const count = Math.max(2, Math.floor(samples));
|
|
408
|
+
const points = [];
|
|
409
|
+
for (let i = 0; i <= count; i++) points.push(f(from + (to2 - from) * i / count));
|
|
410
|
+
return curveThrough(points);
|
|
411
|
+
}
|
|
412
|
+
function curveThrough(points) {
|
|
413
|
+
const rings = [];
|
|
414
|
+
let run2 = [];
|
|
415
|
+
const flush = () => {
|
|
416
|
+
if (run2.length >= 2) rings.push(...smoothPath(run2));
|
|
417
|
+
run2 = [];
|
|
418
|
+
};
|
|
419
|
+
for (const point of points) {
|
|
420
|
+
if (Number.isFinite(point[0]) && Number.isFinite(point[1])) run2.push(point);
|
|
421
|
+
else flush();
|
|
422
|
+
}
|
|
423
|
+
flush();
|
|
424
|
+
return rings;
|
|
425
|
+
}
|
|
426
|
+
function concat(...paths) {
|
|
427
|
+
return paths.flat();
|
|
428
|
+
}
|
|
429
|
+
function transformPath(path, m) {
|
|
430
|
+
if (m === IDENTITY) return path;
|
|
431
|
+
const out = new Array(path.length);
|
|
432
|
+
for (let r = 0; r < path.length; r++) {
|
|
433
|
+
const src = path[r].p;
|
|
434
|
+
const dst = new Float64Array(src.length);
|
|
435
|
+
for (let i = 0; i < src.length; i += 2) {
|
|
436
|
+
const x = src[i], y = src[i + 1];
|
|
437
|
+
dst[i] = m[0] * x + m[2] * y + m[4];
|
|
438
|
+
dst[i + 1] = m[1] * x + m[3] * y + m[5];
|
|
439
|
+
}
|
|
440
|
+
out[r] = { p: dst, closed: path[r].closed };
|
|
441
|
+
}
|
|
442
|
+
return out;
|
|
443
|
+
}
|
|
444
|
+
function warpPath(path, f, subdivisions = 4) {
|
|
445
|
+
const dense = subdivisions > 1 ? path.map((r) => subdivideEach(r, subdivisions)) : path;
|
|
446
|
+
return dense.map((r) => {
|
|
447
|
+
const count = segmentCount(r);
|
|
448
|
+
const anchors = [];
|
|
449
|
+
for (let i = 0; i <= count; i++) {
|
|
450
|
+
const mapped = f(r.p[i * 6], r.p[i * 6 + 1]);
|
|
451
|
+
anchors.push(mapped);
|
|
452
|
+
}
|
|
453
|
+
const out = [anchors[0][0], anchors[0][1]];
|
|
454
|
+
for (let i = 0; i < count; i++) {
|
|
455
|
+
const c1 = f(r.p[i * 6 + 2], r.p[i * 6 + 3]);
|
|
456
|
+
const c2 = f(r.p[i * 6 + 4], r.p[i * 6 + 5]);
|
|
457
|
+
out.push(c1[0], c1[1], c2[0], c2[1], anchors[i + 1][0], anchors[i + 1][1]);
|
|
458
|
+
}
|
|
459
|
+
return ring(out, r.closed);
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
function splitInto(src, o, t, left, right) {
|
|
463
|
+
const x0 = src[o], y0 = src[o + 1], x1 = src[o + 2], y1 = src[o + 3];
|
|
464
|
+
const x2 = src[o + 4], y2 = src[o + 5], x3 = src[o + 6], y3 = src[o + 7];
|
|
465
|
+
const ax = x0 + (x1 - x0) * t, ay = y0 + (y1 - y0) * t;
|
|
466
|
+
const bx = x1 + (x2 - x1) * t, by = y1 + (y2 - y1) * t;
|
|
467
|
+
const cx = x2 + (x3 - x2) * t, cy = y2 + (y3 - y2) * t;
|
|
468
|
+
const dx = ax + (bx - ax) * t, dy = ay + (by - ay) * t;
|
|
469
|
+
const ex = bx + (cx - bx) * t, ey = by + (cy - by) * t;
|
|
470
|
+
const fx = dx + (ex - dx) * t, fy = dy + (ey - dy) * t;
|
|
471
|
+
left.push(ax, ay, dx, dy, fx, fy);
|
|
472
|
+
right.length = 0;
|
|
473
|
+
right.push(fx, fy, ex, ey, cx, cy, x3, y3);
|
|
474
|
+
}
|
|
475
|
+
function subdivideEach(r, n) {
|
|
476
|
+
const count = segmentCount(r);
|
|
477
|
+
if (n <= 1 || count === 0) return r;
|
|
478
|
+
const out = [r.p[0], r.p[1]];
|
|
479
|
+
for (let i = 0; i < count; i++) {
|
|
480
|
+
let seg = Array.from(r.p.subarray(i * 6, i * 6 + 8));
|
|
481
|
+
for (let k = n; k > 1; k--) {
|
|
482
|
+
const rest = [];
|
|
483
|
+
splitInto(seg, 0, 1 / k, out, rest);
|
|
484
|
+
seg = rest;
|
|
485
|
+
}
|
|
486
|
+
out.push(seg[2], seg[3], seg[4], seg[5], seg[6], seg[7]);
|
|
487
|
+
}
|
|
488
|
+
return ring(out, r.closed);
|
|
489
|
+
}
|
|
490
|
+
var measures = /* @__PURE__ */ new WeakMap();
|
|
491
|
+
function measure(r) {
|
|
492
|
+
const cached = measures.get(r);
|
|
493
|
+
if (cached) return cached;
|
|
494
|
+
const count = segmentCount(r);
|
|
495
|
+
const total = new Float64Array(count);
|
|
496
|
+
const lut = new Float64Array(count * LUT_STEPS);
|
|
497
|
+
let sum = 0;
|
|
498
|
+
const p = r.p;
|
|
499
|
+
for (let i = 0; i < count; i++) {
|
|
500
|
+
const o = i * 6;
|
|
501
|
+
const x0 = p[o], y0 = p[o + 1], x1 = p[o + 2], y1 = p[o + 3], x2 = p[o + 4], y2 = p[o + 5], x3 = p[o + 6], y3 = p[o + 7];
|
|
502
|
+
let lastX = x0, lastY = y0, acc = 0;
|
|
503
|
+
for (let k = 1; k <= LUT_STEPS; k++) {
|
|
504
|
+
const t = k / LUT_STEPS, u = 1 - t;
|
|
505
|
+
const a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t;
|
|
506
|
+
const x = a * x0 + b * x1 + c * x2 + d * x3;
|
|
507
|
+
const y = a * y0 + b * y1 + c * y2 + d * y3;
|
|
508
|
+
acc += Math.hypot(x - lastX, y - lastY);
|
|
509
|
+
lut[i * LUT_STEPS + k - 1] = acc;
|
|
510
|
+
lastX = x;
|
|
511
|
+
lastY = y;
|
|
512
|
+
}
|
|
513
|
+
sum += acc;
|
|
514
|
+
total[i] = sum;
|
|
515
|
+
}
|
|
516
|
+
const result = { total, lut, length: sum };
|
|
517
|
+
measures.set(r, result);
|
|
518
|
+
return result;
|
|
519
|
+
}
|
|
520
|
+
function ringLength(r) {
|
|
521
|
+
return measure(r).length;
|
|
522
|
+
}
|
|
523
|
+
function pathLength(path) {
|
|
524
|
+
let sum = 0;
|
|
525
|
+
for (const r of path) sum += measure(r).length;
|
|
526
|
+
return sum;
|
|
527
|
+
}
|
|
528
|
+
function locate(m, distance) {
|
|
529
|
+
const count = m.total.length;
|
|
530
|
+
if (count === 0) return [0, 0];
|
|
531
|
+
if (distance <= 0) return [0, 0];
|
|
532
|
+
if (distance >= m.length) return [count - 1, 1];
|
|
533
|
+
let lo = 0, hi = count - 1;
|
|
534
|
+
while (lo < hi) {
|
|
535
|
+
const mid = lo + hi >> 1;
|
|
536
|
+
if (m.total[mid] < distance) lo = mid + 1;
|
|
537
|
+
else hi = mid;
|
|
538
|
+
}
|
|
539
|
+
const before = lo === 0 ? 0 : m.total[lo - 1];
|
|
540
|
+
const local = distance - before;
|
|
541
|
+
const base = lo * LUT_STEPS;
|
|
542
|
+
let k = 0;
|
|
543
|
+
while (k < LUT_STEPS - 1 && m.lut[base + k] < local) k++;
|
|
544
|
+
const start = k === 0 ? 0 : m.lut[base + k - 1];
|
|
545
|
+
const end = m.lut[base + k];
|
|
546
|
+
const fraction = end > start ? (local - start) / (end - start) : 0;
|
|
547
|
+
return [lo, (k + fraction) / LUT_STEPS];
|
|
548
|
+
}
|
|
549
|
+
function trimRing(r, from, to2) {
|
|
550
|
+
const count = segmentCount(r);
|
|
551
|
+
if (count === 0) return null;
|
|
552
|
+
const a = Math.max(0, Math.min(1, from));
|
|
553
|
+
const b = Math.max(0, Math.min(1, to2));
|
|
554
|
+
if (b <= a) return null;
|
|
555
|
+
if (a === 0 && b === 1) return r;
|
|
556
|
+
const m = measure(r);
|
|
557
|
+
if (m.length === 0) return null;
|
|
558
|
+
const [i0, t0] = locate(m, a * m.length);
|
|
559
|
+
const [i1, t1] = locate(m, b * m.length);
|
|
560
|
+
const out = [];
|
|
561
|
+
for (let i = i0; i <= i1; i++) {
|
|
562
|
+
let seg = Array.from(r.p.subarray(i * 6, i * 6 + 8));
|
|
563
|
+
const start = i === i0 ? t0 : 0;
|
|
564
|
+
const end = i === i1 ? t1 : 1;
|
|
565
|
+
if (start > 0) {
|
|
566
|
+
const rest = [];
|
|
567
|
+
splitInto(seg, 0, start, [], rest);
|
|
568
|
+
seg = rest;
|
|
569
|
+
}
|
|
570
|
+
if (end < 1) {
|
|
571
|
+
const left = [];
|
|
572
|
+
const rest = [];
|
|
573
|
+
const scaled = start > 0 ? (end - start) / (1 - start) : end;
|
|
574
|
+
splitInto(seg, 0, scaled, left, rest);
|
|
575
|
+
seg = [seg[0], seg[1], ...left];
|
|
576
|
+
}
|
|
577
|
+
if (out.length === 0) out.push(seg[0], seg[1]);
|
|
578
|
+
out.push(seg[2], seg[3], seg[4], seg[5], seg[6], seg[7]);
|
|
579
|
+
}
|
|
580
|
+
return ring(out, false);
|
|
581
|
+
}
|
|
582
|
+
function trimPath(path, from, to2, each = false) {
|
|
583
|
+
if (from <= 0 && to2 >= 1) return path;
|
|
584
|
+
const out = [];
|
|
585
|
+
if (each) {
|
|
586
|
+
for (const r of path) {
|
|
587
|
+
const piece = trimRing(r, from, to2);
|
|
588
|
+
if (piece) out.push(piece);
|
|
589
|
+
}
|
|
590
|
+
return out;
|
|
591
|
+
}
|
|
592
|
+
const total = pathLength(path);
|
|
593
|
+
if (total === 0) return [];
|
|
594
|
+
let travelled = 0;
|
|
595
|
+
for (const r of path) {
|
|
596
|
+
const length = ringLength(r);
|
|
597
|
+
const start = (from * total - travelled) / (length || 1);
|
|
598
|
+
const end = (to2 * total - travelled) / (length || 1);
|
|
599
|
+
travelled += length;
|
|
600
|
+
if (end <= 0 || start >= 1) continue;
|
|
601
|
+
const piece = trimRing(r, Math.max(0, start), Math.min(1, end));
|
|
602
|
+
if (piece) out.push(piece);
|
|
603
|
+
}
|
|
604
|
+
return out;
|
|
605
|
+
}
|
|
606
|
+
function pointAt(path, fraction) {
|
|
607
|
+
const total = pathLength(path);
|
|
608
|
+
if (total === 0) {
|
|
609
|
+
const first = path[0]?.p;
|
|
610
|
+
return first ? [first[0], first[1]] : [0, 0];
|
|
611
|
+
}
|
|
612
|
+
let remaining = Math.max(0, Math.min(1, fraction)) * total;
|
|
613
|
+
for (let i = 0; i < path.length; i++) {
|
|
614
|
+
const r = path[i];
|
|
615
|
+
const length = ringLength(r);
|
|
616
|
+
if (remaining <= length || i === path.length - 1) {
|
|
617
|
+
const [segment, t] = locate(measure(r), remaining);
|
|
618
|
+
return evaluate(r.p, segment * 6, t);
|
|
619
|
+
}
|
|
620
|
+
remaining -= length;
|
|
621
|
+
}
|
|
622
|
+
return [0, 0];
|
|
623
|
+
}
|
|
624
|
+
function tangentAt(path, fraction) {
|
|
625
|
+
const delta = 2e-3;
|
|
626
|
+
const a = pointAt(path, Math.max(0, fraction - delta));
|
|
627
|
+
const b = pointAt(path, Math.min(1, fraction + delta));
|
|
628
|
+
const length = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;
|
|
629
|
+
return [(b[0] - a[0]) / length, (b[1] - a[1]) / length];
|
|
630
|
+
}
|
|
631
|
+
function evaluate(p, o, t) {
|
|
632
|
+
const u = 1 - t;
|
|
633
|
+
const a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t;
|
|
634
|
+
return [
|
|
635
|
+
a * p[o] + b * p[o + 2] + c * p[o + 4] + d * p[o + 6],
|
|
636
|
+
a * p[o + 1] + b * p[o + 3] + c * p[o + 5] + d * p[o + 7]
|
|
637
|
+
];
|
|
638
|
+
}
|
|
639
|
+
function extendCubicAxis(range, p0, p1, p2, p3) {
|
|
640
|
+
range[0] = Math.min(range[0], p0, p3);
|
|
641
|
+
range[1] = Math.max(range[1], p0, p3);
|
|
642
|
+
if (p1 >= range[0] && p1 <= range[1] && p2 >= range[0] && p2 <= range[1]) return;
|
|
643
|
+
const a = -p0 + 3 * p1 - 3 * p2 + p3;
|
|
644
|
+
const b = 2 * (p0 - 2 * p1 + p2);
|
|
645
|
+
const c = p1 - p0;
|
|
646
|
+
const roots = [];
|
|
647
|
+
if (Math.abs(a) < 1e-9) {
|
|
648
|
+
if (Math.abs(b) > 1e-9) roots.push(-c / b);
|
|
649
|
+
} else {
|
|
650
|
+
const disc = b * b - 4 * a * c;
|
|
651
|
+
if (disc >= 0) {
|
|
652
|
+
const s = Math.sqrt(disc);
|
|
653
|
+
roots.push((-b + s) / (2 * a), (-b - s) / (2 * a));
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
for (const t of roots) {
|
|
657
|
+
if (t <= 0 || t >= 1) continue;
|
|
658
|
+
const u = 1 - t;
|
|
659
|
+
const value = u * u * u * p0 + 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t * p3;
|
|
660
|
+
range[0] = Math.min(range[0], value);
|
|
661
|
+
range[1] = Math.max(range[1], value);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
var ringBounds = /* @__PURE__ */ new WeakMap();
|
|
665
|
+
function boundsOfRing(r) {
|
|
666
|
+
const cached = ringBounds.get(r);
|
|
667
|
+
if (cached) return cached;
|
|
668
|
+
const p = r.p;
|
|
669
|
+
if (p.length < 2) return EMPTY_BOUNDS;
|
|
670
|
+
const xs = [p[0], p[0]];
|
|
671
|
+
const ys = [p[1], p[1]];
|
|
672
|
+
for (let o = 0; o + 7 < p.length; o += 6) {
|
|
673
|
+
extendCubicAxis(xs, p[o], p[o + 2], p[o + 4], p[o + 6]);
|
|
674
|
+
extendCubicAxis(ys, p[o + 1], p[o + 3], p[o + 5], p[o + 7]);
|
|
675
|
+
}
|
|
676
|
+
const bounds = { x: xs[0], y: ys[0], width: xs[1] - xs[0], height: ys[1] - ys[0] };
|
|
677
|
+
ringBounds.set(r, bounds);
|
|
678
|
+
return bounds;
|
|
679
|
+
}
|
|
680
|
+
function pathBounds(path) {
|
|
681
|
+
if (path.length === 0) return null;
|
|
682
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
683
|
+
for (const r of path) {
|
|
684
|
+
if (r.p.length < 2) continue;
|
|
685
|
+
const b = boundsOfRing(r);
|
|
686
|
+
minX = Math.min(minX, b.x);
|
|
687
|
+
minY = Math.min(minY, b.y);
|
|
688
|
+
maxX = Math.max(maxX, b.x + b.width);
|
|
689
|
+
maxY = Math.max(maxY, b.y + b.height);
|
|
690
|
+
}
|
|
691
|
+
if (minX === Infinity) return null;
|
|
692
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
693
|
+
}
|
|
694
|
+
function unionBounds(a, b) {
|
|
695
|
+
if (!a) return b;
|
|
696
|
+
if (!b) return a;
|
|
697
|
+
const x = Math.min(a.x, b.x), y = Math.min(a.y, b.y);
|
|
698
|
+
return { x, y, width: Math.max(a.x + a.width, b.x + b.width) - x, height: Math.max(a.y + a.height, b.y + b.height) - y };
|
|
699
|
+
}
|
|
700
|
+
function signedArea(r) {
|
|
701
|
+
const p = r.p;
|
|
702
|
+
let sum = 0;
|
|
703
|
+
for (let o = 0; o + 7 < p.length; o += 6) {
|
|
704
|
+
sum += p[o] * p[o + 7] - p[o + 6] * p[o + 1];
|
|
705
|
+
}
|
|
706
|
+
return sum / 2;
|
|
707
|
+
}
|
|
708
|
+
function ringCenter(r) {
|
|
709
|
+
const b = boundsOfRing(r);
|
|
710
|
+
return [b.x + b.width / 2, b.y + b.height / 2];
|
|
711
|
+
}
|
|
712
|
+
function reverseRing(r) {
|
|
713
|
+
const p = r.p;
|
|
714
|
+
const out = new Float64Array(p.length);
|
|
715
|
+
for (let i = 0; i < p.length; i += 2) {
|
|
716
|
+
out[i] = p[p.length - 2 - i];
|
|
717
|
+
out[i + 1] = p[p.length - 1 - i];
|
|
718
|
+
}
|
|
719
|
+
return { p: out, closed: r.closed };
|
|
720
|
+
}
|
|
721
|
+
function parseSvgPath(d) {
|
|
722
|
+
const tokens2 = d.match(/[a-zA-Z]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/g);
|
|
723
|
+
if (!tokens2) return [];
|
|
724
|
+
const rings = [];
|
|
725
|
+
let out = [];
|
|
726
|
+
let i = 0, x = 0, y = 0, startX = 0, startY = 0;
|
|
727
|
+
let lastCmd = "", lastCx = 0, lastCy = 0;
|
|
728
|
+
const num = () => Number(tokens2[i++]);
|
|
729
|
+
const flags = () => {
|
|
730
|
+
const token = tokens2[i];
|
|
731
|
+
if (token.length > 1 && (token[0] === "0" || token[0] === "1") && !token.startsWith("0.") && !token.startsWith("1.")) {
|
|
732
|
+
tokens2[i] = token.slice(1);
|
|
733
|
+
return Number(token[0]);
|
|
734
|
+
}
|
|
735
|
+
i++;
|
|
736
|
+
return Number(token);
|
|
737
|
+
};
|
|
738
|
+
const flush = (closed) => {
|
|
739
|
+
if (out.length >= 8) rings.push(ring(out, closed));
|
|
740
|
+
out = [];
|
|
741
|
+
};
|
|
742
|
+
const begin = (px, py) => {
|
|
743
|
+
if (out.length === 0) out.push(px, py);
|
|
744
|
+
};
|
|
745
|
+
let cmd = "";
|
|
746
|
+
while (i < tokens2.length) {
|
|
747
|
+
const token = tokens2[i];
|
|
748
|
+
if (/^[a-zA-Z]$/.test(token)) {
|
|
749
|
+
cmd = token;
|
|
750
|
+
i++;
|
|
751
|
+
} else if (cmd === "M") {
|
|
752
|
+
cmd = "L";
|
|
753
|
+
} else if (cmd === "m") {
|
|
754
|
+
cmd = "l";
|
|
755
|
+
} else if (cmd === "") {
|
|
756
|
+
throw new Error(`Invalid SVG path data: ${d.slice(0, 40)}`);
|
|
757
|
+
}
|
|
758
|
+
const rel = cmd >= "a";
|
|
759
|
+
switch (cmd.toUpperCase()) {
|
|
760
|
+
case "M": {
|
|
761
|
+
flush(false);
|
|
762
|
+
x = (rel ? x : 0) + num();
|
|
763
|
+
y = (rel ? y : 0) + num();
|
|
764
|
+
startX = x;
|
|
765
|
+
startY = y;
|
|
766
|
+
out.push(x, y);
|
|
767
|
+
break;
|
|
768
|
+
}
|
|
769
|
+
case "L": {
|
|
770
|
+
const nx = (rel ? x : 0) + num(), ny = (rel ? y : 0) + num();
|
|
771
|
+
begin(x, y);
|
|
772
|
+
lineInto(out, x, y, nx, ny);
|
|
773
|
+
x = nx;
|
|
774
|
+
y = ny;
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
case "H": {
|
|
778
|
+
const nx = (rel ? x : 0) + num();
|
|
779
|
+
begin(x, y);
|
|
780
|
+
lineInto(out, x, y, nx, y);
|
|
781
|
+
x = nx;
|
|
782
|
+
break;
|
|
783
|
+
}
|
|
784
|
+
case "V": {
|
|
785
|
+
const ny = (rel ? y : 0) + num();
|
|
786
|
+
begin(x, y);
|
|
787
|
+
lineInto(out, x, y, x, ny);
|
|
788
|
+
y = ny;
|
|
789
|
+
break;
|
|
790
|
+
}
|
|
791
|
+
case "C": {
|
|
792
|
+
const ox = rel ? x : 0, oy = rel ? y : 0;
|
|
793
|
+
const c1x = ox + num(), c1y = oy + num(), c2x = ox + num(), c2y = oy + num(), nx = ox + num(), ny = oy + num();
|
|
794
|
+
begin(x, y);
|
|
795
|
+
out.push(c1x, c1y, c2x, c2y, nx, ny);
|
|
796
|
+
lastCx = c2x;
|
|
797
|
+
lastCy = c2y;
|
|
798
|
+
x = nx;
|
|
799
|
+
y = ny;
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
case "S": {
|
|
803
|
+
const ox = rel ? x : 0, oy = rel ? y : 0;
|
|
804
|
+
const smooth2 = "CScs".includes(lastCmd);
|
|
805
|
+
const c1x = smooth2 ? 2 * x - lastCx : x, c1y = smooth2 ? 2 * y - lastCy : y;
|
|
806
|
+
const c2x = ox + num(), c2y = oy + num(), nx = ox + num(), ny = oy + num();
|
|
807
|
+
begin(x, y);
|
|
808
|
+
out.push(c1x, c1y, c2x, c2y, nx, ny);
|
|
809
|
+
lastCx = c2x;
|
|
810
|
+
lastCy = c2y;
|
|
811
|
+
x = nx;
|
|
812
|
+
y = ny;
|
|
813
|
+
break;
|
|
814
|
+
}
|
|
815
|
+
case "Q": {
|
|
816
|
+
const ox = rel ? x : 0, oy = rel ? y : 0;
|
|
817
|
+
const qx = ox + num(), qy = oy + num(), nx = ox + num(), ny = oy + num();
|
|
818
|
+
begin(x, y);
|
|
819
|
+
out.push(x + (qx - x) * 2 / 3, y + (qy - y) * 2 / 3, nx + (qx - nx) * 2 / 3, ny + (qy - ny) * 2 / 3, nx, ny);
|
|
820
|
+
lastCx = qx;
|
|
821
|
+
lastCy = qy;
|
|
822
|
+
x = nx;
|
|
823
|
+
y = ny;
|
|
824
|
+
break;
|
|
825
|
+
}
|
|
826
|
+
case "T": {
|
|
827
|
+
const ox = rel ? x : 0, oy = rel ? y : 0;
|
|
828
|
+
const smooth2 = "QTqt".includes(lastCmd);
|
|
829
|
+
const qx = smooth2 ? 2 * x - lastCx : x, qy = smooth2 ? 2 * y - lastCy : y;
|
|
830
|
+
const nx = ox + num(), ny = oy + num();
|
|
831
|
+
begin(x, y);
|
|
832
|
+
out.push(x + (qx - x) * 2 / 3, y + (qy - y) * 2 / 3, nx + (qx - nx) * 2 / 3, ny + (qy - ny) * 2 / 3, nx, ny);
|
|
833
|
+
lastCx = qx;
|
|
834
|
+
lastCy = qy;
|
|
835
|
+
x = nx;
|
|
836
|
+
y = ny;
|
|
837
|
+
break;
|
|
838
|
+
}
|
|
839
|
+
case "A": {
|
|
840
|
+
const rx = num(), ry = num(), angle = num(), large = flags(), sweep = flags();
|
|
841
|
+
const nx = (rel ? x : 0) + num(), ny = (rel ? y : 0) + num();
|
|
842
|
+
begin(x, y);
|
|
843
|
+
arcInto(out, x, y, rx, ry, angle, large === 1, sweep === 1, nx, ny);
|
|
844
|
+
x = nx;
|
|
845
|
+
y = ny;
|
|
846
|
+
break;
|
|
847
|
+
}
|
|
848
|
+
case "Z": {
|
|
849
|
+
if (out.length > 0) {
|
|
850
|
+
if (x !== startX || y !== startY) lineInto(out, x, y, startX, startY);
|
|
851
|
+
flush(true);
|
|
852
|
+
}
|
|
853
|
+
x = startX;
|
|
854
|
+
y = startY;
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
default:
|
|
858
|
+
throw new Error(`Unsupported SVG path command: ${cmd}`);
|
|
859
|
+
}
|
|
860
|
+
lastCmd = cmd;
|
|
861
|
+
}
|
|
862
|
+
flush(false);
|
|
863
|
+
return rings;
|
|
864
|
+
}
|
|
865
|
+
function arcInto(out, x0, y0, rxIn, ryIn, degrees, large, sweep, x1, y1) {
|
|
866
|
+
let rx = Math.abs(rxIn), ry = Math.abs(ryIn);
|
|
867
|
+
if (rx === 0 || ry === 0 || x0 === x1 && y0 === y1) {
|
|
868
|
+
lineInto(out, x0, y0, x1, y1);
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
const phi = degrees * Math.PI / 180;
|
|
872
|
+
const cos = Math.cos(phi), sin = Math.sin(phi);
|
|
873
|
+
const dx = (x0 - x1) / 2, dy = (y0 - y1) / 2;
|
|
874
|
+
const xp = cos * dx + sin * dy, yp = -sin * dx + cos * dy;
|
|
875
|
+
const lambda = xp * xp / (rx * rx) + yp * yp / (ry * ry);
|
|
876
|
+
if (lambda > 1) {
|
|
877
|
+
const scale = Math.sqrt(lambda);
|
|
878
|
+
rx *= scale;
|
|
879
|
+
ry *= scale;
|
|
880
|
+
}
|
|
881
|
+
const numerator = rx * rx * ry * ry - rx * rx * yp * yp - ry * ry * xp * xp;
|
|
882
|
+
const denominator = rx * rx * yp * yp + ry * ry * xp * xp;
|
|
883
|
+
const factor = (large === sweep ? -1 : 1) * Math.sqrt(Math.max(0, numerator / denominator));
|
|
884
|
+
const cxp = factor * rx * yp / ry, cyp = -factor * ry * xp / rx;
|
|
885
|
+
const cx = cos * cxp - sin * cyp + (x0 + x1) / 2;
|
|
886
|
+
const cy = sin * cxp + cos * cyp + (y0 + y1) / 2;
|
|
887
|
+
const start = Math.atan2((yp - cyp) / ry, (xp - cxp) / rx);
|
|
888
|
+
let delta = Math.atan2((-yp - cyp) / ry, (-xp - cxp) / rx) - start;
|
|
889
|
+
if (sweep && delta < 0) delta += Math.PI * 2;
|
|
890
|
+
if (!sweep && delta > 0) delta -= Math.PI * 2;
|
|
891
|
+
const count = Math.max(1, Math.ceil(Math.abs(delta) / (Math.PI / 2) - 1e-9));
|
|
892
|
+
const step = delta / count;
|
|
893
|
+
const k = 4 / 3 * Math.tan(step / 4);
|
|
894
|
+
for (let i = 0; i < count; i++) {
|
|
895
|
+
const a0 = start + i * step, a1 = a0 + step;
|
|
896
|
+
const c0 = Math.cos(a0), s0 = Math.sin(a0), c1 = Math.cos(a1), s1 = Math.sin(a1);
|
|
897
|
+
const q1x = (c0 - k * s0) * rx, q1y = (s0 + k * c0) * ry;
|
|
898
|
+
const q2x = (c1 + k * s1) * rx, q2y = (s1 - k * c1) * ry;
|
|
899
|
+
const ex = c1 * rx, ey = s1 * ry;
|
|
900
|
+
out.push(
|
|
901
|
+
cos * q1x - sin * q1y + cx,
|
|
902
|
+
sin * q1x + cos * q1y + cy,
|
|
903
|
+
cos * q2x - sin * q2y + cx,
|
|
904
|
+
sin * q2x + cos * q2y + cy,
|
|
905
|
+
i === count - 1 ? x1 : cos * ex - sin * ey + cx,
|
|
906
|
+
i === count - 1 ? y1 : sin * ex + cos * ey + cy
|
|
907
|
+
);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
function fmt(value) {
|
|
911
|
+
const rounded = Math.round(value * 100) / 100;
|
|
912
|
+
return rounded === 0 ? "0" : String(rounded);
|
|
913
|
+
}
|
|
914
|
+
function pathToD(path) {
|
|
915
|
+
const parts = [];
|
|
916
|
+
for (const r of path) {
|
|
917
|
+
const p = r.p;
|
|
918
|
+
if (p.length < 2) continue;
|
|
919
|
+
parts.push("M", fmt(p[0]), " ", fmt(p[1]));
|
|
920
|
+
if (r.straight) {
|
|
921
|
+
for (let o = 2; o + 5 < p.length; o += 6) parts.push("L", fmt(p[o + 4]), " ", fmt(p[o + 5]));
|
|
922
|
+
if (r.closed) parts.push("Z");
|
|
923
|
+
continue;
|
|
924
|
+
}
|
|
925
|
+
for (let o = 2; o + 5 < p.length; o += 6) {
|
|
926
|
+
parts.push("C", fmt(p[o]), " ", fmt(p[o + 1]), " ", fmt(p[o + 2]), " ", fmt(p[o + 3]), " ", fmt(p[o + 4]), " ", fmt(p[o + 5]));
|
|
927
|
+
}
|
|
928
|
+
if (r.closed) parts.push("Z");
|
|
929
|
+
}
|
|
930
|
+
return parts.join("");
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// src/stage/track.ts
|
|
934
|
+
var clock = {
|
|
935
|
+
now: 0,
|
|
936
|
+
mode: "free",
|
|
937
|
+
/** Increments once per rendered frame and invalidates per-frame overlays. */
|
|
938
|
+
frame: 0,
|
|
939
|
+
/** While set, writes to mounted nodes collect here instead of creating keys. */
|
|
940
|
+
capture: null
|
|
941
|
+
};
|
|
942
|
+
var Track = class {
|
|
943
|
+
constructor(initial) {
|
|
944
|
+
this.initial = initial;
|
|
945
|
+
}
|
|
946
|
+
initial;
|
|
947
|
+
keys = [];
|
|
948
|
+
sample(t) {
|
|
949
|
+
const keys = this.keys;
|
|
950
|
+
let lo = 0, hi = keys.length - 1, index = -1;
|
|
951
|
+
while (lo <= hi) {
|
|
952
|
+
const mid = lo + hi >> 1;
|
|
953
|
+
if (keys[mid].t0 <= t) {
|
|
954
|
+
index = mid;
|
|
955
|
+
lo = mid + 1;
|
|
956
|
+
} else {
|
|
957
|
+
hi = mid - 1;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
if (index < 0) return this.initial;
|
|
961
|
+
const key = keys[index];
|
|
962
|
+
if (t >= key.t1 || !key.at) return key.to;
|
|
963
|
+
const span = key.t1 - key.t0;
|
|
964
|
+
const raw = span > 0 ? (t - key.t0) / span : 1;
|
|
965
|
+
return key.at(key.ease ? key.ease(raw) : raw);
|
|
966
|
+
}
|
|
967
|
+
add(key) {
|
|
968
|
+
const keys = this.keys;
|
|
969
|
+
let index = keys.length;
|
|
970
|
+
while (index > 0 && keys[index - 1].t0 > key.t0) index--;
|
|
971
|
+
keys.splice(index, 0, key);
|
|
972
|
+
}
|
|
973
|
+
step(t, value) {
|
|
974
|
+
this.add({ t0: t, t1: t, ease: null, to: value, at: null });
|
|
975
|
+
}
|
|
976
|
+
/** Last time this track changes. */
|
|
977
|
+
get end() {
|
|
978
|
+
let end = 0;
|
|
979
|
+
for (const key of this.keys) if (key.t1 > end) end = key.t1;
|
|
980
|
+
return end;
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
// src/stage/display.ts
|
|
985
|
+
var clockNow = () => clock.now;
|
|
986
|
+
var ALONG_PIECE_LENGTH = 14;
|
|
987
|
+
var MAX_ALONG_PIECES = 96;
|
|
988
|
+
function worldPaint(paint, m) {
|
|
989
|
+
if (!paint || !isGradient(paint)) return paint;
|
|
990
|
+
if (paint.type === "linear") {
|
|
991
|
+
return { ...paint, from: apply(m, paint.from[0], paint.from[1]), to: apply(m, paint.to[0], paint.to[1]) };
|
|
992
|
+
}
|
|
993
|
+
if (paint.type === "radial") {
|
|
994
|
+
return { ...paint, center: apply(m, paint.center[0], paint.center[1]), radius: paint.radius * matScale(m) };
|
|
995
|
+
}
|
|
996
|
+
return paint;
|
|
997
|
+
}
|
|
998
|
+
function sameMatrix(a, b) {
|
|
999
|
+
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5];
|
|
1000
|
+
}
|
|
1001
|
+
var worldCache = /* @__PURE__ */ new WeakMap();
|
|
1002
|
+
function toWorld(node, path, m) {
|
|
1003
|
+
const cached = worldCache.get(node);
|
|
1004
|
+
if (cached && cached.path === path && sameMatrix(cached.matrix, m)) return cached.world;
|
|
1005
|
+
const world = transformPath(path, m);
|
|
1006
|
+
worldCache.set(node, { path, matrix: m, world });
|
|
1007
|
+
return world;
|
|
1008
|
+
}
|
|
1009
|
+
function emit(node, path, m, alpha, ops) {
|
|
1010
|
+
const fill = node.get("fill");
|
|
1011
|
+
const stroke = node.get("stroke");
|
|
1012
|
+
const fillAlpha = fill ? alpha * node.get("fillOpacity") : 0;
|
|
1013
|
+
const strokeAlpha = stroke ? alpha * node.get("strokeOpacity") : 0;
|
|
1014
|
+
const glow = node.get("glow");
|
|
1015
|
+
if (fillAlpha <= 2e-3 && strokeAlpha <= 2e-3 && glow <= 0) return;
|
|
1016
|
+
const world = toWorld(node, path, m);
|
|
1017
|
+
const scale = matScale(m);
|
|
1018
|
+
const trimFrom = node.get("trimFrom"), trimTo = node.get("trimTo");
|
|
1019
|
+
let strokePath = world;
|
|
1020
|
+
if (trimFrom > 0 || trimTo < 1) strokePath = trimPath(world, trimFrom, trimTo, node.get("trimEach"));
|
|
1021
|
+
if (strokePath.length === 0 && fillAlpha <= 2e-3) return;
|
|
1022
|
+
const dash = node.get("dash");
|
|
1023
|
+
const op = {
|
|
1024
|
+
kind: "path",
|
|
1025
|
+
path: world,
|
|
1026
|
+
strokePath,
|
|
1027
|
+
fill: fillAlpha > 2e-3 ? worldPaint(fill, m) : null,
|
|
1028
|
+
fillAlpha,
|
|
1029
|
+
stroke: strokeAlpha > 2e-3 ? worldPaint(stroke, m) : null,
|
|
1030
|
+
strokeAlpha,
|
|
1031
|
+
strokeWidth: node.get("strokeWidth") * scale,
|
|
1032
|
+
cap: node.get("cap"),
|
|
1033
|
+
join: node.get("join"),
|
|
1034
|
+
dash: dash ? dash.map((value) => value * scale) : null,
|
|
1035
|
+
dashOffset: node.get("dashOffset") * scale,
|
|
1036
|
+
glow: glow * scale,
|
|
1037
|
+
glowColor: node.get("glowColor"),
|
|
1038
|
+
blend: node.get("blend"),
|
|
1039
|
+
node
|
|
1040
|
+
};
|
|
1041
|
+
if (op.stroke && isGradient(op.stroke) && op.stroke.type === "along") splitAlong(op, op.stroke, ops);
|
|
1042
|
+
else ops.push(op);
|
|
1043
|
+
}
|
|
1044
|
+
function splitAlong(op, paint, ops) {
|
|
1045
|
+
ops.push({ ...op, stroke: null, strokeAlpha: 0 });
|
|
1046
|
+
const glowless = { ...op, fill: null, fillAlpha: 0, glow: 0 };
|
|
1047
|
+
const length = pathLength(op.strokePath);
|
|
1048
|
+
const full = pathLength(op.path);
|
|
1049
|
+
if (length === 0) return;
|
|
1050
|
+
const fraction = full > 0 ? Math.min(1, length / full) : 1;
|
|
1051
|
+
const pieces = Math.min(MAX_ALONG_PIECES, Math.max(2, Math.ceil(length / ALONG_PIECE_LENGTH)));
|
|
1052
|
+
const overlap = 0.6 / pieces;
|
|
1053
|
+
for (let i = 0; i < pieces; i++) {
|
|
1054
|
+
const from = i / pieces, to2 = Math.min(1, (i + 1) / pieces + overlap);
|
|
1055
|
+
const piece = trimPath(op.strokePath, from, to2);
|
|
1056
|
+
if (piece.length === 0) continue;
|
|
1057
|
+
ops.push({
|
|
1058
|
+
...glowless,
|
|
1059
|
+
strokePath: piece,
|
|
1060
|
+
path: piece,
|
|
1061
|
+
stroke: sampleStops(paint.stops, (i + 0.5) / pieces * fraction),
|
|
1062
|
+
cap: i === 0 || i === pieces - 1 ? op.cap : "butt"
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
function walk(node, parent, screen, alpha, ops) {
|
|
1067
|
+
if (!node.get("visible")) return;
|
|
1068
|
+
const opacity = node.get("opacity") * alpha;
|
|
1069
|
+
if (opacity <= 2e-3) return;
|
|
1070
|
+
const base = node.get("fixed") ? screen : parent;
|
|
1071
|
+
const m = mul(base, node.localMatrix());
|
|
1072
|
+
const path = node.get("path");
|
|
1073
|
+
if (path && path.length > 0) emit(node, path, m, opacity, ops);
|
|
1074
|
+
const image = node.get("image");
|
|
1075
|
+
if (image) ops.push({ kind: "image", image, matrix: m, alpha: opacity });
|
|
1076
|
+
if (node.rawOps) node.rawOps(m, opacity, ops);
|
|
1077
|
+
if (node.draw) {
|
|
1078
|
+
const built = node.draw(clockNow());
|
|
1079
|
+
if (built) {
|
|
1080
|
+
const list2 = Array.isArray(built) ? built : [built];
|
|
1081
|
+
for (const child of list2) walk(child, m, screen, opacity, ops);
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
const children = node.children;
|
|
1085
|
+
if (children.length === 0) return;
|
|
1086
|
+
let sorted = false;
|
|
1087
|
+
for (let i = 0; i < children.length; i++) {
|
|
1088
|
+
if (children[i].get("z") !== 0) {
|
|
1089
|
+
sorted = true;
|
|
1090
|
+
break;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
const ordered = sorted ? children.map((child, index) => ({ child, index, z: child.get("z") })).sort((a, b) => a.z - b.z || a.index - b.index).map((entry) => entry.child) : children;
|
|
1094
|
+
for (const child of ordered) walk(child, m, screen, opacity, ops);
|
|
1095
|
+
}
|
|
1096
|
+
function buildOps(root, camera, width, height) {
|
|
1097
|
+
const screen = [1, 0, 0, 1, width / 2, height / 2];
|
|
1098
|
+
const view = mul(screen, compose(0, 0, -camera.rotation, camera.zoom, camera.zoom, camera.x, camera.y));
|
|
1099
|
+
const ops = [];
|
|
1100
|
+
walk(root, view, screen, 1, ops);
|
|
1101
|
+
return ops;
|
|
1102
|
+
}
|
|
1103
|
+
var GLOW_PASSES = 7;
|
|
1104
|
+
function glowPasses(glow, width) {
|
|
1105
|
+
const out = [];
|
|
1106
|
+
for (let i = GLOW_PASSES; i >= 1; i--) {
|
|
1107
|
+
const k = i / GLOW_PASSES;
|
|
1108
|
+
out.push({ width: width + glow * 2 * k, alpha: 0.16 * (1 - k) * (1 - k) + 0.018 });
|
|
1109
|
+
}
|
|
1110
|
+
return out;
|
|
1111
|
+
}
|
|
1112
|
+
function glowOf(op) {
|
|
1113
|
+
if (op.glowColor) return op.glowColor;
|
|
1114
|
+
const paint = op.stroke ?? op.fill;
|
|
1115
|
+
if (!paint) return null;
|
|
1116
|
+
return isGradient(paint) ? sampleStops(paint.stops, 0.5) : paint;
|
|
1117
|
+
}
|
|
1118
|
+
var dCache = /* @__PURE__ */ new WeakMap();
|
|
1119
|
+
function pathData(path) {
|
|
1120
|
+
let d = dCache.get(path);
|
|
1121
|
+
if (d === void 0) {
|
|
1122
|
+
d = pathToD(path);
|
|
1123
|
+
dCache.set(path, d);
|
|
1124
|
+
}
|
|
1125
|
+
return d;
|
|
1126
|
+
}
|
|
1127
|
+
function fmt2(value) {
|
|
1128
|
+
return String(Math.round(value * 1e3) / 1e3);
|
|
1129
|
+
}
|
|
1130
|
+
function gradientDef(id, paint) {
|
|
1131
|
+
const stops = paint.stops.map(([at, color]) => `<stop offset="${fmt2(at)}" stop-color="${css([color[0], color[1], color[2], 1])}"${color[3] < 1 ? ` stop-opacity="${fmt2(color[3])}"` : ""}/>`).join("");
|
|
1132
|
+
if (paint.type === "linear") {
|
|
1133
|
+
return `<linearGradient id="${id}" gradientUnits="userSpaceOnUse" x1="${fmt2(paint.from[0])}" y1="${fmt2(paint.from[1])}" x2="${fmt2(paint.to[0])}" y2="${fmt2(paint.to[1])}">${stops}</linearGradient>`;
|
|
1134
|
+
}
|
|
1135
|
+
if (paint.type === "radial") {
|
|
1136
|
+
return `<radialGradient id="${id}" gradientUnits="userSpaceOnUse" cx="${fmt2(paint.center[0])}" cy="${fmt2(paint.center[1])}" r="${fmt2(paint.radius)}">${stops}</radialGradient>`;
|
|
1137
|
+
}
|
|
1138
|
+
return "";
|
|
1139
|
+
}
|
|
1140
|
+
function strokeAttrs(op) {
|
|
1141
|
+
let attrs = ` stroke-linecap="${op.cap}" stroke-linejoin="${op.join}"`;
|
|
1142
|
+
if (op.dash) attrs += ` stroke-dasharray="${op.dash.map(fmt2).join(" ")}" stroke-dashoffset="${fmt2(op.dashOffset)}"`;
|
|
1143
|
+
return attrs;
|
|
1144
|
+
}
|
|
1145
|
+
function escapeAttr(value) {
|
|
1146
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
1147
|
+
}
|
|
1148
|
+
function frameToSvg(frame, options = {}) {
|
|
1149
|
+
const scale = options.scale ?? 1;
|
|
1150
|
+
const w = frame.width, h = frame.height;
|
|
1151
|
+
const defs = [];
|
|
1152
|
+
const body = [];
|
|
1153
|
+
let gradientCount = 0;
|
|
1154
|
+
const paintAttr = (paint, attr, alpha) => {
|
|
1155
|
+
if (isGradient(paint)) {
|
|
1156
|
+
const id = `g${gradientCount++}`;
|
|
1157
|
+
defs.push(gradientDef(id, paint));
|
|
1158
|
+
return `${attr}="url(#${id})"${alpha < 1 ? ` ${attr}-opacity="${fmt2(alpha)}"` : ""}`;
|
|
1159
|
+
}
|
|
1160
|
+
const rgba = paint;
|
|
1161
|
+
const a = rgba[3] * alpha;
|
|
1162
|
+
return `${attr}="${css([rgba[0], rgba[1], rgba[2], 1])}"${a < 1 ? ` ${attr}-opacity="${fmt2(a)}"` : ""}`;
|
|
1163
|
+
};
|
|
1164
|
+
if (frame.background && !options.transparent) body.push(`<rect width="${w}" height="${h}" fill="${frame.background}"/>`);
|
|
1165
|
+
for (const op of frame.ops) {
|
|
1166
|
+
if (op.kind === "image") {
|
|
1167
|
+
const m = op.matrix;
|
|
1168
|
+
body.push(`<image href="${escapeAttr(op.image.src)}" width="${op.image.width}" height="${op.image.height}" transform="matrix(${m.map(fmt2).join(" ")})"${op.alpha < 1 ? ` opacity="${fmt2(op.alpha)}"` : ""} preserveAspectRatio="none"/>`);
|
|
1169
|
+
continue;
|
|
1170
|
+
}
|
|
1171
|
+
const style = op.blend !== "normal" ? ` style="mix-blend-mode:${op.blend}"` : "";
|
|
1172
|
+
if (op.glow > 0) {
|
|
1173
|
+
const color = glowOf(op);
|
|
1174
|
+
const glowD = pathData(op.strokePath.length ? op.strokePath : op.path);
|
|
1175
|
+
if (color && glowD) {
|
|
1176
|
+
const base = Math.max(op.fillAlpha, op.strokeAlpha);
|
|
1177
|
+
const mode = frame.dark ? ' style="mix-blend-mode:screen"' : "";
|
|
1178
|
+
const passes = glowPasses(op.glow, op.stroke ? op.strokeWidth : 0).map((pass) => `<path d="${glowD}" fill="none" stroke="${css([color[0], color[1], color[2], 1])}" stroke-opacity="${fmt2(pass.alpha * base)}" stroke-width="${fmt2(pass.width)}" stroke-linecap="round" stroke-linejoin="round"/>`).join("");
|
|
1179
|
+
body.push(`<g${mode}>${passes}</g>`);
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
if (op.fill && op.stroke && op.strokePath === op.path) {
|
|
1183
|
+
body.push(`<path d="${pathData(op.path)}" ${paintAttr(op.fill, "fill", op.fillAlpha)} ${paintAttr(op.stroke, "stroke", op.strokeAlpha)} stroke-width="${fmt2(op.strokeWidth)}"${strokeAttrs(op)}${style}/>`);
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
if (op.fill) body.push(`<path d="${pathData(op.path)}" ${paintAttr(op.fill, "fill", op.fillAlpha)}${style}/>`);
|
|
1187
|
+
if (op.stroke && op.strokePath.length) {
|
|
1188
|
+
body.push(`<path d="${pathData(op.strokePath)}" fill="none" ${paintAttr(op.stroke, "stroke", op.strokeAlpha)} stroke-width="${fmt2(op.strokeWidth)}"${strokeAttrs(op)}${style}/>`);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
const defsBlock = defs.length ? `<defs>${defs.join("")}</defs>` : "";
|
|
1192
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${w * scale}" height="${h * scale}" viewBox="0 0 ${w} ${h}">${defsBlock}${body.join("")}</svg>`;
|
|
1193
|
+
}
|
|
1194
|
+
var path2dCache = /* @__PURE__ */ new WeakMap();
|
|
1195
|
+
function ringsToPath2D(path) {
|
|
1196
|
+
let cached = path2dCache.get(path);
|
|
1197
|
+
if (cached) return cached;
|
|
1198
|
+
cached = new Path2D();
|
|
1199
|
+
for (const r of path) {
|
|
1200
|
+
const p = r.p;
|
|
1201
|
+
if (p.length < 2) continue;
|
|
1202
|
+
cached.moveTo(p[0], p[1]);
|
|
1203
|
+
if (r.straight) for (let o = 2; o + 5 < p.length; o += 6) cached.lineTo(p[o + 4], p[o + 5]);
|
|
1204
|
+
else for (let o = 2; o + 5 < p.length; o += 6) cached.bezierCurveTo(p[o], p[o + 1], p[o + 2], p[o + 3], p[o + 4], p[o + 5]);
|
|
1205
|
+
if (r.closed) cached.closePath();
|
|
1206
|
+
}
|
|
1207
|
+
path2dCache.set(path, cached);
|
|
1208
|
+
return cached;
|
|
1209
|
+
}
|
|
1210
|
+
function canvasPaint(ctx, paint) {
|
|
1211
|
+
if (!isGradient(paint)) return css(paint);
|
|
1212
|
+
const gradient = paint.type === "linear" ? ctx.createLinearGradient(paint.from[0], paint.from[1], paint.to[0], paint.to[1]) : paint.type === "radial" ? ctx.createRadialGradient(paint.center[0], paint.center[1], 0, paint.center[0], paint.center[1], paint.radius) : null;
|
|
1213
|
+
if (!gradient) return css(sampleStops(paint.stops, 0.5));
|
|
1214
|
+
for (const [at, color] of paint.stops) gradient.addColorStop(at, css(color));
|
|
1215
|
+
return gradient;
|
|
1216
|
+
}
|
|
1217
|
+
var BLEND = {
|
|
1218
|
+
normal: "source-over",
|
|
1219
|
+
screen: "screen",
|
|
1220
|
+
multiply: "multiply",
|
|
1221
|
+
overlay: "overlay"
|
|
1222
|
+
};
|
|
1223
|
+
function paintFrame(ctx, frame, scale = 1, images) {
|
|
1224
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
1225
|
+
ctx.globalAlpha = 1;
|
|
1226
|
+
ctx.globalCompositeOperation = "source-over";
|
|
1227
|
+
ctx.clearRect(0, 0, frame.width * scale, frame.height * scale);
|
|
1228
|
+
ctx.setTransform(scale, 0, 0, scale, 0, 0);
|
|
1229
|
+
if (frame.background) {
|
|
1230
|
+
ctx.fillStyle = frame.background;
|
|
1231
|
+
ctx.fillRect(0, 0, frame.width, frame.height);
|
|
1232
|
+
}
|
|
1233
|
+
for (const op of frame.ops) {
|
|
1234
|
+
if (op.kind === "image") {
|
|
1235
|
+
const source = images?.(op.image);
|
|
1236
|
+
if (!source) continue;
|
|
1237
|
+
const m = op.matrix;
|
|
1238
|
+
ctx.save();
|
|
1239
|
+
ctx.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
1240
|
+
ctx.globalAlpha = op.alpha;
|
|
1241
|
+
ctx.drawImage(source, 0, 0, op.image.width, op.image.height);
|
|
1242
|
+
ctx.restore();
|
|
1243
|
+
continue;
|
|
1244
|
+
}
|
|
1245
|
+
ctx.globalCompositeOperation = BLEND[op.blend];
|
|
1246
|
+
if (op.glow > 0) {
|
|
1247
|
+
const color = glowOf(op);
|
|
1248
|
+
if (color) {
|
|
1249
|
+
const base = Math.max(op.fillAlpha, op.strokeAlpha);
|
|
1250
|
+
const previous = ctx.globalCompositeOperation;
|
|
1251
|
+
if (frame.dark) ctx.globalCompositeOperation = "screen";
|
|
1252
|
+
ctx.strokeStyle = css([color[0], color[1], color[2], 1]);
|
|
1253
|
+
ctx.lineCap = "round";
|
|
1254
|
+
ctx.lineJoin = "round";
|
|
1255
|
+
ctx.setLineDash([]);
|
|
1256
|
+
const glowPath = ringsToPath2D(op.strokePath.length ? op.strokePath : op.path);
|
|
1257
|
+
for (const pass of glowPasses(op.glow, op.stroke ? op.strokeWidth : 0)) {
|
|
1258
|
+
ctx.globalAlpha = pass.alpha * base;
|
|
1259
|
+
ctx.lineWidth = pass.width;
|
|
1260
|
+
ctx.stroke(glowPath);
|
|
1261
|
+
}
|
|
1262
|
+
ctx.globalCompositeOperation = previous;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
if (op.fill) {
|
|
1266
|
+
ctx.globalAlpha = op.fillAlpha;
|
|
1267
|
+
ctx.fillStyle = canvasPaint(ctx, op.fill);
|
|
1268
|
+
ctx.fill(ringsToPath2D(op.path));
|
|
1269
|
+
}
|
|
1270
|
+
if (op.stroke && op.strokePath.length) {
|
|
1271
|
+
ctx.globalAlpha = op.strokeAlpha;
|
|
1272
|
+
ctx.strokeStyle = canvasPaint(ctx, op.stroke);
|
|
1273
|
+
ctx.lineWidth = op.strokeWidth;
|
|
1274
|
+
ctx.lineCap = op.cap;
|
|
1275
|
+
ctx.lineJoin = op.join;
|
|
1276
|
+
ctx.setLineDash(op.dash ? [...op.dash] : []);
|
|
1277
|
+
ctx.lineDashOffset = op.dashOffset;
|
|
1278
|
+
ctx.stroke(ringsToPath2D(op.strokePath));
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
ctx.globalAlpha = 1;
|
|
1282
|
+
ctx.globalCompositeOperation = "source-over";
|
|
1283
|
+
}
|
|
1284
|
+
function frameInkBounds(frame) {
|
|
1285
|
+
let result = null;
|
|
1286
|
+
for (const op of frame.ops) {
|
|
1287
|
+
if (op.kind !== "path") continue;
|
|
1288
|
+
const b = pathBounds(op.path);
|
|
1289
|
+
if (!b) continue;
|
|
1290
|
+
const pad = op.stroke ? op.strokeWidth / 2 : 0;
|
|
1291
|
+
const next = { x: b.x - pad, y: b.y - pad, width: b.width + pad * 2, height: b.height + pad * 2 };
|
|
1292
|
+
if (!result) result = next;
|
|
1293
|
+
else {
|
|
1294
|
+
const x = Math.min(result.x, next.x), y = Math.min(result.y, next.y);
|
|
1295
|
+
result = { x, y, width: Math.max(result.x + result.width, next.x + next.width) - x, height: Math.max(result.y + result.height, next.y + next.height) - y };
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
return result;
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// src/stage/ease.ts
|
|
1302
|
+
var clamp01 = (t) => t <= 0 ? 0 : t >= 1 ? 1 : t;
|
|
1303
|
+
var linear = (t) => t;
|
|
1304
|
+
function sigmoid(x) {
|
|
1305
|
+
return 1 / (1 + Math.exp(-x));
|
|
1306
|
+
}
|
|
1307
|
+
function smoothBy(inflection) {
|
|
1308
|
+
const low = sigmoid(-inflection / 2);
|
|
1309
|
+
const high = sigmoid(inflection / 2);
|
|
1310
|
+
return (t) => {
|
|
1311
|
+
if (t <= 0) return 0;
|
|
1312
|
+
if (t >= 1) return 1;
|
|
1313
|
+
return (sigmoid(inflection * (t - 0.5)) - low) / (high - low);
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
var smooth = smoothBy(10);
|
|
1317
|
+
var smoother = (t) => {
|
|
1318
|
+
const x = clamp01(t);
|
|
1319
|
+
return x * x * x * (x * (x * 6 - 15) + 10);
|
|
1320
|
+
};
|
|
1321
|
+
var easeInQuad = (t) => t * t;
|
|
1322
|
+
var easeOutQuad = (t) => 1 - (1 - t) * (1 - t);
|
|
1323
|
+
var easeInCubic = (t) => t * t * t;
|
|
1324
|
+
var easeOutCubic = (t) => 1 - Math.pow(1 - t, 3);
|
|
1325
|
+
var easeInOutCubic = (t) => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
|
1326
|
+
var easeOutQuint = (t) => 1 - Math.pow(1 - t, 5);
|
|
1327
|
+
var easeInOutQuint = (t) => t < 0.5 ? 16 * t * t * t * t * t : 1 - Math.pow(-2 * t + 2, 5) / 2;
|
|
1328
|
+
var easeOutExpo = (t) => t >= 1 ? 1 : 1 - Math.pow(2, -10 * t);
|
|
1329
|
+
var easeInExpo = (t) => t <= 0 ? 0 : Math.pow(2, 10 * t - 10);
|
|
1330
|
+
var rushInto = (t) => 2 * smooth(t / 2);
|
|
1331
|
+
var rushFrom = (t) => 2 * smooth(t / 2 + 0.5) - 1;
|
|
1332
|
+
var slowInto = (t) => Math.sqrt(Math.max(0, 1 - (1 - t) * (1 - t)));
|
|
1333
|
+
function overshoot(amount = 1.70158) {
|
|
1334
|
+
return (t) => {
|
|
1335
|
+
const x = t - 1;
|
|
1336
|
+
return x * x * ((amount + 1) * x + amount) + 1;
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
function spring(options = {}) {
|
|
1340
|
+
const bounce2 = options.bounce ?? 0.28;
|
|
1341
|
+
const frequency = options.frequency ?? 2.2;
|
|
1342
|
+
const decay = -Math.log(Math.max(1e-4, bounce2)) * 2.4;
|
|
1343
|
+
return (t) => {
|
|
1344
|
+
if (t <= 0) return 0;
|
|
1345
|
+
if (t >= 1) return 1;
|
|
1346
|
+
return 1 - Math.exp(-decay * t) * Math.cos(frequency * Math.PI * 2 * t) * (1 - t);
|
|
1347
|
+
};
|
|
1348
|
+
}
|
|
1349
|
+
var bounce = (t) => {
|
|
1350
|
+
const n = 7.5625, d = 2.75;
|
|
1351
|
+
if (t < 1 / d) return n * t * t;
|
|
1352
|
+
if (t < 2 / d) return n * (t -= 1.5 / d) * t + 0.75;
|
|
1353
|
+
if (t < 2.5 / d) return n * (t -= 2.25 / d) * t + 0.9375;
|
|
1354
|
+
return n * (t -= 2.625 / d) * t + 0.984375;
|
|
1355
|
+
};
|
|
1356
|
+
var thereAndBack = (t) => smooth(t < 0.5 ? 2 * t : 2 * (1 - t));
|
|
1357
|
+
function thereAndBackWithPause(pause = 1 / 3) {
|
|
1358
|
+
const a = (1 - pause) / 2;
|
|
1359
|
+
return (t) => {
|
|
1360
|
+
if (t < a) return smooth(t / a);
|
|
1361
|
+
if (t < 1 - a) return 1;
|
|
1362
|
+
return smooth((1 - t) / a);
|
|
1363
|
+
};
|
|
1364
|
+
}
|
|
1365
|
+
function wiggle(cycles = 2) {
|
|
1366
|
+
return (t) => thereAndBack(t) * Math.sin(cycles * Math.PI * 2 * t);
|
|
1367
|
+
}
|
|
1368
|
+
function notQuiteThere(amount = 0.7, inner = smooth) {
|
|
1369
|
+
return (t) => amount * inner(t);
|
|
1370
|
+
}
|
|
1371
|
+
function lingering(hold = 0.25, inner = smooth) {
|
|
1372
|
+
return (t) => inner(clamp01(t / (1 - hold)));
|
|
1373
|
+
}
|
|
1374
|
+
function runningStart(pull = -0.5) {
|
|
1375
|
+
return (t) => {
|
|
1376
|
+
const u = 1 - t;
|
|
1377
|
+
return 15 * u ** 4 * t ** 2 * pull + 20 * u ** 3 * t ** 3 * pull + 15 * u ** 2 * t ** 4 + 6 * u * t ** 5 + t ** 6;
|
|
1378
|
+
};
|
|
1379
|
+
}
|
|
1380
|
+
function during(from, to2, inner = smooth) {
|
|
1381
|
+
return (t) => inner(clamp01((t - from) / (to2 - from)));
|
|
1382
|
+
}
|
|
1383
|
+
function cubicBezier(x1, y1, x2, y2) {
|
|
1384
|
+
const cx = 3 * x1, bx = 3 * (x2 - x1) - cx, ax = 1 - cx - bx;
|
|
1385
|
+
const cy = 3 * y1, by = 3 * (y2 - y1) - cy, ay = 1 - cy - by;
|
|
1386
|
+
const sampleX = (t) => ((ax * t + bx) * t + cx) * t;
|
|
1387
|
+
const sampleY = (t) => ((ay * t + by) * t + cy) * t;
|
|
1388
|
+
const slope = (t) => (3 * ax * t + 2 * bx) * t + cx;
|
|
1389
|
+
return (x) => {
|
|
1390
|
+
if (x <= 0) return 0;
|
|
1391
|
+
if (x >= 1) return 1;
|
|
1392
|
+
let t = x;
|
|
1393
|
+
for (let i = 0; i < 8; i++) {
|
|
1394
|
+
const dx = sampleX(t) - x;
|
|
1395
|
+
const d = slope(t);
|
|
1396
|
+
if (Math.abs(dx) < 1e-5 || Math.abs(d) < 1e-6) break;
|
|
1397
|
+
t -= dx / d;
|
|
1398
|
+
}
|
|
1399
|
+
return sampleY(t);
|
|
1400
|
+
};
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
// src/stage/morph.ts
|
|
1404
|
+
function growTo(r, target) {
|
|
1405
|
+
const count = segmentCount(r);
|
|
1406
|
+
if (count >= target || count === 0) return r;
|
|
1407
|
+
const segments = [];
|
|
1408
|
+
for (let i = 0; i < count; i++) {
|
|
1409
|
+
const o = i * 6;
|
|
1410
|
+
const pts = Array.from(r.p.subarray(o, o + 8));
|
|
1411
|
+
segments.push({ pts, length: chord(pts) });
|
|
1412
|
+
}
|
|
1413
|
+
while (segments.length < target) {
|
|
1414
|
+
let longest = 0;
|
|
1415
|
+
for (let i = 1; i < segments.length; i++) if (segments[i].length > segments[longest].length) longest = i;
|
|
1416
|
+
const [a, b] = bisect(segments[longest].pts);
|
|
1417
|
+
segments.splice(longest, 1, { pts: a, length: chord(a) }, { pts: b, length: chord(b) });
|
|
1418
|
+
}
|
|
1419
|
+
const out = new Float64Array(2 + target * 6);
|
|
1420
|
+
out[0] = r.p[0];
|
|
1421
|
+
out[1] = r.p[1];
|
|
1422
|
+
for (let i = 0; i < target; i++) {
|
|
1423
|
+
const pts = segments[i].pts;
|
|
1424
|
+
for (let k = 0; k < 6; k++) out[2 + i * 6 + k] = pts[2 + k];
|
|
1425
|
+
}
|
|
1426
|
+
return { p: out, closed: r.closed };
|
|
1427
|
+
}
|
|
1428
|
+
function chord(pts) {
|
|
1429
|
+
return Math.hypot(pts[2] - pts[0], pts[3] - pts[1]) + Math.hypot(pts[4] - pts[2], pts[5] - pts[3]) + Math.hypot(pts[6] - pts[4], pts[7] - pts[5]);
|
|
1430
|
+
}
|
|
1431
|
+
function bisect(p) {
|
|
1432
|
+
const ax = (p[0] + p[2]) / 2, ay = (p[1] + p[3]) / 2;
|
|
1433
|
+
const bx = (p[2] + p[4]) / 2, by = (p[3] + p[5]) / 2;
|
|
1434
|
+
const cx = (p[4] + p[6]) / 2, cy = (p[5] + p[7]) / 2;
|
|
1435
|
+
const dx = (ax + bx) / 2, dy = (ay + by) / 2;
|
|
1436
|
+
const ex = (bx + cx) / 2, ey = (by + cy) / 2;
|
|
1437
|
+
const fx = (dx + ex) / 2, fy = (dy + ey) / 2;
|
|
1438
|
+
return [[p[0], p[1], ax, ay, dx, dy, fx, fy], [fx, fy, ex, ey, cx, cy, p[6], p[7]]];
|
|
1439
|
+
}
|
|
1440
|
+
function bestRotation(a, b) {
|
|
1441
|
+
const count = segmentCount(b);
|
|
1442
|
+
if (!a.closed || !b.closed || count < 2) return b;
|
|
1443
|
+
const [ax, ay] = ringCenter(a), [bx, by] = ringCenter(b);
|
|
1444
|
+
const stride = Math.max(1, Math.floor(count / 48));
|
|
1445
|
+
let best = 0, bestCost = Infinity;
|
|
1446
|
+
for (let shift2 = 0; shift2 < count; shift2 += stride) {
|
|
1447
|
+
let cost = 0;
|
|
1448
|
+
for (let i = 0; i < count; i += stride) {
|
|
1449
|
+
const j = (i + shift2) % count;
|
|
1450
|
+
const dx = a.p[i * 6] - ax - (b.p[j * 6] - bx);
|
|
1451
|
+
const dy = a.p[i * 6 + 1] - ay - (b.p[j * 6 + 1] - by);
|
|
1452
|
+
cost += dx * dx + dy * dy;
|
|
1453
|
+
}
|
|
1454
|
+
if (cost < bestCost) {
|
|
1455
|
+
bestCost = cost;
|
|
1456
|
+
best = shift2;
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
if (best === 0) return b;
|
|
1460
|
+
const out = new Float64Array(b.p.length);
|
|
1461
|
+
out[0] = b.p[best * 6];
|
|
1462
|
+
out[1] = b.p[best * 6 + 1];
|
|
1463
|
+
for (let i = 0; i < count; i++) {
|
|
1464
|
+
const j = (i + best) % count;
|
|
1465
|
+
for (let k = 0; k < 6; k++) out[2 + i * 6 + k] = b.p[2 + j * 6 + k];
|
|
1466
|
+
}
|
|
1467
|
+
return { p: out, closed: b.closed };
|
|
1468
|
+
}
|
|
1469
|
+
function pointRing(x, y, count, closed) {
|
|
1470
|
+
const out = new Float64Array(2 + count * 6);
|
|
1471
|
+
for (let i = 0; i < out.length; i += 2) {
|
|
1472
|
+
out[i] = x;
|
|
1473
|
+
out[i + 1] = y;
|
|
1474
|
+
}
|
|
1475
|
+
return { p: out, closed };
|
|
1476
|
+
}
|
|
1477
|
+
function pairRings(from, to2) {
|
|
1478
|
+
const rank = (path) => [...path].sort((a2, b2) => Math.abs(signedArea(b2)) + ringLength(b2) * 0.01 - (Math.abs(signedArea(a2)) + ringLength(a2) * 0.01));
|
|
1479
|
+
const a = rank(from), b = rank(to2);
|
|
1480
|
+
const pairs = [];
|
|
1481
|
+
const free = b.slice();
|
|
1482
|
+
for (const ringA of a) {
|
|
1483
|
+
if (free.length === 0) {
|
|
1484
|
+
const [x, y] = ringCenter(ringA);
|
|
1485
|
+
pairs.push([ringA, pointRing(x, y, segmentCount(ringA), ringA.closed)]);
|
|
1486
|
+
continue;
|
|
1487
|
+
}
|
|
1488
|
+
const [ax, ay] = ringCenter(ringA);
|
|
1489
|
+
let bestIndex = 0, bestCost = Infinity;
|
|
1490
|
+
for (let i = 0; i < Math.min(free.length, 3); i++) {
|
|
1491
|
+
const [bx, by] = ringCenter(free[i]);
|
|
1492
|
+
const cost = Math.hypot(ax - bx, ay - by) + i * 40;
|
|
1493
|
+
if (cost < bestCost) {
|
|
1494
|
+
bestCost = cost;
|
|
1495
|
+
bestIndex = i;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
pairs.push([ringA, free[bestIndex]]);
|
|
1499
|
+
free.splice(bestIndex, 1);
|
|
1500
|
+
}
|
|
1501
|
+
for (const ringB of free) {
|
|
1502
|
+
const [x, y] = ringCenter(ringB);
|
|
1503
|
+
pairs.push([pointRing(x, y, segmentCount(ringB), ringB.closed), ringB]);
|
|
1504
|
+
}
|
|
1505
|
+
return pairs;
|
|
1506
|
+
}
|
|
1507
|
+
function alignPaths(from, to2, minSegments = 12) {
|
|
1508
|
+
const pairs = pairRings(from, to2);
|
|
1509
|
+
const a = [], b = [], closed = [];
|
|
1510
|
+
for (const [ra, rb] of pairs) {
|
|
1511
|
+
const target = Math.max(segmentCount(ra), segmentCount(rb), ra.closed && rb.closed ? minSegments : 1);
|
|
1512
|
+
let x = growTo(ra, target);
|
|
1513
|
+
let y = growTo(rb, target);
|
|
1514
|
+
if (x.closed && y.closed) {
|
|
1515
|
+
if (Math.sign(signedArea(x)) !== Math.sign(signedArea(y)) && signedArea(x) !== 0 && signedArea(y) !== 0) y = reverseRing(y);
|
|
1516
|
+
y = bestRotation(x, y);
|
|
1517
|
+
} else if (!x.closed && !y.closed) {
|
|
1518
|
+
const n = x.p.length;
|
|
1519
|
+
const straight = Math.hypot(x.p[0] - y.p[0], x.p[1] - y.p[1]) + Math.hypot(x.p[n - 2] - y.p[n - 2], x.p[n - 1] - y.p[n - 1]);
|
|
1520
|
+
const flipped = Math.hypot(x.p[0] - y.p[n - 2], x.p[1] - y.p[n - 1]) + Math.hypot(x.p[n - 2] - y.p[0], x.p[n - 1] - y.p[1]);
|
|
1521
|
+
if (flipped < straight) y = reverseRing(y);
|
|
1522
|
+
}
|
|
1523
|
+
a.push(x.p);
|
|
1524
|
+
b.push(y.p);
|
|
1525
|
+
closed.push(x.closed && y.closed);
|
|
1526
|
+
}
|
|
1527
|
+
return { from: a, to: b, closed };
|
|
1528
|
+
}
|
|
1529
|
+
function pathMixer(from, to2) {
|
|
1530
|
+
if (from === to2) return () => to2;
|
|
1531
|
+
if (pathLength(from) === 0 && from.length === 0) return (alpha) => alpha >= 1 ? to2 : from;
|
|
1532
|
+
const aligned = alignPaths(from, to2);
|
|
1533
|
+
return (alpha) => {
|
|
1534
|
+
if (alpha <= 0) return from;
|
|
1535
|
+
if (alpha >= 1) return to2;
|
|
1536
|
+
const rings = new Array(aligned.from.length);
|
|
1537
|
+
for (let r = 0; r < rings.length; r++) {
|
|
1538
|
+
const a = aligned.from[r], b = aligned.to[r];
|
|
1539
|
+
const out = new Float64Array(a.length);
|
|
1540
|
+
for (let i = 0; i < a.length; i++) out[i] = a[i] + (b[i] - a[i]) * alpha;
|
|
1541
|
+
rings[r] = { p: out, closed: aligned.closed[r] };
|
|
1542
|
+
}
|
|
1543
|
+
return rings;
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
function matchSequence(a, b) {
|
|
1547
|
+
const n = a.length, m = b.length;
|
|
1548
|
+
const table = Array.from({ length: n + 1 }, () => new Uint16Array(m + 1));
|
|
1549
|
+
for (let i2 = n - 1; i2 >= 0; i2--) {
|
|
1550
|
+
for (let j2 = m - 1; j2 >= 0; j2--) {
|
|
1551
|
+
table[i2][j2] = a[i2] === b[j2] ? table[i2 + 1][j2 + 1] + 1 : Math.max(table[i2 + 1][j2], table[i2][j2 + 1]);
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
const pairs = [];
|
|
1555
|
+
let i = 0, j = 0;
|
|
1556
|
+
while (i < n && j < m) {
|
|
1557
|
+
if (a[i] === b[j]) {
|
|
1558
|
+
pairs.push([i++, j++]);
|
|
1559
|
+
} else if (table[i + 1][j] >= table[i][j + 1]) {
|
|
1560
|
+
i++;
|
|
1561
|
+
} else {
|
|
1562
|
+
j++;
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
return pairs;
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
// src/stage/node.ts
|
|
1569
|
+
var DEFAULT_PROPS = {
|
|
1570
|
+
x: 0,
|
|
1571
|
+
y: 0,
|
|
1572
|
+
rotation: 0,
|
|
1573
|
+
sx: 1,
|
|
1574
|
+
sy: 1,
|
|
1575
|
+
px: 0,
|
|
1576
|
+
py: 0,
|
|
1577
|
+
opacity: 1,
|
|
1578
|
+
visible: true,
|
|
1579
|
+
z: 0,
|
|
1580
|
+
path: null,
|
|
1581
|
+
fill: null,
|
|
1582
|
+
fillOpacity: 1,
|
|
1583
|
+
stroke: null,
|
|
1584
|
+
strokeOpacity: 1,
|
|
1585
|
+
strokeWidth: 3,
|
|
1586
|
+
cap: "round",
|
|
1587
|
+
join: "round",
|
|
1588
|
+
dash: null,
|
|
1589
|
+
dashOffset: 0,
|
|
1590
|
+
trimFrom: 0,
|
|
1591
|
+
trimTo: 1,
|
|
1592
|
+
trimEach: false,
|
|
1593
|
+
glow: 0,
|
|
1594
|
+
glowColor: null,
|
|
1595
|
+
blend: "normal",
|
|
1596
|
+
fixed: false,
|
|
1597
|
+
image: null
|
|
1598
|
+
};
|
|
1599
|
+
var NUMERIC = /* @__PURE__ */ new Set([
|
|
1600
|
+
"x",
|
|
1601
|
+
"y",
|
|
1602
|
+
"rotation",
|
|
1603
|
+
"sx",
|
|
1604
|
+
"sy",
|
|
1605
|
+
"px",
|
|
1606
|
+
"py",
|
|
1607
|
+
"opacity",
|
|
1608
|
+
"z",
|
|
1609
|
+
"fillOpacity",
|
|
1610
|
+
"strokeOpacity",
|
|
1611
|
+
"strokeWidth",
|
|
1612
|
+
"dashOffset",
|
|
1613
|
+
"trimFrom",
|
|
1614
|
+
"trimTo",
|
|
1615
|
+
"glow"
|
|
1616
|
+
]);
|
|
1617
|
+
function mixer(prop, from, to2) {
|
|
1618
|
+
if (NUMERIC.has(prop)) {
|
|
1619
|
+
const a = from, b = to2;
|
|
1620
|
+
return ((alpha) => a + (b - a) * alpha);
|
|
1621
|
+
}
|
|
1622
|
+
if (prop === "fill" || prop === "stroke") {
|
|
1623
|
+
const a = from, b = to2;
|
|
1624
|
+
if (a && b) return ((alpha) => mixPaint(a, b, alpha));
|
|
1625
|
+
return null;
|
|
1626
|
+
}
|
|
1627
|
+
if (prop === "glowColor") {
|
|
1628
|
+
const a = from, b = to2;
|
|
1629
|
+
if (a && b) return ((alpha) => mixColor(a, b, alpha));
|
|
1630
|
+
return null;
|
|
1631
|
+
}
|
|
1632
|
+
if (prop === "path") {
|
|
1633
|
+
const a = from, b = to2;
|
|
1634
|
+
if (a && b) return pathMixer(a, b);
|
|
1635
|
+
return null;
|
|
1636
|
+
}
|
|
1637
|
+
return null;
|
|
1638
|
+
}
|
|
1639
|
+
var nextId = 1;
|
|
1640
|
+
var Node = class _Node {
|
|
1641
|
+
id = nextId++;
|
|
1642
|
+
parent = null;
|
|
1643
|
+
children = [];
|
|
1644
|
+
/** Free-form metadata, such as the source character of a glyph. */
|
|
1645
|
+
data = {};
|
|
1646
|
+
/** Label shown in timelines and diagnostics. */
|
|
1647
|
+
label = "";
|
|
1648
|
+
base = Object.create(DEFAULT_PROPS);
|
|
1649
|
+
tracks = null;
|
|
1650
|
+
overlay = null;
|
|
1651
|
+
overlayFrame = -1;
|
|
1652
|
+
/** On stage: writes become timeline keys instead of initial state. */
|
|
1653
|
+
mounted = false;
|
|
1654
|
+
/** Rebuilt on every frame from timeline values. */
|
|
1655
|
+
draw = null;
|
|
1656
|
+
/** Emits draw operations directly, for objects with thousands of marks such as 3D meshes. */
|
|
1657
|
+
rawOps = null;
|
|
1658
|
+
constructor(path) {
|
|
1659
|
+
if (path) this.base.path = path;
|
|
1660
|
+
}
|
|
1661
|
+
// ── Raw property access ───────────────────────────────────────────────
|
|
1662
|
+
get(prop) {
|
|
1663
|
+
const overlay = this.overlay;
|
|
1664
|
+
if (overlay !== null && overlay.has(prop) && (this.overlayFrame === clock.frame || clock.capture !== null)) {
|
|
1665
|
+
return overlay.get(prop);
|
|
1666
|
+
}
|
|
1667
|
+
const track = this.tracks?.get(prop);
|
|
1668
|
+
if (track) return track.sample(clock.now);
|
|
1669
|
+
return this.base[prop];
|
|
1670
|
+
}
|
|
1671
|
+
set(prop, value) {
|
|
1672
|
+
if (clock.capture) {
|
|
1673
|
+
this.overlay ??= /* @__PURE__ */ new Map();
|
|
1674
|
+
this.overlay.set(prop, value);
|
|
1675
|
+
clock.capture.add(this);
|
|
1676
|
+
return this;
|
|
1677
|
+
}
|
|
1678
|
+
if (!this.mounted) {
|
|
1679
|
+
this.base[prop] = value;
|
|
1680
|
+
return this;
|
|
1681
|
+
}
|
|
1682
|
+
if (clock.mode === "render") {
|
|
1683
|
+
this.overlay ??= /* @__PURE__ */ new Map();
|
|
1684
|
+
if (this.overlayFrame !== clock.frame) {
|
|
1685
|
+
this.overlay.clear();
|
|
1686
|
+
this.overlayFrame = clock.frame;
|
|
1687
|
+
}
|
|
1688
|
+
this.overlay.set(prop, value);
|
|
1689
|
+
return this;
|
|
1690
|
+
}
|
|
1691
|
+
this.track(prop).step(clock.now, value);
|
|
1692
|
+
return this;
|
|
1693
|
+
}
|
|
1694
|
+
track(prop) {
|
|
1695
|
+
this.tracks ??= /* @__PURE__ */ new Map();
|
|
1696
|
+
let track = this.tracks.get(prop);
|
|
1697
|
+
if (!track) {
|
|
1698
|
+
track = new Track(this.base[prop]);
|
|
1699
|
+
this.tracks.set(prop, track);
|
|
1700
|
+
}
|
|
1701
|
+
return track;
|
|
1702
|
+
}
|
|
1703
|
+
/** Record a change from the current value to `to` over [t0, t1]. */
|
|
1704
|
+
tween(prop, to2, t0, t1, ease) {
|
|
1705
|
+
const previous = clock.now;
|
|
1706
|
+
clock.now = t0;
|
|
1707
|
+
const from = this.get(prop);
|
|
1708
|
+
clock.now = previous;
|
|
1709
|
+
const at = t1 > t0 ? mixer(prop, from, to2) : null;
|
|
1710
|
+
const key = { t0, t1, ease, to: to2, at };
|
|
1711
|
+
this.track(prop).add(key);
|
|
1712
|
+
}
|
|
1713
|
+
// ── Tree ──────────────────────────────────────────────────────────────
|
|
1714
|
+
add(...nodes) {
|
|
1715
|
+
for (const child of nodes) {
|
|
1716
|
+
if (child.parent === this) continue;
|
|
1717
|
+
child.parent?.drop(child);
|
|
1718
|
+
child.parent = this;
|
|
1719
|
+
this.children.push(child);
|
|
1720
|
+
if (this.mounted) mountTree(child, clock.now);
|
|
1721
|
+
}
|
|
1722
|
+
return this;
|
|
1723
|
+
}
|
|
1724
|
+
drop(child) {
|
|
1725
|
+
const index = this.children.indexOf(child);
|
|
1726
|
+
if (index >= 0) this.children.splice(index, 1);
|
|
1727
|
+
child.parent = null;
|
|
1728
|
+
return this;
|
|
1729
|
+
}
|
|
1730
|
+
/** Self and all descendants, parents first. */
|
|
1731
|
+
all() {
|
|
1732
|
+
const out = [];
|
|
1733
|
+
const visit = (node) => {
|
|
1734
|
+
out.push(node);
|
|
1735
|
+
for (const child of node.children) visit(child);
|
|
1736
|
+
};
|
|
1737
|
+
visit(this);
|
|
1738
|
+
return out;
|
|
1739
|
+
}
|
|
1740
|
+
/** Descendants that draw something. A leaf group returns itself if it draws. */
|
|
1741
|
+
leaves() {
|
|
1742
|
+
return this.all().filter((node) => node.get("path") !== null || node.draw !== null || node.get("image") !== null);
|
|
1743
|
+
}
|
|
1744
|
+
/** A static copy of the current state, off stage. */
|
|
1745
|
+
clone() {
|
|
1746
|
+
const copy = Object.create(Object.getPrototypeOf(this));
|
|
1747
|
+
Object.assign(copy, this);
|
|
1748
|
+
copy.id = nextId++;
|
|
1749
|
+
copy.parent = null;
|
|
1750
|
+
copy.mounted = false;
|
|
1751
|
+
copy.tracks = null;
|
|
1752
|
+
copy.overlay = null;
|
|
1753
|
+
copy.overlayFrame = -1;
|
|
1754
|
+
copy.data = { ...this.data };
|
|
1755
|
+
copy.base = Object.create(DEFAULT_PROPS);
|
|
1756
|
+
for (const key of Object.keys(DEFAULT_PROPS)) {
|
|
1757
|
+
;
|
|
1758
|
+
copy.base[key] = this.get(key);
|
|
1759
|
+
}
|
|
1760
|
+
copy.children = this.children.map((child) => {
|
|
1761
|
+
const next = child.clone();
|
|
1762
|
+
next.parent = copy;
|
|
1763
|
+
return next;
|
|
1764
|
+
});
|
|
1765
|
+
return copy;
|
|
1766
|
+
}
|
|
1767
|
+
// ── Transforms ────────────────────────────────────────────────────────
|
|
1768
|
+
localMatrix() {
|
|
1769
|
+
return compose(this.get("x"), this.get("y"), this.get("rotation"), this.get("sx"), this.get("sy"), this.get("px"), this.get("py"));
|
|
1770
|
+
}
|
|
1771
|
+
worldMatrix() {
|
|
1772
|
+
let m = this.localMatrix();
|
|
1773
|
+
for (let node = this.parent; node; node = node.parent) m = mul(node.localMatrix(), m);
|
|
1774
|
+
return m;
|
|
1775
|
+
}
|
|
1776
|
+
toWorld(x, y) {
|
|
1777
|
+
return apply(this.worldMatrix(), x, y);
|
|
1778
|
+
}
|
|
1779
|
+
/** Stage-space bounds of this node and its descendants. */
|
|
1780
|
+
bounds() {
|
|
1781
|
+
return this.boundsFrom(this.parent ? this.parent.worldMatrix() : IDENTITY) ?? { x: 0, y: 0, width: 0, height: 0 };
|
|
1782
|
+
}
|
|
1783
|
+
boundsFrom(parent) {
|
|
1784
|
+
const m = mul(parent, this.localMatrix());
|
|
1785
|
+
let result = null;
|
|
1786
|
+
const path = this.get("path");
|
|
1787
|
+
if (path) result = pathBounds(transformPath(path, m));
|
|
1788
|
+
const image = this.get("image");
|
|
1789
|
+
if (image) result = unionBounds(result, boundsOfRect(m, image.width, image.height));
|
|
1790
|
+
if (this.draw) {
|
|
1791
|
+
const built = this.draw(clock.now);
|
|
1792
|
+
const list2 = built === null ? [] : Array.isArray(built) ? built : [built];
|
|
1793
|
+
for (const child of list2) result = unionBounds(result, child.boundsFrom(m));
|
|
1794
|
+
}
|
|
1795
|
+
for (const child of this.children) {
|
|
1796
|
+
if (child.get("visible")) result = unionBounds(result, child.boundsFrom(m));
|
|
1797
|
+
}
|
|
1798
|
+
return result;
|
|
1799
|
+
}
|
|
1800
|
+
get center() {
|
|
1801
|
+
const b = this.bounds();
|
|
1802
|
+
return [b.x + b.width / 2, b.y + b.height / 2];
|
|
1803
|
+
}
|
|
1804
|
+
get top() {
|
|
1805
|
+
const b = this.bounds();
|
|
1806
|
+
return [b.x + b.width / 2, b.y];
|
|
1807
|
+
}
|
|
1808
|
+
get bottom() {
|
|
1809
|
+
const b = this.bounds();
|
|
1810
|
+
return [b.x + b.width / 2, b.y + b.height];
|
|
1811
|
+
}
|
|
1812
|
+
get left() {
|
|
1813
|
+
const b = this.bounds();
|
|
1814
|
+
return [b.x, b.y + b.height / 2];
|
|
1815
|
+
}
|
|
1816
|
+
get right() {
|
|
1817
|
+
const b = this.bounds();
|
|
1818
|
+
return [b.x + b.width, b.y + b.height / 2];
|
|
1819
|
+
}
|
|
1820
|
+
get width() {
|
|
1821
|
+
return this.bounds().width;
|
|
1822
|
+
}
|
|
1823
|
+
get height() {
|
|
1824
|
+
return this.bounds().height;
|
|
1825
|
+
}
|
|
1826
|
+
/** Set the local position of the origin. */
|
|
1827
|
+
at(x, y) {
|
|
1828
|
+
const [px, py] = typeof x === "number" ? [x, y ?? 0] : x;
|
|
1829
|
+
this.set("x", px);
|
|
1830
|
+
this.set("y", py);
|
|
1831
|
+
return this;
|
|
1832
|
+
}
|
|
1833
|
+
shift(dx, dy) {
|
|
1834
|
+
const [ox, oy] = typeof dx === "number" ? [dx, dy ?? 0] : dx;
|
|
1835
|
+
this.set("x", this.get("x") + ox);
|
|
1836
|
+
this.set("y", this.get("y") + oy);
|
|
1837
|
+
return this;
|
|
1838
|
+
}
|
|
1839
|
+
/** Move so the center of the bounds lands on a stage point or on another node's center. */
|
|
1840
|
+
moveTo(target) {
|
|
1841
|
+
const destination = target instanceof _Node ? target.center : target;
|
|
1842
|
+
const [cx, cy] = this.center;
|
|
1843
|
+
const inverse = this.parent ? invert(this.parent.worldMatrix()) : IDENTITY;
|
|
1844
|
+
const [lx0, ly0] = apply(inverse, cx, cy);
|
|
1845
|
+
const [lx1, ly1] = apply(inverse, destination[0], destination[1]);
|
|
1846
|
+
return this.shift(lx1 - lx0, ly1 - ly0);
|
|
1847
|
+
}
|
|
1848
|
+
/** Move the origin to a stage point without changing how anything looks. */
|
|
1849
|
+
pivotAt(point) {
|
|
1850
|
+
const parent = this.parent ? this.parent.worldMatrix() : IDENTITY;
|
|
1851
|
+
const before = mul(parent, this.localMatrix());
|
|
1852
|
+
const [lx, ly] = apply(invert(before), point[0], point[1]);
|
|
1853
|
+
const [nx, ny] = apply(invert(parent), point[0], point[1]);
|
|
1854
|
+
this.set("px", lx);
|
|
1855
|
+
this.set("py", ly);
|
|
1856
|
+
this.set("x", nx);
|
|
1857
|
+
this.set("y", ny);
|
|
1858
|
+
return this;
|
|
1859
|
+
}
|
|
1860
|
+
/** Scale around the center, or around a given stage point. */
|
|
1861
|
+
scale(factor, about) {
|
|
1862
|
+
const [fx, fy] = typeof factor === "number" ? [factor, factor] : factor;
|
|
1863
|
+
this.pivotAt(about ?? this.center);
|
|
1864
|
+
this.set("sx", this.get("sx") * fx);
|
|
1865
|
+
this.set("sy", this.get("sy") * fy);
|
|
1866
|
+
return this;
|
|
1867
|
+
}
|
|
1868
|
+
/** Rotate around the center, or around a given stage point. */
|
|
1869
|
+
rotate(radians, about) {
|
|
1870
|
+
this.pivotAt(about ?? this.center);
|
|
1871
|
+
this.set("rotation", this.get("rotation") + radians);
|
|
1872
|
+
return this;
|
|
1873
|
+
}
|
|
1874
|
+
/** Scale to an exact on-screen width or height, keeping proportions. */
|
|
1875
|
+
fitTo(size) {
|
|
1876
|
+
const b = this.bounds();
|
|
1877
|
+
const scales = [];
|
|
1878
|
+
if (size.width !== void 0 && b.width > 0) scales.push(size.width / b.width);
|
|
1879
|
+
if (size.height !== void 0 && b.height > 0) scales.push(size.height / b.height);
|
|
1880
|
+
return scales.length ? this.scale(Math.min(...scales)) : this;
|
|
1881
|
+
}
|
|
1882
|
+
// ── Layout ────────────────────────────────────────────────────────────
|
|
1883
|
+
/** Place beside another node or stage point, with a gap measured between bounds. */
|
|
1884
|
+
nextTo(target, side = "right", gap = 28) {
|
|
1885
|
+
const [dx, dy] = direction(side);
|
|
1886
|
+
const b = target instanceof _Node ? target.bounds() : { x: target[0], y: target[1], width: 0, height: 0 };
|
|
1887
|
+
const own = this.bounds();
|
|
1888
|
+
const cx = b.x + b.width / 2, cy = b.y + b.height / 2;
|
|
1889
|
+
const anchorX = cx + dx * (b.width / 2 + gap + own.width / 2);
|
|
1890
|
+
const anchorY = cy + dy * (b.height / 2 + gap + own.height / 2);
|
|
1891
|
+
return this.moveTo([anchorX, anchorY]);
|
|
1892
|
+
}
|
|
1893
|
+
/** Line up one edge with another node's same edge. */
|
|
1894
|
+
alignTo(target, edge) {
|
|
1895
|
+
const own = this.bounds();
|
|
1896
|
+
const b = target instanceof _Node ? target.bounds() : { x: target[0], y: target[1], width: 0, height: 0 };
|
|
1897
|
+
if (edge === "left") return this.shift(b.x - own.x, 0);
|
|
1898
|
+
if (edge === "right") return this.shift(b.x + b.width - own.x - own.width, 0);
|
|
1899
|
+
if (edge === "top") return this.shift(0, b.y - own.y);
|
|
1900
|
+
return this.shift(0, b.y + b.height - own.y - own.height);
|
|
1901
|
+
}
|
|
1902
|
+
/** Lay out children in a row or column, keeping the group's center. */
|
|
1903
|
+
arrange(side = "right", gap = 24) {
|
|
1904
|
+
const [dx, dy] = direction(side);
|
|
1905
|
+
const center = this.center;
|
|
1906
|
+
let previous = null;
|
|
1907
|
+
for (const child of this.children) {
|
|
1908
|
+
if (previous) {
|
|
1909
|
+
const b = previous.bounds(), own = child.bounds();
|
|
1910
|
+
const px = b.x + b.width / 2 + dx * (b.width / 2 + gap + own.width / 2);
|
|
1911
|
+
const py = b.y + b.height / 2 + dy * (b.height / 2 + gap + own.height / 2);
|
|
1912
|
+
child.moveTo([dx === 0 ? b.x + b.width / 2 : px, dy === 0 ? b.y + b.height / 2 : py]);
|
|
1913
|
+
}
|
|
1914
|
+
previous = child;
|
|
1915
|
+
}
|
|
1916
|
+
return this.moveTo(center);
|
|
1917
|
+
}
|
|
1918
|
+
// ── Paint ─────────────────────────────────────────────────────────────
|
|
1919
|
+
fill(color, opacity) {
|
|
1920
|
+
this.set("fill", color === null ? null : toPaint(color));
|
|
1921
|
+
if (opacity !== void 0) this.set("fillOpacity", opacity);
|
|
1922
|
+
return this;
|
|
1923
|
+
}
|
|
1924
|
+
stroke(color, width, opacity) {
|
|
1925
|
+
this.set("stroke", color === null ? null : toPaint(color));
|
|
1926
|
+
if (width !== void 0) this.set("strokeWidth", width);
|
|
1927
|
+
if (opacity !== void 0) this.set("strokeOpacity", opacity);
|
|
1928
|
+
return this;
|
|
1929
|
+
}
|
|
1930
|
+
/** Recolor this node and its descendants, keeping whichever paints exist. */
|
|
1931
|
+
color(color) {
|
|
1932
|
+
const paint = toPaint(color);
|
|
1933
|
+
for (const node of this.all()) {
|
|
1934
|
+
if (node.get("stroke")) node.set("stroke", paint);
|
|
1935
|
+
if (node.get("fill")) node.set("fill", paint);
|
|
1936
|
+
if (node.get("glowColor") && !isGradient(paint)) node.set("glowColor", paint);
|
|
1937
|
+
}
|
|
1938
|
+
return this;
|
|
1939
|
+
}
|
|
1940
|
+
opacity(value) {
|
|
1941
|
+
return this.set("opacity", value);
|
|
1942
|
+
}
|
|
1943
|
+
/** Soft light around the shape. The radius is in stage pixels. */
|
|
1944
|
+
glow(radius, color) {
|
|
1945
|
+
this.set("glow", radius);
|
|
1946
|
+
if (color !== void 0) {
|
|
1947
|
+
const paint = toPaint(color);
|
|
1948
|
+
this.set("glowColor", isGradient(paint) ? null : paint);
|
|
1949
|
+
}
|
|
1950
|
+
return this;
|
|
1951
|
+
}
|
|
1952
|
+
dash(pattern, offset = 0) {
|
|
1953
|
+
this.set("dash", pattern);
|
|
1954
|
+
return this.set("dashOffset", offset);
|
|
1955
|
+
}
|
|
1956
|
+
zIndex(value) {
|
|
1957
|
+
return this.set("z", value);
|
|
1958
|
+
}
|
|
1959
|
+
/** Pin to the screen so the camera does not move this node. */
|
|
1960
|
+
pin() {
|
|
1961
|
+
return this.set("fixed", true);
|
|
1962
|
+
}
|
|
1963
|
+
hide() {
|
|
1964
|
+
return this.set("visible", false);
|
|
1965
|
+
}
|
|
1966
|
+
show() {
|
|
1967
|
+
return this.set("visible", true);
|
|
1968
|
+
}
|
|
1969
|
+
named(label) {
|
|
1970
|
+
this.label = label;
|
|
1971
|
+
return this;
|
|
1972
|
+
}
|
|
1973
|
+
};
|
|
1974
|
+
function boundsOfRect(m, width, height) {
|
|
1975
|
+
const corners = [apply(m, 0, 0), apply(m, width, 0), apply(m, width, height), apply(m, 0, height)];
|
|
1976
|
+
const xs = corners.map((c) => c[0]), ys = corners.map((c) => c[1]);
|
|
1977
|
+
const x = Math.min(...xs), y = Math.min(...ys);
|
|
1978
|
+
return { x, y, width: Math.max(...xs) - x, height: Math.max(...ys) - y };
|
|
1979
|
+
}
|
|
1980
|
+
function direction(side) {
|
|
1981
|
+
if (typeof side !== "string") return side;
|
|
1982
|
+
if (side === "left") return [-1, 0];
|
|
1983
|
+
if (side === "right") return [1, 0];
|
|
1984
|
+
if (side === "top" || side === "up") return [0, -1];
|
|
1985
|
+
return [0, 1];
|
|
1986
|
+
}
|
|
1987
|
+
function mountTree(node, time) {
|
|
1988
|
+
const wasVisible = node.base.visible;
|
|
1989
|
+
for (const entry of node.all()) entry.mounted = true;
|
|
1990
|
+
if (time > 0 || !wasVisible) {
|
|
1991
|
+
const track = node.track("visible");
|
|
1992
|
+
track.initial = false;
|
|
1993
|
+
if (wasVisible) track.step(time, true);
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
function group(...children) {
|
|
1997
|
+
return new Node().add(...children);
|
|
1998
|
+
}
|
|
1999
|
+
function dynamic(build) {
|
|
2000
|
+
const node = new Node();
|
|
2001
|
+
node.draw = build;
|
|
2002
|
+
return node;
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
// src/stage/anim.ts
|
|
2006
|
+
function list(target) {
|
|
2007
|
+
return Array.isArray(target) ? [...target] : [target];
|
|
2008
|
+
}
|
|
2009
|
+
function run(anim, t0, t1, parent) {
|
|
2010
|
+
clock.now = t0;
|
|
2011
|
+
anim.schedule({ t0, t1, ease: anim.ease ?? parent.ease, stage: parent.stage });
|
|
2012
|
+
}
|
|
2013
|
+
function par(...anims) {
|
|
2014
|
+
const duration = Math.max(0, ...anims.map((a) => a.duration));
|
|
2015
|
+
return {
|
|
2016
|
+
label: "par",
|
|
2017
|
+
duration,
|
|
2018
|
+
schedule(ctx) {
|
|
2019
|
+
const k = duration > 0 ? (ctx.t1 - ctx.t0) / duration : 1;
|
|
2020
|
+
for (const anim of anims) run(anim, ctx.t0, ctx.t0 + anim.duration * k, ctx);
|
|
2021
|
+
}
|
|
2022
|
+
};
|
|
2023
|
+
}
|
|
2024
|
+
function seq(...anims) {
|
|
2025
|
+
const duration = anims.reduce((sum, a) => sum + a.duration, 0);
|
|
2026
|
+
return {
|
|
2027
|
+
label: "seq",
|
|
2028
|
+
duration,
|
|
2029
|
+
schedule(ctx) {
|
|
2030
|
+
const k = duration > 0 ? (ctx.t1 - ctx.t0) / duration : 1;
|
|
2031
|
+
let t = ctx.t0;
|
|
2032
|
+
for (const anim of anims) {
|
|
2033
|
+
const next = t + anim.duration * k;
|
|
2034
|
+
run(anim, t, next, ctx);
|
|
2035
|
+
t = next;
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
};
|
|
2039
|
+
}
|
|
2040
|
+
function lagged(anims, options = {}) {
|
|
2041
|
+
const lag = options.lag ?? 0.15;
|
|
2042
|
+
const longest = Math.max(0, ...anims.map((a) => a.duration));
|
|
2043
|
+
const natural = anims.length === 0 ? 0 : longest * (1 + (anims.length - 1) * lag);
|
|
2044
|
+
return {
|
|
2045
|
+
label: "lagged",
|
|
2046
|
+
duration: options.duration ?? natural,
|
|
2047
|
+
schedule(ctx) {
|
|
2048
|
+
const total = ctx.t1 - ctx.t0;
|
|
2049
|
+
const each = anims.length === 0 ? 0 : total / (1 + (anims.length - 1) * lag);
|
|
2050
|
+
anims.forEach((anim, i) => {
|
|
2051
|
+
const start = ctx.t0 + i * lag * each;
|
|
2052
|
+
run(anim, start, start + each, ctx);
|
|
2053
|
+
});
|
|
2054
|
+
}
|
|
2055
|
+
};
|
|
2056
|
+
}
|
|
2057
|
+
function delay(anim, seconds) {
|
|
2058
|
+
return {
|
|
2059
|
+
label: anim.label,
|
|
2060
|
+
duration: anim.duration + seconds,
|
|
2061
|
+
ease: anim.ease,
|
|
2062
|
+
schedule(ctx) {
|
|
2063
|
+
const k = anim.duration + seconds > 0 ? (ctx.t1 - ctx.t0) / (anim.duration + seconds) : 1;
|
|
2064
|
+
run(anim, ctx.t0 + seconds * k, ctx.t1, ctx);
|
|
2065
|
+
}
|
|
2066
|
+
};
|
|
2067
|
+
}
|
|
2068
|
+
function retime(anim, options) {
|
|
2069
|
+
return { ...anim, duration: options.duration ?? anim.duration, ease: options.ease ?? anim.ease };
|
|
2070
|
+
}
|
|
2071
|
+
function call(fn) {
|
|
2072
|
+
return { label: "call", duration: 0, schedule: () => fn() };
|
|
2073
|
+
}
|
|
2074
|
+
function ensureVisible(node, t, stage) {
|
|
2075
|
+
if (!node.mounted) {
|
|
2076
|
+
let top = node;
|
|
2077
|
+
while (top.parent && top.parent !== stage.root) top = top.parent;
|
|
2078
|
+
stage.mountAt(top, t);
|
|
2079
|
+
}
|
|
2080
|
+
for (let n = node; n && n !== stage.root; n = n.parent) {
|
|
2081
|
+
const previous = clock.now;
|
|
2082
|
+
clock.now = t;
|
|
2083
|
+
const visible = n.get("visible");
|
|
2084
|
+
clock.now = previous;
|
|
2085
|
+
if (!visible) n.track("visible").step(t, true);
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
function hideAt(node, t) {
|
|
2089
|
+
node.track("visible").step(t, false);
|
|
2090
|
+
}
|
|
2091
|
+
function holdUntil(node, t0, start) {
|
|
2092
|
+
if (start <= t0) return;
|
|
2093
|
+
node.track("visible").step(t0, false);
|
|
2094
|
+
node.track("visible").step(start, true);
|
|
2095
|
+
}
|
|
2096
|
+
var stepAt = (node, prop, value, t) => {
|
|
2097
|
+
node.track(prop).step(t, value);
|
|
2098
|
+
};
|
|
2099
|
+
function valueAt(node, prop, t) {
|
|
2100
|
+
const previous = clock.now;
|
|
2101
|
+
clock.now = t;
|
|
2102
|
+
const value = node.get(prop);
|
|
2103
|
+
clock.now = previous;
|
|
2104
|
+
return value;
|
|
2105
|
+
}
|
|
2106
|
+
function solid2(paint) {
|
|
2107
|
+
return isGradient(paint) ? sampleStops(paint.stops, 0.5) : paint;
|
|
2108
|
+
}
|
|
2109
|
+
function drawable(target) {
|
|
2110
|
+
return list(target).flatMap((node) => node.leaves().filter((leaf) => leaf.get("path")));
|
|
2111
|
+
}
|
|
2112
|
+
function drawLeaf(leaf, t0, t1, ease, outline) {
|
|
2113
|
+
const d = t1 - t0;
|
|
2114
|
+
const fill = leaf.get("fill"), stroke = leaf.get("stroke");
|
|
2115
|
+
const fillTarget = leaf.get("fillOpacity");
|
|
2116
|
+
const strokeTarget = leaf.get("strokeOpacity");
|
|
2117
|
+
if (fill && !stroke) {
|
|
2118
|
+
const width = valueAt(leaf, "strokeWidth", t0);
|
|
2119
|
+
stepAt(leaf, "stroke", solid2(fill), t0);
|
|
2120
|
+
stepAt(leaf, "strokeWidth", outline, t0);
|
|
2121
|
+
stepAt(leaf, "strokeOpacity", 1, t0);
|
|
2122
|
+
stepAt(leaf, "trimTo", 0, t0);
|
|
2123
|
+
stepAt(leaf, "fillOpacity", 0, t0);
|
|
2124
|
+
leaf.tween("trimTo", 1, t0, t0 + d * 0.72, ease);
|
|
2125
|
+
leaf.tween("fillOpacity", fillTarget, t0 + d * 0.42, t1, ease);
|
|
2126
|
+
leaf.tween("strokeOpacity", 0, t0 + d * 0.62, t1, ease);
|
|
2127
|
+
stepAt(leaf, "stroke", null, t1);
|
|
2128
|
+
stepAt(leaf, "strokeWidth", width, t1);
|
|
2129
|
+
stepAt(leaf, "strokeOpacity", strokeTarget, t1);
|
|
2130
|
+
return;
|
|
2131
|
+
}
|
|
2132
|
+
stepAt(leaf, "trimTo", 0, t0);
|
|
2133
|
+
leaf.tween("trimTo", 1, t0, fill ? t0 + d * 0.8 : t1, ease);
|
|
2134
|
+
if (fill) {
|
|
2135
|
+
stepAt(leaf, "fillOpacity", 0, t0);
|
|
2136
|
+
leaf.tween("fillOpacity", fillTarget, t0 + d * 0.5, t1, ease);
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
function drawAnim(label, target, options, defaults) {
|
|
2140
|
+
const nodes = list(target);
|
|
2141
|
+
const count = drawable(nodes).length;
|
|
2142
|
+
const lag = options.lag ?? defaults.lag;
|
|
2143
|
+
const natural = options.duration ?? (count <= 1 ? defaults.duration : Math.min(defaults.duration * 2.4, defaults.duration + count * 0.035));
|
|
2144
|
+
return {
|
|
2145
|
+
label,
|
|
2146
|
+
duration: natural,
|
|
2147
|
+
ease: options.ease,
|
|
2148
|
+
schedule(ctx) {
|
|
2149
|
+
for (const node of nodes) ensureVisible(node, ctx.t0, ctx.stage);
|
|
2150
|
+
const leaves = drawable(nodes);
|
|
2151
|
+
if (leaves.length === 0) return;
|
|
2152
|
+
const total = ctx.t1 - ctx.t0;
|
|
2153
|
+
const each = total / (1 + (leaves.length - 1) * lag);
|
|
2154
|
+
leaves.forEach((leaf, i) => {
|
|
2155
|
+
const start = ctx.t0 + i * lag * each;
|
|
2156
|
+
clock.now = start;
|
|
2157
|
+
holdUntil(leaf, ctx.t0, start);
|
|
2158
|
+
drawLeaf(leaf, start, start + each, ctx.ease, defaults.outline);
|
|
2159
|
+
});
|
|
2160
|
+
clock.now = ctx.t0;
|
|
2161
|
+
}
|
|
2162
|
+
};
|
|
2163
|
+
}
|
|
2164
|
+
function draw(target, options = {}) {
|
|
2165
|
+
return drawAnim("draw", target, options, { lag: 0.04, duration: 1.3, outline: 2 });
|
|
2166
|
+
}
|
|
2167
|
+
function write(target, options = {}) {
|
|
2168
|
+
return drawAnim("write", target, options, { lag: 0.22, duration: 1.1, outline: 1.3 });
|
|
2169
|
+
}
|
|
2170
|
+
function erase(target, options = {}) {
|
|
2171
|
+
const nodes = list(target);
|
|
2172
|
+
return {
|
|
2173
|
+
label: "erase",
|
|
2174
|
+
duration: options.duration ?? 0.8,
|
|
2175
|
+
ease: options.ease,
|
|
2176
|
+
schedule(ctx) {
|
|
2177
|
+
const leaves = drawable(nodes);
|
|
2178
|
+
const lag = options.lag ?? 0.02;
|
|
2179
|
+
const each = (ctx.t1 - ctx.t0) / (1 + Math.max(0, leaves.length - 1) * lag);
|
|
2180
|
+
leaves.forEach((leaf, i) => {
|
|
2181
|
+
const start = ctx.t0 + i * lag * each;
|
|
2182
|
+
clock.now = start;
|
|
2183
|
+
const fill = leaf.get("fill");
|
|
2184
|
+
if (leaf.get("stroke")) leaf.tween("trimFrom", 1, start, start + each, ctx.ease);
|
|
2185
|
+
if (fill) leaf.tween("fillOpacity", 0, start, start + each * 0.7, ctx.ease);
|
|
2186
|
+
});
|
|
2187
|
+
for (const node of nodes) hideAt(node, ctx.t1);
|
|
2188
|
+
}
|
|
2189
|
+
};
|
|
2190
|
+
}
|
|
2191
|
+
function fadeIn(target, options = {}) {
|
|
2192
|
+
const nodes = list(target);
|
|
2193
|
+
return {
|
|
2194
|
+
label: "fadeIn",
|
|
2195
|
+
duration: options.duration ?? 0.8,
|
|
2196
|
+
ease: options.ease,
|
|
2197
|
+
schedule(ctx) {
|
|
2198
|
+
const items = options.lag !== void 0 && nodes.length === 1 && nodes[0].children.length > 0 ? nodes[0].children : nodes;
|
|
2199
|
+
for (const node of nodes) ensureVisible(node, ctx.t0, ctx.stage);
|
|
2200
|
+
const lag = options.lag ?? 0;
|
|
2201
|
+
const each = (ctx.t1 - ctx.t0) / (1 + Math.max(0, items.length - 1) * lag);
|
|
2202
|
+
items.forEach((node, i) => {
|
|
2203
|
+
const start = ctx.t0 + i * lag * each, end = start + each;
|
|
2204
|
+
clock.now = start;
|
|
2205
|
+
holdUntil(node, ctx.t0, start);
|
|
2206
|
+
const opacity = node.get("opacity");
|
|
2207
|
+
stepAt(node, "opacity", 0, start);
|
|
2208
|
+
node.tween("opacity", opacity, start, end, ctx.ease);
|
|
2209
|
+
if (options.shift) {
|
|
2210
|
+
const x = node.get("x"), y = node.get("y");
|
|
2211
|
+
stepAt(node, "x", x + options.shift[0], start);
|
|
2212
|
+
stepAt(node, "y", y + options.shift[1], start);
|
|
2213
|
+
node.tween("x", x, start, end, ctx.ease);
|
|
2214
|
+
node.tween("y", y, start, end, ctx.ease);
|
|
2215
|
+
}
|
|
2216
|
+
if (options.scale !== void 0) scaleFrom(node, options.scale, start, end, ctx.ease);
|
|
2217
|
+
});
|
|
2218
|
+
clock.now = ctx.t0;
|
|
2219
|
+
}
|
|
2220
|
+
};
|
|
2221
|
+
}
|
|
2222
|
+
function scaleFrom(node, factor, t0, t1, ease, about) {
|
|
2223
|
+
const previous = clock.now;
|
|
2224
|
+
clock.now = t0;
|
|
2225
|
+
node.pivotAt(about ?? node.center);
|
|
2226
|
+
const sx = node.get("sx"), sy = node.get("sy");
|
|
2227
|
+
stepAt(node, "sx", sx * factor, t0);
|
|
2228
|
+
stepAt(node, "sy", sy * factor, t0);
|
|
2229
|
+
node.tween("sx", sx, t0, t1, ease);
|
|
2230
|
+
node.tween("sy", sy, t0, t1, ease);
|
|
2231
|
+
clock.now = previous;
|
|
2232
|
+
}
|
|
2233
|
+
function fadeOut(target, options = {}) {
|
|
2234
|
+
const nodes = list(target);
|
|
2235
|
+
return {
|
|
2236
|
+
label: "fadeOut",
|
|
2237
|
+
duration: options.duration ?? 0.6,
|
|
2238
|
+
ease: options.ease,
|
|
2239
|
+
schedule(ctx) {
|
|
2240
|
+
for (const node of nodes) {
|
|
2241
|
+
const opacity = node.get("opacity");
|
|
2242
|
+
node.tween("opacity", 0, ctx.t0, ctx.t1, ctx.ease);
|
|
2243
|
+
if (options.shift) {
|
|
2244
|
+
node.tween("x", node.get("x") + options.shift[0], ctx.t0, ctx.t1, ctx.ease);
|
|
2245
|
+
node.tween("y", node.get("y") + options.shift[1], ctx.t0, ctx.t1, ctx.ease);
|
|
2246
|
+
}
|
|
2247
|
+
if (options.scale !== void 0) {
|
|
2248
|
+
node.pivotAt(node.center);
|
|
2249
|
+
node.tween("sx", node.get("sx") * options.scale, ctx.t0, ctx.t1, ctx.ease);
|
|
2250
|
+
node.tween("sy", node.get("sy") * options.scale, ctx.t0, ctx.t1, ctx.ease);
|
|
2251
|
+
}
|
|
2252
|
+
hideAt(node, ctx.t1);
|
|
2253
|
+
stepAt(node, "opacity", opacity, ctx.t1);
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
};
|
|
2257
|
+
}
|
|
2258
|
+
function grow(target, options = {}) {
|
|
2259
|
+
const nodes = list(target);
|
|
2260
|
+
return {
|
|
2261
|
+
label: "grow",
|
|
2262
|
+
duration: options.duration ?? 0.9,
|
|
2263
|
+
ease: options.ease,
|
|
2264
|
+
schedule(ctx) {
|
|
2265
|
+
for (const node of nodes) {
|
|
2266
|
+
ensureVisible(node, ctx.t0, ctx.stage);
|
|
2267
|
+
scaleFrom(node, 1e-3, ctx.t0, ctx.t1, ctx.ease, options.from);
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
};
|
|
2271
|
+
}
|
|
2272
|
+
function appear(target) {
|
|
2273
|
+
const nodes = list(target);
|
|
2274
|
+
return { label: "appear", duration: 0, schedule: (ctx) => {
|
|
2275
|
+
for (const node of nodes) ensureVisible(node, ctx.t0, ctx.stage);
|
|
2276
|
+
} };
|
|
2277
|
+
}
|
|
2278
|
+
function vanish(target) {
|
|
2279
|
+
const nodes = list(target);
|
|
2280
|
+
return { label: "vanish", duration: 0, schedule: (ctx) => {
|
|
2281
|
+
for (const node of nodes) hideAt(node, ctx.t0);
|
|
2282
|
+
} };
|
|
2283
|
+
}
|
|
2284
|
+
function to(node, change, options = {}) {
|
|
2285
|
+
return {
|
|
2286
|
+
label: "to",
|
|
2287
|
+
duration: options.duration ?? 1,
|
|
2288
|
+
ease: options.ease,
|
|
2289
|
+
schedule(ctx) {
|
|
2290
|
+
ensureVisible(node, ctx.t0, ctx.stage);
|
|
2291
|
+
const touched = /* @__PURE__ */ new Set();
|
|
2292
|
+
clock.capture = touched;
|
|
2293
|
+
try {
|
|
2294
|
+
change(node);
|
|
2295
|
+
} finally {
|
|
2296
|
+
clock.capture = null;
|
|
2297
|
+
}
|
|
2298
|
+
for (const entry of touched) {
|
|
2299
|
+
const target = entry;
|
|
2300
|
+
const values = new Map(target.overlay);
|
|
2301
|
+
target.overlay.clear();
|
|
2302
|
+
applyChanges(target, values, ctx, options.restore === true);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
};
|
|
2306
|
+
}
|
|
2307
|
+
function applyChanges(node, values, ctx, restore) {
|
|
2308
|
+
const { t0, t1, ease } = ctx;
|
|
2309
|
+
const props = values;
|
|
2310
|
+
const restoreValues = /* @__PURE__ */ new Map();
|
|
2311
|
+
const px = props.get("px"), py = props.get("py");
|
|
2312
|
+
if (px !== void 0 || py !== void 0) {
|
|
2313
|
+
const nextPx = px ?? node.get("px"), nextPy = py ?? node.get("py");
|
|
2314
|
+
const m = compose(node.get("x"), node.get("y"), node.get("rotation"), node.get("sx"), node.get("sy"), node.get("px"), node.get("py"));
|
|
2315
|
+
const [startX, startY] = apply(m, nextPx, nextPy);
|
|
2316
|
+
stepAt(node, "px", nextPx, t0);
|
|
2317
|
+
stepAt(node, "py", nextPy, t0);
|
|
2318
|
+
stepAt(node, "x", startX, t0);
|
|
2319
|
+
stepAt(node, "y", startY, t0);
|
|
2320
|
+
props.delete("px");
|
|
2321
|
+
props.delete("py");
|
|
2322
|
+
}
|
|
2323
|
+
clock.now = t0;
|
|
2324
|
+
for (const [prop, value] of props) {
|
|
2325
|
+
const from = node.get(prop);
|
|
2326
|
+
if (from === value) continue;
|
|
2327
|
+
if (restore) restoreValues.set(prop, from);
|
|
2328
|
+
node.tween(prop, value, t0, t1, ease);
|
|
2329
|
+
}
|
|
2330
|
+
for (const [prop, value] of restoreValues) stepAt(node, prop, value, t1);
|
|
2331
|
+
}
|
|
2332
|
+
function moveTo(node, target, options = {}) {
|
|
2333
|
+
return { ...to(node, (n) => n.moveTo(target), options), label: "moveTo" };
|
|
2334
|
+
}
|
|
2335
|
+
function shift(node, dx, dy, options = {}) {
|
|
2336
|
+
return { ...to(node, (n) => n.shift(dx, dy), options), label: "shift" };
|
|
2337
|
+
}
|
|
2338
|
+
function scaleBy(node, factor, options = {}) {
|
|
2339
|
+
return { ...to(node, (n) => n.scale(factor, options.about), options), label: "scale" };
|
|
2340
|
+
}
|
|
2341
|
+
function rotate(node, radians, options = {}) {
|
|
2342
|
+
return { ...to(node, (n) => n.rotate(radians, options.about), options), label: "rotate" };
|
|
2343
|
+
}
|
|
2344
|
+
function recolor(node, color, options = {}) {
|
|
2345
|
+
return { ...to(node, (n) => n.color(color), options), label: "recolor" };
|
|
2346
|
+
}
|
|
2347
|
+
function fade(node, opacity, options = {}) {
|
|
2348
|
+
return { ...to(node, (n) => n.opacity(opacity), { duration: 0.6, ...options }), label: "fade" };
|
|
2349
|
+
}
|
|
2350
|
+
function along2(node, track, options = {}) {
|
|
2351
|
+
return {
|
|
2352
|
+
label: "along",
|
|
2353
|
+
duration: options.duration ?? 2,
|
|
2354
|
+
ease: options.ease,
|
|
2355
|
+
schedule(ctx) {
|
|
2356
|
+
ensureVisible(node, ctx.t0, ctx.stage);
|
|
2357
|
+
const path = track instanceof Node ? transformPath(track.get("path") ?? [], track.worldMatrix()) : track;
|
|
2358
|
+
const inverse = node.parent ? invertSafe(node.parent.worldMatrix()) : null;
|
|
2359
|
+
const position = (alpha) => {
|
|
2360
|
+
const [x, y] = pointAt(path, alpha);
|
|
2361
|
+
return inverse ? apply(inverse, x, y) : [x, y];
|
|
2362
|
+
};
|
|
2363
|
+
const [endX, endY] = position(1);
|
|
2364
|
+
node.track("x").add({ t0: ctx.t0, t1: ctx.t1, ease: ctx.ease, to: endX, at: (a) => position(a)[0] });
|
|
2365
|
+
node.track("y").add({ t0: ctx.t0, t1: ctx.t1, ease: ctx.ease, to: endY, at: (a) => position(a)[1] });
|
|
2366
|
+
if (options.orient) {
|
|
2367
|
+
const angle = (alpha) => {
|
|
2368
|
+
const [dx, dy] = tangentAt(path, alpha);
|
|
2369
|
+
return Math.atan2(dy, dx);
|
|
2370
|
+
};
|
|
2371
|
+
node.track("rotation").add({ t0: ctx.t0, t1: ctx.t1, ease: ctx.ease, to: angle(1), at: angle });
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
};
|
|
2375
|
+
}
|
|
2376
|
+
function invertSafe(m) {
|
|
2377
|
+
const det = m[0] * m[3] - m[1] * m[2];
|
|
2378
|
+
if (Math.abs(det) < 1e-12) return [1, 0, 0, 1, 0, 0];
|
|
2379
|
+
const i = 1 / det;
|
|
2380
|
+
return [m[3] * i, -m[1] * i, -m[2] * i, m[0] * i, (m[2] * m[5] - m[3] * m[4]) * i, (m[1] * m[4] - m[0] * m[5]) * i];
|
|
2381
|
+
}
|
|
2382
|
+
function warp(target, f, options = {}) {
|
|
2383
|
+
const nodes = list(target);
|
|
2384
|
+
return {
|
|
2385
|
+
label: "warp",
|
|
2386
|
+
duration: options.duration ?? 2,
|
|
2387
|
+
ease: options.ease,
|
|
2388
|
+
schedule(ctx) {
|
|
2389
|
+
for (const leaf of drawable(nodes)) {
|
|
2390
|
+
ensureVisible(leaf, ctx.t0, ctx.stage);
|
|
2391
|
+
const path = leaf.get("path");
|
|
2392
|
+
const world = leaf.worldMatrix();
|
|
2393
|
+
const inverse = invertSafe(world);
|
|
2394
|
+
const start = path.map((r) => subdivideEach(r, options.subdivisions ?? 6));
|
|
2395
|
+
const end = warpPath(start, (x, y) => {
|
|
2396
|
+
const [wx, wy] = apply(world, x, y);
|
|
2397
|
+
const [mx, my] = f(wx, wy);
|
|
2398
|
+
return apply(inverse, mx, my);
|
|
2399
|
+
}, 1);
|
|
2400
|
+
leaf.track("path").add({
|
|
2401
|
+
t0: ctx.t0,
|
|
2402
|
+
t1: ctx.t1,
|
|
2403
|
+
ease: ctx.ease,
|
|
2404
|
+
to: end,
|
|
2405
|
+
at: (alpha) => start.map((r, i) => {
|
|
2406
|
+
const a = r.p, b = end[i].p;
|
|
2407
|
+
const out = new Float64Array(a.length);
|
|
2408
|
+
for (let k = 0; k < a.length; k++) out[k] = a[k] + (b[k] - a[k]) * alpha;
|
|
2409
|
+
return { p: out, closed: r.closed };
|
|
2410
|
+
})
|
|
2411
|
+
});
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
};
|
|
2415
|
+
}
|
|
2416
|
+
function applyMatrix(target, matrix, options = {}) {
|
|
2417
|
+
const [ox, oy] = options.origin ?? [0, 0];
|
|
2418
|
+
const flip = options.yUp === false ? 1 : -1;
|
|
2419
|
+
const [[a, b], [c, d]] = matrix;
|
|
2420
|
+
return {
|
|
2421
|
+
...warp(target, (x, y) => {
|
|
2422
|
+
const ux = x - ox, uy = (y - oy) * flip;
|
|
2423
|
+
const vx = a * ux + b * uy, vy = c * ux + d * uy;
|
|
2424
|
+
return [ox + vx, oy + vy * flip];
|
|
2425
|
+
}, { subdivisions: 1, ...options }),
|
|
2426
|
+
label: "applyMatrix"
|
|
2427
|
+
};
|
|
2428
|
+
}
|
|
2429
|
+
function chainOpacity(node) {
|
|
2430
|
+
let value = 1;
|
|
2431
|
+
for (let n = node; n; n = n.parent) value *= n.get("opacity");
|
|
2432
|
+
return value;
|
|
2433
|
+
}
|
|
2434
|
+
function bake(leaf) {
|
|
2435
|
+
const m = leaf.worldMatrix();
|
|
2436
|
+
const opacity = chainOpacity(leaf);
|
|
2437
|
+
let z = 0;
|
|
2438
|
+
let fixed = false;
|
|
2439
|
+
for (let n = leaf; n; n = n.parent) {
|
|
2440
|
+
z += n.get("z");
|
|
2441
|
+
fixed ||= n.get("fixed");
|
|
2442
|
+
}
|
|
2443
|
+
return {
|
|
2444
|
+
path: transformPath(leaf.get("path") ?? [], m),
|
|
2445
|
+
fill: leaf.get("fill"),
|
|
2446
|
+
fillOpacity: leaf.get("fill") ? leaf.get("fillOpacity") * opacity : 0,
|
|
2447
|
+
stroke: leaf.get("stroke"),
|
|
2448
|
+
strokeOpacity: leaf.get("stroke") ? leaf.get("strokeOpacity") * opacity : 0,
|
|
2449
|
+
strokeWidth: leaf.get("strokeWidth") * matScale(m),
|
|
2450
|
+
glow: leaf.get("glow"),
|
|
2451
|
+
glowColor: leaf.get("glowColor"),
|
|
2452
|
+
cap: leaf.get("cap"),
|
|
2453
|
+
dash: leaf.get("dash"),
|
|
2454
|
+
z,
|
|
2455
|
+
fixed,
|
|
2456
|
+
key: leaf.data["key"]
|
|
2457
|
+
};
|
|
2458
|
+
}
|
|
2459
|
+
function ghost(stage, from, to2, ctx) {
|
|
2460
|
+
const node = new Node();
|
|
2461
|
+
node.base.visible = false;
|
|
2462
|
+
node.base.path = from.path;
|
|
2463
|
+
node.base.cap = from.cap;
|
|
2464
|
+
node.base.z = from.z;
|
|
2465
|
+
node.base.fixed = from.fixed;
|
|
2466
|
+
node.base.fill = from.fill ?? to2.fill;
|
|
2467
|
+
node.base.stroke = from.stroke ?? to2.stroke;
|
|
2468
|
+
node.base.fillOpacity = from.fillOpacity;
|
|
2469
|
+
node.base.strokeOpacity = from.strokeOpacity;
|
|
2470
|
+
node.base.strokeWidth = from.strokeWidth || to2.strokeWidth;
|
|
2471
|
+
node.base.glow = from.glow;
|
|
2472
|
+
node.base.glowColor = from.glowColor ?? to2.glowColor;
|
|
2473
|
+
stage.root.add(node);
|
|
2474
|
+
node.track("visible").initial = false;
|
|
2475
|
+
node.track("visible").keys.length = 0;
|
|
2476
|
+
stepAt(node, "visible", true, ctx.appear ?? ctx.t0);
|
|
2477
|
+
node.track("path").add({ t0: ctx.t0, t1: ctx.t1, ease: ctx.ease, to: to2.path, at: pathMixer(from.path, to2.path) });
|
|
2478
|
+
if (from.fill && to2.fill) node.tween("fill", to2.fill, ctx.t0, ctx.t1, ctx.ease);
|
|
2479
|
+
if (from.stroke && to2.stroke) node.tween("stroke", to2.stroke, ctx.t0, ctx.t1, ctx.ease);
|
|
2480
|
+
node.tween("fillOpacity", to2.fillOpacity, ctx.t0, ctx.t1, ctx.ease);
|
|
2481
|
+
node.tween("strokeOpacity", to2.strokeOpacity, ctx.t0, ctx.t1, ctx.ease);
|
|
2482
|
+
node.tween("strokeWidth", to2.strokeWidth || from.strokeWidth, ctx.t0, ctx.t1, ctx.ease);
|
|
2483
|
+
if (from.glow !== to2.glow) node.tween("glow", to2.glow, ctx.t0, ctx.t1, ctx.ease);
|
|
2484
|
+
stepAt(node, "visible", false, ctx.t1);
|
|
2485
|
+
return node;
|
|
2486
|
+
}
|
|
2487
|
+
function pairCounts(from, to2) {
|
|
2488
|
+
const count = Math.max(from.length, to2.length);
|
|
2489
|
+
const pairs = [];
|
|
2490
|
+
for (let i = 0; i < count; i++) {
|
|
2491
|
+
pairs.push([
|
|
2492
|
+
from[Math.min(from.length - 1, Math.floor(i * from.length / count))],
|
|
2493
|
+
to2[Math.min(to2.length - 1, Math.floor(i * to2.length / count))]
|
|
2494
|
+
]);
|
|
2495
|
+
}
|
|
2496
|
+
return pairs;
|
|
2497
|
+
}
|
|
2498
|
+
function morph(from, to2, options = {}) {
|
|
2499
|
+
const a = list(from), b = list(to2);
|
|
2500
|
+
return {
|
|
2501
|
+
label: "morph",
|
|
2502
|
+
duration: options.duration ?? 1.2,
|
|
2503
|
+
ease: options.ease,
|
|
2504
|
+
schedule(ctx) {
|
|
2505
|
+
prepareTargets(b, ctx);
|
|
2506
|
+
const fromLeaves = drawable(a).map(bake);
|
|
2507
|
+
const toLeaves = drawable(b).map(bake);
|
|
2508
|
+
if (fromLeaves.length === 0 || toLeaves.length === 0) {
|
|
2509
|
+
for (const node of a) hideAt(node, ctx.t0);
|
|
2510
|
+
for (const node of b) ensureVisible(node, ctx.t0, ctx.stage);
|
|
2511
|
+
return;
|
|
2512
|
+
}
|
|
2513
|
+
const pairs = pairCounts(fromLeaves, toLeaves);
|
|
2514
|
+
const lag = options.lag ?? 0;
|
|
2515
|
+
const each = (ctx.t1 - ctx.t0) / (1 + Math.max(0, pairs.length - 1) * lag);
|
|
2516
|
+
pairs.forEach(([x, y], i) => {
|
|
2517
|
+
const start = ctx.t0 + i * lag * each;
|
|
2518
|
+
ghost(ctx.stage, x, y, { t0: start, t1: start + each, ease: ctx.ease, appear: ctx.t0 });
|
|
2519
|
+
});
|
|
2520
|
+
finishMorph(a, b, ctx);
|
|
2521
|
+
}
|
|
2522
|
+
};
|
|
2523
|
+
}
|
|
2524
|
+
function prepareTargets(targets, ctx) {
|
|
2525
|
+
clock.now = ctx.t0;
|
|
2526
|
+
for (const node of targets) {
|
|
2527
|
+
ctx.stage.mountAt(node, ctx.t0);
|
|
2528
|
+
hideAt(node, ctx.t0);
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
function finishMorph(a, b, ctx) {
|
|
2532
|
+
for (const node of a) hideAt(node, ctx.t0);
|
|
2533
|
+
for (const node of b) node.track("visible").step(ctx.t1, true);
|
|
2534
|
+
clock.now = ctx.t0;
|
|
2535
|
+
}
|
|
2536
|
+
function morphMatching(from, to2, options = {}) {
|
|
2537
|
+
return {
|
|
2538
|
+
label: "morphMatching",
|
|
2539
|
+
duration: options.duration ?? 1.4,
|
|
2540
|
+
ease: options.ease,
|
|
2541
|
+
schedule(ctx) {
|
|
2542
|
+
prepareTargets([to2], ctx);
|
|
2543
|
+
const keyOf = options.key ?? ((leaf) => leaf.data["key"]);
|
|
2544
|
+
const fromLeaves = drawable(from), toLeaves = drawable(to2);
|
|
2545
|
+
const fromBaked = fromLeaves.map(bake), toBaked = toLeaves.map(bake);
|
|
2546
|
+
const pairs = matchSequence(fromLeaves.map(keyOf), toLeaves.map(keyOf));
|
|
2547
|
+
const usedFrom = new Set(pairs.map(([i]) => i)), usedTo = new Set(pairs.map(([, j]) => j));
|
|
2548
|
+
for (const [i, j] of pairs) ghost(ctx.stage, fromBaked[i], toBaked[j], ctx);
|
|
2549
|
+
const span = ctx.t1 - ctx.t0;
|
|
2550
|
+
const offset = options.fadeShift ?? [0, 0];
|
|
2551
|
+
const shifted = (bakedPath, dx, dy) => transformPath(bakedPath, [1, 0, 0, 1, dx, dy]);
|
|
2552
|
+
fromBaked.forEach((baked, i) => {
|
|
2553
|
+
if (usedFrom.has(i)) return;
|
|
2554
|
+
ghost(ctx.stage, baked, {
|
|
2555
|
+
...baked,
|
|
2556
|
+
path: shifted(baked.path, offset[0], offset[1]),
|
|
2557
|
+
fillOpacity: 0,
|
|
2558
|
+
strokeOpacity: 0
|
|
2559
|
+
}, { t0: ctx.t0, t1: ctx.t0 + span * 0.55, ease: ctx.ease });
|
|
2560
|
+
});
|
|
2561
|
+
toBaked.forEach((baked, j) => {
|
|
2562
|
+
if (usedTo.has(j)) return;
|
|
2563
|
+
ghost(ctx.stage, {
|
|
2564
|
+
...baked,
|
|
2565
|
+
path: shifted(baked.path, -offset[0], -offset[1]),
|
|
2566
|
+
fillOpacity: 0,
|
|
2567
|
+
strokeOpacity: 0
|
|
2568
|
+
}, baked, { t0: ctx.t0 + span * 0.35, t1: ctx.t1, ease: ctx.ease });
|
|
2569
|
+
});
|
|
2570
|
+
finishMorph([from], [to2], ctx);
|
|
2571
|
+
}
|
|
2572
|
+
};
|
|
2573
|
+
}
|
|
2574
|
+
function indicate(target, options = {}) {
|
|
2575
|
+
const nodes = list(target);
|
|
2576
|
+
const anims = nodes.map((node) => retime(to(node, (n) => {
|
|
2577
|
+
n.scale(options.scale ?? 1.14);
|
|
2578
|
+
n.color(options.color ?? "yellow");
|
|
2579
|
+
}, { restore: true }), { ease: options.ease ?? thereAndBack }));
|
|
2580
|
+
return { ...par(...anims), label: "indicate", duration: options.duration ?? 0.9 };
|
|
2581
|
+
}
|
|
2582
|
+
function shake(node, options = {}) {
|
|
2583
|
+
return {
|
|
2584
|
+
label: "shake",
|
|
2585
|
+
duration: options.duration ?? 0.8,
|
|
2586
|
+
ease: wiggle(options.cycles ?? 3),
|
|
2587
|
+
schedule(ctx) {
|
|
2588
|
+
clock.now = ctx.t0;
|
|
2589
|
+
const [cx, cy] = node.center;
|
|
2590
|
+
node.pivotAt([cx, cy]);
|
|
2591
|
+
const rotation = node.get("rotation");
|
|
2592
|
+
const angle = options.angle ?? 0.08;
|
|
2593
|
+
const track = node.track("rotation");
|
|
2594
|
+
track.add({ t0: ctx.t0, t1: ctx.t1, ease: ctx.ease, to: rotation, at: (alpha) => rotation + angle * alpha });
|
|
2595
|
+
}
|
|
2596
|
+
};
|
|
2597
|
+
}
|
|
2598
|
+
function circumscribe(target, options = {}) {
|
|
2599
|
+
return {
|
|
2600
|
+
label: "circumscribe",
|
|
2601
|
+
duration: options.duration ?? 1.3,
|
|
2602
|
+
ease: options.ease ?? linear,
|
|
2603
|
+
schedule(ctx) {
|
|
2604
|
+
const b = target.bounds();
|
|
2605
|
+
const pad = options.pad ?? 14;
|
|
2606
|
+
const outline = new Node(rectPath(b.x - pad, b.y - pad, b.width + pad * 2, b.height + pad * 2, options.radius ?? 12)).stroke(options.color ?? "yellow", options.width ?? 3).glow(8);
|
|
2607
|
+
ctx.stage.add(outline);
|
|
2608
|
+
const t0 = ctx.t0, t1 = ctx.t1, mid = t0 + (t1 - t0) * 0.55;
|
|
2609
|
+
stepAt(outline, "trimTo", 0, t0);
|
|
2610
|
+
outline.tween("trimTo", 1, t0, mid, smooth);
|
|
2611
|
+
outline.tween("trimFrom", 1, mid - (t1 - t0) * 0.08, t1, smooth);
|
|
2612
|
+
hideAt(outline, t1);
|
|
2613
|
+
}
|
|
2614
|
+
};
|
|
2615
|
+
}
|
|
2616
|
+
function flash(point, options = {}) {
|
|
2617
|
+
return {
|
|
2618
|
+
label: "flash",
|
|
2619
|
+
duration: options.duration ?? 0.7,
|
|
2620
|
+
ease: options.ease ?? smooth,
|
|
2621
|
+
schedule(ctx) {
|
|
2622
|
+
const center = point instanceof Node ? point.center : point;
|
|
2623
|
+
const radius = options.radius ?? 54;
|
|
2624
|
+
const rays = options.rays ?? 12;
|
|
2625
|
+
for (let i = 0; i < rays; i++) {
|
|
2626
|
+
const angle = i / rays * Math.PI * 2;
|
|
2627
|
+
const cos = Math.cos(angle), sin = Math.sin(angle);
|
|
2628
|
+
const ray = new Node(linePath([center[0] + cos * radius * 0.35, center[1] + sin * radius * 0.35], [center[0] + cos * radius, center[1] + sin * radius])).stroke(options.color ?? "yellow", 2.5).glow(5);
|
|
2629
|
+
ctx.stage.add(ray);
|
|
2630
|
+
stepAt(ray, "trimTo", 0, ctx.t0);
|
|
2631
|
+
ray.tween("trimTo", 1, ctx.t0, ctx.t0 + (ctx.t1 - ctx.t0) * 0.6, ctx.ease);
|
|
2632
|
+
ray.tween("trimFrom", 1, ctx.t0 + (ctx.t1 - ctx.t0) * 0.25, ctx.t1, ctx.ease);
|
|
2633
|
+
hideAt(ray, ctx.t1);
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2636
|
+
};
|
|
2637
|
+
}
|
|
2638
|
+
function ripple(point, options = {}) {
|
|
2639
|
+
return {
|
|
2640
|
+
label: "ripple",
|
|
2641
|
+
duration: options.duration ?? 1,
|
|
2642
|
+
ease: options.ease ?? smooth,
|
|
2643
|
+
schedule(ctx) {
|
|
2644
|
+
const center = point instanceof Node ? point.center : point;
|
|
2645
|
+
const ring2 = new Node(circlePath(0, 0, options.radius ?? 90)).stroke(options.color ?? "blue", options.width ?? 3).at(center);
|
|
2646
|
+
ctx.stage.add(ring2);
|
|
2647
|
+
stepAt(ring2, "sx", 0.05, ctx.t0);
|
|
2648
|
+
stepAt(ring2, "sy", 0.05, ctx.t0);
|
|
2649
|
+
ring2.tween("sx", 1, ctx.t0, ctx.t1, ctx.ease);
|
|
2650
|
+
ring2.tween("sy", 1, ctx.t0, ctx.t1, ctx.ease);
|
|
2651
|
+
ring2.tween("strokeOpacity", 0, ctx.t0 + (ctx.t1 - ctx.t0) * 0.2, ctx.t1, ctx.ease);
|
|
2652
|
+
hideAt(ring2, ctx.t1);
|
|
2653
|
+
}
|
|
2654
|
+
};
|
|
2655
|
+
}
|
|
2656
|
+
function pulse(line, options = {}) {
|
|
2657
|
+
return {
|
|
2658
|
+
label: "pulse",
|
|
2659
|
+
duration: options.duration ?? 1.1,
|
|
2660
|
+
ease: options.ease ?? smooth,
|
|
2661
|
+
schedule(ctx) {
|
|
2662
|
+
const path = line.get("path");
|
|
2663
|
+
if (!path) return;
|
|
2664
|
+
const lit = new Node(transformPath(path, line.worldMatrix())).stroke(options.color ?? "yellow", options.width ?? line.get("strokeWidth") + 1.5).glow(options.glow ?? 10);
|
|
2665
|
+
ctx.stage.add(lit);
|
|
2666
|
+
const length = options.length ?? 0.18;
|
|
2667
|
+
stepAt(lit, "trimFrom", 0, ctx.t0);
|
|
2668
|
+
stepAt(lit, "trimTo", 0, ctx.t0);
|
|
2669
|
+
lit.track("trimTo").add({ t0: ctx.t0, t1: ctx.t1, ease: ctx.ease, to: 1, at: (a) => Math.min(1, a * (1 + length)) });
|
|
2670
|
+
lit.track("trimFrom").add({ t0: ctx.t0, t1: ctx.t1, ease: ctx.ease, to: 1, at: (a) => Math.max(0, a * (1 + length) - length) });
|
|
2671
|
+
hideAt(lit, ctx.t1);
|
|
2672
|
+
}
|
|
2673
|
+
};
|
|
2674
|
+
}
|
|
2675
|
+
var Value = class {
|
|
2676
|
+
track;
|
|
2677
|
+
constructor(initial) {
|
|
2678
|
+
this.track = new Track(initial);
|
|
2679
|
+
}
|
|
2680
|
+
get value() {
|
|
2681
|
+
return this.track.sample(clock.now);
|
|
2682
|
+
}
|
|
2683
|
+
/** Set at the current point on the timeline. */
|
|
2684
|
+
set(value) {
|
|
2685
|
+
this.track.step(clock.now, value);
|
|
2686
|
+
return this;
|
|
2687
|
+
}
|
|
2688
|
+
to(value, options = {}) {
|
|
2689
|
+
const track = this.track;
|
|
2690
|
+
return {
|
|
2691
|
+
label: "value",
|
|
2692
|
+
duration: options.duration ?? 1,
|
|
2693
|
+
ease: options.ease,
|
|
2694
|
+
schedule(ctx) {
|
|
2695
|
+
const from = track.sample(ctx.t0);
|
|
2696
|
+
track.add({ t0: ctx.t0, t1: ctx.t1, ease: ctx.ease, to: value, at: (a) => from + (value - from) * a });
|
|
2697
|
+
}
|
|
2698
|
+
};
|
|
2699
|
+
}
|
|
2700
|
+
};
|
|
2701
|
+
|
|
2702
|
+
// src/stage/stage.ts
|
|
2703
|
+
var ASPECTS = {
|
|
2704
|
+
"16:9": [1920, 1080],
|
|
2705
|
+
"9:16": [1080, 1920],
|
|
2706
|
+
"1:1": [1080, 1080],
|
|
2707
|
+
"4:3": [1440, 1080],
|
|
2708
|
+
"21:9": [2520, 1080]
|
|
2709
|
+
};
|
|
2710
|
+
var Stage = class {
|
|
2711
|
+
root = new Node();
|
|
2712
|
+
/** Move, zoom, and rotate the view with to(s.camera, ...). `sx` is the zoom. */
|
|
2713
|
+
camera = new Node();
|
|
2714
|
+
width;
|
|
2715
|
+
height;
|
|
2716
|
+
fps;
|
|
2717
|
+
look;
|
|
2718
|
+
chapters = [];
|
|
2719
|
+
clips = [];
|
|
2720
|
+
captions = [];
|
|
2721
|
+
updaters = [];
|
|
2722
|
+
time = 0;
|
|
2723
|
+
ending = 0;
|
|
2724
|
+
constructor(options) {
|
|
2725
|
+
const [w, h] = ASPECTS[options.aspect ?? "16:9"];
|
|
2726
|
+
this.width = options.width ?? w;
|
|
2727
|
+
this.height = options.height ?? h;
|
|
2728
|
+
this.fps = options.fps ?? 60;
|
|
2729
|
+
this.look = resolveLook(options.look);
|
|
2730
|
+
this.root.mounted = true;
|
|
2731
|
+
this.camera.mounted = true;
|
|
2732
|
+
}
|
|
2733
|
+
/** Half of the frame in each direction. */
|
|
2734
|
+
get right() {
|
|
2735
|
+
return this.width / 2;
|
|
2736
|
+
}
|
|
2737
|
+
get left() {
|
|
2738
|
+
return -this.width / 2;
|
|
2739
|
+
}
|
|
2740
|
+
get top() {
|
|
2741
|
+
return -this.height / 2;
|
|
2742
|
+
}
|
|
2743
|
+
get bottom() {
|
|
2744
|
+
return this.height / 2;
|
|
2745
|
+
}
|
|
2746
|
+
/** Put nodes on stage now. Returns the first, so setup reads as one line. */
|
|
2747
|
+
add(first, ...rest) {
|
|
2748
|
+
clock.now = this.time;
|
|
2749
|
+
for (const node of [first, ...rest]) {
|
|
2750
|
+
if (!node.mounted) {
|
|
2751
|
+
node.parent?.drop(node);
|
|
2752
|
+
node.parent = this.root;
|
|
2753
|
+
this.root.children.push(node);
|
|
2754
|
+
mountTree(node, this.time);
|
|
2755
|
+
} else if (!node.get("visible")) {
|
|
2756
|
+
node.track("visible").step(this.time, true);
|
|
2757
|
+
}
|
|
2758
|
+
}
|
|
2759
|
+
return first;
|
|
2760
|
+
}
|
|
2761
|
+
/** Put a node on stage so it first shows at `time`, which may differ from the cursor. */
|
|
2762
|
+
mountAt(node, time) {
|
|
2763
|
+
if (node.mounted) return;
|
|
2764
|
+
node.parent?.drop(node);
|
|
2765
|
+
node.parent = this.root;
|
|
2766
|
+
this.root.children.push(node);
|
|
2767
|
+
mountTree(node, time);
|
|
2768
|
+
}
|
|
2769
|
+
remove(...nodes) {
|
|
2770
|
+
for (const node of nodes) node.track("visible").step(this.time, false);
|
|
2771
|
+
}
|
|
2772
|
+
/** Run animations together and advance the timeline past them. */
|
|
2773
|
+
play(...items) {
|
|
2774
|
+
const anims = items.filter((item) => "schedule" in item);
|
|
2775
|
+
const options = items.find((item) => !("schedule" in item)) ?? {};
|
|
2776
|
+
if (anims.length === 0) return this;
|
|
2777
|
+
const natural = Math.max(...anims.map((anim) => anim.duration));
|
|
2778
|
+
const duration = options.duration ?? natural;
|
|
2779
|
+
const t0 = this.time;
|
|
2780
|
+
const t1 = t0 + duration;
|
|
2781
|
+
for (const anim of anims) {
|
|
2782
|
+
clock.now = t0;
|
|
2783
|
+
const end = options.duration !== void 0 ? t1 : t0 + anim.duration;
|
|
2784
|
+
anim.schedule({ t0, t1: end, ease: options.ease ?? anim.ease ?? smooth, stage: this });
|
|
2785
|
+
}
|
|
2786
|
+
this.clips.push({ t0, t1, labels: options.label ? [options.label] : anims.map((anim) => anim.label) });
|
|
2787
|
+
this.time = t1;
|
|
2788
|
+
this.ending = Math.max(this.ending, t1);
|
|
2789
|
+
clock.now = t1;
|
|
2790
|
+
return this;
|
|
2791
|
+
}
|
|
2792
|
+
/** Hold on the current picture. */
|
|
2793
|
+
wait(seconds = 1) {
|
|
2794
|
+
this.time += seconds;
|
|
2795
|
+
this.ending = Math.max(this.ending, this.time);
|
|
2796
|
+
clock.now = this.time;
|
|
2797
|
+
return this;
|
|
2798
|
+
}
|
|
2799
|
+
/** Author something at another time, then return to where you were. */
|
|
2800
|
+
at(time, build) {
|
|
2801
|
+
const saved = this.time;
|
|
2802
|
+
this.time = time;
|
|
2803
|
+
clock.now = time;
|
|
2804
|
+
build();
|
|
2805
|
+
this.ending = Math.max(this.ending, this.time);
|
|
2806
|
+
this.time = saved;
|
|
2807
|
+
clock.now = saved;
|
|
2808
|
+
return this;
|
|
2809
|
+
}
|
|
2810
|
+
/** Mark a chapter for the player, presentation steps, and the timeline. */
|
|
2811
|
+
chapter(title) {
|
|
2812
|
+
this.chapters.push({ title, time: this.time });
|
|
2813
|
+
return this;
|
|
2814
|
+
}
|
|
2815
|
+
/** Show a caption for a span of time. Captions also export as a VTT sidecar. */
|
|
2816
|
+
caption(text2, duration = 3) {
|
|
2817
|
+
this.captions.push({ t0: this.time, t1: this.time + duration, text: text2 });
|
|
2818
|
+
this.ending = Math.max(this.ending, this.time + duration);
|
|
2819
|
+
return this;
|
|
2820
|
+
}
|
|
2821
|
+
/** A number that animates, for driving dynamic drawings. */
|
|
2822
|
+
value(initial = 0) {
|
|
2823
|
+
return new Value(initial);
|
|
2824
|
+
}
|
|
2825
|
+
/** Re-run a function each frame while the node shows. It can set any property on the node. */
|
|
2826
|
+
update(node, fn, window = {}) {
|
|
2827
|
+
this.updaters.push({
|
|
2828
|
+
node,
|
|
2829
|
+
fn,
|
|
2830
|
+
from: window.from ?? this.time,
|
|
2831
|
+
to: window.to ?? Infinity
|
|
2832
|
+
});
|
|
2833
|
+
return node;
|
|
2834
|
+
}
|
|
2835
|
+
get duration() {
|
|
2836
|
+
return Math.max(this.ending, this.time);
|
|
2837
|
+
}
|
|
2838
|
+
};
|
|
2839
|
+
function film(a, b) {
|
|
2840
|
+
const options = typeof a === "function" ? {} : a;
|
|
2841
|
+
const build = typeof a === "function" ? a : b;
|
|
2842
|
+
if (!build) throw new Error("film(options, build) needs a build function.");
|
|
2843
|
+
const definition = {
|
|
2844
|
+
kind: "vizmatic.film",
|
|
2845
|
+
options,
|
|
2846
|
+
build,
|
|
2847
|
+
compile: (overrides) => compileFilm(definition, overrides)
|
|
2848
|
+
};
|
|
2849
|
+
return definition;
|
|
2850
|
+
}
|
|
2851
|
+
function isFilm(value) {
|
|
2852
|
+
return !!value && typeof value === "object" && value.kind === "vizmatic.film";
|
|
2853
|
+
}
|
|
2854
|
+
var Suspend = class {
|
|
2855
|
+
constructor(promise) {
|
|
2856
|
+
this.promise = promise;
|
|
2857
|
+
}
|
|
2858
|
+
promise;
|
|
2859
|
+
/** Lets a second copy of the engine recognize this without instanceof. */
|
|
2860
|
+
isVizmaticSuspend = true;
|
|
2861
|
+
};
|
|
2862
|
+
function isSuspend(error) {
|
|
2863
|
+
return typeof error === "object" && error !== null && error.isVizmaticSuspend === true;
|
|
2864
|
+
}
|
|
2865
|
+
var CompiledFilm = class {
|
|
2866
|
+
width;
|
|
2867
|
+
height;
|
|
2868
|
+
fps;
|
|
2869
|
+
duration;
|
|
2870
|
+
title;
|
|
2871
|
+
description;
|
|
2872
|
+
look;
|
|
2873
|
+
bloom;
|
|
2874
|
+
background;
|
|
2875
|
+
/** The author's chosen poster time, when set. */
|
|
2876
|
+
posterTime;
|
|
2877
|
+
bleed;
|
|
2878
|
+
chapters;
|
|
2879
|
+
clips;
|
|
2880
|
+
captions;
|
|
2881
|
+
stage;
|
|
2882
|
+
constructor(stage, options, overrides) {
|
|
2883
|
+
this.stage = stage;
|
|
2884
|
+
this.width = stage.width;
|
|
2885
|
+
this.height = stage.height;
|
|
2886
|
+
this.fps = overrides.fps ?? stage.fps;
|
|
2887
|
+
this.duration = Math.max(stage.duration, 1 / this.fps);
|
|
2888
|
+
this.title = options.title ?? "";
|
|
2889
|
+
this.description = options.description ?? "";
|
|
2890
|
+
this.look = stage.look;
|
|
2891
|
+
this.bloom = overrides.bloom ?? options.bloom ?? stage.look.bloom;
|
|
2892
|
+
const background = overrides.background !== void 0 ? overrides.background : options.background !== void 0 ? options.background : stage.look.background;
|
|
2893
|
+
this.background = background === null ? null : stage.look.colors[background] ?? background;
|
|
2894
|
+
this.posterTime = options.poster;
|
|
2895
|
+
this.bleed = options.bleed === true;
|
|
2896
|
+
this.chapters = stage.chapters;
|
|
2897
|
+
this.clips = stage.clips;
|
|
2898
|
+
this.captions = stage.captions;
|
|
2899
|
+
}
|
|
2900
|
+
get frameCount() {
|
|
2901
|
+
return Math.max(1, Math.round(this.duration * this.fps));
|
|
2902
|
+
}
|
|
2903
|
+
/** The draw list at a time in seconds. May throw Suspend if a drawing needs a new asset. */
|
|
2904
|
+
frame(time) {
|
|
2905
|
+
const t = Math.max(0, Math.min(this.duration, time));
|
|
2906
|
+
const stage = this.stage;
|
|
2907
|
+
const previous = { mode: clock.mode, now: clock.now };
|
|
2908
|
+
setColorTokens(stage.look.colors);
|
|
2909
|
+
clock.mode = "render";
|
|
2910
|
+
clock.now = t;
|
|
2911
|
+
clock.frame++;
|
|
2912
|
+
try {
|
|
2913
|
+
for (const updater of stage.updaters) {
|
|
2914
|
+
if (t >= updater.from && t <= updater.to) updater.fn(updater.node, t);
|
|
2915
|
+
}
|
|
2916
|
+
const camera = stage.camera;
|
|
2917
|
+
const ops = buildOps(stage.root, {
|
|
2918
|
+
x: camera.get("x"),
|
|
2919
|
+
y: camera.get("y"),
|
|
2920
|
+
zoom: camera.get("sx"),
|
|
2921
|
+
rotation: camera.get("rotation")
|
|
2922
|
+
}, stage.width, stage.height);
|
|
2923
|
+
return { width: stage.width, height: stage.height, background: this.background, bloom: this.bloom, dark: stage.look.dark, ops };
|
|
2924
|
+
} finally {
|
|
2925
|
+
clock.mode = previous.mode;
|
|
2926
|
+
clock.now = previous.now;
|
|
2927
|
+
}
|
|
2928
|
+
}
|
|
2929
|
+
/** Standalone SVG for a time. */
|
|
2930
|
+
svg(time, options = {}) {
|
|
2931
|
+
return frameToSvg(this.frame(time), options);
|
|
2932
|
+
}
|
|
2933
|
+
};
|
|
2934
|
+
async function compileFilm(definition, overrides = {}) {
|
|
2935
|
+
const options = { ...definition.options, ...overrides.look ? { look: overrides.look } : {}, ...overrides.fps ? { fps: overrides.fps } : {} };
|
|
2936
|
+
for (let attempt = 0; attempt < 64; attempt++) {
|
|
2937
|
+
const stage = new Stage(options);
|
|
2938
|
+
setColorTokens(stage.look.colors);
|
|
2939
|
+
const saved = { mode: clock.mode, now: clock.now };
|
|
2940
|
+
clock.mode = "compile";
|
|
2941
|
+
clock.now = 0;
|
|
2942
|
+
try {
|
|
2943
|
+
await definition.build(stage);
|
|
2944
|
+
clock.mode = saved.mode;
|
|
2945
|
+
clock.now = saved.now;
|
|
2946
|
+
const compiled = new CompiledFilm(stage, options, overrides);
|
|
2947
|
+
await probe(compiled);
|
|
2948
|
+
return compiled;
|
|
2949
|
+
} catch (error) {
|
|
2950
|
+
clock.mode = saved.mode;
|
|
2951
|
+
clock.now = saved.now;
|
|
2952
|
+
clock.capture = null;
|
|
2953
|
+
if (error instanceof Suspend) {
|
|
2954
|
+
await error.promise;
|
|
2955
|
+
continue;
|
|
2956
|
+
}
|
|
2957
|
+
throw error;
|
|
2958
|
+
}
|
|
2959
|
+
}
|
|
2960
|
+
throw new Error("The film kept requesting new assets while compiling.");
|
|
2961
|
+
}
|
|
2962
|
+
async function probe(film2) {
|
|
2963
|
+
const samples = [0, 0.25, 0.5, 0.75, 1];
|
|
2964
|
+
for (const fraction of samples) {
|
|
2965
|
+
for (let attempt = 0; attempt < 16; attempt++) {
|
|
2966
|
+
try {
|
|
2967
|
+
film2.frame(film2.duration * fraction);
|
|
2968
|
+
break;
|
|
2969
|
+
} catch (error) {
|
|
2970
|
+
if (!(error instanceof Suspend)) throw error;
|
|
2971
|
+
await error.promise;
|
|
2972
|
+
}
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
}
|
|
2976
|
+
|
|
2977
|
+
// src/stage/assets.ts
|
|
2978
|
+
var assetBase = null;
|
|
2979
|
+
function setAssetBase(url) {
|
|
2980
|
+
assetBase = url;
|
|
2981
|
+
}
|
|
2982
|
+
var isNode = typeof process !== "undefined" && !!process.versions?.node && typeof globalThis.document === "undefined";
|
|
2983
|
+
function moduleUrl() {
|
|
2984
|
+
const url = import.meta.url;
|
|
2985
|
+
if (url) return url;
|
|
2986
|
+
return `file://${typeof __filename !== "undefined" ? __filename : ""}`;
|
|
2987
|
+
}
|
|
2988
|
+
async function nodeRead(relative) {
|
|
2989
|
+
const fsName = "node:fs/promises", pathName = "node:path", urlName = "node:url";
|
|
2990
|
+
const [fs, path, url] = await Promise.all([import(fsName), import(pathName), import(urlName)]);
|
|
2991
|
+
let directory = path.dirname(url.fileURLToPath(moduleUrl()));
|
|
2992
|
+
for (let depth = 0; depth < 6; depth++) {
|
|
2993
|
+
const candidate = path.join(directory, "assets", relative);
|
|
2994
|
+
try {
|
|
2995
|
+
const bytes = await fs.readFile(candidate);
|
|
2996
|
+
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
2997
|
+
} catch (error) {
|
|
2998
|
+
if (error.code !== "ENOENT") throw error;
|
|
2999
|
+
}
|
|
3000
|
+
directory = path.dirname(directory);
|
|
3001
|
+
}
|
|
3002
|
+
throw new Error(`Bundled asset not found: assets/${relative}`);
|
|
3003
|
+
}
|
|
3004
|
+
async function loadBinaryAsset(relative) {
|
|
3005
|
+
if (isNode) return nodeRead(relative);
|
|
3006
|
+
const base = assetBase ?? new URL("./assets/", moduleUrl()).href;
|
|
3007
|
+
const response = await fetch(new URL(relative, base));
|
|
3008
|
+
const type = response.headers.get("content-type") ?? "";
|
|
3009
|
+
if (!response.ok || type.includes("text/html")) {
|
|
3010
|
+
throw new Error(`Could not load ${relative} from ${response.url} (HTTP ${response.status}, ${type || "no content type"}). Call setAssetBase() with the folder that holds fonts/.`);
|
|
3011
|
+
}
|
|
3012
|
+
return response.arrayBuffer();
|
|
3013
|
+
}
|
|
3014
|
+
|
|
3015
|
+
// src/stage/font-types.ts
|
|
3016
|
+
function curveFree(contours) {
|
|
3017
|
+
return { contours: contours.filter((contour) => contour.length >= 8) };
|
|
3018
|
+
}
|
|
3019
|
+
|
|
3020
|
+
// src/stage/font.ts
|
|
3021
|
+
var Reader = class {
|
|
3022
|
+
constructor(bytes, offset = 0) {
|
|
3023
|
+
this.bytes = bytes;
|
|
3024
|
+
this.offset = offset;
|
|
3025
|
+
this.view = new DataView(bytes);
|
|
3026
|
+
}
|
|
3027
|
+
bytes;
|
|
3028
|
+
offset;
|
|
3029
|
+
view;
|
|
3030
|
+
seek(offset) {
|
|
3031
|
+
this.offset = offset;
|
|
3032
|
+
return this;
|
|
3033
|
+
}
|
|
3034
|
+
get position() {
|
|
3035
|
+
return this.offset;
|
|
3036
|
+
}
|
|
3037
|
+
u8() {
|
|
3038
|
+
return this.view.getUint8(this.offset++);
|
|
3039
|
+
}
|
|
3040
|
+
i8() {
|
|
3041
|
+
return this.view.getInt8(this.offset++);
|
|
3042
|
+
}
|
|
3043
|
+
u16() {
|
|
3044
|
+
const v = this.view.getUint16(this.offset);
|
|
3045
|
+
this.offset += 2;
|
|
3046
|
+
return v;
|
|
3047
|
+
}
|
|
3048
|
+
i16() {
|
|
3049
|
+
const v = this.view.getInt16(this.offset);
|
|
3050
|
+
this.offset += 2;
|
|
3051
|
+
return v;
|
|
3052
|
+
}
|
|
3053
|
+
u32() {
|
|
3054
|
+
const v = this.view.getUint32(this.offset);
|
|
3055
|
+
this.offset += 4;
|
|
3056
|
+
return v;
|
|
3057
|
+
}
|
|
3058
|
+
tag() {
|
|
3059
|
+
let out = "";
|
|
3060
|
+
for (let i = 0; i < 4; i++) out += String.fromCharCode(this.u8());
|
|
3061
|
+
return out;
|
|
3062
|
+
}
|
|
3063
|
+
};
|
|
3064
|
+
function parseFont(bytes) {
|
|
3065
|
+
const r = new Reader(bytes);
|
|
3066
|
+
const version = r.u32();
|
|
3067
|
+
if (version !== 65536 && version !== 1953658213) {
|
|
3068
|
+
throw new Error("Stage text needs a TrueType (.ttf) font with glyf outlines.");
|
|
3069
|
+
}
|
|
3070
|
+
const tableCount = r.u16();
|
|
3071
|
+
r.seek(12);
|
|
3072
|
+
const tables = /* @__PURE__ */ new Map();
|
|
3073
|
+
for (let i = 0; i < tableCount; i++) {
|
|
3074
|
+
const tag = r.tag();
|
|
3075
|
+
r.u32();
|
|
3076
|
+
tables.set(tag, { offset: r.u32(), length: r.u32() });
|
|
3077
|
+
}
|
|
3078
|
+
const need = (tag) => {
|
|
3079
|
+
const table = tables.get(tag);
|
|
3080
|
+
if (!table) throw new Error(`Font is missing its ${tag} table.`);
|
|
3081
|
+
return table;
|
|
3082
|
+
};
|
|
3083
|
+
const head = need("head");
|
|
3084
|
+
r.seek(head.offset + 18);
|
|
3085
|
+
const unitsPerEm = r.u16();
|
|
3086
|
+
r.seek(head.offset + 50);
|
|
3087
|
+
const longLoca = r.i16() === 1;
|
|
3088
|
+
const hhea = need("hhea");
|
|
3089
|
+
r.seek(hhea.offset + 4);
|
|
3090
|
+
const ascent = r.i16();
|
|
3091
|
+
const descent = r.i16();
|
|
3092
|
+
const lineGap = r.i16();
|
|
3093
|
+
r.seek(hhea.offset + 34);
|
|
3094
|
+
const hMetrics = r.u16();
|
|
3095
|
+
r.seek(need("maxp").offset + 4);
|
|
3096
|
+
const glyphCount = r.u16();
|
|
3097
|
+
const hmtx = need("hmtx");
|
|
3098
|
+
const loca = need("loca");
|
|
3099
|
+
const glyf = need("glyf");
|
|
3100
|
+
const cmap = parseCmap(r, need("cmap").offset);
|
|
3101
|
+
const kerning = tables.has("GPOS") ? parseKerning(r, tables.get("GPOS").offset) : () => 0;
|
|
3102
|
+
const advance = (glyph) => {
|
|
3103
|
+
const index = Math.min(glyph, hMetrics - 1);
|
|
3104
|
+
r.seek(hmtx.offset + index * 4);
|
|
3105
|
+
return r.u16();
|
|
3106
|
+
};
|
|
3107
|
+
const outlines = /* @__PURE__ */ new Map();
|
|
3108
|
+
const contours = (glyph, depth) => {
|
|
3109
|
+
if (glyph >= glyphCount || depth > 6) return [];
|
|
3110
|
+
r.seek(loca.offset + (longLoca ? glyph * 4 : glyph * 2));
|
|
3111
|
+
const start = longLoca ? r.u32() : r.u16() * 2;
|
|
3112
|
+
const end = longLoca ? r.u32() : r.u16() * 2;
|
|
3113
|
+
if (start === end) return [];
|
|
3114
|
+
r.seek(glyf.offset + start);
|
|
3115
|
+
const count = r.i16();
|
|
3116
|
+
r.seek(r.position + 8);
|
|
3117
|
+
if (count < 0) return composite(glyph, depth);
|
|
3118
|
+
const ends = [];
|
|
3119
|
+
for (let i = 0; i < count; i++) ends.push(r.u16());
|
|
3120
|
+
const instructionLength = r.u16();
|
|
3121
|
+
r.seek(r.position + instructionLength);
|
|
3122
|
+
const total = (ends[ends.length - 1] ?? -1) + 1;
|
|
3123
|
+
const flags = [];
|
|
3124
|
+
while (flags.length < total) {
|
|
3125
|
+
const flag = r.u8();
|
|
3126
|
+
flags.push(flag);
|
|
3127
|
+
if (flag & 8) {
|
|
3128
|
+
let repeat = r.u8();
|
|
3129
|
+
while (repeat-- > 0) flags.push(flag);
|
|
3130
|
+
}
|
|
3131
|
+
}
|
|
3132
|
+
const xs = new Array(total);
|
|
3133
|
+
let x = 0;
|
|
3134
|
+
for (let i = 0; i < total; i++) {
|
|
3135
|
+
const flag = flags[i];
|
|
3136
|
+
if (flag & 2) {
|
|
3137
|
+
const dx = r.u8();
|
|
3138
|
+
x += flag & 16 ? dx : -dx;
|
|
3139
|
+
} else if (!(flag & 16)) {
|
|
3140
|
+
x += r.i16();
|
|
3141
|
+
}
|
|
3142
|
+
xs[i] = x;
|
|
3143
|
+
}
|
|
3144
|
+
const ys = new Array(total);
|
|
3145
|
+
let y = 0;
|
|
3146
|
+
for (let i = 0; i < total; i++) {
|
|
3147
|
+
const flag = flags[i];
|
|
3148
|
+
if (flag & 4) {
|
|
3149
|
+
const dy = r.u8();
|
|
3150
|
+
y += flag & 32 ? dy : -dy;
|
|
3151
|
+
} else if (!(flag & 32)) {
|
|
3152
|
+
y += r.i16();
|
|
3153
|
+
}
|
|
3154
|
+
ys[i] = y;
|
|
3155
|
+
}
|
|
3156
|
+
const out = [];
|
|
3157
|
+
let first = 0;
|
|
3158
|
+
for (const last of ends) {
|
|
3159
|
+
out.push(contourToCubics(xs, ys, flags, first, last));
|
|
3160
|
+
first = last + 1;
|
|
3161
|
+
}
|
|
3162
|
+
return out;
|
|
3163
|
+
};
|
|
3164
|
+
function composite(_glyph, depth) {
|
|
3165
|
+
const parts = [];
|
|
3166
|
+
let more = true;
|
|
3167
|
+
while (more) {
|
|
3168
|
+
const flags = r.u16();
|
|
3169
|
+
const glyph = r.u16();
|
|
3170
|
+
const words = (flags & 1) !== 0;
|
|
3171
|
+
const xy = (flags & 2) !== 0;
|
|
3172
|
+
let dx = 0, dy = 0;
|
|
3173
|
+
if (words) {
|
|
3174
|
+
dx = r.i16();
|
|
3175
|
+
dy = r.i16();
|
|
3176
|
+
} else {
|
|
3177
|
+
dx = r.i8();
|
|
3178
|
+
dy = r.i8();
|
|
3179
|
+
}
|
|
3180
|
+
if (!xy) {
|
|
3181
|
+
dx = 0;
|
|
3182
|
+
dy = 0;
|
|
3183
|
+
}
|
|
3184
|
+
let a = 1, b = 0, c = 0, d = 1;
|
|
3185
|
+
const f2 = () => r.i16() / 16384;
|
|
3186
|
+
if (flags & 8) {
|
|
3187
|
+
a = d = f2();
|
|
3188
|
+
} else if (flags & 64) {
|
|
3189
|
+
a = f2();
|
|
3190
|
+
d = f2();
|
|
3191
|
+
} else if (flags & 128) {
|
|
3192
|
+
a = f2();
|
|
3193
|
+
b = f2();
|
|
3194
|
+
c = f2();
|
|
3195
|
+
d = f2();
|
|
3196
|
+
}
|
|
3197
|
+
parts.push({ glyph, m: [a, b, c, d, dx, dy] });
|
|
3198
|
+
more = (flags & 32) !== 0;
|
|
3199
|
+
}
|
|
3200
|
+
const out = [];
|
|
3201
|
+
for (const part of parts) {
|
|
3202
|
+
for (const contour of contours(part.glyph, depth + 1)) {
|
|
3203
|
+
const moved = new Array(contour.length);
|
|
3204
|
+
const [a, b, c, d, e, f] = part.m;
|
|
3205
|
+
for (let i = 0; i < contour.length; i += 2) {
|
|
3206
|
+
moved[i] = a * contour[i] + c * contour[i + 1] + e;
|
|
3207
|
+
moved[i + 1] = b * contour[i] + d * contour[i + 1] + f;
|
|
3208
|
+
}
|
|
3209
|
+
out.push(moved);
|
|
3210
|
+
}
|
|
3211
|
+
}
|
|
3212
|
+
return out;
|
|
3213
|
+
}
|
|
3214
|
+
return {
|
|
3215
|
+
unitsPerEm,
|
|
3216
|
+
ascent,
|
|
3217
|
+
descent,
|
|
3218
|
+
lineGap,
|
|
3219
|
+
glyphFor(code) {
|
|
3220
|
+
return cmap(code);
|
|
3221
|
+
},
|
|
3222
|
+
advance,
|
|
3223
|
+
kern(left, right) {
|
|
3224
|
+
return kerning(left, right);
|
|
3225
|
+
},
|
|
3226
|
+
outline(glyph) {
|
|
3227
|
+
if (outlines.has(glyph)) return outlines.get(glyph);
|
|
3228
|
+
const raw = contours(glyph, 0);
|
|
3229
|
+
const result = raw.length ? curveFree(raw) : null;
|
|
3230
|
+
outlines.set(glyph, result);
|
|
3231
|
+
return result;
|
|
3232
|
+
}
|
|
3233
|
+
};
|
|
3234
|
+
}
|
|
3235
|
+
function contourToCubics(xs, ys, flags, first, last) {
|
|
3236
|
+
const count = last - first + 1;
|
|
3237
|
+
if (count < 2) return [];
|
|
3238
|
+
const at = (i) => first + (i % count + count) % count;
|
|
3239
|
+
const on = (i) => (flags[at(i)] & 1) !== 0;
|
|
3240
|
+
let startIndex = 0;
|
|
3241
|
+
while (startIndex < count && !on(startIndex)) startIndex++;
|
|
3242
|
+
let sx, sy;
|
|
3243
|
+
if (startIndex === count) {
|
|
3244
|
+
sx = (xs[at(0)] + xs[at(count - 1)]) / 2;
|
|
3245
|
+
sy = (ys[at(0)] + ys[at(count - 1)]) / 2;
|
|
3246
|
+
startIndex = 0;
|
|
3247
|
+
} else {
|
|
3248
|
+
sx = xs[at(startIndex)];
|
|
3249
|
+
sy = ys[at(startIndex)];
|
|
3250
|
+
startIndex += 1;
|
|
3251
|
+
}
|
|
3252
|
+
const out = [sx, sy];
|
|
3253
|
+
let cx = sx, cy = sy;
|
|
3254
|
+
let controlX = 0, controlY = 0, hasControl = false;
|
|
3255
|
+
const quad = (qx, qy, ex, ey) => {
|
|
3256
|
+
out.push(cx + (qx - cx) * 2 / 3, cy + (qy - cy) * 2 / 3, ex + (qx - ex) * 2 / 3, ey + (qy - ey) * 2 / 3, ex, ey);
|
|
3257
|
+
cx = ex;
|
|
3258
|
+
cy = ey;
|
|
3259
|
+
};
|
|
3260
|
+
const line = (ex, ey) => {
|
|
3261
|
+
out.push(cx + (ex - cx) / 3, cy + (ey - cy) / 3, cx + (ex - cx) * 2 / 3, cy + (ey - cy) * 2 / 3, ex, ey);
|
|
3262
|
+
cx = ex;
|
|
3263
|
+
cy = ey;
|
|
3264
|
+
};
|
|
3265
|
+
for (let k = 0; k < count; k++) {
|
|
3266
|
+
const i = startIndex + k;
|
|
3267
|
+
const x = xs[at(i)], y = ys[at(i)];
|
|
3268
|
+
if (on(i)) {
|
|
3269
|
+
if (hasControl) {
|
|
3270
|
+
quad(controlX, controlY, x, y);
|
|
3271
|
+
hasControl = false;
|
|
3272
|
+
} else if (x !== cx || y !== cy) {
|
|
3273
|
+
line(x, y);
|
|
3274
|
+
}
|
|
3275
|
+
} else {
|
|
3276
|
+
if (hasControl) quad(controlX, controlY, (controlX + x) / 2, (controlY + y) / 2);
|
|
3277
|
+
controlX = x;
|
|
3278
|
+
controlY = y;
|
|
3279
|
+
hasControl = true;
|
|
3280
|
+
}
|
|
3281
|
+
}
|
|
3282
|
+
if (hasControl) quad(controlX, controlY, sx, sy);
|
|
3283
|
+
else if (cx !== sx || cy !== sy) line(sx, sy);
|
|
3284
|
+
return out;
|
|
3285
|
+
}
|
|
3286
|
+
function parseCmap(r, offset) {
|
|
3287
|
+
r.seek(offset + 2);
|
|
3288
|
+
const count = r.u16();
|
|
3289
|
+
let best = -1, bestScore = -1;
|
|
3290
|
+
for (let i = 0; i < count; i++) {
|
|
3291
|
+
const platform = r.u16();
|
|
3292
|
+
const encoding = r.u16();
|
|
3293
|
+
const sub = r.u32();
|
|
3294
|
+
const score = platform === 3 && encoding === 10 ? 4 : platform === 0 && encoding >= 4 ? 3 : platform === 3 && encoding === 1 ? 2 : platform === 0 ? 1 : 0;
|
|
3295
|
+
if (score > bestScore) {
|
|
3296
|
+
bestScore = score;
|
|
3297
|
+
best = offset + sub;
|
|
3298
|
+
}
|
|
3299
|
+
}
|
|
3300
|
+
if (best < 0) return () => 0;
|
|
3301
|
+
r.seek(best);
|
|
3302
|
+
const format = r.u16();
|
|
3303
|
+
if (format === 12) {
|
|
3304
|
+
r.seek(best + 12);
|
|
3305
|
+
const groups = r.u32();
|
|
3306
|
+
const table = [];
|
|
3307
|
+
for (let i = 0; i < groups; i++) table.push([r.u32(), r.u32(), r.u32()]);
|
|
3308
|
+
return (code) => {
|
|
3309
|
+
let lo = 0, hi = table.length - 1;
|
|
3310
|
+
while (lo <= hi) {
|
|
3311
|
+
const mid = lo + hi >> 1;
|
|
3312
|
+
const [start, end, glyph] = table[mid];
|
|
3313
|
+
if (code < start) hi = mid - 1;
|
|
3314
|
+
else if (code > end) lo = mid + 1;
|
|
3315
|
+
else return glyph + code - start;
|
|
3316
|
+
}
|
|
3317
|
+
return 0;
|
|
3318
|
+
};
|
|
3319
|
+
}
|
|
3320
|
+
if (format === 4) {
|
|
3321
|
+
r.seek(best + 6);
|
|
3322
|
+
const segX2 = r.u16();
|
|
3323
|
+
const segs = segX2 / 2;
|
|
3324
|
+
const endsAt = best + 14;
|
|
3325
|
+
const startsAt = endsAt + segX2 + 2;
|
|
3326
|
+
const deltasAt = startsAt + segX2;
|
|
3327
|
+
const rangesAt = deltasAt + segX2;
|
|
3328
|
+
const map = /* @__PURE__ */ new Map();
|
|
3329
|
+
for (let i = 0; i < segs; i++) {
|
|
3330
|
+
const end = r.seek(endsAt + i * 2).u16();
|
|
3331
|
+
const start = r.seek(startsAt + i * 2).u16();
|
|
3332
|
+
const delta = r.seek(deltasAt + i * 2).i16();
|
|
3333
|
+
const range = r.seek(rangesAt + i * 2).u16();
|
|
3334
|
+
for (let c = start; c <= end && c !== 65535; c++) {
|
|
3335
|
+
let glyph;
|
|
3336
|
+
if (range === 0) glyph = c + delta & 65535;
|
|
3337
|
+
else {
|
|
3338
|
+
const raw = r.seek(rangesAt + i * 2 + range + (c - start) * 2).u16();
|
|
3339
|
+
glyph = raw === 0 ? 0 : raw + delta & 65535;
|
|
3340
|
+
}
|
|
3341
|
+
if (glyph) map.set(c, glyph);
|
|
3342
|
+
}
|
|
3343
|
+
}
|
|
3344
|
+
return (code) => map.get(code) ?? 0;
|
|
3345
|
+
}
|
|
3346
|
+
return () => 0;
|
|
3347
|
+
}
|
|
3348
|
+
function parseKerning(r, gpos) {
|
|
3349
|
+
const lookups = [];
|
|
3350
|
+
try {
|
|
3351
|
+
r.seek(gpos + 6);
|
|
3352
|
+
const featureList = gpos + r.u16();
|
|
3353
|
+
const lookupList = gpos + r.u16();
|
|
3354
|
+
r.seek(featureList);
|
|
3355
|
+
const featureCount = r.u16();
|
|
3356
|
+
const indices = /* @__PURE__ */ new Set();
|
|
3357
|
+
for (let i = 0; i < featureCount; i++) {
|
|
3358
|
+
r.seek(featureList + 2 + i * 6);
|
|
3359
|
+
const tag = r.tag();
|
|
3360
|
+
const offset = r.u16();
|
|
3361
|
+
if (tag !== "kern") continue;
|
|
3362
|
+
r.seek(featureList + offset + 2);
|
|
3363
|
+
const count = r.u16();
|
|
3364
|
+
for (let k = 0; k < count; k++) indices.add(r.u16());
|
|
3365
|
+
}
|
|
3366
|
+
for (const index of [...indices].sort((a, b) => a - b)) {
|
|
3367
|
+
r.seek(lookupList + 2 + index * 2);
|
|
3368
|
+
const lookup = lookupList + r.u16();
|
|
3369
|
+
r.seek(lookup);
|
|
3370
|
+
const lookupType = r.u16();
|
|
3371
|
+
r.u16();
|
|
3372
|
+
const subCount = r.u16();
|
|
3373
|
+
const subs = [];
|
|
3374
|
+
for (let i = 0; i < subCount; i++) subs.push(lookup + r.u16());
|
|
3375
|
+
const tables = [];
|
|
3376
|
+
for (const first of subs) {
|
|
3377
|
+
let sub = first;
|
|
3378
|
+
let type = lookupType;
|
|
3379
|
+
if (type === 9) {
|
|
3380
|
+
r.seek(sub + 2);
|
|
3381
|
+
type = r.u16();
|
|
3382
|
+
sub += r.u32();
|
|
3383
|
+
}
|
|
3384
|
+
if (type === 2) tables.push(readPairs(r, sub));
|
|
3385
|
+
}
|
|
3386
|
+
lookups.push(tables);
|
|
3387
|
+
}
|
|
3388
|
+
} catch {
|
|
3389
|
+
}
|
|
3390
|
+
const cache2 = /* @__PURE__ */ new Map();
|
|
3391
|
+
return (left, right) => {
|
|
3392
|
+
const key = left * 65536 + right;
|
|
3393
|
+
const cached = cache2.get(key);
|
|
3394
|
+
if (cached !== void 0) return cached;
|
|
3395
|
+
let total = 0;
|
|
3396
|
+
for (const tables of lookups) {
|
|
3397
|
+
for (const table of tables) {
|
|
3398
|
+
const value = table(left, right);
|
|
3399
|
+
if (value !== void 0) {
|
|
3400
|
+
total += value;
|
|
3401
|
+
break;
|
|
3402
|
+
}
|
|
3403
|
+
}
|
|
3404
|
+
}
|
|
3405
|
+
cache2.set(key, total);
|
|
3406
|
+
return total;
|
|
3407
|
+
};
|
|
3408
|
+
}
|
|
3409
|
+
function readCoverage(r, offset) {
|
|
3410
|
+
r.seek(offset);
|
|
3411
|
+
const format = r.u16();
|
|
3412
|
+
const out = /* @__PURE__ */ new Set();
|
|
3413
|
+
if (format === 1) {
|
|
3414
|
+
const count = r.u16();
|
|
3415
|
+
for (let i = 0; i < count; i++) out.add(r.u16());
|
|
3416
|
+
} else if (format === 2) {
|
|
3417
|
+
const count = r.u16();
|
|
3418
|
+
for (let i = 0; i < count; i++) {
|
|
3419
|
+
const start = r.u16(), end = r.u16();
|
|
3420
|
+
r.u16();
|
|
3421
|
+
for (let glyph = start; glyph <= end; glyph++) out.add(glyph);
|
|
3422
|
+
}
|
|
3423
|
+
}
|
|
3424
|
+
return out;
|
|
3425
|
+
}
|
|
3426
|
+
function readClasses(r, offset) {
|
|
3427
|
+
r.seek(offset);
|
|
3428
|
+
const format = r.u16();
|
|
3429
|
+
const classes = /* @__PURE__ */ new Map();
|
|
3430
|
+
if (format === 1) {
|
|
3431
|
+
const start = r.u16();
|
|
3432
|
+
const count = r.u16();
|
|
3433
|
+
for (let i = 0; i < count; i++) classes.set(start + i, r.u16());
|
|
3434
|
+
} else if (format === 2) {
|
|
3435
|
+
const count = r.u16();
|
|
3436
|
+
for (let i = 0; i < count; i++) {
|
|
3437
|
+
const start = r.u16(), end = r.u16(), value = r.u16();
|
|
3438
|
+
for (let glyph = start; glyph <= end; glyph++) classes.set(glyph, value);
|
|
3439
|
+
}
|
|
3440
|
+
}
|
|
3441
|
+
return (glyph) => classes.get(glyph) ?? 0;
|
|
3442
|
+
}
|
|
3443
|
+
function valueSize(format) {
|
|
3444
|
+
let count = 0;
|
|
3445
|
+
for (let bit = 0; bit < 8; bit++) if (format & 1 << bit) count++;
|
|
3446
|
+
return count * 2;
|
|
3447
|
+
}
|
|
3448
|
+
function readAdvance(r, format) {
|
|
3449
|
+
let advance = 0;
|
|
3450
|
+
if (format & 1) r.i16();
|
|
3451
|
+
if (format & 2) r.i16();
|
|
3452
|
+
if (format & 4) advance = r.i16();
|
|
3453
|
+
if (format & 8) r.i16();
|
|
3454
|
+
for (let bit = 4; bit < 8; bit++) if (format & 1 << bit) r.u16();
|
|
3455
|
+
return advance;
|
|
3456
|
+
}
|
|
3457
|
+
function readPairs(r, sub) {
|
|
3458
|
+
r.seek(sub);
|
|
3459
|
+
const format = r.u16();
|
|
3460
|
+
const coverageOffset = r.u16();
|
|
3461
|
+
const value1 = r.u16();
|
|
3462
|
+
const value2 = r.u16();
|
|
3463
|
+
const size1 = valueSize(value1);
|
|
3464
|
+
const size2 = valueSize(value2);
|
|
3465
|
+
if (format === 1) {
|
|
3466
|
+
const setCount = r.u16();
|
|
3467
|
+
const offsets = [];
|
|
3468
|
+
for (let i = 0; i < setCount; i++) offsets.push(r.u16());
|
|
3469
|
+
r.seek(sub + coverageOffset);
|
|
3470
|
+
const coverageFormat = r.u16();
|
|
3471
|
+
const glyphs = [];
|
|
3472
|
+
if (coverageFormat === 1) {
|
|
3473
|
+
const count = r.u16();
|
|
3474
|
+
for (let i = 0; i < count; i++) glyphs.push(r.u16());
|
|
3475
|
+
} else if (coverageFormat === 2) {
|
|
3476
|
+
const count = r.u16();
|
|
3477
|
+
for (let i = 0; i < count; i++) {
|
|
3478
|
+
const start = r.u16(), end = r.u16();
|
|
3479
|
+
r.u16();
|
|
3480
|
+
for (let glyph = start; glyph <= end; glyph++) glyphs.push(glyph);
|
|
3481
|
+
}
|
|
3482
|
+
}
|
|
3483
|
+
const sets = /* @__PURE__ */ new Map();
|
|
3484
|
+
for (let i = 0; i < setCount; i++) {
|
|
3485
|
+
r.seek(sub + offsets[i]);
|
|
3486
|
+
const count = r.u16();
|
|
3487
|
+
const pairs = /* @__PURE__ */ new Map();
|
|
3488
|
+
for (let k = 0; k < count; k++) {
|
|
3489
|
+
const second = r.u16();
|
|
3490
|
+
const advance = readAdvance(r, value1);
|
|
3491
|
+
r.seek(r.position + size2);
|
|
3492
|
+
pairs.set(second, advance);
|
|
3493
|
+
}
|
|
3494
|
+
if (glyphs[i] !== void 0) sets.set(glyphs[i], pairs);
|
|
3495
|
+
}
|
|
3496
|
+
return (left, right) => sets.get(left)?.get(right);
|
|
3497
|
+
}
|
|
3498
|
+
if (format === 2) {
|
|
3499
|
+
const class1Offset = r.u16();
|
|
3500
|
+
const class2Offset = r.u16();
|
|
3501
|
+
const class1Count = r.u16();
|
|
3502
|
+
const class2Count = r.u16();
|
|
3503
|
+
const matrix = r.position;
|
|
3504
|
+
const coverage = readCoverage(r, sub + coverageOffset);
|
|
3505
|
+
const class1 = readClasses(r, sub + class1Offset);
|
|
3506
|
+
const class2 = readClasses(r, sub + class2Offset);
|
|
3507
|
+
return (left, right) => {
|
|
3508
|
+
if (!coverage.has(left)) return void 0;
|
|
3509
|
+
const c1 = class1(left), c2 = class2(right);
|
|
3510
|
+
if (c1 >= class1Count || c2 >= class2Count) return void 0;
|
|
3511
|
+
r.seek(matrix + c1 * class2Count * (size1 + size2) + c2 * (size1 + size2));
|
|
3512
|
+
return readAdvance(r, value1);
|
|
3513
|
+
};
|
|
3514
|
+
}
|
|
3515
|
+
return () => void 0;
|
|
3516
|
+
}
|
|
3517
|
+
var FILES = {
|
|
3518
|
+
sans: { "400": "Inter-Regular.ttf", "600": "Inter-SemiBold.ttf", "700": "Inter-Bold.ttf" },
|
|
3519
|
+
serif: { "400": "InstrumentSerif-Regular.ttf", "italic": "InstrumentSerif-Italic.ttf" },
|
|
3520
|
+
mono: { "400": "JetBrainsMono-Regular.ttf" },
|
|
3521
|
+
noto: { "400": "NotoSans-Regular.ttf" }
|
|
3522
|
+
};
|
|
3523
|
+
var fonts = /* @__PURE__ */ new Map();
|
|
3524
|
+
var pending = /* @__PURE__ */ new Map();
|
|
3525
|
+
function fontFor(family, weight = 400, italic = false) {
|
|
3526
|
+
const files = FILES[family];
|
|
3527
|
+
const file = (italic ? files["italic"] : void 0) ?? files[String(weight)] ?? files["400"];
|
|
3528
|
+
const loaded = fonts.get(file);
|
|
3529
|
+
if (loaded) return loaded;
|
|
3530
|
+
let waiting = pending.get(file);
|
|
3531
|
+
if (!waiting) {
|
|
3532
|
+
waiting = loadBinaryAsset(`fonts/${file}`).then((bytes) => {
|
|
3533
|
+
fonts.set(file, parseFont(bytes));
|
|
3534
|
+
pending.delete(file);
|
|
3535
|
+
});
|
|
3536
|
+
pending.set(file, waiting);
|
|
3537
|
+
}
|
|
3538
|
+
throw new Suspend(waiting);
|
|
3539
|
+
}
|
|
3540
|
+
var glyphPaths = /* @__PURE__ */ new WeakMap();
|
|
3541
|
+
function glyphPath(font, glyph, size) {
|
|
3542
|
+
let bySize = glyphPaths.get(font);
|
|
3543
|
+
if (!bySize) glyphPaths.set(font, bySize = /* @__PURE__ */ new Map());
|
|
3544
|
+
let byGlyph = bySize.get(size);
|
|
3545
|
+
if (!byGlyph) bySize.set(size, byGlyph = /* @__PURE__ */ new Map());
|
|
3546
|
+
const cached = byGlyph.get(glyph);
|
|
3547
|
+
if (cached) return cached;
|
|
3548
|
+
const outline = font.outline(glyph);
|
|
3549
|
+
const scale = size / font.unitsPerEm;
|
|
3550
|
+
const path = outline ? outline.contours.map((contour) => {
|
|
3551
|
+
const out = new Float64Array(contour.length);
|
|
3552
|
+
for (let i = 0; i < contour.length; i += 2) {
|
|
3553
|
+
out[i] = contour[i] * scale;
|
|
3554
|
+
out[i + 1] = -contour[i + 1] * scale;
|
|
3555
|
+
}
|
|
3556
|
+
return ring(out, true);
|
|
3557
|
+
}) : [];
|
|
3558
|
+
byGlyph.set(glyph, path);
|
|
3559
|
+
return path;
|
|
3560
|
+
}
|
|
3561
|
+
|
|
3562
|
+
// src/stage/text.ts
|
|
3563
|
+
var TextNode = class extends Node {
|
|
3564
|
+
chars = [];
|
|
3565
|
+
source = "";
|
|
3566
|
+
/** Glyph nodes whose characters match, such as every "x". */
|
|
3567
|
+
find(fragment) {
|
|
3568
|
+
const out = [];
|
|
3569
|
+
const keys = this.chars.map((glyph) => glyph.data["key"]).join("");
|
|
3570
|
+
const needle = fragment.replace(/\s/g, "");
|
|
3571
|
+
for (let at = keys.indexOf(needle); at >= 0; at = keys.indexOf(needle, at + 1)) {
|
|
3572
|
+
out.push(...this.chars.slice(at, at + needle.length));
|
|
3573
|
+
}
|
|
3574
|
+
return out;
|
|
3575
|
+
}
|
|
3576
|
+
};
|
|
3577
|
+
function centered(path) {
|
|
3578
|
+
const b = pathBounds(path);
|
|
3579
|
+
if (!b) return { path, x: 0, y: 0 };
|
|
3580
|
+
const x = b.x + b.width / 2, y = b.y + b.height / 2;
|
|
3581
|
+
return { path: transformPath(path, [1, 0, 0, 1, -x, -y]), x, y };
|
|
3582
|
+
}
|
|
3583
|
+
function text(content, options = {}) {
|
|
3584
|
+
const size = options.size ?? 40;
|
|
3585
|
+
const font = fontFor(options.font ?? "sans", options.weight ?? 400, options.italic ?? false);
|
|
3586
|
+
const scale = size / font.unitsPerEm;
|
|
3587
|
+
const tracking = (options.tracking ?? 0) * size;
|
|
3588
|
+
const leading = (options.leading ?? 1.3) * size;
|
|
3589
|
+
const paint = toPaint(options.color ?? "ink");
|
|
3590
|
+
const source = options.uppercase ? content.toUpperCase() : content;
|
|
3591
|
+
const measureWord = (word) => {
|
|
3592
|
+
const glyphs = [];
|
|
3593
|
+
let x = 0, previous = 0;
|
|
3594
|
+
for (const char of word) {
|
|
3595
|
+
const code = char.codePointAt(0);
|
|
3596
|
+
const glyph = font.glyphFor(code);
|
|
3597
|
+
if (previous) x += font.kern(previous, glyph) * scale;
|
|
3598
|
+
glyphs.push({ code, glyph, x, char });
|
|
3599
|
+
x += font.advance(glyph) * scale + tracking;
|
|
3600
|
+
previous = glyph;
|
|
3601
|
+
}
|
|
3602
|
+
return { glyphs, width: x };
|
|
3603
|
+
};
|
|
3604
|
+
const lines = [];
|
|
3605
|
+
const space = font.advance(font.glyphFor(32)) * scale + tracking;
|
|
3606
|
+
for (const paragraph of source.split("\n")) {
|
|
3607
|
+
let line = [];
|
|
3608
|
+
let width = 0;
|
|
3609
|
+
for (const word of paragraph.split(" ")) {
|
|
3610
|
+
const measured = measureWord(word);
|
|
3611
|
+
const gap = line.length > 0 ? space : 0;
|
|
3612
|
+
if (options.maxWidth !== void 0 && line.length > 0 && width + gap + measured.width > options.maxWidth) {
|
|
3613
|
+
lines.push({ glyphs: line, width });
|
|
3614
|
+
line = [];
|
|
3615
|
+
width = 0;
|
|
3616
|
+
}
|
|
3617
|
+
const offset = width + (line.length > 0 ? space : 0);
|
|
3618
|
+
for (const glyph of measured.glyphs) line.push({ ...glyph, x: glyph.x + offset });
|
|
3619
|
+
width = offset + measured.width;
|
|
3620
|
+
}
|
|
3621
|
+
lines.push({ glyphs: line, width });
|
|
3622
|
+
}
|
|
3623
|
+
const blockWidth = Math.max(0, ...lines.map((line) => line.width));
|
|
3624
|
+
const align = options.align ?? "left";
|
|
3625
|
+
const node = new TextNode();
|
|
3626
|
+
node.source = content;
|
|
3627
|
+
lines.forEach((line, row) => {
|
|
3628
|
+
const start = align === "left" ? 0 : align === "center" ? (blockWidth - line.width) / 2 : blockWidth - line.width;
|
|
3629
|
+
for (const placed of line.glyphs) {
|
|
3630
|
+
const path = glyphPath(font, placed.glyph, size);
|
|
3631
|
+
if (path.length === 0) continue;
|
|
3632
|
+
const shape = centered(path);
|
|
3633
|
+
const leaf = new Node(shape.path).at(start + placed.x + shape.x, row * leading + shape.y);
|
|
3634
|
+
leaf.base.fill = paint;
|
|
3635
|
+
leaf.base.trimEach = true;
|
|
3636
|
+
leaf.data["key"] = placed.char;
|
|
3637
|
+
node.chars.push(leaf);
|
|
3638
|
+
node.children.push(leaf);
|
|
3639
|
+
leaf.parent = node;
|
|
3640
|
+
}
|
|
3641
|
+
});
|
|
3642
|
+
const ascent = font.ascent * scale;
|
|
3643
|
+
const height = (lines.length - 1) * leading;
|
|
3644
|
+
node.base.px = blockWidth / 2;
|
|
3645
|
+
node.base.py = height / 2 - ascent * 0.36;
|
|
3646
|
+
node.data["size"] = size;
|
|
3647
|
+
return node;
|
|
3648
|
+
}
|
|
3649
|
+
|
|
3650
|
+
// src/stage/svg-import.ts
|
|
3651
|
+
function decode(value) {
|
|
3652
|
+
return value.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(Number(code))).replace(/&#x([\da-fA-F]+);/g, (_, code) => String.fromCodePoint(parseInt(code, 16))).replace(/&/g, "&");
|
|
3653
|
+
}
|
|
3654
|
+
function parseXml(source) {
|
|
3655
|
+
const root = { name: "#root", attrs: {}, children: [], text: "" };
|
|
3656
|
+
const stack = [root];
|
|
3657
|
+
const token = /<!--[\s\S]*?-->|<!\[CDATA\[([\s\S]*?)\]\]>|<\?[\s\S]*?\?>|<!DOCTYPE[^>]*>|<\/([\w:.-]+)\s*>|<([\w:.-]+)((?:\s+[\w:.-]+\s*=\s*(?:"[^"]*"|'[^']*'))*)\s*(\/?)>|([^<]+)/g;
|
|
3658
|
+
for (let match = token.exec(source); match; match = token.exec(source)) {
|
|
3659
|
+
const top = stack[stack.length - 1];
|
|
3660
|
+
const [, cdata, closing, opening, attributes, selfClosing, content] = match;
|
|
3661
|
+
if (cdata !== void 0) {
|
|
3662
|
+
top.text += cdata;
|
|
3663
|
+
} else if (closing) {
|
|
3664
|
+
if (stack.length > 1) stack.pop();
|
|
3665
|
+
} else if (opening) {
|
|
3666
|
+
const element = { name: opening.replace(/^.*:/, ""), attrs: {}, children: [], text: "" };
|
|
3667
|
+
for (const attr of (attributes ?? "").matchAll(/([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g)) {
|
|
3668
|
+
element.attrs[attr[1]] = decode(attr[2] ?? attr[3] ?? "");
|
|
3669
|
+
}
|
|
3670
|
+
top.children.push(element);
|
|
3671
|
+
if (!selfClosing) stack.push(element);
|
|
3672
|
+
} else if (content) {
|
|
3673
|
+
top.text += decode(content);
|
|
3674
|
+
}
|
|
3675
|
+
}
|
|
3676
|
+
return root;
|
|
3677
|
+
}
|
|
3678
|
+
function parseTransform(value) {
|
|
3679
|
+
if (!value) return IDENTITY;
|
|
3680
|
+
let m = IDENTITY;
|
|
3681
|
+
for (const match of value.matchAll(/(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/g)) {
|
|
3682
|
+
const a = match[2].trim().split(/[\s,]+/).filter(Boolean).map(Number);
|
|
3683
|
+
const name = match[1];
|
|
3684
|
+
if (name === "matrix" && a.length === 6) m = mul(m, a);
|
|
3685
|
+
else if (name === "translate") m = mul(m, [1, 0, 0, 1, a[0] ?? 0, a[1] ?? 0]);
|
|
3686
|
+
else if (name === "scale") m = mul(m, [a[0] ?? 1, 0, 0, a[1] ?? a[0] ?? 1, 0, 0]);
|
|
3687
|
+
else if (name === "rotate") {
|
|
3688
|
+
const r = (a[0] ?? 0) * Math.PI / 180;
|
|
3689
|
+
const c = Math.cos(r), s = Math.sin(r);
|
|
3690
|
+
const cx = a[1] ?? 0, cy = a[2] ?? 0;
|
|
3691
|
+
m = mul(m, [1, 0, 0, 1, cx, cy]);
|
|
3692
|
+
m = mul(m, [c, s, -s, c, 0, 0]);
|
|
3693
|
+
m = mul(m, [1, 0, 0, 1, -cx, -cy]);
|
|
3694
|
+
} else if (name === "skewX") m = mul(m, [1, 0, Math.tan((a[0] ?? 0) * Math.PI / 180), 1, 0, 0]);
|
|
3695
|
+
else if (name === "skewY") m = mul(m, [1, Math.tan((a[0] ?? 0) * Math.PI / 180), 0, 1, 0, 0]);
|
|
3696
|
+
}
|
|
3697
|
+
return m;
|
|
3698
|
+
}
|
|
3699
|
+
var NOT_DRAWN = /* @__PURE__ */ new Set(["defs", "clipPath", "mask", "pattern", "filter", "symbol", "marker", "linearGradient", "radialGradient", "title", "desc", "metadata", "style", "script"]);
|
|
3700
|
+
var SvgNode = class extends Node {
|
|
3701
|
+
byId = /* @__PURE__ */ new Map();
|
|
3702
|
+
pictureWidth = 0;
|
|
3703
|
+
pictureHeight = 0;
|
|
3704
|
+
/** Find an element by its SVG id. */
|
|
3705
|
+
find(id) {
|
|
3706
|
+
const found = this.byId.get(id);
|
|
3707
|
+
if (!found) throw new Error(`The SVG has no element with id ${JSON.stringify(id)}.`);
|
|
3708
|
+
return found;
|
|
3709
|
+
}
|
|
3710
|
+
};
|
|
3711
|
+
function parseCssRules(root) {
|
|
3712
|
+
const rules = /* @__PURE__ */ new Map();
|
|
3713
|
+
const visit = (element) => {
|
|
3714
|
+
if (element.name === "style") {
|
|
3715
|
+
for (const rule of element.text.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
|
|
3716
|
+
const body = parseStyle(rule[2]);
|
|
3717
|
+
for (const selector of rule[1].split(",")) {
|
|
3718
|
+
const key = selector.trim();
|
|
3719
|
+
rules.set(key, { ...rules.get(key), ...body });
|
|
3720
|
+
}
|
|
3721
|
+
}
|
|
3722
|
+
}
|
|
3723
|
+
element.children.forEach(visit);
|
|
3724
|
+
};
|
|
3725
|
+
visit(root);
|
|
3726
|
+
return rules;
|
|
3727
|
+
}
|
|
3728
|
+
function parseStyle(value) {
|
|
3729
|
+
const out = {};
|
|
3730
|
+
if (!value) return out;
|
|
3731
|
+
for (const part of value.split(";")) {
|
|
3732
|
+
const index = part.indexOf(":");
|
|
3733
|
+
if (index > 0) out[part.slice(0, index).trim()] = part.slice(index + 1).trim();
|
|
3734
|
+
}
|
|
3735
|
+
return out;
|
|
3736
|
+
}
|
|
3737
|
+
function safeColor(value) {
|
|
3738
|
+
if (!value || value === "none" || value === "transparent") return null;
|
|
3739
|
+
try {
|
|
3740
|
+
return parseColor(value);
|
|
3741
|
+
} catch {
|
|
3742
|
+
return null;
|
|
3743
|
+
}
|
|
3744
|
+
}
|
|
3745
|
+
function splitIntoGlyphs(path) {
|
|
3746
|
+
const items = path.map((r) => {
|
|
3747
|
+
const b = pathBounds([r]);
|
|
3748
|
+
return { ring: r, b, area: b.width * b.height, owner: -1 };
|
|
3749
|
+
});
|
|
3750
|
+
const order = items.map((_, i) => i).sort((a, b) => items[b].area - items[a].area);
|
|
3751
|
+
for (let k = 0; k < order.length; k++) {
|
|
3752
|
+
const inner = items[order[k]];
|
|
3753
|
+
for (let j = k - 1; j >= 0; j--) {
|
|
3754
|
+
const outer = items[order[j]];
|
|
3755
|
+
if (outer.owner !== -1) continue;
|
|
3756
|
+
const inside = inner.b.x >= outer.b.x - 0.01 && inner.b.y >= outer.b.y - 0.01 && inner.b.x + inner.b.width <= outer.b.x + outer.b.width + 0.01 && inner.b.y + inner.b.height <= outer.b.y + outer.b.height + 0.01;
|
|
3757
|
+
if (inside) {
|
|
3758
|
+
inner.owner = order[j];
|
|
3759
|
+
break;
|
|
3760
|
+
}
|
|
3761
|
+
}
|
|
3762
|
+
}
|
|
3763
|
+
const glyphs = [];
|
|
3764
|
+
const ownerToGlyph = /* @__PURE__ */ new Map();
|
|
3765
|
+
items.forEach((item, i) => {
|
|
3766
|
+
if (item.owner !== -1) return;
|
|
3767
|
+
const glyph = { x: item.b.x, rings: [item.ring] };
|
|
3768
|
+
ownerToGlyph.set(i, glyph);
|
|
3769
|
+
glyphs.push(glyph);
|
|
3770
|
+
});
|
|
3771
|
+
items.forEach((item) => {
|
|
3772
|
+
if (item.owner !== -1) ownerToGlyph.get(item.owner)?.rings.push(item.ring);
|
|
3773
|
+
});
|
|
3774
|
+
return glyphs.sort((a, b) => a.x - b.x).map((glyph) => glyph.rings);
|
|
3775
|
+
}
|
|
3776
|
+
function fromSvg(source, options = {}) {
|
|
3777
|
+
const root = parseXml(source);
|
|
3778
|
+
const svg = root.children.find((child) => child.name === "svg");
|
|
3779
|
+
if (!svg) throw new Error("fromSvg needs markup with an <svg> element.");
|
|
3780
|
+
const rules = parseCssRules(svg);
|
|
3781
|
+
const defs = /* @__PURE__ */ new Map();
|
|
3782
|
+
const index = (element) => {
|
|
3783
|
+
if (element.attrs["id"]) defs.set(element.attrs["id"], element);
|
|
3784
|
+
element.children.forEach(index);
|
|
3785
|
+
};
|
|
3786
|
+
index(svg);
|
|
3787
|
+
const box = (svg.attrs["viewBox"] ?? "").trim().split(/[\s,]+/).map(Number);
|
|
3788
|
+
const declaredW = parseFloat(svg.attrs["width"] ?? ""), declaredH = parseFloat(svg.attrs["height"] ?? "");
|
|
3789
|
+
const viewW = box.length === 4 && box[2] ? box[2] : declaredW || 300;
|
|
3790
|
+
const viewH = box.length === 4 && box[3] ? box[3] : declaredH || 150;
|
|
3791
|
+
const viewX = box.length === 4 ? box[0] : 0, viewY = box.length === 4 ? box[1] : 0;
|
|
3792
|
+
const naturalW = Number.isFinite(declaredW) && declaredW > 0 ? declaredW : viewW;
|
|
3793
|
+
const naturalH = Number.isFinite(declaredH) && declaredH > 0 ? declaredH : viewH;
|
|
3794
|
+
let scale = 1;
|
|
3795
|
+
if (options.width !== void 0 || options.height !== void 0) {
|
|
3796
|
+
const sx = options.width !== void 0 ? options.width / viewW : Infinity;
|
|
3797
|
+
const sy = options.height !== void 0 ? options.height / viewH : Infinity;
|
|
3798
|
+
scale = Math.min(sx, sy);
|
|
3799
|
+
} else {
|
|
3800
|
+
scale = Math.min(naturalW / viewW, naturalH / viewH);
|
|
3801
|
+
}
|
|
3802
|
+
const center = options.center ?? true;
|
|
3803
|
+
const offsetX = center ? -(viewX + viewW / 2) * scale : -viewX * scale;
|
|
3804
|
+
const offsetY = center ? -(viewY + viewH / 2) * scale : -viewY * scale;
|
|
3805
|
+
const base = [scale, 0, 0, scale, offsetX, offsetY];
|
|
3806
|
+
const out = new SvgNode();
|
|
3807
|
+
out.pictureWidth = viewW * scale;
|
|
3808
|
+
out.pictureHeight = viewH * scale;
|
|
3809
|
+
const splitting = options.splitGlyphs ?? true;
|
|
3810
|
+
const keepBackground = options.background ?? false;
|
|
3811
|
+
let firstShape = true;
|
|
3812
|
+
const gradientOf = (id, m, path) => {
|
|
3813
|
+
const element = defs.get(id);
|
|
3814
|
+
if (!element) return null;
|
|
3815
|
+
if (element.name === "pattern") {
|
|
3816
|
+
const inner = element.children.find((child) => child.name === "linearGradient");
|
|
3817
|
+
if (inner) return linear2(inner, m, path, true);
|
|
3818
|
+
const fill = element.children.find((child) => child.attrs["fill"]?.startsWith("url(#"));
|
|
3819
|
+
const nested = fill?.attrs["fill"]?.match(/url\(#([^)]+)\)/)?.[1];
|
|
3820
|
+
return nested ? gradientOf(nested, m, path) : null;
|
|
3821
|
+
}
|
|
3822
|
+
if (element.name === "linearGradient") return linear2(element, m, path, element.attrs["gradientUnits"] !== "userSpaceOnUse");
|
|
3823
|
+
if (element.name === "radialGradient") {
|
|
3824
|
+
const stops = stopsOf(element);
|
|
3825
|
+
return stops.length ? stops[stops.length - 1][1] : null;
|
|
3826
|
+
}
|
|
3827
|
+
return null;
|
|
3828
|
+
};
|
|
3829
|
+
const stopsOf = (element) => {
|
|
3830
|
+
const inherited = element.attrs["href"] ?? element.attrs["xlink:href"];
|
|
3831
|
+
const source2 = element.children.some((child) => child.name === "stop") || !inherited ? element : defs.get(inherited.replace(/^#/, "")) ?? element;
|
|
3832
|
+
return source2.children.filter((child) => child.name === "stop").map((stop) => {
|
|
3833
|
+
const style2 = parseStyle(stop.attrs["style"]);
|
|
3834
|
+
const offsetRaw = stop.attrs["offset"] ?? "0";
|
|
3835
|
+
const offset = offsetRaw.endsWith("%") ? parseFloat(offsetRaw) / 100 : parseFloat(offsetRaw);
|
|
3836
|
+
const color = safeColor(style2["stop-color"] ?? stop.attrs["stop-color"] ?? "#000000") ?? [0, 0, 0, 1];
|
|
3837
|
+
const alpha = parseFloat(style2["stop-opacity"] ?? stop.attrs["stop-opacity"] ?? "1");
|
|
3838
|
+
return [offset, [color[0], color[1], color[2], color[3] * alpha]];
|
|
3839
|
+
});
|
|
3840
|
+
};
|
|
3841
|
+
const linear2 = (element, m, path, objectBox) => {
|
|
3842
|
+
const stops = stopsOf(element);
|
|
3843
|
+
if (stops.length === 0) return null;
|
|
3844
|
+
const coordinate = (value, fallback) => {
|
|
3845
|
+
if (value === void 0) return fallback;
|
|
3846
|
+
return value.endsWith("%") ? parseFloat(value) / 100 : parseFloat(value);
|
|
3847
|
+
};
|
|
3848
|
+
const x1 = coordinate(element.attrs["x1"], 0), y1 = coordinate(element.attrs["y1"], 0);
|
|
3849
|
+
const x2 = coordinate(element.attrs["x2"], 1), y2 = coordinate(element.attrs["y2"], 0);
|
|
3850
|
+
let from, to2;
|
|
3851
|
+
if (objectBox) {
|
|
3852
|
+
const b = pathBounds(path) ?? { x: 0, y: 0, width: 1, height: 1 };
|
|
3853
|
+
from = [b.x + x1 * b.width, b.y + y1 * b.height];
|
|
3854
|
+
to2 = [b.x + x2 * b.width, b.y + y2 * b.height];
|
|
3855
|
+
} else {
|
|
3856
|
+
from = apply(m, x1, y1);
|
|
3857
|
+
to2 = apply(m, x2, y2);
|
|
3858
|
+
}
|
|
3859
|
+
return { type: "linear", from, to: to2, stops };
|
|
3860
|
+
};
|
|
3861
|
+
const shapePath = (element) => {
|
|
3862
|
+
const a = element.attrs;
|
|
3863
|
+
const num = (key, fallback = 0) => {
|
|
3864
|
+
const value = parseFloat(a[key] ?? "");
|
|
3865
|
+
return Number.isFinite(value) ? value : fallback;
|
|
3866
|
+
};
|
|
3867
|
+
switch (element.name) {
|
|
3868
|
+
case "path":
|
|
3869
|
+
return a["d"] ? parseSvgPath(a["d"]) : null;
|
|
3870
|
+
case "rect": {
|
|
3871
|
+
const w = num("width"), h = num("height");
|
|
3872
|
+
if (w <= 0 || h <= 0) return null;
|
|
3873
|
+
return rectPath(num("x"), num("y"), w, h, num("rx", num("ry")));
|
|
3874
|
+
}
|
|
3875
|
+
case "circle":
|
|
3876
|
+
return num("r") > 0 ? arcPath(num("cx"), num("cy"), num("r"), num("r"), 0, Math.PI * 2) : null;
|
|
3877
|
+
case "ellipse":
|
|
3878
|
+
return num("rx") > 0 && num("ry") > 0 ? arcPath(num("cx"), num("cy"), num("rx"), num("ry"), 0, Math.PI * 2) : null;
|
|
3879
|
+
case "line":
|
|
3880
|
+
return polyPath([[num("x1"), num("y1")], [num("x2"), num("y2")]]);
|
|
3881
|
+
case "polyline":
|
|
3882
|
+
case "polygon": {
|
|
3883
|
+
const values = (a["points"] ?? "").trim().split(/[\s,]+/).filter(Boolean).map(Number);
|
|
3884
|
+
const points = [];
|
|
3885
|
+
for (let i = 0; i + 1 < values.length; i += 2) points.push([values[i], values[i + 1]]);
|
|
3886
|
+
return points.length >= 2 ? polyPath(points, element.name === "polygon") : null;
|
|
3887
|
+
}
|
|
3888
|
+
default:
|
|
3889
|
+
return null;
|
|
3890
|
+
}
|
|
3891
|
+
};
|
|
3892
|
+
const styleOf = (element, inherited) => {
|
|
3893
|
+
const a = element.attrs;
|
|
3894
|
+
const cls = (a["class"] ?? "").split(/\s+/).filter(Boolean);
|
|
3895
|
+
const merged = { ...rules.get(element.name) };
|
|
3896
|
+
for (const name of cls) Object.assign(merged, rules.get(`.${name}`));
|
|
3897
|
+
if (a["id"]) Object.assign(merged, rules.get(`#${a["id"]}`));
|
|
3898
|
+
const pick = (key) => parseStyle(a["style"])[key] ?? a[key] ?? merged[key];
|
|
3899
|
+
const style2 = { ...inherited };
|
|
3900
|
+
const color = pick("color");
|
|
3901
|
+
if (color) style2.color = color;
|
|
3902
|
+
const fill = pick("fill");
|
|
3903
|
+
if (fill) style2.fill = fill === "currentColor" ? style2.color : fill;
|
|
3904
|
+
const stroke = pick("stroke");
|
|
3905
|
+
if (stroke) style2.stroke = stroke === "currentColor" ? style2.color : stroke;
|
|
3906
|
+
const width = pick("stroke-width");
|
|
3907
|
+
if (width !== void 0) style2.strokeWidth = parseFloat(width) || 0;
|
|
3908
|
+
const fo = pick("fill-opacity");
|
|
3909
|
+
if (fo !== void 0) style2.fillOpacity = parseFloat(fo);
|
|
3910
|
+
const so = pick("stroke-opacity");
|
|
3911
|
+
if (so !== void 0) style2.strokeOpacity = parseFloat(so);
|
|
3912
|
+
const cap = pick("stroke-linecap");
|
|
3913
|
+
if (cap === "round" || cap === "butt" || cap === "square") style2.cap = cap;
|
|
3914
|
+
const join = pick("stroke-linejoin");
|
|
3915
|
+
if (join === "round" || join === "miter" || join === "bevel") style2.join = join;
|
|
3916
|
+
const dash = pick("stroke-dasharray");
|
|
3917
|
+
if (dash !== void 0) style2.dash = dash === "none" ? null : dash.trim().split(/[\s,]+/).map(Number);
|
|
3918
|
+
const dashOffset = pick("stroke-dashoffset");
|
|
3919
|
+
if (dashOffset !== void 0) style2.dashOffset = parseFloat(dashOffset) || 0;
|
|
3920
|
+
const size = pick("font-size");
|
|
3921
|
+
if (size !== void 0) style2.fontSize = parseFloat(size) || style2.fontSize;
|
|
3922
|
+
const anchor = pick("text-anchor");
|
|
3923
|
+
if (anchor) style2.anchor = anchor;
|
|
3924
|
+
return { style: style2, opacity: parseFloat(pick("opacity") ?? "1") };
|
|
3925
|
+
};
|
|
3926
|
+
const leafFrom = (path, m, element, style2) => {
|
|
3927
|
+
const world = transformPath(path, m);
|
|
3928
|
+
const scaleOf = matScale(m);
|
|
3929
|
+
const url = (value) => value?.match(/^url\(#([^)]+)\)/)?.[1];
|
|
3930
|
+
const fillId = url(style2.fill), strokeId = url(style2.stroke);
|
|
3931
|
+
const fill = fillId ? gradientOf(fillId, m, world) : safeColor(style2.fill);
|
|
3932
|
+
const stroke = strokeId ? gradientOf(strokeId, m, world) : safeColor(style2.stroke);
|
|
3933
|
+
const visibleFill = fill && style2.fillOpacity > 0 && !(Array.isArray(fill) && fill[3] === 0);
|
|
3934
|
+
const visibleStroke = stroke && style2.strokeOpacity > 0 && style2.strokeWidth > 0 && !(Array.isArray(stroke) && stroke[3] === 0);
|
|
3935
|
+
if (!visibleFill && !visibleStroke) return [];
|
|
3936
|
+
const clippedStroke = element.attrs["clip-path"] ? 0.5 : 1;
|
|
3937
|
+
const pieces = splitting && visibleFill && !visibleStroke && world.length > 1 ? splitIntoGlyphs(world) : [world];
|
|
3938
|
+
return pieces.map((piece) => {
|
|
3939
|
+
const b = pathBounds(piece) ?? { x: 0, y: 0, width: 0, height: 0 };
|
|
3940
|
+
const cx = b.x + b.width / 2, cy = b.y + b.height / 2;
|
|
3941
|
+
const leaf = new Node(transformPath(piece, [1, 0, 0, 1, -cx, -cy])).at(cx, cy);
|
|
3942
|
+
if (visibleFill) {
|
|
3943
|
+
leaf.base.fill = shiftPaint(fill, -cx, -cy);
|
|
3944
|
+
leaf.base.fillOpacity = style2.fillOpacity;
|
|
3945
|
+
}
|
|
3946
|
+
if (visibleStroke) {
|
|
3947
|
+
leaf.base.stroke = shiftPaint(stroke, -cx, -cy);
|
|
3948
|
+
leaf.base.strokeWidth = style2.strokeWidth * scaleOf * clippedStroke;
|
|
3949
|
+
leaf.base.strokeOpacity = style2.strokeOpacity;
|
|
3950
|
+
leaf.base.cap = style2.cap;
|
|
3951
|
+
leaf.base.join = style2.join;
|
|
3952
|
+
if (style2.dash) leaf.base.dash = style2.dash.map((value) => value * scaleOf);
|
|
3953
|
+
leaf.base.dashOffset = style2.dashOffset * scaleOf;
|
|
3954
|
+
}
|
|
3955
|
+
leaf.base.trimEach = true;
|
|
3956
|
+
return leaf;
|
|
3957
|
+
});
|
|
3958
|
+
};
|
|
3959
|
+
const walk2 = (element, parent, inherited, parentMatrix) => {
|
|
3960
|
+
if (NOT_DRAWN.has(element.name)) return;
|
|
3961
|
+
if (element.attrs["filter"]) return;
|
|
3962
|
+
const { style: style2, opacity } = styleOf(element, inherited);
|
|
3963
|
+
const m = mul(parentMatrix, parseTransform(element.attrs["transform"]));
|
|
3964
|
+
const register = (node) => {
|
|
3965
|
+
if (element.attrs["id"]) out.byId.set(element.attrs["id"], node);
|
|
3966
|
+
};
|
|
3967
|
+
if (element.name === "g" || element.name === "svg" || element.name === "a") {
|
|
3968
|
+
const group3 = new Node();
|
|
3969
|
+
if (opacity < 1) group3.base.opacity = opacity;
|
|
3970
|
+
register(group3);
|
|
3971
|
+
parent.add(group3);
|
|
3972
|
+
for (const child of element.children) walk2(child, group3, style2, m);
|
|
3973
|
+
return;
|
|
3974
|
+
}
|
|
3975
|
+
if (element.name === "use") {
|
|
3976
|
+
const href = (element.attrs["href"] ?? element.attrs["xlink:href"] ?? "").replace(/^#/, "");
|
|
3977
|
+
const referenced = defs.get(href);
|
|
3978
|
+
if (!referenced) return;
|
|
3979
|
+
const placed = mul(m, [1, 0, 0, 1, parseFloat(element.attrs["x"] ?? "0") || 0, parseFloat(element.attrs["y"] ?? "0") || 0]);
|
|
3980
|
+
const group3 = new Node();
|
|
3981
|
+
if (opacity < 1) group3.base.opacity = opacity;
|
|
3982
|
+
parent.add(group3);
|
|
3983
|
+
if (referenced.name === "symbol") for (const child of referenced.children) walk2(child, group3, style2, placed);
|
|
3984
|
+
else walk2(referenced, group3, style2, placed);
|
|
3985
|
+
return;
|
|
3986
|
+
}
|
|
3987
|
+
if (element.name === "image") {
|
|
3988
|
+
const href = element.attrs["href"] ?? element.attrs["xlink:href"];
|
|
3989
|
+
if (!href) return;
|
|
3990
|
+
const width = parseFloat(element.attrs["width"] ?? "0"), height = parseFloat(element.attrs["height"] ?? "0");
|
|
3991
|
+
const [x, y] = apply(m, parseFloat(element.attrs["x"] ?? "0") || 0, parseFloat(element.attrs["y"] ?? "0") || 0);
|
|
3992
|
+
const unit = matScale(m);
|
|
3993
|
+
const image = { src: href, width, height };
|
|
3994
|
+
const node = new Node().at(x, y);
|
|
3995
|
+
node.base.image = image;
|
|
3996
|
+
node.base.sx = unit;
|
|
3997
|
+
node.base.sy = unit;
|
|
3998
|
+
if (opacity < 1) node.base.opacity = opacity;
|
|
3999
|
+
register(node);
|
|
4000
|
+
parent.add(node);
|
|
4001
|
+
return;
|
|
4002
|
+
}
|
|
4003
|
+
if (element.name === "text") {
|
|
4004
|
+
const body = (element.text + element.children.map((child) => child.text).join("")).trim();
|
|
4005
|
+
if (!body) return;
|
|
4006
|
+
const color = safeColor(style2.fill) ?? [0, 0, 0, 1];
|
|
4007
|
+
const size = style2.fontSize * matScale(m);
|
|
4008
|
+
const label = text(body, { size, color });
|
|
4009
|
+
const [x, y] = apply(m, parseFloat(element.attrs["x"] ?? "0") || 0, parseFloat(element.attrs["y"] ?? "0") || 0);
|
|
4010
|
+
const half = label.get("px");
|
|
4011
|
+
label.at(style2.anchor === "middle" ? x : style2.anchor === "end" ? x - half : x + half, y - size * 0.34);
|
|
4012
|
+
if (opacity < 1) label.base.opacity = opacity;
|
|
4013
|
+
register(label);
|
|
4014
|
+
parent.add(label);
|
|
4015
|
+
return;
|
|
4016
|
+
}
|
|
4017
|
+
const path = shapePath(element);
|
|
4018
|
+
if (!path || path.length === 0) return;
|
|
4019
|
+
const isBackground = firstShape && element.name === "rect" && !keepBackground && parseFloat(element.attrs["width"] ?? "0") >= viewW - 0.5 && parseFloat(element.attrs["height"] ?? "0") >= viewH - 0.5;
|
|
4020
|
+
firstShape = false;
|
|
4021
|
+
if (isBackground) return;
|
|
4022
|
+
const leaves = leafFrom(path, m, element, style2);
|
|
4023
|
+
if (leaves.length === 0) return;
|
|
4024
|
+
if (leaves.length === 1) {
|
|
4025
|
+
if (opacity < 1) leaves[0].base.opacity = opacity;
|
|
4026
|
+
register(leaves[0]);
|
|
4027
|
+
parent.add(leaves[0]);
|
|
4028
|
+
return;
|
|
4029
|
+
}
|
|
4030
|
+
const group2 = new Node();
|
|
4031
|
+
if (opacity < 1) group2.base.opacity = opacity;
|
|
4032
|
+
group2.add(...leaves);
|
|
4033
|
+
register(group2);
|
|
4034
|
+
parent.add(group2);
|
|
4035
|
+
};
|
|
4036
|
+
const initial = {
|
|
4037
|
+
fill: "#000000",
|
|
4038
|
+
stroke: null,
|
|
4039
|
+
strokeWidth: 1,
|
|
4040
|
+
fillOpacity: 1,
|
|
4041
|
+
strokeOpacity: 1,
|
|
4042
|
+
cap: "butt",
|
|
4043
|
+
join: "miter",
|
|
4044
|
+
dash: null,
|
|
4045
|
+
dashOffset: 0,
|
|
4046
|
+
color: "#000000",
|
|
4047
|
+
fontSize: 16,
|
|
4048
|
+
anchor: "start"
|
|
4049
|
+
};
|
|
4050
|
+
const { style } = styleOf(svg, initial);
|
|
4051
|
+
for (const child of svg.children) walk2(child, out, style, base);
|
|
4052
|
+
return out;
|
|
4053
|
+
}
|
|
4054
|
+
function shiftPaint(paint, dx, dy) {
|
|
4055
|
+
if (Array.isArray(paint)) return paint;
|
|
4056
|
+
const gradient = paint;
|
|
4057
|
+
if (gradient.type === "linear") {
|
|
4058
|
+
return { ...gradient, from: [gradient.from[0] + dx, gradient.from[1] + dy], to: [gradient.to[0] + dx, gradient.to[1] + dy] };
|
|
4059
|
+
}
|
|
4060
|
+
if (gradient.type === "radial") return { ...gradient, center: [gradient.center[0] + dx, gradient.center[1] + dy] };
|
|
4061
|
+
return gradient;
|
|
4062
|
+
}
|
|
4063
|
+
|
|
4064
|
+
export {
|
|
4065
|
+
midnight,
|
|
4066
|
+
paper,
|
|
4067
|
+
blueprint,
|
|
4068
|
+
chalk,
|
|
4069
|
+
neon,
|
|
4070
|
+
LOOKS,
|
|
4071
|
+
resolveLook,
|
|
4072
|
+
parseColor,
|
|
4073
|
+
toPaint,
|
|
4074
|
+
isGradient,
|
|
4075
|
+
css,
|
|
4076
|
+
mixColor,
|
|
4077
|
+
sampleStops,
|
|
4078
|
+
linearGradient,
|
|
4079
|
+
radialGradient,
|
|
4080
|
+
along,
|
|
4081
|
+
ramp,
|
|
4082
|
+
IDENTITY,
|
|
4083
|
+
EMPTY_BOUNDS,
|
|
4084
|
+
mul,
|
|
4085
|
+
invert,
|
|
4086
|
+
apply,
|
|
4087
|
+
compose,
|
|
4088
|
+
matScale,
|
|
4089
|
+
ring,
|
|
4090
|
+
segmentCount,
|
|
4091
|
+
linePath,
|
|
4092
|
+
polyPath,
|
|
4093
|
+
smoothPath,
|
|
4094
|
+
arcPath,
|
|
4095
|
+
circlePath,
|
|
4096
|
+
rectPath,
|
|
4097
|
+
parametricPath,
|
|
4098
|
+
curveThrough,
|
|
4099
|
+
concat,
|
|
4100
|
+
transformPath,
|
|
4101
|
+
warpPath,
|
|
4102
|
+
subdivideEach,
|
|
4103
|
+
ringLength,
|
|
4104
|
+
pathLength,
|
|
4105
|
+
trimRing,
|
|
4106
|
+
trimPath,
|
|
4107
|
+
pointAt,
|
|
4108
|
+
tangentAt,
|
|
4109
|
+
pathBounds,
|
|
4110
|
+
unionBounds,
|
|
4111
|
+
signedArea,
|
|
4112
|
+
ringCenter,
|
|
4113
|
+
reverseRing,
|
|
4114
|
+
parseSvgPath,
|
|
4115
|
+
pathToD,
|
|
4116
|
+
clock,
|
|
4117
|
+
buildOps,
|
|
4118
|
+
frameToSvg,
|
|
4119
|
+
paintFrame,
|
|
4120
|
+
frameInkBounds,
|
|
4121
|
+
linear,
|
|
4122
|
+
smoothBy,
|
|
4123
|
+
smooth,
|
|
4124
|
+
smoother,
|
|
4125
|
+
easeInQuad,
|
|
4126
|
+
easeOutQuad,
|
|
4127
|
+
easeInCubic,
|
|
4128
|
+
easeOutCubic,
|
|
4129
|
+
easeInOutCubic,
|
|
4130
|
+
easeOutQuint,
|
|
4131
|
+
easeInOutQuint,
|
|
4132
|
+
easeOutExpo,
|
|
4133
|
+
easeInExpo,
|
|
4134
|
+
rushInto,
|
|
4135
|
+
rushFrom,
|
|
4136
|
+
slowInto,
|
|
4137
|
+
overshoot,
|
|
4138
|
+
spring,
|
|
4139
|
+
bounce,
|
|
4140
|
+
thereAndBack,
|
|
4141
|
+
thereAndBackWithPause,
|
|
4142
|
+
wiggle,
|
|
4143
|
+
notQuiteThere,
|
|
4144
|
+
lingering,
|
|
4145
|
+
runningStart,
|
|
4146
|
+
during,
|
|
4147
|
+
cubicBezier,
|
|
4148
|
+
matchSequence,
|
|
4149
|
+
Node,
|
|
4150
|
+
direction,
|
|
4151
|
+
group,
|
|
4152
|
+
dynamic,
|
|
4153
|
+
par,
|
|
4154
|
+
seq,
|
|
4155
|
+
lagged,
|
|
4156
|
+
delay,
|
|
4157
|
+
retime,
|
|
4158
|
+
call,
|
|
4159
|
+
draw,
|
|
4160
|
+
write,
|
|
4161
|
+
erase,
|
|
4162
|
+
fadeIn,
|
|
4163
|
+
fadeOut,
|
|
4164
|
+
grow,
|
|
4165
|
+
appear,
|
|
4166
|
+
vanish,
|
|
4167
|
+
to,
|
|
4168
|
+
moveTo,
|
|
4169
|
+
shift,
|
|
4170
|
+
scaleBy,
|
|
4171
|
+
rotate,
|
|
4172
|
+
recolor,
|
|
4173
|
+
fade,
|
|
4174
|
+
along2,
|
|
4175
|
+
warp,
|
|
4176
|
+
applyMatrix,
|
|
4177
|
+
morph,
|
|
4178
|
+
morphMatching,
|
|
4179
|
+
indicate,
|
|
4180
|
+
shake,
|
|
4181
|
+
circumscribe,
|
|
4182
|
+
flash,
|
|
4183
|
+
ripple,
|
|
4184
|
+
pulse,
|
|
4185
|
+
Value,
|
|
4186
|
+
Stage,
|
|
4187
|
+
film,
|
|
4188
|
+
isFilm,
|
|
4189
|
+
Suspend,
|
|
4190
|
+
isSuspend,
|
|
4191
|
+
CompiledFilm,
|
|
4192
|
+
compileFilm,
|
|
4193
|
+
setAssetBase,
|
|
4194
|
+
parseFont,
|
|
4195
|
+
fontFor,
|
|
4196
|
+
glyphPath,
|
|
4197
|
+
TextNode,
|
|
4198
|
+
text,
|
|
4199
|
+
SvgNode,
|
|
4200
|
+
fromSvg
|
|
4201
|
+
};
|