@mkbabb/keyframes.js 2.2.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,51 +1,21 @@
1
- import { a as e, i as t, n, o as r, r as i, t as a } from "./leaves-Bu89334e.js";
2
- //#region src/animation/playback.ts
3
- var o = class {
4
- _rafId = null;
5
- _startTime = void 0;
6
- _resolve = null;
7
- play(e, r) {
8
- if (e <= 0) throw Error("RAFPlayback.play() requires a duration > 0.");
9
- return this.stop(), new Promise((i) => {
10
- this._resolve = i, this._startTime = void 0;
11
- let a = (i) => {
12
- this._startTime === void 0 && (this._startTime = i);
13
- let o = n((i - this._startTime) / e, 0, 1);
14
- r(o), o < 1 ? this._rafId = t(a) : this._cleanup();
15
- };
16
- this._rafId = t(a);
17
- });
18
- }
19
- stop() {
20
- this._rafId !== null && a(this._rafId), this._cleanup();
21
- }
22
- _cleanup() {
23
- this._rafId = null, this._startTime = void 0;
24
- let e = this._resolve;
25
- this._resolve = null, e?.();
26
- }
27
- };
28
- //#endregion
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";
29
3
  //#region src/animation/numeric.ts
30
- function s() {
31
- return typeof window > "u" || typeof window.matchMedia != "function" ? !1 : window.matchMedia("(prefers-reduced-motion: reduce)").matches;
32
- }
33
- var c = (e) => e, l = class {
4
+ var _ = class {
34
5
  keyframes;
35
6
  segments;
36
7
  positions;
37
- timingFn;
38
8
  result;
39
9
  _duration;
40
10
  _respectReducedMotion;
41
- _pendingEasingName = null;
42
- _easingReady = null;
43
- _playback = new o();
11
+ _easingFn;
12
+ _playback = new l();
44
13
  constructor(e, t) {
45
14
  if (e.length < 2) throw Error("NumericAnimation requires at least 2 keyframes.");
46
15
  this.keyframes = e.map((e) => ({ ...e })), this._duration = t?.duration ?? 0, this._respectReducedMotion = t?.respectReducedMotion ?? !1;
47
16
  let n = t?.timingFunction;
48
- if (typeof n == "function" ? this.timingFn = n : typeof n == "string" ? (this.timingFn = c, this._pendingEasingName = n) : this.timingFn = c, t?.positions) {
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) {
49
19
  if (t.positions.length !== e.length) throw Error("positions length must match keyframes length.");
50
20
  this.positions = t.positions;
51
21
  } else this.positions = e.map((t, n) => n / (e.length - 1) * 100);
@@ -64,23 +34,14 @@ var c = (e) => e, l = class {
64
34
  keys: r,
65
35
  startVals: r.map((e) => t[e]),
66
36
  stopVals: r.map((e) => n[e]),
67
- timingFunction: this.timingFn
37
+ timingFunction: this._easingFn
68
38
  };
69
39
  }
70
- ready() {
71
- if (this._pendingEasingName === null) return Promise.resolve();
72
- if (this._easingReady) return this._easingReady;
73
- let e = this._pendingEasingName;
74
- return this._easingReady = import("./engine-DuAFoqZF.js").then((t) => {
75
- let n = t.getTimingFunction(e);
76
- n && (this.timingFn = n, this.segments = this.buildSegments()), this._pendingEasingName = null;
77
- }), this._easingReady;
78
- }
79
- at(t) {
80
- let a = n(t, 0, 1) * 100, o = r(this.segments, a, (e) => e.startPos, (e) => e.stopPos);
81
- o === -1 && (o = this.segments.length - 1);
82
- let s = this.segments[o], c = e(n(a, s.startPos, s.stopPos), s.startPos, s.stopPos, 0, 1), l = s.timingFunction(c);
83
- for (let e = 0; e < s.keys.length; e++) this.result[s.keys[e]] = i(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);
84
45
  return this.result;
85
46
  }
86
47
  updateKeyframe(e, t) {
@@ -88,289 +49,16 @@ var c = (e) => e, l = class {
88
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;
89
50
  }
90
51
  async play(e, t) {
91
- await this.ready();
92
52
  let n = t ?? this._duration;
93
- if (this._respectReducedMotion && s()) {
94
- let t = this.at(1);
95
- e?.(t);
96
- return;
97
- }
98
53
  await this._playback.play(n, (t) => {
99
54
  let n = this.at(t);
100
55
  e?.(n);
101
- });
56
+ }, { respectReducedMotion: this._respectReducedMotion });
102
57
  }
103
58
  stop() {
104
59
  this._playback.stop();
105
60
  }
106
- }, u = {
107
- damping: .1,
108
- snapThreshold: .001,
109
- targetEpsilon: 0,
110
- initial: 0,
111
- clamp: !0,
112
- respectReducedMotion: !1
113
- };
114
- function d() {
115
- return typeof window > "u" || typeof window.matchMedia != "function" ? !1 : window.matchMedia("(prefers-reduced-motion: reduce)").matches;
116
- }
117
- var f = class {
118
- options;
119
- targetValue;
120
- currentValue;
121
- isSettled;
122
- _rafId = null;
123
- _lastFrameT = 0;
124
- _onFrame = void 0;
125
- constructor(e) {
126
- this.options = {
127
- ...u,
128
- ...e
129
- }, this.targetValue = this.options.initial, this.currentValue = this.options.initial, this.isSettled = !0;
130
- }
131
- get target() {
132
- return this.targetValue;
133
- }
134
- get current() {
135
- return this.currentValue;
136
- }
137
- get settled() {
138
- return this.isSettled;
139
- }
140
- setTarget(e) {
141
- this.options.clamp && (e = Math.max(0, Math.min(1, e)));
142
- let t = Math.abs(e - this.targetValue);
143
- if (t > 0 && t >= this.options.targetEpsilon) {
144
- if (this.targetValue = e, this.options.respectReducedMotion && d()) {
145
- this.currentValue = e, this.isSettled = !0, this._onFrame?.(e);
146
- return;
147
- }
148
- this.isSettled = !1, this._onFrame && this._rafId === null && this._startLoop();
149
- }
150
- }
151
- tick() {
152
- 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);
153
- }
154
- tickDt(e) {
155
- if (this.isSettled) return this.currentValue;
156
- let t = 1 - Math.exp(-this.options.damping * e / 16.667);
157
- 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;
158
- }
159
- snap() {
160
- this.currentValue = this.targetValue, this.isSettled = !0, this._stopLoop();
161
- }
162
- reset(e) {
163
- let t = e ?? 0;
164
- this.targetValue = t, this.currentValue = t, this.isSettled = !0, this._stopLoop();
165
- }
166
- play(e) {
167
- if (this._onFrame = e, this.options.respectReducedMotion && d()) {
168
- this.currentValue = this.targetValue, this.isSettled = !0, e?.(this.currentValue);
169
- return;
170
- }
171
- if (this.isSettled) {
172
- e?.(this.currentValue);
173
- return;
174
- }
175
- this._startLoop();
176
- }
177
- stop() {
178
- this._onFrame = void 0, this._stopLoop();
179
- }
180
- _startLoop() {
181
- if (this._rafId !== null) return;
182
- this._lastFrameT = 0;
183
- let e = (n) => {
184
- let r = this._lastFrameT ? n - this._lastFrameT : 16.667;
185
- this._lastFrameT = n;
186
- let i = this.tickDt(r);
187
- if (this._onFrame?.(i), this.isSettled) {
188
- this._stopLoop();
189
- return;
190
- }
191
- this._rafId = t(e);
192
- };
193
- this._rafId = t(e);
194
- }
195
- _stopLoop() {
196
- this._rafId !== null && (a(this._rafId), this._rafId = null), this._lastFrameT = 0;
197
- }
198
- }, p = {
199
- response: .5,
200
- dampingFraction: .86,
201
- initial: 0,
202
- initialVelocity: 0,
203
- settleThreshold: .001,
204
- velocitySettleThreshold: .001,
205
- respectReducedMotion: !1
206
- };
207
- function m() {
208
- return typeof window > "u" || typeof window.matchMedia != "function" ? !1 : window.matchMedia("(prefers-reduced-motion: reduce)").matches;
209
- }
210
- var h = class {
211
- options;
212
- targetValue;
213
- currentValue;
214
- currentVelocity;
215
- isSettled;
216
- elapsed;
217
- originValue;
218
- originVelocity;
219
- omega;
220
- zeta;
221
- omegaD;
222
- subscribers = /* @__PURE__ */ new Set();
223
- disposed = !1;
224
- _rafId = null;
225
- _lastFrameT = 0;
226
- _onFrame = void 0;
227
- constructor(e) {
228
- this.options = {
229
- ...p,
230
- ...e
231
- }, 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;
232
- }
233
- get target() {
234
- return this.targetValue;
235
- }
236
- get value() {
237
- return this.currentValue;
238
- }
239
- get velocity() {
240
- return this.currentVelocity;
241
- }
242
- get settled() {
243
- return this.isSettled;
244
- }
245
- set target(e) {
246
- e === this.targetValue && !this.options.initialVelocity || this.reseatTarget(e);
247
- }
248
- reseatTarget(e) {
249
- if (this.targetValue = e, this.options.respectReducedMotion && m()) {
250
- this.currentValue = e, this.currentVelocity = 0, this.isSettled = !0, this.originValue = e, this.originVelocity = 0, this.elapsed = 0, this.emit(), this._stopLoop();
251
- return;
252
- }
253
- this.originValue = this.currentValue, this.originVelocity = this.currentVelocity, this.elapsed = 0, this.isSettled = !1, this._onFrame && this._rafId === null && this._startLoop();
254
- }
255
- tick(e) {
256
- return this.disposed || this.isSettled || e <= 0 ? this.currentValue : (this.elapsed += e, this.evaluateAt(this.elapsed), this.checkSettled(), this.emit(), this.currentValue);
257
- }
258
- tickToTime(e) {
259
- return this.disposed || this.isSettled ? this.currentValue : (this.elapsed = Math.max(0, e), this.evaluateAt(this.elapsed), this.checkSettled(), this.emit(), this.currentValue);
260
- }
261
- evaluateAt(e) {
262
- let t = this.originValue - this.targetValue, n = this.originVelocity, r = this.omega, i = this.zeta, a, o;
263
- if (i < 1) {
264
- 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);
265
- a = c * (l * d + u * f), o = c * ((u * s - l * i * r) * d - (l * s + u * i * r) * f);
266
- } else if (i === 1) {
267
- let i = Math.exp(-r * e), s = t, c = n + r * t;
268
- a = i * (s + c * e), o = i * (c - r * (s + c * e));
269
- } else {
270
- 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);
271
- a = u * f + d * p, o = u * c * f + d * l * p;
272
- }
273
- this.currentValue = this.targetValue + a, this.currentVelocity = o;
274
- }
275
- checkSettled() {
276
- let e = Math.abs(this.currentValue - this.targetValue), t = Math.abs(this.currentVelocity);
277
- e < this.options.settleThreshold && t < this.options.velocitySettleThreshold && (this.currentValue = this.targetValue, this.currentVelocity = 0, this.isSettled = !0);
278
- }
279
- snap() {
280
- this.currentValue = this.targetValue, this.currentVelocity = 0, this.originValue = this.targetValue, this.originVelocity = 0, this.elapsed = 0, this.isSettled = !0, this.emit(), this._stopLoop();
281
- }
282
- reset(e, t) {
283
- let n = e ?? 0, r = t ?? 0;
284
- 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();
285
- }
286
- subscribe(e) {
287
- return this.subscribers.add(e), () => {
288
- this.subscribers.delete(e);
289
- };
290
- }
291
- emit() {
292
- if (this.subscribers.size !== 0) for (let e of this.subscribers) e(this.currentValue, this.currentVelocity);
293
- }
294
- dispose() {
295
- this.disposed = !0, this._stopLoop(), this.subscribers.clear();
296
- }
297
- play(e) {
298
- if (!this.disposed) {
299
- if (this._onFrame = e, this.options.respectReducedMotion && m()) {
300
- this.currentValue = this.targetValue, this.currentVelocity = 0, this.isSettled = !0, e?.(this.currentValue, this.currentVelocity);
301
- return;
302
- }
303
- if (this.isSettled) {
304
- e?.(this.currentValue, this.currentVelocity);
305
- return;
306
- }
307
- this._startLoop();
308
- }
309
- }
310
- stop() {
311
- this._onFrame = void 0, this._stopLoop();
312
- }
313
- _startLoop() {
314
- if (this._rafId !== null) return;
315
- this._lastFrameT = 0;
316
- let e = (n) => {
317
- let r = this._lastFrameT ? n - this._lastFrameT : 16.667;
318
- if (this._lastFrameT = n, this.tick(r / 1e3), this._onFrame?.(this.currentValue, this.currentVelocity), this.isSettled) {
319
- this._stopLoop();
320
- return;
321
- }
322
- this._rafId = t(e);
323
- };
324
- this._rafId = t(e);
325
- }
326
- _stopLoop() {
327
- this._rafId !== null && (a(this._rafId), this._rafId = null), this._lastFrameT = 0;
328
- }
329
- };
330
- //#endregion
331
- //#region src/animation/springLinearStops.ts
332
- function g(e) {
333
- let t = e.sampleCount ?? 24, n = e.settleThreshold ?? .001, r = e.maxDuration ?? e.response * 4, i = new h({
334
- response: e.response,
335
- dampingFraction: e.dampingFraction,
336
- initial: 0,
337
- settleThreshold: n,
338
- velocitySettleThreshold: n
339
- });
340
- i.target = 1;
341
- let a = ["0"], o = r / (t + 1);
342
- for (let e = 1; e <= t; e++) {
343
- i.tick(o);
344
- let n = e / (t + 1) * 100, r = i.settled ? 1 : i.value;
345
- a.push(`${r.toFixed(5)} ${n.toFixed(3)}%`);
346
- }
347
- return a.push("1"), i.dispose(), `linear(${a.join(", ")})`;
348
- }
349
- //#endregion
350
- //#region src/animation/springTimingFunction.ts
351
- function _(e) {
352
- let t = e.sampleCount ?? 64, n = e.settleThreshold ?? .001, r = e.maxDuration ?? e.response * 4, i = new h({
353
- response: e.response,
354
- dampingFraction: e.dampingFraction,
355
- initial: 0,
356
- settleThreshold: n,
357
- velocitySettleThreshold: n
358
- });
359
- i.target = 1;
360
- let a = new Float64Array(t + 1);
361
- a[0] = 0;
362
- let o = r / t;
363
- for (let e = 1; e <= t; e++) i.tick(o), a[e] = i.settled ? 1 : i.value;
364
- return a[t] = 1, i.dispose(), (e) => {
365
- if (e <= 0) return 0;
366
- if (e >= 1) return 1;
367
- let n = e * t, r = Math.floor(n), i = n - r, o = a[r];
368
- return o + (a[r + 1] - o) * i;
369
- };
370
- }
371
- //#endregion
372
- //#region src/animation/morph.ts
373
- var v = (e) => {
61
+ }, v = (e) => {
374
62
  if (e instanceof HTMLElement) {
375
63
  let t = e.getBoundingClientRect();
376
64
  return {
@@ -391,7 +79,7 @@ var v = (e) => {
391
79
  }
392
80
  measure(e, t) {
393
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;
394
- return this.animation = new l([{
82
+ return this.animation = new _([{
395
83
  translateX: 0,
396
84
  translateY: 0,
397
85
  scaleX: 1,
@@ -406,9 +94,6 @@ var v = (e) => {
406
94
  duration: this.duration
407
95
  }), this;
408
96
  }
409
- ready() {
410
- return this.animation.ready();
411
- }
412
97
  at(e) {
413
98
  return this.animation.at(e);
414
99
  }
@@ -428,83 +113,346 @@ var v = (e) => {
428
113
  stop() {
429
114
  this.animation.stop();
430
115
  }
431
- }, b = (e) => Math.max(0, Math.min(1, e)), x = class {
432
- smoother;
433
- easingFn;
434
- currentProgress = 0;
435
- boundaryEpsilon;
436
- _easingReady = null;
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);
437
188
  constructor(e) {
438
- this.boundaryEpsilon = e?.boundaryEpsilon ?? .005;
439
- let t = e?.easing;
440
- typeof t == "function" ? this.easingFn = t : typeof t == "string" ? (this.easingFn = null, this.resolveEasingName(t)) : this.easingFn = null, e?.smoothing === !1 ? this.smoother = null : this.smoother = new f(e?.smoothing);
441
- }
442
- resolveEasingName(e) {
443
- return this._easingReady ||= import("./engine-DuAFoqZF.js").then((t) => {
444
- let n = t.getTimingFunction(e);
445
- n && (this.easingFn = n);
446
- }), this._easingReady;
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);
447
190
  }
448
- ready() {
449
- return this._easingReady ?? Promise.resolve();
191
+ get dragging() {
192
+ return this.capturedPointerId !== void 0;
450
193
  }
451
- applyPipeline() {
452
- let e = b(this.sample());
453
- this.easingFn && (e = this.easingFn(e));
454
- let t = this.boundaryEpsilon;
455
- return e <= t ? e = 0 : e >= 1 - t && (e = 1), e;
194
+ get value() {
195
+ return this.spring.value;
456
196
  }
457
- finalizeProgress(e) {
458
- 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;
459
199
  }
460
- _advance(e) {
461
- let t = this.applyPipeline();
462
- 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;
463
202
  }
464
- tick() {
465
- return this._advance();
203
+ attach(e) {
204
+ return this.detach(), this.element = e, e.addEventListener("pointerdown", this.onPointerDown), () => this.detach();
466
205
  }
467
- tickDt(e) {
468
- 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);
469
209
  }
470
- get progress() {
471
- 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
+ };
472
214
  }
473
- get settled() {
474
- return this.smoother == null || this.smoother.settled;
215
+ emit(e, t) {
216
+ for (let n of this.subscribers) n(e, t);
475
217
  }
476
- snap() {
477
- 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);
478
257
  }
479
- reset(e) {
480
- let t = e ?? 0;
481
- 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;
482
263
  }
483
- }, S = class extends x {
484
- threshold;
485
- getScrollY;
486
- 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;
487
311
  constructor(e) {
488
- 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);
489
313
  }
490
- sample() {
491
- let e = this.getViewportHeight() * this.threshold;
492
- 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;
493
321
  }
494
- }, C = class extends x {
495
- value = 0;
496
- constructor(e) {
497
- super({
498
- smoothing: !1,
499
- ...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);
500
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;
397
+ }
398
+ _isForwardMonotone() {
399
+ return this._rate === 1 && this._repeatCount === 1 && !this._yoyoOn;
501
400
  }
502
- set(e) {
503
- this.value = e;
401
+ _restPhase() {
402
+ let e = this.duration;
403
+ return this._yoyoOn && (this._repeatCount - 1) % 2 == 1 ? 0 : e;
504
404
  }
505
- sample() {
506
- return this.value;
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;
507
413
  }
508
- }, w = () => import("./engine-DuAFoqZF.js");
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
+ };
509
457
  //#endregion
510
- export { y as ElementMorph, C as ManualTimeline, l as NumericAnimation, o as RAFPlayback, S as ScrollTimeline, f 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 };