@mkbabb/keyframes.js 6.4.0 → 6.6.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/README.md CHANGED
@@ -628,7 +628,93 @@ Math.max(...Array.from({ length: 101 }, (_, i) => spring.fn(i / 100))) > 1; // =
628
628
  spring.css === springLinearStops({ response: 0.5, dampingFraction: 0.45 }); // => true
629
629
  ```
630
630
 
631
- Options (both): `response`, `dampingFraction`, `sampleCount` (default 24 stops / 64 samples), `settleThreshold` (default `1e-3`), `maxDuration` (default `4 × response` — raise it for very underdamped springs, ζ < 0.3).
631
+ Options (both): `response`, `dampingFraction`, `sampleCount` (default 24 stops / 64 samples), `settleThreshold` (default `1e-3`), `maxDuration` (default `4 × response` — raise it for very underdamped springs, ζ < 0.3; or `"settle"` to span exactly `settleTime` of the same options, so the pinned end stop jumps by at most `settleThreshold`; it throws a `RangeError` when that is `Infinity` or 0), `initialVelocity` (default 0; see below).
632
+
633
+ **Seeded curves.** `initialVelocity` starts the curve already moving, in travel/s on the spring's clock (travel is the leg's distance, so the curve still runs 0 → 1; `< 0` is still heading away). A reversed glide picks up the running speed and bends round instead of turning a corner: `.fn` and `.css` add finer knots inside their first interval (down to 1/4096 of it), so the first segment leaves at the seed's speed to within 2 % at the default sample counts. Time on the curve is `u = t / T` (`T` the resolved `maxDuration`), so for a motion moving at `v` units/s that now has `D = to − current` to go and plays over `P` seconds, pass `initialVelocity = (v / D) · (P / T)` (just `v / D` when `P = T`). Prefer `maxDuration: "settle"`, so `T` includes the seed, and read `T` back as `settleTime(options)` with the same options to set the duration. If `D` is about 0, there is no curve to seed: hand the motion to a live `SpringProgress` (`reset(x, v)`, then `target`), or sample it with [`springKeyframes`](#springkeyframes), which takes absolute positions.
634
+
635
+ ```ts run
636
+ const seeded = springTimingFunction({
637
+ response: 0.5,
638
+ dampingFraction: 0.86,
639
+ initialVelocity: -2, // still moving away from the new target
640
+ maxDuration: "settle",
641
+ });
642
+ seeded.fn(0.02) < 0; // => true
643
+ seeded.fn(1); // => 1
644
+ // The span it used, to set the animation's duration (P = T):
645
+ const T = settleTime({ response: 0.5, dampingFraction: 0.86, initialVelocity: -2 });
646
+ T > 0.85 && T < 0.95; // => true
647
+ ```
648
+
649
+ ### `settleTime`
650
+
651
+ The spring's own settle clock: the seconds until the `0 → 1` step response enters the rest envelope `|x − 1| < settleThreshold` and `|v| < velocitySettleThreshold` and stays there (the last exit, so a ringing spring that passes through the envelope early is not counted as settled). It reads the closed form and never steps. Take a duration from the spring itself instead of a hand-tuned clock.
652
+
653
+ ```ts run
654
+ const seconds = settleTime({ response: 0.5, dampingFraction: 0.45 });
655
+ seconds > 1.6 && seconds < 1.62; // => true
656
+ // Position only (a settle band, no velocity check):
657
+ settleTime({ response: 0.5, dampingFraction: 0.45, velocitySettleThreshold: Infinity }) < seconds; // => true
658
+ // An undamped spring never settles (Infinity):
659
+ Number.isFinite(settleTime({ response: 0.5, dampingFraction: 0 })); // => false
660
+ ```
661
+
662
+ Options: `response`, `dampingFraction`, `initialVelocity` (travel/s, default 0), `settleThreshold` / `velocitySettleThreshold` (default `1e-3`, the same envelope as `SpringProgress`; `Infinity` drops the velocity check). Throws `RangeError` on a non-positive or non-finite `response` or `settleThreshold`, a non-positive `velocitySettleThreshold`, a negative or non-finite `dampingFraction`, or a non-finite velocity. It returns `Infinity` when the spring never settles: undamped (ζ = 0), or damped so hard (ζ ≳ 1e8) that the solver's slow root rounds to 0. It finds an exit however brief, down to `response / 2^32` s. The system is linear, so a live spring over distance `D` with absolute thresholds `ε` / `εv` and velocity `v` settles at `settleTime({ …, initialVelocity: v / D, settleThreshold: ε / D, velocitySettleThreshold: εv / D })`. Reduced motion is not an input: under `respectReducedMotion: true` the spring snaps (duration 0); under a numeric intensity `s` it travels only `s · D`, so with absolute thresholds it settles sooner, at the same formula with `s · D` for `D`.
663
+
664
+ ### `springKeyframes`
665
+
666
+ A spring as keyframes, for a player that runs them itself: give it the spring (`response`, `dampingFraction`), where it starts (`initial`, moving at `initialVelocity` units/s), its `target` and a `tolerance`, and it returns keyframe `offsets` and `values`, the `duration` they span (the spring's settle time, in seconds), and `at(t)`, the spring's position and velocity at any time. Hand them to `element.animate()` and the browser plays the spring, overshoot and all, with no script per frame and no `linear()` easing to depend on.
667
+
668
+ The keyframes are placed adaptively, not at even steps: each segment is the longest straight line that stays within `tolerance` of the true curve, so a near-linear stretch takes few keyframes and a bend takes many, and a looser tolerance takes fewer. The bound holds between keyframes too, not just at them. The first value is `initial` and the last is exactly `target`. Positions are absolute (px, degrees, a 0–1 progress), so a spring released on its own rest (`initial === target`, `initialVelocity ≠ 0`) samples like any other.
669
+
670
+ ```ts run
671
+ const spring = {
672
+ response: 0.4,
673
+ dampingFraction: 0.8,
674
+ tolerance: 0.25, // px
675
+ settleThreshold: 0.1, // px: done once within a tenth of a pixel for good
676
+ velocitySettleThreshold: Infinity, // a position band alone
677
+ };
678
+ const kf = springKeyframes({ ...spring, initial: 0, initialVelocity: 900, target: 240 });
679
+ kf.offsets[0]; // => 0
680
+ kf.offsets[kf.offsets.length - 1]; // => 1
681
+ kf.values[0]; // => 0
682
+ kf.values[kf.values.length - 1]; // => 240
683
+ kf.offsets.length < 40; // => true
684
+ kf.at(0); // => { value: 0, velocity: 900 }
685
+ kf.at(kf.duration); // => { value: 240, velocity: 0 }
686
+
687
+ // Retarget mid-glide: start the next spring from where the body is, at the
688
+ // speed it has, so position and velocity carry across the seam.
689
+ const now = kf.at(0.12);
690
+ const next = springKeyframes({ ...spring, initial: now.value, initialVelocity: now.velocity, target: 80 });
691
+ next.values[0] === now.value; // => true
692
+ next.at(0).velocity === now.velocity; // => true
693
+ ```
694
+
695
+ Playing them, and retargeting a running one:
696
+
697
+ ```ts
698
+ const play = (kf: SpringKeyframes) =>
699
+ element.animate(
700
+ kf.offsets.map((offset, i) => ({ offset, transform: `translateX(${kf.values[i]}px)` })),
701
+ { duration: kf.duration * 1000, fill: "forwards" }, // linear between keyframes
702
+ );
703
+
704
+ let kf = springKeyframes({ ...spring, initial: 0, initialVelocity: 900, target: 240 });
705
+ let animation = play(kf);
706
+
707
+ // Later, a new target while it glides. Read the time off the animation: it
708
+ // starts a frame or more after animate() is called, so a clock started at the
709
+ // call runs ahead of what is on screen.
710
+ const here = kf.at((animation.currentTime as number) / 1000);
711
+ kf = springKeyframes({ ...spring, initial: here.value, initialVelocity: here.velocity, target: 80 });
712
+ animation = play(kf);
713
+ ```
714
+
715
+ At the seam the element is on a straight chord between keyframes, which can sit up to `tolerance` off `at(t)`, so the handover can step by as much as `tolerance`.
716
+
717
+ Options: everything `SpringProgress` takes, with its defaults (`response` 0.5, `dampingFraction` 0.86, `initial` 0, `initialVelocity` 0, `settleThreshold` / `velocitySettleThreshold` `1e-3`, absolute, in value units; `velocitySettleThreshold: Infinity` is a position band alone, which is usually what a played span wants), except `respectReducedMotion` (the sampler is pure; apply reduced motion as you would for `settleTime`), plus `target` and `tolerance` (required; value units, finite and > 0). The span is the last exit from the rest envelope, as `settleTime` counts it, with the position band narrowed to `tolerance / 2` when that is tighter, so the pinned last keyframe is within the tolerance too. `at(t)` takes seconds since the start: before it, the start; from `duration` on, `(target, 0)`. Throws `RangeError` on a non-finite input, a `response`, `tolerance` or `settleThreshold` that is not > 0, a negative `dampingFraction`, a spring that never settles (undamped, or damped past floating-point range), or a tolerance so tight it would need more than 10 000 keyframes.
632
718
 
633
719
  ### `RAFPlayback`
634
720
 
@@ -727,7 +813,19 @@ unsubscribe();
727
813
  draggable.detach(); // remove listeners; `.dispose()` tears down spring and all
728
814
  ```
