@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.
package/dist/keyframes.js CHANGED
@@ -1,70 +1,21 @@
1
- import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s } from "./css-easing-DoPNckiS.js";
2
- //#region src/animation/internal/easing-resolvable.ts
3
- var c = (e) => e, l = class {
4
- _fn;
5
- _pendingName = null;
6
- _ready = null;
7
- _onResolved;
8
- _warned = !1;
9
- constructor(e, t) {
10
- this._onResolved = t, typeof e == "function" ? this._fn = e : typeof e == "string" ? (this._fn = c, this._pendingName = e, this.ready()) : this._fn = c;
11
- }
12
- get fn() {
13
- return this._fn;
14
- }
15
- get pending() {
16
- return this._pendingName !== null;
17
- }
18
- ready() {
19
- if (this._pendingName === null) return Promise.resolve();
20
- if (this._ready) return this._ready;
21
- let e = this._pendingName;
22
- return this._ready = import("./engine-B7JGZTdU.js").then((t) => {
23
- let n = t.getTimingFunction(e);
24
- n && (this._fn = n, this._onResolved?.(n)), this._pendingName = null;
25
- }), this._ready;
26
- }
27
- warnIfPending(e) {
28
- this._pendingName !== null && !this._warned && (this._warned = !0, console.warn(`[keyframes] ${e}: easing "${this._pendingName}" is still resolving — interpolating with the identity fallback until it lands. \`await .ready()\` before the first synchronous \`.at()\` to interpolate with the named curve from the first frame.`));
29
- }
30
- }, u = class {
31
- _rafId = null;
32
- _startTime = void 0;
33
- _resolve = null;
34
- play(t, n, r) {
35
- if (t <= 0) throw Error("RAFPlayback.play() requires a duration > 0.");
36
- return this.stop(), r?.respectReducedMotion && o() ? (n(1), Promise.resolve()) : new Promise((r) => {
37
- this._resolve = r, this._startTime = void 0;
38
- let i = (r) => {
39
- this._startTime === void 0 && (this._startTime = r);
40
- let a = e((r - this._startTime) / t, 0, 1);
41
- n(a), a < 1 ? this._rafId = s(i) : this._cleanup();
42
- };
43
- this._rafId = s(i);
44
- });
45
- }
46
- stop() {
47
- this._rafId !== null && n(this._rafId), this._cleanup();
48
- }
49
- _cleanup() {
50
- this._rafId = null, this._startTime = void 0;
51
- let e = this._resolve;
52
- this._resolve = null, e?.();
53
- }
54
- }, d = class {
1
+ import { a as e, c as t, d as n, g as r, h as i, i as a, l as o, m as s, n as c, o as l, p as u, r as d, t as f, u as p } from "./timeline-5rnU0j6r.js";
2
+ import { n as m, r as h, t as g } from "./springTimingFunction-BgkJPP-_.js";
3
+ //#region src/animation/numeric.ts
4
+ var _ = class {
55
5
  keyframes;
56
6
  segments;
57
7
  positions;
58
8
  result;
59
9
  _duration;
60
10
  _respectReducedMotion;
61
- _easing;
62
- _playback = new u();
11
+ _easingFn;
12
+ _playback = new l();
63
13
  constructor(e, t) {
64
14
  if (e.length < 2) throw Error("NumericAnimation requires at least 2 keyframes.");
65
- if (this.keyframes = e.map((e) => ({ ...e })), this._duration = t?.duration ?? 0, this._respectReducedMotion = t?.respectReducedMotion ?? !1, this._easing = new l(t?.timingFunction, () => {
66
- this.segments = this.buildSegments();
67
- }), t?.positions) {
15
+ this.keyframes = e.map((e) => ({ ...e })), this._duration = t?.duration ?? 0, this._respectReducedMotion = t?.respectReducedMotion ?? !1;
16
+ let n = t?.timingFunction;
17
+ if (typeof n == "string") throw new i("timingFunction", n, "NumericAnimation accepts a callable TimingFunction or a typed Easing; resolve a string name first via `await resolveEasing(name)`.");
18
+ if (this._easingFn = n == null ? (e) => e : s(n).fn, t?.positions) {
68
19
  if (t.positions.length !== e.length) throw Error("positions length must match keyframes length.");
69
20
  this.positions = t.positions;
70
21
  } else this.positions = e.map((t, n) => n / (e.length - 1) * 100);
@@ -83,18 +34,14 @@ var c = (e) => e, l = class {
83
34
  keys: r,
84
35
  startVals: r.map((e) => t[e]),
85
36
  stopVals: r.map((e) => n[e]),
86
- timingFunction: this._easing.fn
37
+ timingFunction: this._easingFn
87
38
  };
88
39
  }
89
- ready() {
90
- return this._easing.ready();
91
- }
92
- at(n) {
93
- this._easing.warnIfPending("NumericAnimation.at()");
94
- let i = e(n, 0, 1) * 100, o = r(this.segments, i, (e) => e.startPos, (e) => e.stopPos);
95
- o === -1 && (o = this.segments.length - 1);
96
- let s = this.segments[o], c = t(e(i, s.startPos, s.stopPos), s.startPos, s.stopPos, 0, 1), l = s.timingFunction(c);
97
- for (let e = 0; e < s.keys.length; e++) this.result[s.keys[e]] = a(s.startVals[e], s.stopVals[e], l);
40
+ at(e) {
41
+ let r = t(e, 0, 1) * 100, i = n(this.segments, r, (e) => e.startPos, (e) => e.stopPos);
42
+ i === -1 && (i = this.segments.length - 1);
43
+ let a = this.segments[i], s = p(t(r, a.startPos, a.stopPos), a.startPos, a.stopPos, 0, 1), c = a.timingFunction(s);
44
+ for (let e = 0; e < a.keys.length; e++) this.result[a.keys[e]] = o(a.startVals[e], a.stopVals[e], c);
98
45
  return this.result;
99
46
  }
100
47
  updateKeyframe(e, t) {
@@ -102,7 +49,6 @@ var c = (e) => e, l = class {
102
49
  return Object.assign(this.keyframes[e], t), e > 0 && (this.segments[e - 1] = this.buildSegment(e - 1)), e < this.keyframes.length - 1 && (this.segments[e] = this.buildSegment(e)), this;
103
50
  }
104
51
  async play(e, t) {
105
- await this.ready();
106
52
  let n = t ?? this._duration;
107
53
  await this._playback.play(n, (t) => {
108
54
  let n = this.at(t);
@@ -112,271 +58,7 @@ var c = (e) => e, l = class {
112
58
  stop() {
113
59
  this._playback.stop();
114
60
  }
115
- }, f = {
116
- damping: .1,
117
- snapThreshold: .001,
118
- targetEpsilon: 0,
119
- initial: 0,
120
- clamp: !0,
121
- respectReducedMotion: !1
122
- }, p = class {
123
- options;
124
- targetValue;
125
- currentValue;
126
- isSettled;
127
- _rafId = null;
128
- _lastFrameT = 0;
129
- _onFrame = void 0;
130
- constructor(e) {
131
- this.options = {
132
- ...f,
133
- ...e
134
- }, this.targetValue = this.options.initial, this.currentValue = this.options.initial, this.isSettled = !0;
135
- }
136
- get target() {
137
- return this.targetValue;
138
- }
139
- get current() {
140
- return this.currentValue;
141
- }
142
- get settled() {
143
- return this.isSettled;
144
- }
145
- setTarget(e) {
146
- this.options.clamp && (e = Math.max(0, Math.min(1, e)));
147
- let t = Math.abs(e - this.targetValue);
148
- if (t > 0 && t >= this.options.targetEpsilon) {
149
- if (this.targetValue = e, this.options.respectReducedMotion && o()) {
150
- this.currentValue = e, this.isSettled = !0, this._onFrame?.(e);
151
- return;
152
- }
153
- this.isSettled = !1, this._onFrame && this._rafId === null && this._startLoop();
154
- }
155
- }
156
- tick() {
157
- return this.isSettled ? this.currentValue : (this.currentValue += (this.targetValue - this.currentValue) * this.options.damping, Math.abs(this.targetValue - this.currentValue) < this.options.snapThreshold && (this.currentValue = this.targetValue, this.isSettled = !0), this.options.clamp && (this.currentValue = Math.max(0, Math.min(1, this.currentValue))), this.currentValue);
158
- }
159
- tickDt(e) {
160
- if (this.isSettled) return this.currentValue;
161
- let t = 1 - Math.exp(-this.options.damping * e / 16.667);
162
- return this.currentValue += (this.targetValue - this.currentValue) * t, Math.abs(this.targetValue - this.currentValue) < this.options.snapThreshold && (this.currentValue = this.targetValue, this.isSettled = !0), this.options.clamp && (this.currentValue = Math.max(0, Math.min(1, this.currentValue))), this.currentValue;
163
- }
164
- snap() {
165
- this.currentValue = this.targetValue, this.isSettled = !0, this._stopLoop();
166
- }
167
- reset(e) {
168
- let t = e ?? 0;
169
- this.targetValue = t, this.currentValue = t, this.isSettled = !0, this._stopLoop();
170
- }
171
- play(e) {
172
- if (this._onFrame = e, this.options.respectReducedMotion && o()) {
173
- this.currentValue = this.targetValue, this.isSettled = !0, e?.(this.currentValue);
174
- return;
175
- }
176
- if (this.isSettled) {
177
- e?.(this.currentValue);
178
- return;
179
- }
180
- this._startLoop();
181
- }
182
- stop() {
183
- this._onFrame = void 0, this._stopLoop();
184
- }
185
- _startLoop() {
186
- if (this._rafId !== null) return;
187
- this._lastFrameT = 0;
188
- let e = (t) => {
189
- let n = this._lastFrameT ? t - this._lastFrameT : 16.667;
190
- this._lastFrameT = t;
191
- let r = this.tickDt(n);
192
- if (this._onFrame?.(r), this.isSettled) {
193
- this._stopLoop();
194
- return;
195
- }
196
- this._rafId = s(e);
197
- };
198
- this._rafId = s(e);
199
- }
200
- _stopLoop() {
201
- this._rafId !== null && (n(this._rafId), this._rafId = null), this._lastFrameT = 0;
202
- }
203
- }, m = {
204
- response: .5,
205
- dampingFraction: .86,
206
- initial: 0,
207
- initialVelocity: 0,
208
- settleThreshold: .001,
209
- velocitySettleThreshold: .001,
210
- respectReducedMotion: !1
211
- }, h = class {
212
- options;
213
- targetValue;
214
- currentValue;
215
- currentVelocity;
216
- isSettled;
217
- elapsed;
218
- originValue;
219
- originVelocity;
220
- omega;
221
- zeta;
222
- omegaD;
223
- subscribers = /* @__PURE__ */ new Set();
224
- disposed = !1;
225
- _rafId = null;
226
- _lastFrameT = 0;
227
- _onFrame = void 0;
228
- constructor(e) {
229
- this.options = {
230
- ...m,
231
- ...e
232
- }, this.targetValue = this.options.initial, this.currentValue = this.options.initial, this.currentVelocity = this.options.initialVelocity, this.isSettled = this.options.initialVelocity === 0, this.elapsed = 0, this.originValue = this.currentValue, this.originVelocity = this.currentVelocity, this.omega = 2 * Math.PI / this.options.response, this.zeta = this.options.dampingFraction, this.omegaD = this.zeta < 1 ? this.omega * Math.sqrt(1 - this.zeta * this.zeta) : 0;
233
- }
234
- get target() {
235
- return this.targetValue;
236
- }
237
- get value() {
238
- return this.currentValue;
239
- }
240
- get velocity() {
241
- return this.currentVelocity;
242
- }
243
- get settled() {
244
- return this.isSettled;
245
- }
246
- set target(e) {
247
- e === this.targetValue && !this.options.initialVelocity || this.reseatTarget(e);
248
- }
249
- reseatTarget(e) {
250
- if (this.targetValue = e, this.options.respectReducedMotion && o()) {
251
- this.currentValue = e, this.currentVelocity = 0, this.isSettled = !0, this.originValue = e, this.originVelocity = 0, this.elapsed = 0, this.emit(), this._stopLoop();
252
- return;
253
- }
254
- this.originValue = this.currentValue, this.originVelocity = this.currentVelocity, this.elapsed = 0, this.isSettled = !1, this._onFrame && this._rafId === null && this._startLoop();
255
- }
256
- tick(e) {
257
- return this.disposed || this.isSettled || e <= 0 ? this.currentValue : (this.elapsed += e, this.evaluateAt(this.elapsed), this.checkSettled(), this.emit(), this.currentValue);
258
- }
259
- tickToTime(e) {
260
- return this.disposed || this.isSettled ? this.currentValue : (this.elapsed = Math.max(0, e), this.evaluateAt(this.elapsed), this.checkSettled(), this.emit(), this.currentValue);
261
- }
262
- evaluateAt(e) {
263
- let t = this.originValue - this.targetValue, n = this.originVelocity, r = this.omega, i = this.zeta, a, o;
264
- if (i < 1) {
265
- let s = this.omegaD, c = Math.exp(-i * r * e), l = t, u = (n + i * r * t) / s, d = Math.cos(s * e), f = Math.sin(s * e);
266
- a = c * (l * d + u * f), o = c * ((u * s - l * i * r) * d - (l * s + u * i * r) * f);
267
- } else if (i === 1) {
268
- let i = Math.exp(-r * e), s = t, c = n + r * t;
269
- a = i * (s + c * e), o = i * (c - r * (s + c * e));
270
- } else {
271
- let s = r * Math.sqrt(i * i - 1), c = -i * r + s, l = -i * r - s, u = (n - l * t) / (c - l), d = t - u, f = Math.exp(c * e), p = Math.exp(l * e);
272
- a = u * f + d * p, o = u * c * f + d * l * p;
273
- }
274
- this.currentValue = this.targetValue + a, this.currentVelocity = o;
275
- }
276
- checkSettled() {
277
- let e = Math.abs(this.currentValue - this.targetValue), t = Math.abs(this.currentVelocity);
278
- e < this.options.settleThreshold && t < this.options.velocitySettleThreshold && (this.currentValue = this.targetValue, this.currentVelocity = 0, this.isSettled = !0);
279
- }
280
- snap() {
281
- this.currentValue = this.targetValue, this.currentVelocity = 0, this.originValue = this.targetValue, this.originVelocity = 0, this.elapsed = 0, this.isSettled = !0, this.emit(), this._stopLoop();
282
- }
283
- reset(e, t) {
284
- let n = e ?? 0, r = t ?? 0;
285
- this.targetValue = n, this.currentValue = n, this.currentVelocity = r, this.originValue = n, this.originVelocity = r, this.elapsed = 0, this.isSettled = r === 0, this.emit(), this._stopLoop();
286
- }
287
- subscribe(e) {
288
- return this.subscribers.add(e), () => {
289
- this.subscribers.delete(e);
290
- };
291
- }
292
- emit() {
293
- if (this.subscribers.size !== 0) for (let e of this.subscribers) e(this.currentValue, this.currentVelocity);
294
- }
295
- dispose() {
296
- this.disposed = !0, this._stopLoop(), this.subscribers.clear();
297
- }
298
- play(e) {
299
- if (!this.disposed) {
300
- if (this._onFrame = e, this.options.respectReducedMotion && o()) {
301
- this.currentValue = this.targetValue, this.currentVelocity = 0, this.isSettled = !0, e?.(this.currentValue, this.currentVelocity);
302
- return;
303
- }
304
- if (this.isSettled) {
305
- e?.(this.currentValue, this.currentVelocity);
306
- return;
307
- }
308
- this._startLoop();
309
- }
310
- }
311
- stop() {
312
- this._onFrame = void 0, this._stopLoop();
313
- }
314
- _startLoop() {
315
- if (this._rafId !== null) return;
316
- this._lastFrameT = 0;
317
- let e = (t) => {
318
- let n = this._lastFrameT ? t - this._lastFrameT : 16.667;
319
- if (this._lastFrameT = t, this.tick(n / 1e3), this._onFrame?.(this.currentValue, this.currentVelocity), this.isSettled) {
320
- this._stopLoop();
321
- return;
322
- }
323
- this._rafId = s(e);
324
- };
325
- this._rafId = s(e);
326
- }
327
- _stopLoop() {
328
- this._rafId !== null && (n(this._rafId), this._rafId = null), this._lastFrameT = 0;
329
- }
330
- };
331
- //#endregion
332
- //#region src/animation/springLinearStops.ts
333
- function g(e) {
334
- let t = e.sampleCount ?? 24, n = e.settleThreshold ?? .001, r = e.maxDuration ?? e.response * 4, i = new h({
335
- response: e.response,
336
- dampingFraction: e.dampingFraction,
337
- initial: 0,
338
- settleThreshold: n,
339
- velocitySettleThreshold: n
340
- });
341
- i.target = 1;
342
- let a = ["0"], o = r / (t + 1);
343
- for (let e = 1; e <= t; e++) {
344
- i.tick(o);
345
- let n = e / (t + 1) * 100, r = i.settled ? 1 : i.value;
346
- a.push(`${r.toFixed(5)} ${n.toFixed(3)}%`);
347
- }
348
- return a.push("1"), i.dispose(), `linear(${a.join(", ")})`;
349
- }
350
- //#endregion
351
- //#region src/animation/springTimingFunction.ts
352
- function _(e) {
353
- let t = e.sampleCount ?? 64, n = e.settleThreshold ?? .001, r = e.maxDuration ?? e.response * 4, a = new h({
354
- response: e.response,
355
- dampingFraction: e.dampingFraction,
356
- initial: 0,
357
- settleThreshold: n,
358
- velocitySettleThreshold: n
359
- });
360
- a.target = 1;
361
- let o = new Float64Array(t + 1);
362
- o[0] = 0;
363
- let s = r / t;
364
- for (let e = 1; e <= t; e++) a.tick(s), o[e] = a.settled ? 1 : a.value;
365
- o[t] = 1, a.dispose();
366
- let c = (e) => {
367
- if (e <= 0) return 0;
368
- if (e >= 1) return 1;
369
- let n = e * t, r = Math.floor(n), i = n - r, a = o[r];
370
- return a + (o[r + 1] - a) * i;
371
- }, l = {
372
- response: e.response,
373
- dampingFraction: e.dampingFraction
374
- };
375
- return e.settleThreshold !== void 0 && (l.settleThreshold = e.settleThreshold), e.maxDuration !== void 0 && (l.maxDuration = e.maxDuration), i(c, g(l));
376
- }
377
- //#endregion
378
- //#region src/animation/morph.ts
379
- var v = (e) => {
61
+ }, v = (e) => {
380
62
  if (e instanceof HTMLElement) {
381
63
  let t = e.getBoundingClientRect();
382
64
  return {
@@ -397,7 +79,7 @@ var v = (e) => {
397
79
  }
398
80
  measure(e, t) {
399
81
  let n = v(e), r = v(t), i = r.x - n.x, a = r.y - n.y, o = n.width === 0 ? 1 : r.width / n.width, s = n.height === 0 ? 1 : r.height / n.height;
400
- return this.animation = new d([{
82
+ return this.animation = new _([{
401
83
  translateX: 0,
402
84
  translateY: 0,
403
85
  scaleX: 1,
@@ -412,9 +94,6 @@ var v = (e) => {
412
94
  duration: this.duration
413
95
  }), this;
414
96
  }
415
- ready() {
416
- return this.animation.ready();
417
- }
418
97
  at(e) {
419
98
  return this.animation.at(e);
420
99
  }
@@ -434,75 +113,346 @@ var v = (e) => {
434
113
  stop() {
435
114
  this.animation.stop();
436
115
  }
437
- }, b = (e) => Math.max(0, Math.min(1, e)), x = class {
438
- smoother;
439
- _easing;
440
- currentProgress = 0;
441
- boundaryEpsilon;
116
+ }, b = (e, n) => {
117
+ if (typeof e == "number") return t(e, 0, Math.max(0, n - 1));
118
+ switch (e) {
119
+ case "first": return 0;
120
+ case "last": return n - 1;
121
+ case "center": return (n - 1) / 2;
122
+ case "edges": return -1;
123
+ }
124
+ }, x = (e, t, n) => n === "edges" ? Math.min(e, t - 1 - e) : Math.abs(e - b(n, t)), S = (e, t) => {
125
+ if (e <= 1) return 0;
126
+ if (t === "edges") return Math.floor((e - 1) / 2);
127
+ let n = b(t, e);
128
+ return Math.max(n, e - 1 - n);
129
+ };
130
+ function C(e, t = {}) {
131
+ let { each: n = 100, from: r = "first", ease: a } = t;
132
+ if (typeof a == "string") throw new i("ease", a, "stagger accepts a callable TimingFunction or a typed Easing; resolve a string name first via `await resolveEasing(name)`.");
133
+ let o = a ? s(a).fn : void 0, c = typeof e == "number" ? e : e.length, l = (e, t) => {
134
+ if (t <= 1) return 0;
135
+ let i = S(t, r);
136
+ if (i === 0) return 0;
137
+ let a = x(e, t, r);
138
+ return o ? o(a / i) * n * i : a * n;
139
+ }, u = ((e, t = c) => l(e, t));
140
+ return u.delays = (e = c) => Array.from({ length: e }, (t, n) => l(n, e)), u;
141
+ }
142
+ //#endregion
143
+ //#region src/animation/flip.ts
144
+ var w = 300, T = (e) => {
145
+ let t = e.getBoundingClientRect();
146
+ return {
147
+ x: t.x,
148
+ y: t.y,
149
+ width: t.width,
150
+ height: t.height
151
+ };
152
+ }, E = (e, t) => {
153
+ let n = e.at(1), r = n.translateX, i = n.translateY, a = n.scaleX, o = n.scaleY, s = e.at(t);
154
+ return `translate(${s.translateX - r}px, ${s.translateY - i}px) scale(${a === 0 ? 1 : s.scaleX / a}, ${o === 0 ? 1 : s.scaleY / o})`;
155
+ }, D = (e, t, n, r) => (e.style.transformOrigin = r, e.style.transform = E(t, 0), n <= 0 ? (e.style.transform = E(t, 1), Promise.resolve()) : new l().play(n, (n) => {
156
+ e.style.transform = E(t, n);
157
+ }));
158
+ function O(e, t, n = {}) {
159
+ let { duration: r = w, timingFunction: i, transformOrigin: a = "top left" } = n, o = T(e);
160
+ return t(), D(e, new y(o, T(e), {
161
+ ...i === void 0 ? {} : { timingFunction: i },
162
+ transformOrigin: a
163
+ }), r, a);
164
+ }
165
+ function k(e, t, n = {}) {
166
+ let { duration: r = w, timingFunction: i, transformOrigin: a = "top left" } = n, o = new y(T(e), T(t), {
167
+ ...i === void 0 ? {} : { timingFunction: i },
168
+ duration: r,
169
+ transformOrigin: a
170
+ });
171
+ return o.apply(e, 0), r <= 0 ? (o.apply(e, 1), Promise.resolve()) : o.play(e, r);
172
+ }
173
+ //#endregion
174
+ //#region src/animation/drag.ts
175
+ var A = 100, j = class {
176
+ spring;
177
+ axis;
178
+ transform;
179
+ velocityWindow;
180
+ element;
181
+ capturedPointerId;
182
+ samples = [];
183
+ subscribers = /* @__PURE__ */ new Set();
184
+ springUnsub;
185
+ onPointerDown = (e) => this.handleDown(e);
186
+ onPointerMove = (e) => this.handleMove(e);
187
+ onPointerUp = (e) => this.handleUp(e);
442
188
  constructor(e) {
443
- this.boundaryEpsilon = e?.boundaryEpsilon ?? .005, this._easing = new l(e?.easing), e?.smoothing === !1 ? this.smoother = null : this.smoother = new p(e?.smoothing);
189
+ this.axis = e?.axis ?? "x", this.transform = e?.transform ?? ((e) => e), this.velocityWindow = e?.velocityWindow ?? A, this.spring = e?.spring ?? new h(e?.springOptions);
444
190
  }
445
- ready() {
446
- return this._easing.ready();
191
+ get dragging() {
192
+ return this.capturedPointerId !== void 0;
447
193
  }
448
- applyPipeline() {
449
- this._easing.warnIfPending("Timeline.tick()");
450
- let e = b(this.sample());
451
- e = this._easing.fn(e);
452
- let t = this.boundaryEpsilon;
453
- return e <= t ? e = 0 : e >= 1 - t && (e = 1), e;
194
+ get value() {
195
+ return this.spring.value;
454
196
  }
455
- finalizeProgress(e) {
456
- return this.smoother && (this.smoother.setTarget(e), (e <= 0 || e >= 1) && this.smoother.snap()), this.currentProgress = this.smoother ? this.smoother.current : e, this.currentProgress;
197
+ get velocity() {
198
+ return this.spring.velocity;
457
199
  }
458
- _advance(e) {
459
- let t = this.applyPipeline();
460
- return this.smoother && t > 0 && t < 1 ? (this.smoother.setTarget(t), e === void 0 ? this.smoother.tick() : this.smoother.tickDt(e), this.currentProgress = this.smoother.current) : this.finalizeProgress(t), this.currentProgress;
200
+ get settled() {
201
+ return this.spring.settled;
461
202
  }
462
- tick() {
463
- return this._advance();
203
+ attach(e) {
204
+ return this.detach(), this.element = e, e.addEventListener("pointerdown", this.onPointerDown), () => this.detach();
464
205
  }
465
- tickDt(e) {
466
- return this._advance(e);
206
+ detach() {
207
+ let e = this.element;
208
+ e && (e.removeEventListener("pointerdown", this.onPointerDown), this.endGesture(e), this.element = void 0);
467
209
  }
468
- get progress() {
469
- return this.currentProgress;
210
+ subscribe(e) {
211
+ return this.subscribers.add(e), this.springUnsub ||= this.spring.subscribe((e, t) => this.emit(e, t)), () => {
212
+ this.subscribers.delete(e), this.subscribers.size === 0 && (this.springUnsub?.(), this.springUnsub = void 0);
213
+ };
470
214
  }
471
- get settled() {
472
- return this.smoother == null || this.smoother.settled;
215
+ emit(e, t) {
216
+ for (let n of this.subscribers) n(e, t);
473
217
  }
474
- snap() {
475
- this.smoother?.snap(), this.smoother && (this.currentProgress = this.smoother.current);
218
+ dispose() {
219
+ this.detach(), this.springUnsub?.(), this.springUnsub = void 0, this.subscribers.clear(), this.spring.dispose();
220
+ }
221
+ coordOf(e) {
222
+ return this.transform(this.axis === "x" ? e.clientX : e.clientY);
223
+ }
224
+ handleDown(e) {
225
+ let t = this.element;
226
+ if (!t || this.dragging) return;
227
+ this.capturedPointerId = e.pointerId, t.setPointerCapture?.(e.pointerId), t.addEventListener("pointermove", this.onPointerMove), t.addEventListener("pointerup", this.onPointerUp), t.addEventListener("pointercancel", this.onPointerUp);
228
+ let n = this.coordOf(e);
229
+ this.spring.reset(n, 0), this.samples = [{
230
+ value: n,
231
+ t: e.timeStamp
232
+ }];
233
+ }
234
+ handleMove(e) {
235
+ if (e.pointerId !== this.capturedPointerId) return;
236
+ let t = this.coordOf(e);
237
+ this.spring.target = t, this.pushSample(t, e.timeStamp);
238
+ }
239
+ handleUp(e) {
240
+ if (e.pointerId !== this.capturedPointerId) return;
241
+ let t = this.element, n = this.coordOf(e);
242
+ this.pushSample(n, e.timeStamp);
243
+ let r = this.estimateVelocity();
244
+ t && this.endGesture(t), this.spring.reset(n, r);
245
+ }
246
+ endGesture(e) {
247
+ this.capturedPointerId !== void 0 && e.releasePointerCapture?.(this.capturedPointerId), e.removeEventListener("pointermove", this.onPointerMove), e.removeEventListener("pointerup", this.onPointerUp), e.removeEventListener("pointercancel", this.onPointerUp), this.capturedPointerId = void 0;
248
+ }
249
+ pushSample(e, t) {
250
+ this.samples.push({
251
+ value: e,
252
+ t
253
+ });
254
+ let n = t - this.velocityWindow, r = 0;
255
+ for (; r < this.samples.length - 1 && this.samples[r].t < n;) r++;
256
+ r > 0 && this.samples.splice(0, r);
476
257
  }
477
- reset(e) {
478
- let t = e ?? 0;
479
- this.smoother && this.smoother.reset(t), this.currentProgress = t;
258
+ estimateVelocity() {
259
+ let e = this.samples.length;
260
+ if (e < 2) return 0;
261
+ let t = this.samples[0], n = this.samples[e - 1], r = n.t - t.t;
262
+ return r <= 0 ? 0 : (n.value - t.value) / r * 1e3;
480
263
  }
481
- }, S = class extends x {
482
- threshold;
483
- getScrollY;
484
- getViewportHeight;
264
+ };
265
+ function M(e, t) {
266
+ let n = new j(t);
267
+ return n.attach(e), n;
268
+ }
269
+ //#endregion
270
+ //#region src/animation/decay.ts
271
+ var N = 5;
272
+ function P(e) {
273
+ let t = e.initial ?? 0, n = e.velocity, r = e.friction ?? N;
274
+ if (r <= 0) throw Error("decay() requires a friction coefficient > 0.");
275
+ let i = t + n / r;
276
+ return (e) => {
277
+ if (e <= 0) return {
278
+ value: t,
279
+ velocity: n
280
+ };
281
+ let a = Math.exp(-r * e);
282
+ return {
283
+ value: i - n / r * a,
284
+ velocity: n * a
285
+ };
286
+ };
287
+ }
288
+ function F(e) {
289
+ let t = e.friction ?? N;
290
+ if (t <= 0) throw Error("decayRest() requires a friction coefficient > 0.");
291
+ return (e.initial ?? 0) + e.velocity / t;
292
+ }
293
+ //#endregion
294
+ //#region src/animation/sequence.ts
295
+ var I = class {
296
+ entries = [];
297
+ labels = /* @__PURE__ */ new Map();
298
+ cursor = 0;
299
+ _time = 0;
300
+ respectReducedMotion;
301
+ playback = new l();
302
+ _boundFrame;
303
+ _resolvePlay = null;
304
+ _playOrigin = void 0;
305
+ _playingPromise = null;
306
+ _rate = 1;
307
+ _repeatCount = 1;
308
+ _yoyoOn = !1;
309
+ _paused = !1;
310
+ _lastClock = void 0;
485
311
  constructor(e) {
486
- super(e), this.threshold = e?.threshold ?? .35, this.getScrollY = e?.getScrollY ?? (() => window.scrollY), this.getViewportHeight = e?.getViewportHeight ?? (() => window.innerHeight);
312
+ this.respectReducedMotion = e?.respectReducedMotion ?? !1, this._boundFrame = this._frame.bind(this);
487
313
  }
488
- sample() {
489
- let e = this.getViewportHeight() * this.threshold;
490
- return e <= 0 ? 0 : this.getScrollY() / e;
314
+ get duration() {
315
+ let e = 0;
316
+ for (let t of this.entries) {
317
+ let n = t.at + t.animation.options.duration;
318
+ n > e && (e = n);
319
+ }
320
+ return e;
491
321
  }
492
- }, C = class extends x {
493
- value = 0;
494
- constructor(e) {
495
- super({
496
- smoothing: !1,
497
- ...e
322
+ get time() {
323
+ return this._time;
324
+ }
325
+ get progress() {
326
+ return this.duration === 0 ? 0 : this._time / this.duration;
327
+ }
328
+ set progress(e) {
329
+ this.seek(t(e, 0, 1) * this.duration);
330
+ }
331
+ get rate() {
332
+ return this._rate;
333
+ }
334
+ label(e, t) {
335
+ return this.labels.set(e, this.resolvePosition(t)), this;
336
+ }
337
+ add(e, t) {
338
+ let n = this.resolvePosition(t);
339
+ return this.entries.push({
340
+ animation: e,
341
+ at: n
342
+ }), this.entries.sort((e, t) => e.at - t.at), this.cursor = n + e.options.duration, this;
343
+ }
344
+ resolvePosition(e) {
345
+ if (e === void 0) return this.cursor;
346
+ if (typeof e == "number") return e;
347
+ let t = /^([+-])=(\d*\.?\d+)$/.exec(e);
348
+ if (t) {
349
+ let e = t[1] === "+" ? 1 : -1;
350
+ return this.cursor + e * Number.parseFloat(t[2]);
351
+ }
352
+ let n = this.labels.get(e);
353
+ if (n === void 0) throw Error(`Sequence: unknown position "${e}". Register it with .label("${e}", at) first, or pass a number / "+=n" / "-=n".`);
354
+ return n;
355
+ }
356
+ seek(e) {
357
+ return this._time = e, this._applyAt(e), this;
358
+ }
359
+ _applyAt(e) {
360
+ for (let { animation: n, at: r } of this.entries) {
361
+ let i = t(e - r, 0, n.options.duration);
362
+ n.interpFrames(i, !0);
363
+ }
364
+ }
365
+ _fold(e) {
366
+ let n = this.duration;
367
+ if (n <= 0) return 0;
368
+ let r = t(e, 0, n * this._repeatCount), i = Math.floor(r / n), a = r - i * n;
369
+ return a === 0 && i > 0 && (--i, a = n), this._yoyoOn && i % 2 == 1 ? n - a : a;
370
+ }
371
+ setTargets(...e) {
372
+ for (let { animation: t } of this.entries) t.setTargets(...e);
373
+ return this;
374
+ }
375
+ play() {
376
+ if (this._playingPromise) return this._playingPromise;
377
+ if (this.respectReducedMotion && L()) return this.seek(this.duration), Promise.resolve();
378
+ for (let { animation: e, at: t } of this.entries) e.startTime = t, e.started = !0, e.managed = !0;
379
+ this._time = 0, this._paused = !1, this._playOrigin = void 0, this._lastClock = void 0;
380
+ let e = new Promise((e) => {
381
+ this._resolvePlay = e, this.playback.loop(this._boundFrame);
498
382
  });
383
+ return this._playingPromise = e, e.finally(() => {
384
+ this._playingPromise = null;
385
+ }), e;
386
+ }
387
+ async _frame(e) {
388
+ this._playOrigin === void 0 && (this._playOrigin = this._rate === 0 ? e : e - this._time / this._rate), this._lastClock = e;
389
+ let t = (e - this._playOrigin) * this._rate, n = this.duration * this._repeatCount, r = this._rate >= 0 ? t >= n : t <= 0, i = r ? this._rate >= 0 ? this._restPhase() : 0 : this._fold(t);
390
+ if (this._time = i, this._isForwardMonotone()) for (let { animation: e, at: t } of this.entries) i >= t && i - t < e.options.duration && await e.advanceTo(i);
391
+ if (this._applyAt(i), r) {
392
+ this._settle();
393
+ let e = this._resolvePlay;
394
+ return this._resolvePlay = null, e?.(), !1;
395
+ }
396
+ return !0;
499
397
  }
500
- set(e) {
501
- this.value = e;
398
+ _isForwardMonotone() {
399
+ return this._rate === 1 && this._repeatCount === 1 && !this._yoyoOn;
502
400
  }
503
- sample() {
504
- return this.value;
401
+ _restPhase() {
402
+ let e = this.duration;
403
+ return this._yoyoOn && (this._repeatCount - 1) % 2 == 1 ? 0 : e;
505
404
  }
506
- }, w = () => import("./engine-B7JGZTdU.js");
405
+ _reanchor() {
406
+ if (!this._playingPromise) return;
407
+ let e = this._lastClock;
408
+ if (e === void 0) {
409
+ this._playOrigin = void 0;
410
+ return;
411
+ }
412
+ this._playOrigin = this._rate === 0 ? e : e - this._time / this._rate;
413
+ }
414
+ _settle() {
415
+ for (let { animation: e } of this.entries) e.managed = !1, e.started = !1, e.startTime = void 0;
416
+ this._playOrigin = void 0, this._lastClock = void 0, this._paused = !1;
417
+ }
418
+ stop() {
419
+ this.playback.stop(), this._settle();
420
+ let e = this._resolvePlay;
421
+ return this._resolvePlay = null, e?.(), this;
422
+ }
423
+ pause() {
424
+ return !this._playingPromise || this._paused ? this : (this._paused = !0, this.playback.stop(), this._playOrigin = void 0, this);
425
+ }
426
+ resume() {
427
+ return !this._playingPromise || !this._paused ? this : (this._paused = !1, this._playOrigin = void 0, this._lastClock = void 0, this.playback.loop(this._boundFrame), this);
428
+ }
429
+ timeScale(e) {
430
+ if (!Number.isFinite(e)) throw Error(`Sequence.timeScale(n): n must be a finite number, got ${e}.`);
431
+ return this._rate = e, this._reanchor(), this;
432
+ }
433
+ reverse() {
434
+ return this._rate = -this._rate, this._reanchor(), this;
435
+ }
436
+ repeat(e) {
437
+ if (e !== Infinity && (!Number.isInteger(e) || e < 1)) throw Error(`Sequence.repeat(count): count must be a positive integer or Infinity, got ${e}.`);
438
+ return this._repeatCount = e, this;
439
+ }
440
+ yoyo(e = !0) {
441
+ return this._yoyoOn = e, this;
442
+ }
443
+ }, L = () => typeof matchMedia == "function" && matchMedia("(prefers-reduced-motion: reduce)").matches, R = async () => {
444
+ let [e, t, n, r] = await Promise.all([
445
+ import("./engine-BZeJ58xD.js"),
446
+ import("./animate-DjKYCuyH.js"),
447
+ import("./motion-path-723lMAlR.js"),
448
+ import("./animations-CK1FLX_G.js")
449
+ ]);
450
+ return Object.assign({
451
+ animate: t.animate,
452
+ MotionPath: n.MotionPath,
453
+ fromMotionPath: n.fromMotionPath,
454
+ presets: r
455
+ }, e);
456
+ };
507
457
  //#endregion
508
- export { y as ElementMorph, C as ManualTimeline, d as NumericAnimation, u as RAFPlayback, S as ScrollTimeline, p as SmoothProgress, h as SpringProgress, x as Timeline, w as loadAnimationEngine, g as springLinearStops, _ as springTimingFunction };
458
+ export { i as AnimationOptionError, j as Draggable, y as ElementMorph, f as ManualTimeline, _ as NumericAnimation, l as RAFPlayback, c as ScrollTimeline, I as Sequence, e as SmoothProgress, h as SpringProgress, d as Timeline, r as UnknownEasingError, a as createNativeTimeline, P as decay, F as decayRest, M as drag, O as flip, k as flipShared, R as loadAnimationEngine, u as resolveEasing, m as springLinearStops, g as springTimingFunction, C as stagger, s as toEasing };