@mkbabb/keyframes.js 3.0.0 → 4.0.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,1031 @@
1
+ import { _ as e, c as t, d as n, f as r, h as i, o as a, s as o } from "./timeline-5rnU0j6r.js";
2
+ import { COLOR_SPACE_RANGES as s, COMPUTED_UNITS as c, CSSCubicBezier as l, CSSFunction as u, CSSValues as d, FunctionValue as f, ValueArray as p, ValueUnit as m, clamp as h, convertToMs as g, cssLinear as _, easeInOutCubic as v, extractAnimationOptions as y, extractKeyframes as ee, extractProperties as te, flattenObject as b, isObject as ne, jumpTerms as re, lerp as ie, lerpValue as ae, normalizeValueUnits as oe, parseCSSStylesheet as x, parseCSSTime as S, parseCSSValueUnit as se, prepareInterpVar as ce, scale as le, seekPreviousValue as C, sleep as w, steppedEase as T, timingFunctions as E, tryParse as ue, unflattenObject as de, unflattenObjectToString as D } from "@mkbabb/value.js";
3
+ import { any as fe } from "@mkbabb/parse-that";
4
+ //#region src/animation/adapter.ts
5
+ var pe = (e) => {
6
+ let t = {};
7
+ for (let n of e.declarations) t[n.name] = n.value;
8
+ return t;
9
+ }, me = (e) => {
10
+ let t = [];
11
+ for (let n of e.selectors) n.kind === "percent" ? t.push(`${n.value}%`) : t.push(n.name);
12
+ return t;
13
+ }, O = (e) => {
14
+ let t = ee(e);
15
+ for (let e of t.values()) if (e.length > 0) return e;
16
+ return [];
17
+ }, k = (e) => {
18
+ let t = typeof e == "string" ? x(e) : e, n = O(t);
19
+ typeof e == "string" && n.length === 0 && e.trim() && (t = x(`@keyframes anonymous {\n${e.trim()}\n}`), n = O(t));
20
+ let r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map();
21
+ for (let e of n) {
22
+ let t = pe(e);
23
+ for (let n of me(e)) {
24
+ let o = r.get(n);
25
+ r.set(n, {
26
+ ...o ?? {},
27
+ ...t
28
+ }), e.timingFunction != null && i.set(n, e.timingFunction);
29
+ let s = e.composition;
30
+ s != null && a.set(n, s);
31
+ }
32
+ }
33
+ return {
34
+ keyframes: r,
35
+ timingFunctions: i,
36
+ composition: a,
37
+ properties: te(t),
38
+ options: y(t)
39
+ };
40
+ }, A = [
41
+ "normal",
42
+ "reverse",
43
+ "alternate",
44
+ "alternate-reverse"
45
+ ], j = [
46
+ "none",
47
+ "forwards",
48
+ "backwards",
49
+ "both"
50
+ ], M = Object.keys(s), N = [
51
+ "shorter",
52
+ "longer",
53
+ "increasing",
54
+ "decreasing"
55
+ ], P = {
56
+ duration: 1e3,
57
+ delay: 0,
58
+ iterationCount: 1,
59
+ direction: "normal",
60
+ fillMode: "forwards",
61
+ timingFunction: { fn: v },
62
+ useWAAPI: !0,
63
+ respectReducedMotion: !1,
64
+ colorSpace: "oklab"
65
+ }, F = {
66
+ zIndex: 0,
67
+ weight: 1,
68
+ blendMode: "replace",
69
+ enabled: !0
70
+ }, I = null, L = () => typeof MessageChannel == "function" ? () => new Promise((e) => {
71
+ let t = new MessageChannel();
72
+ t.port1.onmessage = () => {
73
+ t.port1.close(), e();
74
+ }, t.port2.postMessage(void 0);
75
+ }) : () => new Promise((e) => setTimeout(e, 0));
76
+ function R() {
77
+ let e = globalThis.scheduler;
78
+ return e && typeof e.yield == "function" ? e.yield() : (I ??= L(), I());
79
+ }
80
+ //#endregion
81
+ //#region src/animation/group.ts
82
+ var z = (e) => e instanceof m && typeof e.value == "number", B = class e {
83
+ animations = {};
84
+ transform;
85
+ superKey;
86
+ paused = !1;
87
+ started = !1;
88
+ done = !1;
89
+ respectReducedMotion = !1;
90
+ static YIELD_BATCH = 32;
91
+ singleTarget = !0;
92
+ lastTickTime = 0;
93
+ playback = new a();
94
+ resolvePromise = null;
95
+ _playingPromise = null;
96
+ _boundFrame;
97
+ _entries = [];
98
+ _entriesDirty = !0;
99
+ _grouped = {};
100
+ _groupedKeys = [];
101
+ _groupedKeysDirty = !0;
102
+ constructor(...e) {
103
+ this._boundFrame = this._frame.bind(this);
104
+ let t = [];
105
+ for (let n of e) {
106
+ let e, r;
107
+ n instanceof $ ? e = n : (e = n.animation, r = n.layer), this.transform == null && e.frames[0] != null && (this.transform = e.frames[0].transform);
108
+ let i = Q(e);
109
+ this.animations[i] = {
110
+ values: {},
111
+ animation: e,
112
+ layer: {
113
+ ...F,
114
+ ...r
115
+ }
116
+ }, e.managed = !0, t.push(e);
117
+ }
118
+ this.singleTarget = t.every((e) => e.targets[0] === t[0]?.targets[0]), this.invalidateEntries();
119
+ }
120
+ getEntries() {
121
+ return this._entriesDirty &&= (this._entries = Object.values(this.animations), this._entries.sort((e, t) => e.layer.zIndex - t.layer.zIndex), !1), this._entries;
122
+ }
123
+ invalidateEntries() {
124
+ this._entriesDirty = !0, this._groupedKeysDirty = !0;
125
+ }
126
+ computeGroupedKeys() {
127
+ let e = /* @__PURE__ */ new Set();
128
+ for (let t of this.getEntries()) {
129
+ let n = t.layer.properties;
130
+ for (let r of t.animation.flatKeys) n && !n.has(r) || e.add(r);
131
+ }
132
+ this._groupedKeys = [...e];
133
+ }
134
+ setSuperKey(e) {
135
+ this.superKey = e;
136
+ for (let t of this.getEntries()) t.animation.superKey = e;
137
+ return this;
138
+ }
139
+ setTargets(...e) {
140
+ let t = this.getEntries();
141
+ for (let n of t) n.animation.setTargets(...e);
142
+ return this.singleTarget = t.every((e) => e.animation.targets[0] === t[0]?.animation.targets[0]), this;
143
+ }
144
+ onStart() {
145
+ return this.started = !0, this;
146
+ }
147
+ onEnd() {
148
+ return this;
149
+ }
150
+ transformFramesGrouped(e) {
151
+ let t = this._grouped;
152
+ this._groupedKeysDirty &&= (this.computeGroupedKeys(), !1);
153
+ let n = this._groupedKeys;
154
+ for (let e = 0; e < n.length; e++) t[n[e]] = void 0;
155
+ let r = this.getEntries(), i = !0;
156
+ for (let e of r) {
157
+ let { animation: n, layer: r, values: a } = e;
158
+ if (i &&= n.done, !r.enabled) continue;
159
+ n.interpFrames(n.t, !1, a);
160
+ let o = r.properties;
161
+ switch (r.blendMode) {
162
+ case "replace":
163
+ for (let e in a) {
164
+ if (o && !o.has(e)) continue;
165
+ let n = a[e];
166
+ n !== void 0 && (t[e] = n);
167
+ }
168
+ break;
169
+ case "add":
170
+ for (let e in a) {
171
+ if (o && !o.has(e)) continue;
172
+ let n = a[e];
173
+ if (n === void 0) continue;
174
+ let r = t[e];
175
+ r === void 0 ? t[e] = n : z(r) && z(n) ? r.value += n.value : t[e] = n;
176
+ }
177
+ break;
178
+ case "weighted":
179
+ for (let e in a) {
180
+ if (o && !o.has(e)) continue;
181
+ let n = a[e];
182
+ if (n !== void 0) if (t[e] !== void 0) {
183
+ let i = t[e];
184
+ z(i) && z(n) ? i.value = ie(i.value, n.value, r.weight) : t[e] = n;
185
+ } else t[e] = n;
186
+ }
187
+ break;
188
+ }
189
+ }
190
+ this.done = i;
191
+ for (let e = 0; e < n.length; e++) {
192
+ let r = n[e];
193
+ t[r] === void 0 && delete t[r];
194
+ }
195
+ return this.transform(t, e), t;
196
+ }
197
+ render() {
198
+ let e = this.lastTickTime || performance.now();
199
+ if (this.singleTarget) this.transformFramesGrouped(e);
200
+ else for (let e of this.getEntries()) e.animation.interpFrames(e.animation.t, !0);
201
+ }
202
+ setChildTime(e, t) {
203
+ let n = typeof e == "string" ? e : Q(e), r = this.animations[n];
204
+ if (!r) throw Error(`AnimationGroup.setChildTime: no animation registered for key "${n}". Known keys: ${Object.keys(this.animations).join(", ") || "(none)"}.`);
205
+ let i = r.animation;
206
+ return i.t = t, i.startTime !== void 0 && (i.pausedTime = i.startTime + t), this;
207
+ }
208
+ async advanceTo(t) {
209
+ this.lastTickTime = t, this.started || this.onStart();
210
+ let n = this.getEntries(), r = e.YIELD_BATCH;
211
+ if (n.length <= r) await this._advanceSlice(n, t);
212
+ else for (let e = 0; e < n.length; e += r) await this._advanceSlice(n.slice(e, e + r), t), e + r < n.length && await R();
213
+ return this.done && this.onEnd(), this;
214
+ }
215
+ async _advanceSlice(e, t) {
216
+ let n = [];
217
+ for (let r of e) {
218
+ let e = r.animation;
219
+ (!e.paused || e.pausedTime === 0) && n.push(e.advanceTo(t));
220
+ }
221
+ await Promise.all(n);
222
+ }
223
+ async _frame(e) {
224
+ if (await this.advanceTo(e), this.paused) return !1;
225
+ if (this.singleTarget) this.transformFramesGrouped(e);
226
+ else {
227
+ let e = !0;
228
+ for (let t of this.getEntries()) t.animation.interpFrames(t.animation.t, !0), e &&= t.animation.done;
229
+ this.done = e;
230
+ }
231
+ return this.done ? (this.settle(), this._resolvePlay(), !1) : !0;
232
+ }
233
+ _resolvePlay() {
234
+ let e = this.resolvePromise;
235
+ this.resolvePromise = null, e?.();
236
+ }
237
+ async play() {
238
+ if (this._playingPromise) return this._playingPromise;
239
+ let e = o(this.respectReducedMotion, () => this._playReducedMotion(), () => new Promise((e) => {
240
+ this.resolvePromise = e, this.playback.loop(this._boundFrame);
241
+ }));
242
+ return this._playingPromise = e, e.finally(() => {
243
+ this._playingPromise = null;
244
+ }), e;
245
+ }
246
+ _playReducedMotion() {
247
+ this.onStart();
248
+ let e = this.lastTickTime || performance.now();
249
+ this.lastTickTime = e;
250
+ for (let e of this.getEntries()) {
251
+ let t = e.animation;
252
+ t.started = !0, t.t = t.options.duration, t.interpFrames(t.t, !0);
253
+ }
254
+ return this.singleTarget && this.transformFramesGrouped(e), this.settle(), Promise.resolve();
255
+ }
256
+ pause() {
257
+ if (!this.started || this.paused) return this;
258
+ this.paused = !0;
259
+ let e = this.lastTickTime || performance.now();
260
+ for (let t of this.getEntries()) {
261
+ let n = t.animation;
262
+ n.pause(), n.pausedTime === 0 && (n.pausedTime = e);
263
+ }
264
+ return this.playback.stop(), this.render(), this;
265
+ }
266
+ resume() {
267
+ if (!this.started || !this.paused) return this;
268
+ this.paused = !1;
269
+ for (let e of this.getEntries()) e.animation.paused = !1;
270
+ return this.playback.loop(this._boundFrame), this;
271
+ }
272
+ toggle() {
273
+ return this.paused ? this.resume() : this.pause();
274
+ }
275
+ settle() {
276
+ for (let e of this.getEntries()) e.animation.managed = !1, e.animation.settle();
277
+ return this.started = !1, this.done = !1, this.paused = !1, this.lastTickTime = 0, this;
278
+ }
279
+ reset() {
280
+ for (let e of this.getEntries()) {
281
+ let t = e.animation;
282
+ t.started && t.frames.length > 0 && t.interpFrames(0, !0);
283
+ }
284
+ return this.settle();
285
+ }
286
+ stop() {
287
+ return this.playback.stop(), this.reset(), this._resolvePlay(), this;
288
+ }
289
+ playing() {
290
+ return !(!this.started || this.paused);
291
+ }
292
+ forcePause() {
293
+ this.paused = !0;
294
+ for (let e of this.getEntries()) e.animation.paused = !0;
295
+ }
296
+ forcePlay() {
297
+ this.paused = !1;
298
+ for (let e of this.getEntries()) e.animation.paused = !1;
299
+ }
300
+ setLayerConfig(e, t) {
301
+ let n = typeof e == "string" ? e : Q(e), r = this.animations[n];
302
+ if (!r) throw Error(`AnimationGroup.setLayerConfig: no animation registered for key "${n}". Known keys: ${Object.keys(this.animations).join(", ") || "(none)"}.`);
303
+ return Object.assign(r.layer, t), this.invalidateEntries(), this;
304
+ }
305
+ setLayerEnabled(e, t) {
306
+ return this.setLayerConfig(e, { enabled: t });
307
+ }
308
+ getLayerConfig(e) {
309
+ let t = typeof e == "string" ? e : Q(e);
310
+ return this.animations[t]?.layer;
311
+ }
312
+ }, V = (e) => {
313
+ if (e instanceof m) return [e.clone()];
314
+ if (e instanceof f) return e.values.flatMap((e) => V(e));
315
+ if (e instanceof p) return e.flatMap((e) => V(e));
316
+ throw TypeError(`Expected ValueUnit/FunctionValue/ValueArray, got ${typeof e}`);
317
+ }, he = (e) => {
318
+ let t = e.split(".").pop(), n = e.split(".").shift();
319
+ if (!t || !n) throw Error(`Invalid flattened key: ${e}`);
320
+ return {
321
+ mainKey: n,
322
+ childKey: t
323
+ };
324
+ }, H = (e, t, n) => (e.setProperty(t), n !== t && e.setSubProperty(n), e), ge = /^\s*cubic-bezier\s*\(\s*(-?[\d.]+)\s*,\s*(-?[\d.]+)\s*,\s*(-?[\d.]+)\s*,\s*(-?[\d.]+)\s*\)\s*$/i, _e = /^\s*steps\s*\(\s*(\d+)\s*(?:,\s*(jump-start|jump-end|jump-none|jump-both|start|end|both)\s*)?\)\s*$/i, U = /^\s*linear\s*\(\s*(.+)\s*\)\s*$/i, ve = (e) => {
325
+ let t = [];
326
+ for (let n of e.split(",")) {
327
+ let e = n.trim().split(/\s+/).filter(Boolean);
328
+ if (e.length === 0) return;
329
+ let r = Number.parseFloat(e[0]);
330
+ if (!Number.isFinite(r)) return;
331
+ let i = e.slice(1);
332
+ if (i.length === 0) {
333
+ t.push({ output: r });
334
+ continue;
335
+ }
336
+ for (let e of i) {
337
+ let n = Number.parseFloat(e.endsWith("%") ? e.slice(0, -1) : e);
338
+ if (!Number.isFinite(n)) return;
339
+ t.push({
340
+ output: r,
341
+ input: n
342
+ });
343
+ }
344
+ }
345
+ return t.length >= 2 ? t : void 0;
346
+ }, W = (e) => {
347
+ if (e == null) return;
348
+ if (typeof e != "string") return e;
349
+ let t = e.match(ge);
350
+ if (t) {
351
+ let e = t.slice(1, 5).map((e) => Number.parseFloat(e ?? ""));
352
+ if (e.length === 4 && e.every((e) => Number.isFinite(e))) {
353
+ let [t, n, r, i] = e;
354
+ return l(t, n, r, i);
355
+ }
356
+ }
357
+ let n = e.match(_e);
358
+ if (n) {
359
+ let e = Number.parseInt(n[1], 10), t = n[2]?.toLowerCase() ?? "end";
360
+ if (e > 0 && re.includes(t)) return T(e, t);
361
+ }
362
+ if (e === "step-start") return T(1, "jump-start");
363
+ if (e === "step-end") return T(1, "jump-end");
364
+ let r = e.match(U);
365
+ if (r) {
366
+ let e = ve(r[1]);
367
+ if (e) return _(e);
368
+ }
369
+ let i = E[e];
370
+ if (typeof i == "function" && i.length <= 1) return i;
371
+ }, G = /* @__PURE__ */ new Map();
372
+ function ye(e) {
373
+ let t = b(e), n = (e, t) => {
374
+ let { childKey: r, mainKey: i } = he(e);
375
+ if (t instanceof m) return H(new p(...V(t)), i, r);
376
+ if (t instanceof f) return H(new p(...t.values.flatMap((t) => V(n(e, t)))), i, r);
377
+ if (t instanceof p) return H(new p(...t.flatMap((t) => V(n(e, t)))), i, r);
378
+ let a = String(t), o = `${r}:${a}`, s = G.get(o);
379
+ if (s) return H(s.clone(), i, r);
380
+ let c = H(new p(...V(ue(fe(u.FunctionArgs.map((e) => (e.setSubProperty(r), e)), d.Value), a))), i, r);
381
+ return G.set(o, c.clone()), c;
382
+ };
383
+ return Object.entries(t).reduce((e, [t, r]) => (e[t] = n(t, r), e), {});
384
+ }
385
+ var K = (e, t, n, r, i = "oklab", a) => {
386
+ let o = r[t], s = r[n];
387
+ if (!o || !s) throw Error(`Invalid interpolation frame bounds (${t} -> ${n}).`);
388
+ let c = o[e], l = s[e];
389
+ if (!c || !l) throw Error(`Missing variable "${e}" in interpolation bounds.`);
390
+ let u = Math.max(c.length, l.length), d = (t) => {
391
+ let n = t.map((t) => {
392
+ if (!(t instanceof m)) throw TypeError(`Interpolation for "${e}" requires ValueUnit leaves.`);
393
+ return t;
394
+ });
395
+ for (; n.length < u;) n.push(new m(0));
396
+ return n;
397
+ }, f = d(c), p = d(l);
398
+ return f.map((t, n) => {
399
+ let r = p[n];
400
+ if (!r) throw Error(`Missing right-hand interpolation value at index ${n}.`);
401
+ if (!(t instanceof m) || !(r instanceof m)) throw TypeError(`Interpolation for "${e}" requires ValueUnit leaves.`);
402
+ let o = { colorSpace: i };
403
+ return a !== void 0 && (o.hueMethod = a), ce(oe(t, r, o));
404
+ });
405
+ };
406
+ function be(e, t, n) {
407
+ let [r, i] = [e.start, t.start];
408
+ return {
409
+ start: r.value * n / 100,
410
+ stop: i.value * n / 100
411
+ };
412
+ }
413
+ function q(e, t, n = !0) {
414
+ e = n ? e : b(e);
415
+ let r = D(e);
416
+ t.forEach((e) => {
417
+ Object.entries(r).forEach(([t, n]) => {
418
+ e.style.setProperty(t, n);
419
+ });
420
+ });
421
+ }
422
+ //#endregion
423
+ //#region src/animation/frame-compiler.ts
424
+ var J = (e, t) => {
425
+ if (typeof t == "function") return { fn: t };
426
+ if (typeof t == "object") {
427
+ if (typeof t.fn == "function") return t;
428
+ throw new i(e, t, "an Easing must carry a callable `fn`");
429
+ }
430
+ let n = W(t);
431
+ if (!n) throw new i(e, t, "unknown timing function — pass a callable TimingFunction, a typed Easing, a registry name, or a cubic-bezier() literal");
432
+ let a = r(t);
433
+ return a ? {
434
+ fn: n,
435
+ css: a
436
+ } : { fn: n };
437
+ }, xe = 1e6, Se = class {
438
+ options;
439
+ templateFrames = [];
440
+ parsedVars = [];
441
+ frames = [];
442
+ frameId = 0;
443
+ constructor(e) {
444
+ this.options = e;
445
+ }
446
+ convertFrameStart(e) {
447
+ if (e.start.unit === "s" || e.start.unit === "ms" || !e.start.unit) {
448
+ let t = e.start.unit === "s" ? "s" : "ms", n = g(e.start.value, t);
449
+ e.start.value = n / this.options.duration * 100, e.start.unit = "%";
450
+ }
451
+ return e.start.value = h(e.start.value, 0, 100), e;
452
+ }
453
+ addFrame(e, t, n, r) {
454
+ typeof e == "number" ? e = String(e) + "%" : typeof e == "string" ? e = e : e instanceof m && (e = String(e));
455
+ let i = se(e), a = {
456
+ id: this.frameId,
457
+ start: i,
458
+ vars: t,
459
+ transform: n,
460
+ timingFunction: r == null ? this.options.timingFunction : J("timingFunction", r)
461
+ };
462
+ this.convertFrameStart(a), this.templateFrames.push(a), this.frameId += 1;
463
+ }
464
+ createFrame(e, t) {
465
+ let n = this.templateFrames[e], r = this.templateFrames[t], i = {
466
+ start: e,
467
+ stop: t
468
+ }, a = be(n, r, this.options.duration), o = n.transform;
469
+ if (o == null) {
470
+ let t = C(e, this.templateFrames, (e) => e.transform != null);
471
+ o = this.templateFrames[t].transform;
472
+ }
473
+ let s = n.timingFunction;
474
+ if (s == null) {
475
+ let t = C(e, this.templateFrames, (e) => e.timingFunction != null);
476
+ s = this.templateFrames[t].timingFunction;
477
+ }
478
+ return {
479
+ id: e * xe + t,
480
+ ixs: i,
481
+ start: n.start,
482
+ time: a,
483
+ vars: void 0,
484
+ flatVars: void 0,
485
+ interpVars: {},
486
+ allInterpVars: [],
487
+ transform: o,
488
+ timingFunction: s
489
+ };
490
+ }
491
+ buildVarIndex() {
492
+ let e = /* @__PURE__ */ new Map();
493
+ for (let t = 0; t < this.parsedVars.length; t++) for (let n of Object.keys(this.parsedVars[t])) {
494
+ let r = e.get(n);
495
+ r || (r = [], e.set(n, r)), r.push(t);
496
+ }
497
+ return e;
498
+ }
499
+ reconcileVars(e, t) {
500
+ let n = this.parsedVars[e];
501
+ if (n) for (let r of Object.keys(n)) {
502
+ let n = t.get(r);
503
+ if (!n) continue;
504
+ let i = -1;
505
+ for (let t of n) if (t > e) {
506
+ i = t;
507
+ break;
508
+ }
509
+ if (i === -1) continue;
510
+ let [a, o] = [e, i], s = this.frames.findIndex((e) => e.ixs.start === a && e.ixs.stop === o), c = s === -1 ? this.createFrame(a, o) : this.frames[s];
511
+ c.interpVars[r] = K(r, a, o, this.parsedVars, this.options.colorSpace, this.options.hueMethod), s === -1 && this.frames.push(c);
512
+ }
513
+ }
514
+ parse(e) {
515
+ this.frames = [], this.templateFrames.sort((e, t) => e.start.value - t.start.value), this.parsedVars = this.templateFrames.map((t) => {
516
+ let n = ye(t.vars);
517
+ return Object.values(n).forEach((t) => {
518
+ t.setTargets(e);
519
+ }), n;
520
+ });
521
+ for (let e = 0; e < this.templateFrames.length - 1; e++) this.frames.push(this.createFrame(e, e + 1));
522
+ let t = this.buildVarIndex();
523
+ this.frames.forEach((e, n) => this.reconcileVars(n, t)), this.frames.sort((e, t) => e.time.start === t.time.start ? e.time.stop - t.time.stop : e.time.start - t.time.start), this.frames = this.frames.filter((e) => e.interpVars != null && Object.keys(e.interpVars).length > 0), this.frames.forEach((e) => this.finalizeFrameVars(e));
524
+ }
525
+ finalizeFrameVars(e) {
526
+ e.flatVars = Object.entries(e.interpVars).reduce((e, [t, n]) => (e[t] = n.map((e) => e.value), e), {}), e.vars = de(e.flatVars), e.allInterpVars = Object.values(e.interpVars).flat();
527
+ }
528
+ renormalizeColors() {
529
+ for (let e of this.frames) {
530
+ for (let t of Object.keys(e.interpVars)) e.interpVars[t] = K(t, e.ixs.start, e.ixs.stop, this.parsedVars, this.options.colorSpace, this.options.hueMethod);
531
+ this.finalizeFrameVars(e);
532
+ }
533
+ }
534
+ }, Ce = new Set([
535
+ ...c,
536
+ "%",
537
+ "vh",
538
+ "vw",
539
+ "vmin",
540
+ "vmax",
541
+ "vi",
542
+ "vb",
543
+ "svh",
544
+ "svw",
545
+ "svmin",
546
+ "svmax",
547
+ "svi",
548
+ "svb",
549
+ "lvh",
550
+ "lvw",
551
+ "lvmin",
552
+ "lvmax",
553
+ "lvi",
554
+ "lvb",
555
+ "dvh",
556
+ "dvw",
557
+ "dvmin",
558
+ "dvmax",
559
+ "dvi",
560
+ "dvb",
561
+ "cqw",
562
+ "cqh",
563
+ "cqi",
564
+ "cqb",
565
+ "cqmin",
566
+ "cqmax"
567
+ ]), Y = (e) => typeof e == "string" && Ce.has(e), X = new Set(["offset-distance"]), we = (e) => X.has(e) || X.has(e.split(".").pop() ?? e);
568
+ function Te(e) {
569
+ if (!e.targets || e.targets.length === 0) return {
570
+ eligible: !1,
571
+ reason: "no DOM targets"
572
+ };
573
+ if (typeof e.targets[0]?.animate != "function") return {
574
+ eligible: !1,
575
+ reason: "target does not implement Element.animate()"
576
+ };
577
+ for (let t of e.frames) if (!e.usesDefaultRenderer(t.transform)) return {
578
+ eligible: !1,
579
+ reason: "custom transform function (not the default DOM renderer)"
580
+ };
581
+ let t = e.frames[0]?.timingFunction;
582
+ if (t && e.frames.length > 1) {
583
+ for (let n = 1; n < e.frames.length; n++) if (e.frames[n].timingFunction.fn !== t.fn) return {
584
+ eligible: !1,
585
+ reason: "non-uniform per-frame timing function (WAAPI supports one easing per animation)"
586
+ };
587
+ if (t.css !== void 0) return {
588
+ eligible: !1,
589
+ reason: "CSS-twinned easing across multiple segments (WAAPI restarts the curve per segment)"
590
+ };
591
+ }
592
+ if (t && t.css === void 0) return {
593
+ eligible: !1,
594
+ reason: "easing has no faithful CSS twin (would run bare linear on the compositor)"
595
+ };
596
+ for (let t of e.frames) for (let [e, n] of Object.entries(t.interpVars)) {
597
+ let t = we(e);
598
+ for (let e of n) {
599
+ let n = Y(e.start?.unit) && !(t && e.start?.unit === "%"), r = Y(e.stop?.unit) && !(t && e.stop?.unit === "%");
600
+ if (n || r) return {
601
+ eligible: !1,
602
+ reason: `layout-dependent unit (${String(e.start?.unit ?? e.stop?.unit)}) would freeze to px under WAAPI`
603
+ };
604
+ if (e.start?.unit === "color" || e.stop?.unit === "color") return {
605
+ eligible: !1,
606
+ reason: "color interpolation requires perceptual lerp"
607
+ };
608
+ }
609
+ }
610
+ return { eligible: !0 };
611
+ }
612
+ var Ee = 8;
613
+ function De(e) {
614
+ let n = e.options.duration, r = [], i = /* @__PURE__ */ new Set();
615
+ for (let t of e.frames) i.add(t.time.start), i.add(t.time.stop);
616
+ let a = [...i].sort((e, t) => e - t), o = new Set(a);
617
+ if (n > 0) for (let e = 1; e < a.length; e++) {
618
+ let t = a[e - 1], n = a[e] - t;
619
+ if (!(n <= 0)) for (let e = 1; e <= Ee; e++) o.add(t + n * e / 9);
620
+ }
621
+ for (let i of [...o].sort((e, t) => e - t)) {
622
+ let a = e.interpFrames(i, !1);
623
+ if (Object.keys(a).length === 0) continue;
624
+ let o = D(a);
625
+ r.push({
626
+ offset: t(i / n, 0, 1),
627
+ ...o
628
+ });
629
+ }
630
+ return r;
631
+ }
632
+ var Oe = {
633
+ normal: "normal",
634
+ reverse: "reverse",
635
+ alternate: "alternate",
636
+ "alternate-reverse": "alternate-reverse"
637
+ }, ke = {
638
+ none: "none",
639
+ forwards: "forwards",
640
+ backwards: "backwards",
641
+ both: "both"
642
+ };
643
+ function Ae(e) {
644
+ let t = e.options, n = Oe[t.direction], r = ke[t.fillMode];
645
+ if (n == null) throw TypeError(`Unrecognised animation direction "${t.direction}".`);
646
+ if (r == null) throw TypeError(`Unrecognised animation fill mode "${t.fillMode}".`);
647
+ let i = (e.frames[0]?.timingFunction ?? e.options.timingFunction).css ?? "linear";
648
+ return {
649
+ duration: t.duration,
650
+ delay: t.delay,
651
+ iterations: t.iterationCount === Infinity ? Infinity : t.iterationCount,
652
+ direction: n,
653
+ fill: r,
654
+ easing: i
655
+ };
656
+ }
657
+ async function je(e) {
658
+ let t = De(e), n = Ae(e), r = e.targets.map((e) => e.animate(t, n));
659
+ e._waAnimations = r, e.playback.loop(async (t) => {
660
+ if (e.done) return !1;
661
+ if (await e.advanceTo(t), e.paused) for (let e of r) e.pause();
662
+ else for (let e of r) e.playState === "paused" && e.play();
663
+ return !e.done;
664
+ });
665
+ try {
666
+ await Promise.all(r.map((e) => e.finished));
667
+ for (let t of r) try {
668
+ e.restPosition === "final" && typeof t.commitStyles == "function" && t.commitStyles(), t.cancel();
669
+ } catch {}
670
+ } catch {} finally {
671
+ e.playback.stop(), e._waAnimations = [];
672
+ }
673
+ }
674
+ //#endregion
675
+ //#region src/animation/engine.ts
676
+ var Z = (e) => {
677
+ try {
678
+ let t = S(e);
679
+ return Number.isFinite(t) ? t : void 0;
680
+ } catch {
681
+ return;
682
+ }
683
+ }, Me = (e) => typeof e != "object" || !e ? !1 : typeof e.clone == "function", Q = (e) => typeof e == "string" ? e : e.name ?? String(e.id), Ne = 0, $ = class {
684
+ id = Ne++;
685
+ name;
686
+ superKey;
687
+ targets;
688
+ options;
689
+ compiler;
690
+ playback = new a();
691
+ _waAnimations = [];
692
+ startTime = void 0;
693
+ pausedTime = 0;
694
+ t = 0;
695
+ iteration = 0;
696
+ started = !1;
697
+ done = !1;
698
+ reversed = !1;
699
+ paused = !1;
700
+ managed = !1;
701
+ waapiIneligibleReason = void 0;
702
+ unflatten = !0;
703
+ resolvePromise = null;
704
+ _playingPromise = null;
705
+ _boundFrame = this._frame.bind(this);
706
+ _interpOut = {};
707
+ _stableKeys = [];
708
+ _ctorOptions = {};
709
+ get flatKeys() {
710
+ return this._stableKeys;
711
+ }
712
+ _defaultTransform = (e) => q(e, this.targets);
713
+ usesDefaultRenderer(e) {
714
+ return e === this._defaultTransform;
715
+ }
716
+ dispatchAnimationEvent(e) {
717
+ if (!(typeof AnimationEvent > "u")) for (let t of this.targets) typeof t?.dispatchEvent == "function" && t.dispatchEvent(new AnimationEvent(e, {
718
+ animationName: this.name ?? "",
719
+ elapsedTime: this.t / 1e3
720
+ }));
721
+ }
722
+ constructor(e, t, n, r) {
723
+ this.options = {}, this.compiler = new Se(this.options), this._ctorOptions = e ?? {}, this.setOptions({
724
+ ...P,
725
+ ...this._ctorOptions
726
+ }), this.targets = t == null ? [] : Array.isArray(t) ? t : [t], this.name = n, this.superKey = r;
727
+ }
728
+ get templateFrames() {
729
+ return this.compiler.templateFrames;
730
+ }
731
+ set templateFrames(e) {
732
+ this.compiler.templateFrames = e;
733
+ }
734
+ get parsedVars() {
735
+ return this.compiler.parsedVars;
736
+ }
737
+ get frames() {
738
+ return this.compiler.frames;
739
+ }
740
+ get frameId() {
741
+ return this.compiler.frameId;
742
+ }
743
+ addFrame(e, t, n, r) {
744
+ return this.compiler.addFrame(e, t, n, r), this;
745
+ }
746
+ parse() {
747
+ return this.compiler.parse(this.targets), this.computeStableKeys(), this;
748
+ }
749
+ computeStableKeys() {
750
+ let e = /* @__PURE__ */ new Set();
751
+ for (let t of this.frames) for (let n in t.flatVars) e.add(n);
752
+ this._stableKeys = [...e];
753
+ }
754
+ setTimingFunction(e) {
755
+ return this.options.timingFunction = e == null ? P.timingFunction : J("timingFunction", e), this;
756
+ }
757
+ setIterationCount(t) {
758
+ return t == null ? (this.options.iterationCount = P.iterationCount, this) : t === "infinite" || t === "∞" || t === "Infinity" || t === Infinity ? (this.options.iterationCount = Infinity, this) : (this.options.iterationCount = e("iterationCount", t, (e) => {
759
+ let t = typeof e == "string" ? Number.parseFloat(e.trim()) : e;
760
+ return typeof t == "number" && !Number.isNaN(t) && t >= 0 ? t : void 0;
761
+ }, "expected a non-negative count, \"infinite\", or Infinity"), this);
762
+ }
763
+ setDuration(t) {
764
+ if (t == null) return this;
765
+ let n = e("duration", t, (e) => {
766
+ let t = typeof e == "string" ? Z(e) : e;
767
+ return typeof t == "number" && isFinite(t) && t > 0 ? t : void 0;
768
+ }, "expected a positive duration in milliseconds or a CSS time string"), r = n / this.options.duration;
769
+ for (let e = 0; e < this.frames.length; e++) {
770
+ let t = this.frames[e];
771
+ t.time.start *= r, t.time.stop *= r;
772
+ }
773
+ return this.options.duration = n, this;
774
+ }
775
+ setDelay(t) {
776
+ return t == null ? (this.options.delay = P.delay, this) : (this.options.delay = e("delay", t, (e) => {
777
+ let t = typeof e == "string" ? Z(e) : e;
778
+ return typeof t == "number" && isFinite(t) ? t : void 0;
779
+ }, "expected a delay in milliseconds or a CSS time string"), this);
780
+ }
781
+ setDirection(e) {
782
+ if (e == null) e = P.direction;
783
+ else if (!A.includes(e)) throw new i("direction", e, `expected one of: ${A.join(", ")}`);
784
+ return this.options.direction = e, this.reversed = !1, (this.options.direction === "reverse" || this.options.direction === "alternate-reverse" && this.iteration % 2 == 0 || this.options.direction === "alternate" && this.iteration % 2 == 1) && (this.reversed = !0), this;
785
+ }
786
+ setFillMode(e) {
787
+ if (e == null) e = P.fillMode;
788
+ else if (!j.includes(e)) throw new i("fillMode", e, `expected one of: ${j.join(", ")}`);
789
+ return this.options.fillMode = e, this;
790
+ }
791
+ setUseWAAPI(e) {
792
+ if (e == null) return this.options.useWAAPI = P.useWAAPI, this;
793
+ if (typeof e != "boolean") throw new i("useWAAPI", e, "expected a boolean");
794
+ return this.options.useWAAPI = e, this;
795
+ }
796
+ setRespectReducedMotion(e) {
797
+ if (e == null) return this.options.respectReducedMotion = P.respectReducedMotion, this;
798
+ if (typeof e != "boolean") throw new i("respectReducedMotion", e, "expected a boolean");
799
+ return this.options.respectReducedMotion = e, this;
800
+ }
801
+ setColorSpace(e) {
802
+ if (e == null) return this.options.colorSpace = P.colorSpace, this;
803
+ if (!M.includes(e)) throw new i("colorSpace", e, `expected one of: ${M.join(", ")}`);
804
+ return this.options.colorSpace = e, this.frames.length > 0 && this.compiler.renormalizeColors(), this;
805
+ }
806
+ setHueMethod(e) {
807
+ if (e == null) return this;
808
+ if (!N.includes(e)) throw new i("hueMethod", e, `expected one of: ${N.join(", ")}`);
809
+ return this.options.hueMethod = e, this.frames.length > 0 && this.compiler.renormalizeColors(), this;
810
+ }
811
+ setOptions(e) {
812
+ return this.setTimingFunction(e.timingFunction), this.setDuration(e.duration), this.setIterationCount(e.iterationCount), this.setDelay(e.delay), this.setDirection(e.direction), this.setFillMode(e.fillMode), this.setUseWAAPI(e.useWAAPI), this.setRespectReducedMotion(e.respectReducedMotion), this.setColorSpace(e.colorSpace), this.setHueMethod(e.hueMethod), this;
813
+ }
814
+ reverse() {
815
+ if (this.startTime !== void 0) {
816
+ let e = this.t, t = this.options.duration - 2 * e;
817
+ this.startTime -= t;
818
+ }
819
+ return this.reversed = !this.reversed, this;
820
+ }
821
+ fillForwards() {
822
+ this.interpFrames(this.options.duration, !0);
823
+ }
824
+ fillBackwards() {
825
+ this.interpFrames(0, !0);
826
+ }
827
+ get restPosition() {
828
+ return this.options.fillMode === "forwards" || this.options.fillMode === "both" ? "final" : "initial";
829
+ }
830
+ paintRest() {
831
+ this.restPosition === "final" ? this.fillForwards() : this.fillBackwards();
832
+ }
833
+ at(e, t = !1) {
834
+ let n = this.reversed;
835
+ this.reversed = !1;
836
+ let r = h(e, 0, 1) * this.options.duration, i = this.interpFrames(r, t);
837
+ return this.reversed = n, i;
838
+ }
839
+ interpFrames(e, t = !1, r) {
840
+ e = this.reversed ? this.options.duration - e : e;
841
+ let i = this.frames, a = i.length, o = n(i, e, (e) => e.time.start, (e) => e.time.stop);
842
+ if (o === -1) return r === void 0 ? {} : (this.clearBuffer(r), r);
843
+ let s = o, c = o;
844
+ for (let t = o - 1; t >= 0; t--) {
845
+ let n = i[t];
846
+ if (e < n.time.start || e > n.time.stop) break;
847
+ s = t;
848
+ }
849
+ for (let t = o + 1; t < a; t++) {
850
+ let n = i[t];
851
+ if (e < n.time.start || e > n.time.stop) break;
852
+ c = t;
853
+ }
854
+ for (let n = s; n <= c; n++) this.processFrame(i[n], e, t);
855
+ if (s === c) {
856
+ let e = i[o].flatVars;
857
+ return r === void 0 ? e : (this.clearBuffer(r), Object.assign(r, e), r);
858
+ }
859
+ let l = r ?? {};
860
+ r !== void 0 && this.clearBuffer(r);
861
+ for (let e = s; e <= c; e++) Object.assign(l, i[e].flatVars);
862
+ return l;
863
+ }
864
+ clearBuffer(e) {
865
+ let t = this._stableKeys;
866
+ for (let n = 0; n < t.length; n++) e[t[n]] = void 0;
867
+ }
868
+ processFrame(e, t, n) {
869
+ let { start: r, stop: i } = e.time, a = r === i ? 1 : le(t, r, i, 0, 1), o = e.timingFunction.fn(a);
870
+ for (let t of e.allInterpVars) ae(o, t);
871
+ n && e.transform(this.unflatten ? e.vars : e.flatVars, t);
872
+ }
873
+ async onStart() {
874
+ this.reversed = !1, (this.options.direction === "reverse" || this.options.direction === "alternate-reverse" && this.iteration % 2 == 0 || this.options.direction === "alternate" && this.iteration % 2 == 1) && this.reverse(), (this.options.fillMode === "backwards" || this.options.fillMode === "both") && this.fillBackwards(), this.options.delay > 0 && (this.paused = !0, await w(this.options.delay), this.paused = !1), this.started = !0;
875
+ }
876
+ async onEnd() {
877
+ this.paintRest(), this.startTime = void 0, this.iteration >= this.options.iterationCount - 1 ? (this.done = !0, this.iteration = 0, this.dispatchAnimationEvent("animationend")) : (this.iteration += 1, this.dispatchAnimationEvent("animationiteration"));
878
+ }
879
+ async advanceTo(e) {
880
+ if (this.startTime === void 0 && (await this.onStart(), this.startTime = e + this.options.delay, this.dispatchAnimationEvent("animationstart")), this.paused && this.pausedTime === 0) return this.pausedTime = e, this.t;
881
+ if (this.pausedTime > 0 && !this.paused) {
882
+ let t = e - this.pausedTime;
883
+ this.startTime += t, this.pausedTime = 0;
884
+ }
885
+ return this.t = e - this.startTime, this.t >= this.options.duration && (await this.onEnd(), this.t = this.options.duration), this.t;
886
+ }
887
+ async _frame(e) {
888
+ return o(this.options.respectReducedMotion, () => !0, () => !1) ? (await this._snapToReducedMotion(), !1) : (e = await this.advanceTo(e), this.paused ? !1 : this.done ? (this.settle(), this._resolvePlay(), !1) : (this.interpFrames(e, !0, this._interpOut), !0));
889
+ }
890
+ _resolvePlay() {
891
+ let e = this.resolvePromise;
892
+ this.resolvePromise = null, e?.();
893
+ }
894
+ _playRAF() {
895
+ return new Promise((e) => {
896
+ this.resolvePromise = e, this.playback.loop(this._boundFrame);
897
+ });
898
+ }
899
+ async _playWAAPI() {
900
+ await je(this), this.settle();
901
+ }
902
+ _cancelWAAPI() {
903
+ if (this._waAnimations.length !== 0) {
904
+ for (let e of this._waAnimations) try {
905
+ e.cancel();
906
+ } catch {}
907
+ this._waAnimations = [];
908
+ }
909
+ }
910
+ async _playReducedMotion() {
911
+ this.started = !0, this.dispatchAnimationEvent("animationstart"), this.fillForwards(), this.iteration = 0, this.done = !0, this.dispatchAnimationEvent("animationend"), this.settle();
912
+ }
913
+ async _snapToReducedMotion() {
914
+ this._cancelWAAPI(), this.fillForwards(), this.iteration = 0, this.done = !0, this.dispatchAnimationEvent("animationend"), this.settle(), this._resolvePlay();
915
+ }
916
+ async play() {
917
+ if (this.managed) throw Error("Animation.play() called on a managed animation — the AnimationGroup owns the rAF loop. Call group.play() instead.");
918
+ if (this._playingPromise) return this._playingPromise;
919
+ let e = o(this.options.respectReducedMotion, () => (this.waapiIneligibleReason = void 0, this._playReducedMotion()), () => {
920
+ if (this.options.useWAAPI) {
921
+ let e = Te(this);
922
+ return e.eligible ? (this.waapiIneligibleReason = void 0, this._playWAAPI()) : (this.waapiIneligibleReason = e.reason, this._playRAF());
923
+ }
924
+ return this.waapiIneligibleReason = void 0, this._playRAF();
925
+ });
926
+ return this._playingPromise = e, e.finally(() => {
927
+ this._playingPromise = null;
928
+ }), e;
929
+ }
930
+ pause() {
931
+ return this.started && (this.paused = !0), this;
932
+ }
933
+ resume() {
934
+ if (this.started && this.paused) if (this.paused = !1, this._waAnimations.length > 0) for (let e of this._waAnimations) e.play();
935
+ else this.playback.running || this.playback.loop(this._boundFrame);
936
+ return this;
937
+ }
938
+ toggle() {
939
+ return this.paused ? this.resume() : this.pause();
940
+ }
941
+ stop() {
942
+ this._cancelWAAPI(), this.playback.stop(), this.settle(), this._resolvePlay();
943
+ }
944
+ playing() {
945
+ return !(!this.started || this.paused);
946
+ }
947
+ get effectiveT() {
948
+ return this.reversed ? this.options.duration - this.t : this.t;
949
+ }
950
+ settle() {
951
+ return this.done = !1, this.started = !1, this.paused = !1, this.reversed = !1, this.iteration = 0, this.startTime = void 0, this.pausedTime = 0, this.t = 0, this;
952
+ }
953
+ reset() {
954
+ return this._cancelWAAPI(), this.started && this.frames.length > 0 && this.fillBackwards(), this.settle();
955
+ }
956
+ setTargets(...e) {
957
+ return this.targets = e, this.frames.forEach((e) => {
958
+ Object.values(e.interpVars).forEach((e) => {
959
+ e.forEach(({ start: e, stop: t, value: n }) => {
960
+ e.setTargets(this.targets), t.setTargets(this.targets), n.setTargets(this.targets);
961
+ });
962
+ });
963
+ }), this;
964
+ }
965
+ group(...e) {
966
+ return new B(this, ...e);
967
+ }
968
+ }, Pe = class extends $ {
969
+ constructor(e, ...t) {
970
+ super(e, t), this.unflatten = !1;
971
+ }
972
+ resolveTransform(e) {
973
+ return this.unflatten = e != null, e ?? this._defaultTransform;
974
+ }
975
+ fromVars(e, t) {
976
+ t = this.resolveTransform(t);
977
+ for (let n = 0; n < e.length; n++) {
978
+ let r = e[n], i = Math.round(n / (e.length - 1) * 100);
979
+ this.addFrame(i, r, t);
980
+ }
981
+ return this.parse(), this;
982
+ }
983
+ fromKeyframes(e, t) {
984
+ t = this.resolveTransform(t), ne(e) && (e = new Map(Object.entries(e)));
985
+ let n = e instanceof Map ? e.entries() : Object.entries(e);
986
+ for (let [e, r] of n) this.addFrame(e, r, t);
987
+ return this.parse(), this;
988
+ }
989
+ propertyRegistry = /* @__PURE__ */ new Map();
990
+ fromString(e, t) {
991
+ t = this.resolveTransform(t);
992
+ let n = k(e);
993
+ this.propertyRegistry = n.properties;
994
+ let i = n.options, a = {};
995
+ i.duration != null && (a.duration = i.duration), i.timingFunction != null && (a.timingFunction = i.timingFunction), i.iterationCount !== void 0 && (a.iterationCount = i.iterationCount === null ? Infinity : i.iterationCount), i.direction != null && (a.direction = i.direction), i.delay != null && (a.delay = i.delay), i.fillMode != null && (a.fillMode = i.fillMode), Object.keys(a).length > 0 && this.setOptions({
996
+ ...a,
997
+ ...this._ctorOptions
998
+ });
999
+ for (let [e, i] of n.keyframes.entries()) {
1000
+ let a = Object.fromEntries(Object.entries(i).map(([e, t]) => [e, Me(t) ? t.clone() : t])), o = n.timingFunctions.get(e), s = o ? W(o) : void 0, c;
1001
+ if (s) {
1002
+ let e = o ? r(o) : void 0;
1003
+ c = e ? {
1004
+ fn: s,
1005
+ css: e
1006
+ } : { fn: s };
1007
+ }
1008
+ this.addFrame(e, a, t, c);
1009
+ }
1010
+ return this.parse(), this.registerProperties(), this;
1011
+ }
1012
+ registerProperties() {
1013
+ if (!(typeof CSS > "u" || typeof CSS.registerProperty != "function")) for (let [e, t] of this.propertyRegistry) {
1014
+ if (t.syntax == null) continue;
1015
+ let n = {
1016
+ name: e,
1017
+ syntax: t.syntax,
1018
+ inherits: t.inherits ?? !1
1019
+ };
1020
+ t.initialValue != null && (n.initialValue = t.initialValue.toString());
1021
+ try {
1022
+ CSS.registerProperty(n);
1023
+ } catch {}
1024
+ }
1025
+ }
1026
+ transform(e) {
1027
+ q(e, this.targets);
1028
+ }
1029
+ };
1030
+ //#endregion
1031
+ export { $ as Animation, B as AnimationGroup, Pe as CSSKeyframesAnimation, A as DIRECTIONS, j as FILL_MODES, F as defaultLayerConfig, P as defaultOptions, Q as getAnimationId, W as getTimingFunction, k as resolveKeyframes };