729
815
 
730
- Options: `axis` (`"x"` | `"y"`, default `"x"`), `transform` (map a client px coordinate → your value domain), `spring` (bring your own `SpringProgress`) or `springOptions`, `velocityWindow` (ms, default 100). Reads: `.value`, `.velocity`, `.dragging`, `.settled`, and `.spring` (the physics core). `drag(element, options)` is the one-call form of `new Draggable(options)` + `.attach(element)`.
816
+ Options: `axis` (`"x"` | `"y"`, default `"x"`), `transform` (map a client px coordinate → your value domain), `spring` (bring your own `SpringProgress`) or `springOptions`, `velocityWindow` (ms, default 100, per `Draggable`), `passThrough` (default `false`; below), `bounds`, `rubberBand`, `snap`. Reads: `.value`, `.velocity`, `.dragging`, `.settled`, and `.spring` (the physics core). `drag(element, options)` is the one-call form of `new Draggable(options)` + `.attach(element)`.
817
+
818
+ **Pass-through.** With `passThrough: true` the pointer's value is written straight through while the pointer is down: each move seats the spring at the pointer's value (rubber-banded past `bounds`) at rest and emits it once, so `.value` is the pointer's with no spring chasing it and no frame loop running. The spring starts only at release, from that value at its release speed (the `velocityWindow` estimate of the value on show, so past `bounds` the rubber-banded speed), then snaps, bounds and flings as it does without the option. The release emission comes once the snap or bounds target is set: read `.value`, `.velocity` and `.spring.target` there to play the rest as keyframes with [`springKeyframes`](#springkeyframes).
819
+
820
+ ```ts run
821
+ const held = drag(element, { passThrough: true, velocityWindow: 80 });
822
+ held.subscribe((value, velocity) => {
823
+ if (held.dragging) element.style.transform = `translateX(${value}px)`; // the pointer, exactly
824
+ // else: the release — springKeyframes({ initial: value, initialVelocity: velocity, target: held.spring.target, … })
825
+ });
826
+ held.dragging; // => false
827
+ held.dispose();
828
+ ```
731
829
 
