@glissade/core 0.11.0 → 0.12.0-pre.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.
@@ -0,0 +1,992 @@
1
+ //#region src/easing.ts
2
+ const c1 = 1.70158;
3
+ const c2 = c1 * 1.525;
4
+ const c3 = 2.70158;
5
+ const c4 = 2 * Math.PI / 3;
6
+ const c5 = 2 * Math.PI / 4.5;
7
+ function bounceOut(t) {
8
+ const n1 = 7.5625;
9
+ const d1 = 2.75;
10
+ if (t < 1 / d1) return n1 * t * t;
11
+ if (t < 2 / d1) return n1 * (t -= 1.5 / d1) * t + .75;
12
+ if (t < 2.5 / d1) return n1 * (t -= 2.25 / d1) * t + .9375;
13
+ return n1 * (t -= 2.625 / d1) * t + .984375;
14
+ }
15
+ const easings = {
16
+ linear: (t) => t,
17
+ easeInQuad: (t) => t * t,
18
+ easeOutQuad: (t) => 1 - (1 - t) * (1 - t),
19
+ easeInOutQuad: (t) => t < .5 ? 2 * t * t : 1 - (-2 * t + 2) ** 2 / 2,
20
+ easeInCubic: (t) => t ** 3,
21
+ easeOutCubic: (t) => 1 - (1 - t) ** 3,
22
+ easeInOutCubic: (t) => t < .5 ? 4 * t ** 3 : 1 - (-2 * t + 2) ** 3 / 2,
23
+ easeInQuart: (t) => t ** 4,
24
+ easeOutQuart: (t) => 1 - (1 - t) ** 4,
25
+ easeInOutQuart: (t) => t < .5 ? 8 * t ** 4 : 1 - (-2 * t + 2) ** 4 / 2,
26
+ easeInQuint: (t) => t ** 5,
27
+ easeOutQuint: (t) => 1 - (1 - t) ** 5,
28
+ easeInOutQuint: (t) => t < .5 ? 16 * t ** 5 : 1 - (-2 * t + 2) ** 5 / 2,
29
+ easeInSine: (t) => 1 - Math.cos(t * Math.PI / 2),
30
+ easeOutSine: (t) => Math.sin(t * Math.PI / 2),
31
+ easeInOutSine: (t) => -(Math.cos(Math.PI * t) - 1) / 2,
32
+ easeInExpo: (t) => t === 0 ? 0 : 2 ** (10 * t - 10),
33
+ easeOutExpo: (t) => t === 1 ? 1 : 1 - 2 ** (-10 * t),
34
+ easeInOutExpo: (t) => t === 0 ? 0 : t === 1 ? 1 : t < .5 ? 2 ** (20 * t - 10) / 2 : (2 - 2 ** (-20 * t + 10)) / 2,
35
+ easeInCirc: (t) => 1 - Math.sqrt(1 - t * t),
36
+ easeOutCirc: (t) => Math.sqrt(1 - (t - 1) * (t - 1)),
37
+ easeInOutCirc: (t) => t < .5 ? (1 - Math.sqrt(1 - (2 * t) ** 2)) / 2 : (Math.sqrt(1 - (-2 * t + 2) ** 2) + 1) / 2,
38
+ easeInBack: (t) => c3 * t ** 3 - c1 * t * t,
39
+ easeOutBack: (t) => 1 + c3 * (t - 1) ** 3 + c1 * (t - 1) ** 2,
40
+ easeInOutBack: (t) => t < .5 ? (2 * t) ** 2 * (3.5949095 * 2 * t - c2) / 2 : ((2 * t - 2) ** 2 * (3.5949095 * (t * 2 - 2) + c2) + 2) / 2,
41
+ easeInElastic: (t) => t === 0 ? 0 : t === 1 ? 1 : -(2 ** (10 * t - 10)) * Math.sin((t * 10 - 10.75) * c4),
42
+ easeOutElastic: (t) => t === 0 ? 0 : t === 1 ? 1 : 2 ** (-10 * t) * Math.sin((t * 10 - .75) * c4) + 1,
43
+ easeInOutElastic: (t) => t === 0 ? 0 : t === 1 ? 1 : t < .5 ? -(2 ** (20 * t - 10) * Math.sin((20 * t - 11.125) * c5)) / 2 : 2 ** (-20 * t + 10) * Math.sin((20 * t - 11.125) * c5) / 2 + 1,
44
+ easeInBounce: (t) => 1 - bounceOut(1 - t),
45
+ easeOutBounce: bounceOut,
46
+ easeInOutBounce: (t) => t < .5 ? (1 - bounceOut(1 - 2 * t)) / 2 : (1 + bounceOut(2 * t - 1)) / 2
47
+ };
48
+ function bounceOutD(t) {
49
+ const n1 = 7.5625;
50
+ const d1 = 2.75;
51
+ if (t < 1 / d1) return 2 * n1 * t;
52
+ if (t < 2 / d1) return 2 * n1 * (t - 1.5 / d1);
53
+ if (t < 2.5 / d1) return 2 * n1 * (t - 2.25 / d1);
54
+ return 2 * n1 * (t - 2.625 / d1);
55
+ }
56
+ const LN2 = Math.LN2;
57
+ /**
58
+ * Analytic derivatives d(u) of every named ease (§B.6) — closed-form, used
59
+ * for reading velocity off in-flight curves at interruption time. Property-
60
+ * tested against central differences at interior points.
61
+ */
62
+ const easingDerivatives = {
63
+ linear: () => 1,
64
+ easeInQuad: (t) => 2 * t,
65
+ easeOutQuad: (t) => 2 * (1 - t),
66
+ easeInOutQuad: (t) => t < .5 ? 4 * t : 4 * (1 - t),
67
+ easeInCubic: (t) => 3 * t ** 2,
68
+ easeOutCubic: (t) => 3 * (1 - t) ** 2,
69
+ easeInOutCubic: (t) => t < .5 ? 12 * t ** 2 : 12 * (1 - t) ** 2,
70
+ easeInQuart: (t) => 4 * t ** 3,
71
+ easeOutQuart: (t) => 4 * (1 - t) ** 3,
72
+ easeInOutQuart: (t) => t < .5 ? 32 * t ** 3 : 32 * (1 - t) ** 3,
73
+ easeInQuint: (t) => 5 * t ** 4,
74
+ easeOutQuint: (t) => 5 * (1 - t) ** 4,
75
+ easeInOutQuint: (t) => t < .5 ? 80 * t ** 4 : 80 * (1 - t) ** 4,
76
+ easeInSine: (t) => Math.PI / 2 * Math.sin(t * Math.PI / 2),
77
+ easeOutSine: (t) => Math.PI / 2 * Math.cos(t * Math.PI / 2),
78
+ easeInOutSine: (t) => Math.PI / 2 * Math.sin(Math.PI * t),
79
+ easeInExpo: (t) => t === 0 ? 0 : 10 * LN2 * 2 ** (10 * t - 10),
80
+ easeOutExpo: (t) => t === 1 ? 0 : 10 * LN2 * 2 ** (-10 * t),
81
+ easeInOutExpo: (t) => t === 0 || t === 1 ? 0 : t < .5 ? 10 * LN2 * 2 ** (20 * t - 10) : 10 * LN2 * 2 ** (-20 * t + 10),
82
+ easeInCirc: (t) => t / Math.sqrt(1 - t * t),
83
+ easeOutCirc: (t) => (1 - t) / Math.sqrt(1 - (t - 1) * (t - 1)),
84
+ easeInOutCirc: (t) => t < .5 ? 2 * t / Math.sqrt(1 - 4 * t * t) : (2 - 2 * t) / Math.sqrt(1 - (-2 * t + 2) ** 2),
85
+ easeInBack: (t) => 3 * c3 * t ** 2 - 2 * c1 * t,
86
+ easeOutBack: (t) => 3 * c3 * (t - 1) ** 2 + 2 * c1 * (t - 1),
87
+ easeInOutBack: (t) => t < .5 ? 12 * 3.5949095 * t ** 2 - 4 * c2 * t : 3 * 3.5949095 * (2 * t - 2) ** 2 + 2 * c2 * (2 * t - 2),
88
+ easeInElastic: (t) => {
89
+ if (t === 0 || t === 1) return 0;
90
+ const theta = (t * 10 - 10.75) * c4;
91
+ const amp = 2 ** (10 * t - 10);
92
+ return -(10 * LN2 * amp * Math.sin(theta) + amp * 10 * c4 * Math.cos(theta));
93
+ },
94
+ easeOutElastic: (t) => {
95
+ if (t === 0 || t === 1) return 0;
96
+ const phi = (t * 10 - .75) * c4;
97
+ const amp = 2 ** (-10 * t);
98
+ return -10 * LN2 * amp * Math.sin(phi) + amp * 10 * c4 * Math.cos(phi);
99
+ },
100
+ easeInOutElastic: (t) => {
101
+ if (t === 0 || t === 1) return 0;
102
+ const psi = (20 * t - 11.125) * c5;
103
+ if (t < .5) {
104
+ const amp = 2 ** (20 * t - 10);
105
+ return -(20 * LN2 * amp * Math.sin(psi) + amp * 20 * c5 * Math.cos(psi)) / 2;
106
+ }
107
+ const amp = 2 ** (-20 * t + 10);
108
+ return (-20 * LN2 * amp * Math.sin(psi) + amp * 20 * c5 * Math.cos(psi)) / 2;
109
+ },
110
+ easeInBounce: (t) => bounceOutD(1 - t),
111
+ easeOutBounce: bounceOutD,
112
+ easeInOutBounce: (t) => t < .5 ? bounceOutD(1 - 2 * t) : bounceOutD(2 * t - 1)
113
+ };
114
+ /** Default property-tween ease (Motion Canvas precedent). */
115
+ const DEFAULT_EASE = "easeInOutCubic";
116
+ function bezierKernel(p1x, p1y, p2x, p2y) {
117
+ const ax = 3 * p1x - 3 * p2x + 1;
118
+ const bx = 3 * p2x - 6 * p1x;
119
+ const cx = 3 * p1x;
120
+ const ay = 3 * p1y - 3 * p2y + 1;
121
+ const by = 3 * p2y - 6 * p1y;
122
+ const cy = 3 * p1y;
123
+ const sampleX = (u) => ((ax * u + bx) * u + cx) * u;
124
+ const sampleY = (u) => ((ay * u + by) * u + cy) * u;
125
+ const sampleDX = (u) => (3 * ax * u + 2 * bx) * u + cx;
126
+ const sampleDY = (u) => (3 * ay * u + 2 * by) * u + cy;
127
+ const solveU = (x) => {
128
+ let u = x;
129
+ for (let i = 0; i < 8; i++) {
130
+ const err = sampleX(u) - x;
131
+ if (Math.abs(err) < 1e-7) return u;
132
+ const d = sampleDX(u);
133
+ if (Math.abs(d) < 1e-6) break;
134
+ u -= err / d;
135
+ }
136
+ let lo = 0;
137
+ let hi = 1;
138
+ u = x;
139
+ while (hi - lo > 1e-7) {
140
+ if (sampleX(u) < x) lo = u;
141
+ else hi = u;
142
+ u = (lo + hi) / 2;
143
+ }
144
+ return u;
145
+ };
146
+ return {
147
+ sampleY,
148
+ sampleDX,
149
+ sampleDY,
150
+ solveU
151
+ };
152
+ }
153
+ /**
154
+ * CSS-style cubic bézier where x is time and y is progress. Newton's method
155
+ * with a bisection fallback for the flat-derivative regions.
156
+ */
157
+ function cubicBezier(p1x, p1y, p2x, p2y) {
158
+ const k = bezierKernel(p1x, p1y, p2x, p2y);
159
+ return (t) => {
160
+ if (t <= 0) return 0;
161
+ if (t >= 1) return 1;
162
+ return k.sampleY(k.solveU(t));
163
+ };
164
+ }
165
+ /** Analytic dy/dx of a cubic bézier ease: y'(s)/x'(s) at the solved parameter (§B.6). */
166
+ function cubicBezierDerivative(p1x, p1y, p2x, p2y) {
167
+ const k = bezierKernel(p1x, p1y, p2x, p2y);
168
+ return (t) => {
169
+ const u = k.solveU(Math.min(1, Math.max(0, t)));
170
+ const dx = k.sampleDX(u);
171
+ if (Math.abs(dx) < 1e-9) return 0;
172
+ return k.sampleDY(u) / dx;
173
+ };
174
+ }
175
+ var UnknownEasingError = class extends Error {
176
+ constructor(name) {
177
+ super(`unknown easing '${name}'; register it via easings or use cubicBezier/spring`);
178
+ this.name = "UnknownEasingError";
179
+ }
180
+ };
181
+ function namedEasing(name) {
182
+ const fn = easings[name];
183
+ if (!fn) throw new UnknownEasingError(name);
184
+ return fn;
185
+ }
186
+ //#endregion
187
+ //#region src/spring.ts
188
+ const DEFAULT_SETTLE_TOLERANCE = .005;
189
+ function params(cfg) {
190
+ const mass = cfg.mass ?? 1;
191
+ if (!(cfg.stiffness > 0) || !(cfg.damping > 0) || !(mass > 0)) throw new RangeError("spring stiffness, damping, and mass must all be > 0");
192
+ return {
193
+ w0: Math.sqrt(cfg.stiffness / mass),
194
+ zeta: cfg.damping / (2 * Math.sqrt(cfg.stiffness * mass))
195
+ };
196
+ }
197
+ /** Raw closed-form spring velocity at time t — d/dt of rawValue's three branches. */
198
+ function rawDerivative(cfg, t) {
199
+ if (t <= 0) return 0;
200
+ const { w0, zeta } = params(cfg);
201
+ if (Math.abs(zeta - 1) < 1e-9) return w0 * w0 * t * Math.exp(-w0 * t);
202
+ if (zeta < 1) {
203
+ const wd = w0 * Math.sqrt(1 - zeta * zeta);
204
+ return Math.exp(-zeta * w0 * t) * (w0 * w0 / wd) * Math.sin(wd * t);
205
+ }
206
+ const s = Math.sqrt(zeta * zeta - 1);
207
+ const r1 = -w0 * (zeta - s);
208
+ const r2 = -w0 * (zeta + s);
209
+ return r1 * r2 * (Math.exp(r1 * t) - Math.exp(r2 * t)) / (r1 - r2);
210
+ }
211
+ /** Raw closed-form spring position at time t (seconds). Approaches 1, may overshoot. */
212
+ function rawValue(cfg, t) {
213
+ if (t <= 0) return 0;
214
+ const { w0, zeta } = params(cfg);
215
+ if (Math.abs(zeta - 1) < 1e-9) return 1 - Math.exp(-w0 * t) * (1 + w0 * t);
216
+ if (zeta < 1) {
217
+ const wd = w0 * Math.sqrt(1 - zeta * zeta);
218
+ return 1 - Math.exp(-zeta * w0 * t) * (Math.cos(wd * t) + zeta * w0 / wd * Math.sin(wd * t));
219
+ }
220
+ const s = Math.sqrt(zeta * zeta - 1);
221
+ const r1 = -w0 * (zeta - s);
222
+ const r2 = -w0 * (zeta + s);
223
+ return 1 + (r2 * Math.exp(r1 * t) - r1 * Math.exp(r2 * t)) / (r1 - r2);
224
+ }
225
+ /**
226
+ * Settle duration: the earliest time after which |x - 1| stays within
227
+ * settleTolerance. Closed-form via the decay envelope for the underdamped
228
+ * case; bisection on the monotone tail otherwise. Deterministic.
229
+ */
230
+ function duration(cfg, opts) {
231
+ const tol = opts?.settleTolerance ?? DEFAULT_SETTLE_TOLERANCE;
232
+ const { w0, zeta } = params(cfg);
233
+ if (zeta < 1) {
234
+ const wd = w0 * Math.sqrt(1 - zeta * zeta);
235
+ const amp = Math.sqrt(1 + (zeta * w0 / wd) ** 2);
236
+ return Math.log(amp / tol) / (zeta * w0);
237
+ }
238
+ let hi = 1 / w0;
239
+ while (1 - rawValue(cfg, hi) > tol) hi *= 2;
240
+ let lo = 0;
241
+ for (let i = 0; i < 64 && hi - lo > 1e-9; i++) {
242
+ const mid = (lo + hi) / 2;
243
+ if (1 - rawValue(cfg, mid) > tol) lo = mid;
244
+ else hi = mid;
245
+ }
246
+ return hi;
247
+ }
248
+ /**
249
+ * Spring progress at local time t, affinely rescaled so value(duration) = 1
250
+ * exactly — the raw form only approaches 1, and an unscaled curve would snap
251
+ * at the key (§2.7 "endpoint continuity"). May exceed 1 (overshoot).
252
+ */
253
+ function value(cfg, t, opts) {
254
+ const d = duration(cfg, opts);
255
+ return rawValue(cfg, Math.min(t, d)) / rawValue(cfg, d);
256
+ }
257
+ function retarget(cfg, x0, v0) {
258
+ const { w0, zeta } = params(cfg);
259
+ let value0;
260
+ let velocity0;
261
+ let envelope;
262
+ if (Math.abs(zeta - 1) < 1e-9) {
263
+ const b = v0 + w0 * x0;
264
+ value0 = (tau) => tau <= 0 ? x0 : Math.exp(-w0 * tau) * (x0 + b * tau);
265
+ velocity0 = (tau) => tau <= 0 ? v0 : Math.exp(-w0 * tau) * (v0 - w0 * b * tau);
266
+ envelope = (tau) => Math.exp(-w0 * tau) * (Math.abs(x0) + Math.abs(b) * tau);
267
+ } else if (zeta < 1) {
268
+ const wd = w0 * Math.sqrt(1 - zeta * zeta);
269
+ const c2v = (v0 + zeta * w0 * x0) / wd;
270
+ const amp = Math.hypot(x0, c2v);
271
+ value0 = (tau) => tau <= 0 ? x0 : Math.exp(-zeta * w0 * tau) * (x0 * Math.cos(wd * tau) + c2v * Math.sin(wd * tau));
272
+ velocity0 = (tau) => tau <= 0 ? v0 : Math.exp(-zeta * w0 * tau) * (v0 * Math.cos(wd * tau) - (w0 * w0 * x0 + zeta * w0 * v0) / wd * Math.sin(wd * tau));
273
+ envelope = (tau) => Math.exp(-zeta * w0 * tau) * amp;
274
+ } else {
275
+ const s = Math.sqrt(zeta * zeta - 1);
276
+ const rp = w0 * (-zeta + s);
277
+ const rm = w0 * (-zeta - s);
278
+ const cp = (v0 - rm * x0) / (rp - rm);
279
+ const cm = (rp * x0 - v0) / (rp - rm);
280
+ value0 = (tau) => tau <= 0 ? x0 : cp * Math.exp(rp * tau) + cm * Math.exp(rm * tau);
281
+ velocity0 = (tau) => tau <= 0 ? v0 : cp * rp * Math.exp(rp * tau) + cm * rm * Math.exp(rm * tau);
282
+ envelope = (tau) => Math.abs(cp) * Math.exp(rp * tau) + Math.abs(cm) * Math.exp(rm * tau);
283
+ }
284
+ const settleTime = (tol) => {
285
+ const eps = tol ?? Math.abs(x0) * .005 + 1e-6;
286
+ if (envelope(0) <= eps && Math.abs(v0) < 1e-12) return 0;
287
+ let hi = 1 / w0;
288
+ let guard = 0;
289
+ while (envelope(hi) > eps && guard++ < 64) hi *= 2;
290
+ let lo = 0;
291
+ for (let i = 0; i < 64 && hi - lo > 1e-9; i++) {
292
+ const mid = (lo + hi) / 2;
293
+ if (Math.max(envelope(mid), envelope(mid * 1.05 + 1e-6)) > eps) lo = mid;
294
+ else hi = mid;
295
+ }
296
+ return hi;
297
+ };
298
+ return {
299
+ value: value0,
300
+ velocity: velocity0,
301
+ settleTime
302
+ };
303
+ }
304
+ const spring = Object.assign((cfg) => {
305
+ params(cfg);
306
+ return {
307
+ kind: "spring",
308
+ stiffness: cfg.stiffness,
309
+ damping: cfg.damping,
310
+ mass: cfg.mass ?? 1
311
+ };
312
+ }, {
313
+ duration,
314
+ value,
315
+ retarget
316
+ });
317
+ /**
318
+ * Named spring feels (react-spring conventions, mass 1) — a vocabulary instead
319
+ * of hand-tuned stiffness/damping. Use `spring(springPresets.wobbly)` or
320
+ * `springTo(t, a, b, springPresets.gentle)`.
321
+ */
322
+ const springPresets = {
323
+ default: {
324
+ stiffness: 170,
325
+ damping: 26
326
+ },
327
+ gentle: {
328
+ stiffness: 120,
329
+ damping: 14
330
+ },
331
+ wobbly: {
332
+ stiffness: 180,
333
+ damping: 12
334
+ },
335
+ stiff: {
336
+ stiffness: 210,
337
+ damping: 20
338
+ },
339
+ slow: {
340
+ stiffness: 280,
341
+ damping: 60
342
+ },
343
+ molasses: {
344
+ stiffness: 280,
345
+ damping: 120
346
+ }
347
+ };
348
+ /**
349
+ * The spring as a normalized easing over a segment whose length must equal
350
+ * spring.duration(cfg) (validated at the document layer, §2.7).
351
+ */
352
+ function springEasing(cfg) {
353
+ const d = duration(cfg);
354
+ return (p) => value(cfg, p * d);
355
+ }
356
+ /**
357
+ * Analytic d/dp of springEasing (§B.6): oscillator derivative × the affine
358
+ * rescale factor × duration (chain rule p → t = p·D). Flat past p=1,
359
+ * matching value()'s clamp (right-derivative convention).
360
+ */
361
+ function springEasingDerivative(cfg) {
362
+ const d = duration(cfg);
363
+ const scale = d / rawValue(cfg, d);
364
+ return (p) => p >= 1 ? 0 : rawDerivative(cfg, p * d) * scale;
365
+ }
366
+ //#endregion
367
+ //#region src/devWarning.ts
368
+ let devWarn = (msg) => {
369
+ globalThis.console?.warn(`[glissade] ${msg}`);
370
+ };
371
+ function setDevWarning(fn) {
372
+ devWarn = fn;
373
+ }
374
+ /** Internal: emit through the configurable channel. */
375
+ function emitDevWarning(message) {
376
+ devWarn(message);
377
+ }
378
+ //#endregion
379
+ //#region src/color.ts
380
+ const HEX_RE = /^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
381
+ const RGB_RE = /^rgba?\(\s*([\d.]+)\s*[, ]\s*([\d.]+)\s*[, ]\s*([\d.]+)\s*(?:[,/]\s*([\d.]+%?)\s*)?\)$/i;
382
+ var ColorParseError = class extends Error {
383
+ constructor(input) {
384
+ super(`cannot parse color '${input}' (supported: #rgb[a], #rrggbb[aa], rgb()/rgba())`);
385
+ this.name = "ColorParseError";
386
+ }
387
+ };
388
+ function parseColor(input) {
389
+ const hex = HEX_RE.exec(input);
390
+ if (hex) {
391
+ let h = hex[1];
392
+ if (h.length <= 4) h = [...h].map((c) => c + c).join("");
393
+ const n = parseInt(h, 16);
394
+ if (h.length === 6) return {
395
+ r: n >> 16,
396
+ g: n >> 8 & 255,
397
+ b: n & 255,
398
+ a: 1
399
+ };
400
+ return {
401
+ r: n >>> 24 & 255,
402
+ g: n >>> 16 & 255,
403
+ b: n >>> 8 & 255,
404
+ a: (n & 255) / 255
405
+ };
406
+ }
407
+ const rgb = RGB_RE.exec(input);
408
+ if (rgb) {
409
+ const alphaRaw = rgb[4];
410
+ const a = alphaRaw === void 0 ? 1 : alphaRaw.endsWith("%") ? parseFloat(alphaRaw) / 100 : parseFloat(alphaRaw);
411
+ return {
412
+ r: parseFloat(rgb[1]),
413
+ g: parseFloat(rgb[2]),
414
+ b: parseFloat(rgb[3]),
415
+ a
416
+ };
417
+ }
418
+ throw new ColorParseError(input);
419
+ }
420
+ function formatColor(c) {
421
+ const clampByte = (v) => Math.min(255, Math.max(0, Math.round(v)));
422
+ const hex = (v) => clampByte(v).toString(16).padStart(2, "0");
423
+ const base = `#${hex(c.r)}${hex(c.g)}${hex(c.b)}`;
424
+ const a = Math.min(1, Math.max(0, c.a));
425
+ return a >= 1 ? base : `${base}${hex(a * 255)}`;
426
+ }
427
+ function srgbToLinear(c) {
428
+ const v = c / 255;
429
+ return v <= .04045 ? v / 12.92 : ((v + .055) / 1.055) ** 2.4;
430
+ }
431
+ function linearToSrgb(v) {
432
+ return (v <= .0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - .055) * 255;
433
+ }
434
+ function rgbaToOklab(c) {
435
+ const r = srgbToLinear(c.r);
436
+ const g = srgbToLinear(c.g);
437
+ const b = srgbToLinear(c.b);
438
+ const l = Math.cbrt(.4122214708 * r + .5363325363 * g + .0514459929 * b);
439
+ const m = Math.cbrt(.2119034982 * r + .6806995451 * g + .1073969566 * b);
440
+ const s = Math.cbrt(.0883024619 * r + .2817188376 * g + .6299787005 * b);
441
+ return {
442
+ L: .2104542553 * l + .793617785 * m - .0040720468 * s,
443
+ a: 1.9779984951 * l - 2.428592205 * m + .4505937099 * s,
444
+ b: .0259040371 * l + .7827717662 * m - .808675766 * s,
445
+ alpha: c.a
446
+ };
447
+ }
448
+ function oklabToRgba(c) {
449
+ const l = (c.L + .3963377774 * c.a + .2158037573 * c.b) ** 3;
450
+ const m = (c.L - .1055613458 * c.a - .0638541728 * c.b) ** 3;
451
+ const s = (c.L - .0894841775 * c.a - 1.291485548 * c.b) ** 3;
452
+ return {
453
+ r: linearToSrgb(4.0767416621 * l - 3.3077115913 * m + .2309699292 * s),
454
+ g: linearToSrgb(-1.2684380046 * l + 2.6097574011 * m - .3413193965 * s),
455
+ b: linearToSrgb(-.0041960863 * l - .7034186147 * m + 1.707614701 * s),
456
+ a: c.alpha
457
+ };
458
+ }
459
+ /** Interpolate two CSS color strings in OKLab; t may extrapolate. */
460
+ function lerpColor(from, to, t) {
461
+ const a = rgbaToOklab(parseColor(from));
462
+ const b = rgbaToOklab(parseColor(to));
463
+ const mix = (x, y) => x + (y - x) * t;
464
+ return formatColor(oklabToRgba({
465
+ L: mix(a.L, b.L),
466
+ a: mix(a.a, b.a),
467
+ b: mix(a.b, b.b),
468
+ alpha: mix(a.alpha, b.alpha)
469
+ }));
470
+ }
471
+ //#endregion
472
+ //#region src/valueTypes.ts
473
+ /**
474
+ * Value-type registry with pluggable per-type interpolation (DESIGN.md §2.2).
475
+ * `extrapolates` declares whether a type's lerp accepts easedT outside [0,1]
476
+ * (spring overshoot); non-extrapolating types clamp.
477
+ */
478
+ const registry = /* @__PURE__ */ new Map();
479
+ function registerValueType(vt) {
480
+ registry.set(vt.id, vt);
481
+ }
482
+ var UnknownValueTypeError = class extends Error {
483
+ constructor(id) {
484
+ super(`unknown value type '${id}'; register it via registerValueType()`);
485
+ this.name = "UnknownValueTypeError";
486
+ }
487
+ };
488
+ function getValueType(id) {
489
+ const vt = registry.get(id);
490
+ if (!vt) throw new UnknownValueTypeError(id);
491
+ return vt;
492
+ }
493
+ const numberType = {
494
+ id: "number",
495
+ lerp: (a, b, t) => a + (b - a) * t,
496
+ extrapolates: true,
497
+ equals: Object.is,
498
+ add: (a, b) => a + b,
499
+ sub: (a, b) => a - b,
500
+ scale: (a, k) => a * k,
501
+ defaultHandoff: "spring"
502
+ };
503
+ const vec2Equals = (a, b) => a[0] === b[0] && a[1] === b[1];
504
+ const vec2Type = {
505
+ id: "vec2",
506
+ lerp: (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t],
507
+ extrapolates: true,
508
+ equals: vec2Equals,
509
+ add: (a, b) => [a[0] + b[0], a[1] + b[1]],
510
+ sub: (a, b) => [a[0] - b[0], a[1] - b[1]],
511
+ scale: (a, k) => [a[0] * k, a[1] * k],
512
+ defaultHandoff: "spring"
513
+ };
514
+ /** vec2 swept along a circular arc: polar lerp of radius + shortest-path angle (§2.2). */
515
+ const vec2ArcType = {
516
+ id: "vec2-arc",
517
+ lerp: (a, b, t) => {
518
+ const ra = Math.hypot(a[0], a[1]);
519
+ const rb = Math.hypot(b[0], b[1]);
520
+ const angA = Math.atan2(a[1], a[0]);
521
+ let dAng = Math.atan2(b[1], b[0]) - angA;
522
+ while (dAng > Math.PI) dAng -= 2 * Math.PI;
523
+ while (dAng < -Math.PI) dAng += 2 * Math.PI;
524
+ const r = ra + (rb - ra) * t;
525
+ const ang = angA + dAng * t;
526
+ return [r * Math.cos(ang), r * Math.sin(ang)];
527
+ },
528
+ extrapolates: true,
529
+ equals: vec2Equals,
530
+ defaultHandoff: "blend-from-frozen"
531
+ };
532
+ const colorType = {
533
+ id: "color",
534
+ lerp: lerpColor,
535
+ extrapolates: true,
536
+ equals: (a, b) => a === b,
537
+ defaultHandoff: "blend-from-frozen"
538
+ };
539
+ /** Discrete types: hold-only by construction (§2.2); lerp snaps at t=1. */
540
+ function discrete(id) {
541
+ return {
542
+ id,
543
+ lerp: (a, b, t) => t >= 1 ? b : a,
544
+ extrapolates: false,
545
+ equals: (a, b) => Object.is(a, b),
546
+ defaultHandoff: "cut"
547
+ };
548
+ }
549
+ const stringType = discrete("string");
550
+ const booleanType = discrete("boolean");
551
+ const lerpV = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
552
+ /** Topology must match for a morph (contour count, closed flags, vertex counts). */
553
+ function pathTopologyMatches(a, b) {
554
+ if (a.length !== b.length) return false;
555
+ for (let i = 0; i < a.length; i++) {
556
+ const ca = a[i];
557
+ const cb = b[i];
558
+ if (ca.closed !== cb.closed || ca.v.length !== cb.v.length) return false;
559
+ }
560
+ return true;
561
+ }
562
+ let warnedPathTopology = false;
563
+ /**
564
+ * Path morphing (§2.2): pairwise lerp of anchors and tangents — exactly how
565
+ * lottie-web morphs, so imported animations are pixel-faithful. Mismatched
566
+ * topology snaps (hold a, then b at t ≥ 1) with a one-time dev warning; the
567
+ * de Casteljau normalization fallback for arbitrary native morphs is tracked
568
+ * future work. Lerp-only: offsets are not well-defined under mismatched
569
+ * topology, so no add/sub/scale — handoffs blend from the frozen value.
570
+ */
571
+ const pathType = {
572
+ id: "path",
573
+ lerp: (a, b, t) => {
574
+ if (!pathTopologyMatches(a, b)) {
575
+ if (!warnedPathTopology) {
576
+ warnedPathTopology = true;
577
+ emitDevWarning("path lerp with mismatched topology (contour/vertex counts or closed flags differ): snapping instead of morphing — supply matched vertex counts (§2.2)");
578
+ }
579
+ return t >= 1 ? b : a;
580
+ }
581
+ return a.map((ca, i) => {
582
+ const cb = b[i];
583
+ return {
584
+ closed: ca.closed,
585
+ v: ca.v.map((p, j) => lerpV(p, cb.v[j], t)),
586
+ in: ca.in.map((p, j) => lerpV(p, cb.in[j], t)),
587
+ out: ca.out.map((p, j) => lerpV(p, cb.out[j], t))
588
+ };
589
+ });
590
+ },
591
+ extrapolates: false,
592
+ equals: (a, b) => {
593
+ if (a === b) return true;
594
+ if (!pathTopologyMatches(a, b)) return false;
595
+ const eq = (x, y) => x[0] === y[0] && x[1] === y[1];
596
+ return a.every((ca, i) => {
597
+ const cb = b[i];
598
+ return ca.v.every((p, j) => eq(p, cb.v[j])) && ca.in.every((p, j) => eq(p, cb.in[j])) && ca.out.every((p, j) => eq(p, cb.out[j]));
599
+ });
600
+ },
601
+ defaultHandoff: "blend-from-frozen"
602
+ };
603
+ const lerpN = (a, b, t) => a + (b - a) * t;
604
+ const lerpPt = (a, b, t) => [lerpN(a[0], b[0], t), lerpN(a[1], b[1], t)];
605
+ /** Lift a solid color to a uniform gradient matching `shape` (every stop = color),
606
+ * so a color↔gradient tween fades smoothly instead of snapping. */
607
+ function liftColor(color, shape) {
608
+ const stops = shape.stops.map((s) => ({
609
+ offset: s.offset,
610
+ color
611
+ }));
612
+ const interp = shape.interpolation ? { interpolation: shape.interpolation } : {};
613
+ return shape.kind === "radial" ? {
614
+ kind: "radial",
615
+ stops,
616
+ ...shape.center ? { center: shape.center } : {},
617
+ ...shape.radius !== void 0 ? { radius: shape.radius } : {},
618
+ ...interp
619
+ } : {
620
+ kind: "linear",
621
+ stops,
622
+ ...shape.from ? { from: shape.from } : {},
623
+ ...shape.to ? { to: shape.to } : {},
624
+ ...interp
625
+ };
626
+ }
627
+ let warnedPaintShape = false;
628
+ function paintSnap(t, a, b) {
629
+ if (!warnedPaintShape) {
630
+ warnedPaintShape = true;
631
+ emitDevWarning("paint lerp across mismatched gradient shapes (different kind or stop count): snapping instead of interpolating — match the kind + stop count on both keyframes (§2.2)");
632
+ }
633
+ return t >= 1 ? b : a;
634
+ }
635
+ const stopsEqual = (a, b) => a.length === b.length && a.every((s, i) => s.offset === b[i].offset && s.color === b[i].color);
636
+ /**
637
+ * Gradient paint morphing (§2.2): a solid color lifts to a uniform gradient to
638
+ * meet the other side, then matched-kind/matched-stop-count gradients lerp their
639
+ * stops (offset + oklab color) and geometry pairwise; a mismatched kind or stop
640
+ * count snaps (hold a, then b at t ≥ 1) with a one-time dev warning. Lerp-only —
641
+ * no offsets under mismatch — so handoffs blend from the frozen value.
642
+ */
643
+ const paintType = {
644
+ id: "paint",
645
+ lerp: (a, b, t) => {
646
+ if (a.kind === "color" && b.kind === "color") return {
647
+ kind: "color",
648
+ color: lerpColor(a.color, b.color, t)
649
+ };
650
+ if (a.kind === "mesh" && b.kind === "mesh") {
651
+ if (a.points.length !== b.points.length) return paintSnap(t, a, b);
652
+ return {
653
+ kind: "mesh",
654
+ points: a.points.map((pa, i) => {
655
+ const pb = b.points[i];
656
+ return {
657
+ pos: lerpPt(pa.pos, pb.pos, t),
658
+ color: lerpColor(pa.color, pb.color, t)
659
+ };
660
+ }),
661
+ ...a.interpolation ? { interpolation: a.interpolation } : {},
662
+ ...a.bg !== void 0 && b.bg !== void 0 ? { bg: lerpColor(a.bg, b.bg, t) } : a.bg !== void 0 ? { bg: a.bg } : {}
663
+ };
664
+ }
665
+ if (a.kind === "mesh" || b.kind === "mesh") return paintSnap(t, a, b);
666
+ let A = a;
667
+ let B = b;
668
+ if (A.kind === "color" && B.kind !== "color") A = liftColor(A.color, B);
669
+ else if (B.kind === "color" && A.kind !== "color") B = liftColor(B.color, A);
670
+ if (A.kind === "color" || B.kind === "color" || A.kind !== B.kind || A.stops.length !== B.stops.length) return paintSnap(t, a, b);
671
+ const stops = A.stops.map((sa, i) => ({
672
+ offset: lerpN(sa.offset, B.stops[i].offset, t),
673
+ color: lerpColor(sa.color, B.stops[i].color, t)
674
+ }));
675
+ const interp = A.interpolation ? { interpolation: A.interpolation } : {};
676
+ if (A.kind === "radial" && B.kind === "radial") return {
677
+ kind: "radial",
678
+ stops,
679
+ ...A.center && B.center ? { center: lerpPt(A.center, B.center, t) } : A.center ? { center: A.center } : {},
680
+ ...A.radius !== void 0 && B.radius !== void 0 ? { radius: lerpN(A.radius, B.radius, t) } : A.radius !== void 0 ? { radius: A.radius } : {},
681
+ ...interp
682
+ };
683
+ if (A.kind === "linear" && B.kind === "linear") return {
684
+ kind: "linear",
685
+ stops,
686
+ ...A.from && B.from ? { from: lerpPt(A.from, B.from, t) } : A.from ? { from: A.from } : {},
687
+ ...A.to && B.to ? { to: lerpPt(A.to, B.to, t) } : A.to ? { to: A.to } : {},
688
+ ...interp
689
+ };
690
+ return paintSnap(t, a, b);
691
+ },
692
+ extrapolates: false,
693
+ equals: (a, b) => {
694
+ if (a === b) return true;
695
+ if (a.kind !== b.kind) return false;
696
+ if (a.kind === "color") return b.kind === "color" && a.color === b.color;
697
+ if (a.kind === "mesh") {
698
+ if (b.kind !== "mesh") return false;
699
+ if (a.interpolation !== b.interpolation || a.bg !== b.bg) return false;
700
+ if (a.points.length !== b.points.length) return false;
701
+ return a.points.every((p, i) => {
702
+ const q = b.points[i];
703
+ return p.color === q.color && p.pos[0] === q.pos[0] && p.pos[1] === q.pos[1];
704
+ });
705
+ }
706
+ if (b.kind === "color" || b.kind === "mesh") return false;
707
+ if (!stopsEqual(a.stops, b.stops)) return false;
708
+ if (a.interpolation !== b.interpolation) return false;
709
+ if (a.kind === "radial" && b.kind === "radial") return JSON.stringify(a.center) === JSON.stringify(b.center) && a.radius === b.radius;
710
+ if (a.kind === "linear" && b.kind === "linear") return JSON.stringify(a.from) === JSON.stringify(b.from) && JSON.stringify(a.to) === JSON.stringify(b.to);
711
+ return false;
712
+ },
713
+ defaultHandoff: "blend-from-frozen"
714
+ };
715
+ var ValueTypeInferenceError = class extends Error {
716
+ constructor(value) {
717
+ super(`cannot infer a value type for ${JSON.stringify(value)}; register a custom type`);
718
+ this.name = "ValueTypeInferenceError";
719
+ }
720
+ };
721
+ const isContour = (c) => typeof c === "object" && c !== null && typeof c.closed === "boolean" && Array.isArray(c.v) && Array.isArray(c.in) && Array.isArray(c.out);
722
+ const isPaint = (v) => typeof v === "object" && v !== null && (v.kind === "color" || v.kind === "linear" || v.kind === "radial" || v.kind === "mesh");
723
+ /** Infer a registered type id from a sample value (builder + bake authoring surfaces). */
724
+ function inferValueType(value) {
725
+ if (typeof value === "number") return "number";
726
+ if (typeof value === "boolean") return "boolean";
727
+ if (isPaint(value)) return "paint";
728
+ if (Array.isArray(value) && value.length === 2 && value.every((v) => typeof v === "number")) return "vec2";
729
+ if (Array.isArray(value) && value.length > 0 && value.every(isContour)) return "path";
730
+ if (typeof value === "string") try {
731
+ parseColor(value);
732
+ return "color";
733
+ } catch {
734
+ return "string";
735
+ }
736
+ throw new ValueTypeInferenceError(value);
737
+ }
738
+ registerValueType(numberType);
739
+ registerValueType(vec2Type);
740
+ registerValueType(vec2ArcType);
741
+ registerValueType(colorType);
742
+ registerValueType(stringType);
743
+ registerValueType(booleanType);
744
+ registerValueType(pathType);
745
+ registerValueType(paintType);
746
+ //#endregion
747
+ //#region src/track.ts
748
+ /**
749
+ * Track & keyframe model (DESIGN.md §2.2) and sampling (§2.4): binary search
750
+ * per track with a last-segment cursor — sanctioned memoization, semantics
751
+ * identical to a cold search (property-tested).
752
+ */
753
+ var TrackValidationError = class extends Error {
754
+ constructor(target, message) {
755
+ super(`track '${target}': ${message}`);
756
+ this.name = "TrackValidationError";
757
+ }
758
+ };
759
+ function validateTrack(track) {
760
+ const vt = getValueType(track.type);
761
+ if (track.keys.length === 0) throw new TrackValidationError(track.target, "must have at least one key");
762
+ if (vt.defaultHandoff === "cut") {
763
+ for (const k of track.keys) if (k.interp !== "hold") k.interp = "hold";
764
+ }
765
+ for (let i = 1; i < track.keys.length; i++) {
766
+ const prev = track.keys[i - 1];
767
+ const cur = track.keys[i];
768
+ if (cur.t <= prev.t) throw new TrackValidationError(track.target, `keys must be strictly increasing in t (key[${i}] at t=${cur.t} after t=${prev.t})`);
769
+ }
770
+ if (!/^[^/]+\/.+$/.test(track.target)) throw new TrackValidationError(track.target, "target must be '<nodeId>/<prop.path>' (e.g. 'circle/opacity')");
771
+ }
772
+ function key(t, value, easeOrOpts) {
773
+ if (easeOrOpts === void 0) return {
774
+ t,
775
+ value
776
+ };
777
+ if (typeof easeOrOpts === "string" || "kind" in easeOrOpts) return {
778
+ t,
779
+ value,
780
+ ease: easeOrOpts
781
+ };
782
+ const opts = easeOrOpts;
783
+ const k = {
784
+ t,
785
+ value
786
+ };
787
+ if (opts.ease !== void 0) k.ease = opts.ease;
788
+ if (opts.interp !== void 0) k.interp = opts.interp;
789
+ if (opts.id !== void 0) k.id = opts.id;
790
+ if (opts.derived !== void 0) k.derived = opts.derived;
791
+ return k;
792
+ }
793
+ /**
794
+ * The settle-ON-the-beat helper: a spring key must sit at prev.t +
795
+ * spring.duration(cfg) (§2.7), so beat-anchored authoring otherwise means
796
+ * hand-computing the launch time. springTo returns the [launch, settle] key
797
+ * pair with the arithmetic done — spread it into a raw track():
798
+ * track('x/width', 'number', [...springTo(beats.start('drop'), 0, 320, cfg)])
799
+ */
800
+ function springTo(endT, from, to, cfg) {
801
+ const d = spring.duration(cfg);
802
+ if (endT - d < 0) throw new TrackValidationError("springTo", `this spring needs ${d.toFixed(3)}s to settle — endT must be ≥ its duration (got ${endT})`);
803
+ return [key(endT - d, from), key(endT, to, spring(cfg))];
804
+ }
805
+ /**
806
+ * Cascade a set of tracks by shifting each one's key times — the classic
807
+ * stagger for animating a list of nodes with a delay between them. `delay` is
808
+ * the per-index gap in seconds, or a function of the index for non-linear
809
+ * cascades. Pure: returns new tracks, leaving the inputs untouched.
810
+ *
811
+ * stagger(items.map((it, i) => track(`${it.id}/opacity`, 'number',
812
+ * [key(0, 0), key(0.3, 1, 'easeOutCubic')])), 0.08)
813
+ */
814
+ function stagger(tracks, delay) {
815
+ const at = typeof delay === "function" ? delay : (i) => i * delay;
816
+ return tracks.map((tr, i) => {
817
+ const d = at(i);
818
+ return {
819
+ ...tr,
820
+ keys: tr.keys.map((k) => ({
821
+ ...k,
822
+ t: k.t + d
823
+ }))
824
+ };
825
+ });
826
+ }
827
+ function track(target, type, keys, opts) {
828
+ const tr = {
829
+ target,
830
+ type,
831
+ keys
832
+ };
833
+ if (opts?.editable !== void 0) tr.editable = opts.editable;
834
+ validateTrack(tr);
835
+ return tr;
836
+ }
837
+ function resolveEase(spec) {
838
+ if (spec === void 0) return namedEasing("linear");
839
+ if (typeof spec === "string") return namedEasing(spec);
840
+ if (spec.kind === "cubicBezier") return cubicBezier(...spec.pts);
841
+ return springEasing(spec);
842
+ }
843
+ const warnedNumericDerivative = /* @__PURE__ */ new Set();
844
+ /**
845
+ * Analytic d(u) for an ease spec (§B.6). Custom-registered eases without a
846
+ * derivative fall back to a symmetric difference with a one-time dev warning.
847
+ */
848
+ function resolveEaseDerivative(spec) {
849
+ if (spec === void 0) return easingDerivatives["linear"];
850
+ if (typeof spec === "string") {
851
+ const d = easingDerivatives[spec];
852
+ if (d) return d;
853
+ const fn = namedEasing(spec);
854
+ if (!warnedNumericDerivative.has(spec)) {
855
+ warnedNumericDerivative.add(spec);
856
+ emitDevWarning(`easing '${spec}' has no registered derivative; velocity uses a numeric fallback — register one in easingDerivatives for exact interruption handoff`);
857
+ }
858
+ const h = 1 / 1024;
859
+ return (u) => (fn(Math.min(1, u + h)) - fn(Math.max(0, u - h))) / (Math.min(1, u + h) - Math.max(0, u - h));
860
+ }
861
+ if (spec.kind === "cubicBezier") return cubicBezierDerivative(...spec.pts);
862
+ return springEasingDerivative(spec);
863
+ }
864
+ const samplerStates = /* @__PURE__ */ new WeakMap();
865
+ function state(tr) {
866
+ let s = samplerStates.get(tr);
867
+ if (!s) {
868
+ s = {
869
+ cursor: 1,
870
+ easeCache: new Array(tr.keys.length)
871
+ };
872
+ samplerStates.set(tr, s);
873
+ }
874
+ return s;
875
+ }
876
+ function easeFor(tr, s, i) {
877
+ let fn = s.easeCache[i];
878
+ if (!fn) {
879
+ fn = resolveEase(tr.keys[i].ease);
880
+ s.easeCache[i] = fn;
881
+ }
882
+ return fn;
883
+ }
884
+ /**
885
+ * Find i such that keys[i-1].t <= t < keys[i].t, i.e. the index of the
886
+ * arrival key of the segment containing t. Returns 0 if t < first key,
887
+ * keys.length if t >= last key.
888
+ */
889
+ function findSegment(keys, t, hint) {
890
+ const n = keys.length;
891
+ for (let i = Math.max(1, hint - 1); i <= Math.min(n - 1, hint + 1); i++) if (keys[i - 1].t <= t && t < keys[i].t) return i;
892
+ if (t < keys[0].t) return 0;
893
+ if (t >= keys[n - 1].t) return n;
894
+ let lo = 1;
895
+ let hi = n - 1;
896
+ while (lo < hi) {
897
+ const mid = lo + hi >> 1;
898
+ if (keys[mid].t <= t) lo = mid + 1;
899
+ else hi = mid;
900
+ }
901
+ return lo;
902
+ }
903
+ /**
904
+ * Analytic track derivative at time t, in value-units per second of local
905
+ * track time (v2 addendum §B.3/§B.6 conventions, pinned):
906
+ * (a) at a key boundary, velocity is the RIGHT derivative;
907
+ * (b) hold segments and the clamped regions outside the keys have v = 0;
908
+ * (c) types without sub/scale operators return null (no kinetic velocity).
909
+ */
910
+ function velocityAt(tr, t) {
911
+ const vt = getValueType(tr.type);
912
+ if (!vt.sub || !vt.scale) return null;
913
+ const keys = tr.keys;
914
+ const n = keys.length;
915
+ const s = state(tr);
916
+ const i = findSegment(keys, t, s.cursor);
917
+ const zero = vt.scale(vt.sub(keys[0].value, keys[0].value), 0);
918
+ if (i === 0 || i >= n) return zero;
919
+ const arrival = keys[i];
920
+ if (arrival.interp === "hold") return zero;
921
+ const prev = keys[i - 1];
922
+ const segDur = arrival.t - prev.t;
923
+ const p = (t - prev.t) / segDur;
924
+ if (!vt.extrapolates) {
925
+ const eased = easeFor(tr, s, i)(p);
926
+ if (eased < 0 || eased > 1) return zero;
927
+ }
928
+ const d = resolveEaseDerivative(arrival.ease)(p);
929
+ return vt.scale(vt.sub(arrival.value, prev.value), d / segDur);
930
+ }
931
+ /** Pure sample of a track at time t (§2.4). */
932
+ function sampleTrack(tr, t) {
933
+ const keys = tr.keys;
934
+ const n = keys.length;
935
+ const s = state(tr);
936
+ const i = findSegment(keys, t, s.cursor);
937
+ if (i === 0) return keys[0].value;
938
+ if (i >= n) return keys[n - 1].value;
939
+ s.cursor = i;
940
+ const arrival = keys[i];
941
+ const prev = keys[i - 1];
942
+ if (arrival.interp === "hold") return prev.value;
943
+ const vt = getValueType(tr.type);
944
+ const p = (t - prev.t) / (arrival.t - prev.t);
945
+ let easedT = easeFor(tr, s, i)(p);
946
+ if (!vt.extrapolates && (easedT < 0 || easedT > 1)) {
947
+ if (!s.warnedClamp) {
948
+ s.warnedClamp = true;
949
+ emitDevWarning(`track '${tr.target}' (type '${tr.type}') clamped an out-of-range eased value — non-extrapolating types can't overshoot, so a spring/overshooting ease on this track is flattened`);
950
+ }
951
+ easedT = Math.min(1, Math.max(0, easedT));
952
+ }
953
+ return vt.lerp(prev.value, arrival.value, easedT);
954
+ }
955
+ //#endregion
956
+ //#region src/targetRef.ts
957
+ /**
958
+ * Target references (DESIGN.md §2.6): the builder accepts either a canonical
959
+ * target string ('circle/opacity') or a property signal that carries its own
960
+ * path — attached by the scene package at node construction via TARGET_PATH.
961
+ */
962
+ const TARGET_PATH = Symbol.for("glissade.targetPath");
963
+ var UnresolvableTargetError = class extends Error {
964
+ constructor(message) {
965
+ super(message ?? "tween target is not addressable: pass a target string (\"node/prop\") or a property signal of a node that has an explicit id (§3.1 — anonymous nodes cannot be track targets)");
966
+ this.name = "UnresolvableTargetError";
967
+ }
968
+ };
969
+ /** The node-id portion of a canonical `nodeId/prop.path` target. */
970
+ function targetNodeId(target) {
971
+ const slash = target.indexOf("/");
972
+ return slash < 0 ? target : target.slice(0, slash);
973
+ }
974
+ /**
975
+ * The single editable-host rule (§6.4 sub-decision, the 0.9 locked predicate):
976
+ * only a node with an EXPLICIT, non-structural id can host an editable or
977
+ * editor-created track. Structural fallback ids (`~Type.ordinal`, §6.5) are
978
+ * inspection-only and reorder-fragile, so they are never editable nor valid
979
+ * track targets. Lives here (the addressing module) so the builder guard, the
980
+ * scene, and the studio host all share ONE definition.
981
+ */
982
+ function isEditableNodeId(id) {
983
+ return typeof id === "string" && id.length > 0 && id !== "__root" && !id.startsWith("~");
984
+ }
985
+ function resolveTweenTarget(target) {
986
+ const path = typeof target === "string" ? target : target[TARGET_PATH];
987
+ if (typeof path !== "string") throw new UnresolvableTargetError();
988
+ if (targetNodeId(path).startsWith("~")) throw new UnresolvableTargetError(`'${path}': structural ids (~Type.ordinal) are inspection-only and cannot be track targets (§6.5) — give the node an explicit id`);
989
+ return path;
990
+ }
991
+ //#endregion
992
+ export { ColorParseError as A, springEasingDerivative as B, paintType as C, vec2ArcType as D, stringType as E, rgbaToOklab as F, cubicBezierDerivative as G, DEFAULT_EASE as H, emitDevWarning as I, namedEasing as J, easingDerivatives as K, setDevWarning as L, lerpColor as M, oklabToRgba as N, vec2Equals as O, parseColor as P, spring as R, numberType as S, registerValueType as T, UnknownEasingError as U, springPresets as V, cubicBezier as W, ValueTypeInferenceError as _, targetNodeId as a, getValueType as b, resolveEase as c, springTo as d, stagger as f, UnknownValueTypeError as g, velocityAt as h, resolveTweenTarget as i, formatColor as j, vec2Type as k, resolveEaseDerivative as l, validateTrack as m, UnresolvableTargetError as n, TrackValidationError as o, track as p, easings as q, isEditableNodeId as r, key as s, TARGET_PATH as t, sampleTrack as u, booleanType as v, pathType as w, inferValueType as x, colorType as y, springEasing as z };