732
830
  ### `decay` / `decayRest`
733
831
 
@@ -865,9 +963,15 @@ npm run bench # vitest bench
865
963
  npm test -- --run && npm run proof:publish # correctness + packaged public surface
866
964
  ```
867
965
 
868
- ### Local CI repro
966
+ ### Browser checks (local, before a ship)
967
+
968
+ CI runs types, build, the jsdom suites, lint and the publish boundary; it runs no browser. The browser oracles, the demo roster and the Lighthouse audit run locally before a ship:
969
+
970
+ ```sh
971
+ npm run preship # scripts/preship.sh: oracles · build · roster · lighthouse
972
+ ```
869
973
 
870
- A macOS-pass / Linux-fail CI flake (a render-race, an absolute frame/ms threshold) can be reproduced locally — no push, no 30-minute wait — by running the demo-smoke roster inside the same Linux container the CI runner uses:
974
+ To run the demo roster inside a Linux container instead (a macOS-pass / Linux-fail render race):
871
975
 
872
976
  ```sh
873
977
  make ci-linux # node:24-slim container · npm ci → gh-pages → demo:correctness
@@ -192,6 +192,11 @@ export declare class AnimationGroup<V extends Vars> {
192
192
  * still runs its shadow transport loop so events and completion retain the
193
193
  * rAF contract, but it does not overwrite native compositor output. */
194
194
  _waapiDelegated: boolean;
195
+ /** True from a reduced-motion snap until the next rewind. The snap paints
196
+ * every child's FINAL frame and then settles (each child's `started` and
197
+ * `t` back to 0), so nothing else records that the pixels left the initial
198
+ * frame; `reset()` reads this to rewind them. INTERNAL (`./lifecycle`). */
199
+ _snappedToFinal: boolean;
195
200
  resolvePromise: ((value: void | PromiseLike<void>) => void) | null;
196
201
  /** The ONE held play promise the `finished` front-door exposes. INTERNAL
197
202
  * (S.B5) — read/written by the transport free functions in `./lifecycle`. */
@@ -2634,11 +2639,13 @@ declare interface SpringProgressOptions {
2634
2639
  * - `true` — the classic binary snap: under an active query the spring
2635
2640
  * jumps to target at zero velocity (amplitude scale `0`). SSR-safe.
2636
2641
  * - a `number` ∈ [0, 1] — the AMPLITUDE INTENSITY: under an active query
2637
- * the spring keeps its trajectory SHAPE (curve + settle time) but scales
2638
- * its displacement-from-rest (`originValue − targetValue`) to this
2639
- * fraction. At `0.3` the peak overshoot is 30 % of the full-intensity
2640
- * peak; the envelope is preserved by construction (the analytic form
2641
- * makes the scaling exact and free). `1` ≈ full motion, `0` ≡ the snap.
2642
+ * the spring keeps its trajectory SHAPE (the normalised curve) but
2643
+ * scales its displacement-from-rest (`originValue − targetValue`) to
2644
+ * this fraction. At `0.3` the peak overshoot is 30 % of the
2645
+ * full-intensity peak (the analytic form makes the scaling exact and
2646
+ * free). The thresholds are absolute, so the shorter travel settles
2647
+ * SOONER: `settleTime` over `s · D` (see `settleTime`). `1` ≈ full
2648
+ * motion, `0` ≡ the snap.
2642
2649
  *
2643
2650
  * The scale is the CONSUMER's policy (a settings slider, a per-animation
2644
2651
  * field) — the OS exposes only a binary signal; kf provides the mechanism.