@bevelo/motion 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/index.cjs +6364 -0
- package/dist/index.d.cts +1832 -0
- package/dist/index.d.ts +1832 -0
- package/dist/index.js +6278 -0
- package/dist/live/flip.cjs +78 -0
- package/dist/live/flip.d.cts +21 -0
- package/dist/live/flip.d.ts +21 -0
- package/dist/live/flip.js +77 -0
- package/dist/live/presence.cjs +7 -0
- package/dist/live/presence.d.cts +2 -0
- package/dist/live/presence.d.ts +2 -0
- package/dist/live/presence.js +1 -0
- package/dist/live/spring-value.cjs +4 -0
- package/dist/live/spring-value.d.cts +41 -0
- package/dist/live/spring-value.d.ts +41 -0
- package/dist/live/spring-value.js +1 -0
- package/dist/presence-BadXSYGX.cjs +499 -0
- package/dist/presence-CbHtzolg.d.cts +126 -0
- package/dist/presence-CbHtzolg.d.ts +126 -0
- package/dist/presence-Dt789xG9.js +470 -0
- package/dist/spring-CfdnEGs4.d.cts +118 -0
- package/dist/spring-CfdnEGs4.d.ts +118 -0
- package/dist/spring-value-D7dvds3M.cjs +373 -0
- package/dist/spring-value-fKpX3pAG.js +338 -0
- package/dist/timeline.cjs +53 -0
- package/dist/timeline.d.cts +61 -0
- package/dist/timeline.d.ts +61 -0
- package/dist/timeline.js +52 -0
- package/package.json +63 -3
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/** Maps normalised progress (0..1) to an eased fraction (0..1, or beyond for overshoot curves). */
|
|
2
|
+
type Easing = (progress: number) => number;
|
|
3
|
+
/** Constant speed. The only curve with no acceleration, and the default for linear motion. */
|
|
4
|
+
declare const linear: Easing;
|
|
5
|
+
/** Quadratic ease-in: slow start, fast finish. */
|
|
6
|
+
declare const easeIn: Easing;
|
|
7
|
+
/** Quadratic ease-out: fast start, slow finish. The default for exits. */
|
|
8
|
+
declare const easeOut: Easing;
|
|
9
|
+
/** Quadratic ease-in-out: slow at both ends. */
|
|
10
|
+
declare const easeInOut: Easing;
|
|
11
|
+
/** Cubic ease-out: the standard "settle into place" curve. */
|
|
12
|
+
declare const easeOutCubic: Easing;
|
|
13
|
+
/** Cubic ease-in-out: symmetric, used for moves that start and end at rest. */
|
|
14
|
+
declare const easeInOutCubic: Easing;
|
|
15
|
+
/** Quintic ease-out: a sharper stop than cubic, for short distances. */
|
|
16
|
+
declare const easeOutQuint: Easing;
|
|
17
|
+
/** Exponential ease-out: almost all of the travel in the first third. */
|
|
18
|
+
declare const easeOutExpo: Easing;
|
|
19
|
+
/**
|
|
20
|
+
* Ease-out with a slight overshoot before settling. Deliberately not monotone:
|
|
21
|
+
* the overshoot is the point. Use it for elements that arrive rather than grow.
|
|
22
|
+
*/
|
|
23
|
+
declare const easeOutBack: Easing;
|
|
24
|
+
/**
|
|
25
|
+
* CSS `linear()` as an Easing: piecewise-linear through the given y stops,
|
|
26
|
+
* evenly spaced in x, first stop at progress 0 and last at progress 1. This is
|
|
27
|
+
* how a spring is written as a timing function — sampled, not solved — so the
|
|
28
|
+
* curve is exact at every stop and cheap everywhere between.
|
|
29
|
+
*/
|
|
30
|
+
declare function linearEasing(...stops: readonly number[]): Easing;
|
|
31
|
+
/** Entrances: fast start, gentle settle. Panels, toasts, popovers arriving. */
|
|
32
|
+
declare const easeEnter: Easing;
|
|
33
|
+
/** Exits: slow start, fast finish. Things leaving should get out of the way. */
|
|
34
|
+
declare const easeExit: Easing;
|
|
35
|
+
/** On-screen moves: symmetric. Reorders, slides, position changes in place. */
|
|
36
|
+
declare const easeMove: Easing;
|
|
37
|
+
/** Strong ease-out for small click-confirmation parts — a check, a tick. */
|
|
38
|
+
declare const easeEmphasis: Easing;
|
|
39
|
+
/** Overshoot for small confirmations that should feel sprung, not eased. */
|
|
40
|
+
declare const easePop: Easing;
|
|
41
|
+
/** Post-drag settling: a sampled spring (the CSS linear() curve), with a
|
|
42
|
+
slight overshoot and a soft return. Fallback where springs are too heavy. */
|
|
43
|
+
declare const easeSpringSettle: Easing;
|
|
44
|
+
/**
|
|
45
|
+
* The CSS cubic-bezier(x1, y1, x2, y2) curve as an Easing. x controls *when* the
|
|
46
|
+
* motion happens and must stay in 0..1 (the solver needs a monotone x); y controls
|
|
47
|
+
* *how much* and may leave 0..1 for overshoot.
|
|
48
|
+
*
|
|
49
|
+
* Input is clamped to 0..1 and the endpoints are exact, so an animation that
|
|
50
|
+
* finishes on progress 1 lands on the target rather than 1 - 1e-9.
|
|
51
|
+
*
|
|
52
|
+
* Throws a RangeError for non-finite arguments or x values outside 0..1: an
|
|
53
|
+
* unusable curve is a caller bug, and a plausible-looking wrong curve is worse
|
|
54
|
+
* than a loud failure.
|
|
55
|
+
*/
|
|
56
|
+
declare function cubicBezier(x1: number, y1: number, x2: number, y2: number): Easing;
|
|
57
|
+
/** Physical description of a spring plus its endpoints, all optional; see {@link DEFAULT_SPRING}. */
|
|
58
|
+
interface SpringConfig {
|
|
59
|
+
/** Spring constant. Higher pulls harder: shorter duration, more overshoot at equal damping. */
|
|
60
|
+
readonly stiffness?: number;
|
|
61
|
+
/** Friction. Higher removes overshoot; above 2 * sqrt(stiffness * mass) the motion becomes sluggish. */
|
|
62
|
+
readonly damping?: number;
|
|
63
|
+
/** Inertia. Higher is slower to start and harder to stop. */
|
|
64
|
+
readonly mass?: number;
|
|
65
|
+
/** Value at t = 0. Defaults to 0. */
|
|
66
|
+
readonly from?: number;
|
|
67
|
+
/** Resting value the spring settles on. Defaults to 1. */
|
|
68
|
+
readonly to?: number;
|
|
69
|
+
/** Initial velocity in value units per second. Defaults to 0 (released from rest). */
|
|
70
|
+
readonly velocity?: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Defaults chosen to feel like a good UI spring: ~0.6s to settle, almost no
|
|
74
|
+
* overshoot, no perceptible acceleration delay. They are a single declaration so
|
|
75
|
+
* the runtime value and the type cannot drift apart.
|
|
76
|
+
*/
|
|
77
|
+
declare const DEFAULT_SPRING: Required<SpringConfig>;
|
|
78
|
+
/**
|
|
79
|
+
* A solved spring. Every method is a pure function of its argument: two calls
|
|
80
|
+
* with the same timestamp return the same number, in any order, on any frame rate.
|
|
81
|
+
*/
|
|
82
|
+
interface SpringSolution {
|
|
83
|
+
/** Value at `seconds` after release; negative times clamp to the release value. */
|
|
84
|
+
at(seconds: number): number;
|
|
85
|
+
/** Rate of change at `seconds`, in value units per second. */
|
|
86
|
+
velocityAt(seconds: number): number;
|
|
87
|
+
/**
|
|
88
|
+
* First time both the displacement and the velocity are within `epsilon` of
|
|
89
|
+
* rest, in seconds. Finite for every accepted config, and never increasing as
|
|
90
|
+
* damping rises towards critical (past critical, more damping genuinely makes a
|
|
91
|
+
* spring slower; see {@link solveSpring}).
|
|
92
|
+
*/
|
|
93
|
+
duration(epsilon?: number): number;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Solves a spring once, in closed form, for the underdamped, critically damped
|
|
97
|
+
* and overdamped cases, and returns a sampler for the result.
|
|
98
|
+
*
|
|
99
|
+
* Throws a RangeError for non-finite values or a non-positive stiffness, mass or
|
|
100
|
+
* damping: those configs have no finite settling time, and a silent NaN would
|
|
101
|
+
* surface as an element frozen somewhere between two values.
|
|
102
|
+
*/
|
|
103
|
+
declare function solveSpring(config?: SpringConfig): SpringSolution;
|
|
104
|
+
/**
|
|
105
|
+
* A spring as an easing over 0..1, for APIs that take an Easing (keyframes, CSS
|
|
106
|
+
* interpolation). The motion is the spring's: sampled at progress * duration().
|
|
107
|
+
*
|
|
108
|
+
* `from` and `to` in the config are ignored: an easing's output range is fixed at
|
|
109
|
+
* 0..1 by definition, and normalising to a caller's arbitrary range here would
|
|
110
|
+
* make the same config mean different things in different call sites.
|
|
111
|
+
*/
|
|
112
|
+
declare function springEasing(config?: SpringConfig): Easing;
|
|
113
|
+
declare function springFromDuration(durationMs: number, bounce?: number): {
|
|
114
|
+
readonly stiffness: number;
|
|
115
|
+
readonly damping: number;
|
|
116
|
+
readonly mass: number;
|
|
117
|
+
};
|
|
118
|
+
export { linear as C, easeSpringSettle as S, easeOutBack as _, springEasing as a, easeOutQuint as b, cubicBezier as c, easeExit as d, easeIn as f, easeOut as g, easeMove as h, solveSpring as i, easeEmphasis as l, easeInOutCubic as m, SpringConfig as n, springFromDuration as o, easeInOut as p, SpringSolution as r, Easing as s, DEFAULT_SPRING as t, easeEnter as u, easeOutCubic as v, linearEasing as w, easePop as x, easeOutExpo as y };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/** Maps normalised progress (0..1) to an eased fraction (0..1, or beyond for overshoot curves). */
|
|
2
|
+
type Easing = (progress: number) => number;
|
|
3
|
+
/** Constant speed. The only curve with no acceleration, and the default for linear motion. */
|
|
4
|
+
declare const linear: Easing;
|
|
5
|
+
/** Quadratic ease-in: slow start, fast finish. */
|
|
6
|
+
declare const easeIn: Easing;
|
|
7
|
+
/** Quadratic ease-out: fast start, slow finish. The default for exits. */
|
|
8
|
+
declare const easeOut: Easing;
|
|
9
|
+
/** Quadratic ease-in-out: slow at both ends. */
|
|
10
|
+
declare const easeInOut: Easing;
|
|
11
|
+
/** Cubic ease-out: the standard "settle into place" curve. */
|
|
12
|
+
declare const easeOutCubic: Easing;
|
|
13
|
+
/** Cubic ease-in-out: symmetric, used for moves that start and end at rest. */
|
|
14
|
+
declare const easeInOutCubic: Easing;
|
|
15
|
+
/** Quintic ease-out: a sharper stop than cubic, for short distances. */
|
|
16
|
+
declare const easeOutQuint: Easing;
|
|
17
|
+
/** Exponential ease-out: almost all of the travel in the first third. */
|
|
18
|
+
declare const easeOutExpo: Easing;
|
|
19
|
+
/**
|
|
20
|
+
* Ease-out with a slight overshoot before settling. Deliberately not monotone:
|
|
21
|
+
* the overshoot is the point. Use it for elements that arrive rather than grow.
|
|
22
|
+
*/
|
|
23
|
+
declare const easeOutBack: Easing;
|
|
24
|
+
/**
|
|
25
|
+
* CSS `linear()` as an Easing: piecewise-linear through the given y stops,
|
|
26
|
+
* evenly spaced in x, first stop at progress 0 and last at progress 1. This is
|
|
27
|
+
* how a spring is written as a timing function — sampled, not solved — so the
|
|
28
|
+
* curve is exact at every stop and cheap everywhere between.
|
|
29
|
+
*/
|
|
30
|
+
declare function linearEasing(...stops: readonly number[]): Easing;
|
|
31
|
+
/** Entrances: fast start, gentle settle. Panels, toasts, popovers arriving. */
|
|
32
|
+
declare const easeEnter: Easing;
|
|
33
|
+
/** Exits: slow start, fast finish. Things leaving should get out of the way. */
|
|
34
|
+
declare const easeExit: Easing;
|
|
35
|
+
/** On-screen moves: symmetric. Reorders, slides, position changes in place. */
|
|
36
|
+
declare const easeMove: Easing;
|
|
37
|
+
/** Strong ease-out for small click-confirmation parts — a check, a tick. */
|
|
38
|
+
declare const easeEmphasis: Easing;
|
|
39
|
+
/** Overshoot for small confirmations that should feel sprung, not eased. */
|
|
40
|
+
declare const easePop: Easing;
|
|
41
|
+
/** Post-drag settling: a sampled spring (the CSS linear() curve), with a
|
|
42
|
+
slight overshoot and a soft return. Fallback where springs are too heavy. */
|
|
43
|
+
declare const easeSpringSettle: Easing;
|
|
44
|
+
/**
|
|
45
|
+
* The CSS cubic-bezier(x1, y1, x2, y2) curve as an Easing. x controls *when* the
|
|
46
|
+
* motion happens and must stay in 0..1 (the solver needs a monotone x); y controls
|
|
47
|
+
* *how much* and may leave 0..1 for overshoot.
|
|
48
|
+
*
|
|
49
|
+
* Input is clamped to 0..1 and the endpoints are exact, so an animation that
|
|
50
|
+
* finishes on progress 1 lands on the target rather than 1 - 1e-9.
|
|
51
|
+
*
|
|
52
|
+
* Throws a RangeError for non-finite arguments or x values outside 0..1: an
|
|
53
|
+
* unusable curve is a caller bug, and a plausible-looking wrong curve is worse
|
|
54
|
+
* than a loud failure.
|
|
55
|
+
*/
|
|
56
|
+
declare function cubicBezier(x1: number, y1: number, x2: number, y2: number): Easing;
|
|
57
|
+
/** Physical description of a spring plus its endpoints, all optional; see {@link DEFAULT_SPRING}. */
|
|
58
|
+
interface SpringConfig {
|
|
59
|
+
/** Spring constant. Higher pulls harder: shorter duration, more overshoot at equal damping. */
|
|
60
|
+
readonly stiffness?: number;
|
|
61
|
+
/** Friction. Higher removes overshoot; above 2 * sqrt(stiffness * mass) the motion becomes sluggish. */
|
|
62
|
+
readonly damping?: number;
|
|
63
|
+
/** Inertia. Higher is slower to start and harder to stop. */
|
|
64
|
+
readonly mass?: number;
|
|
65
|
+
/** Value at t = 0. Defaults to 0. */
|
|
66
|
+
readonly from?: number;
|
|
67
|
+
/** Resting value the spring settles on. Defaults to 1. */
|
|
68
|
+
readonly to?: number;
|
|
69
|
+
/** Initial velocity in value units per second. Defaults to 0 (released from rest). */
|
|
70
|
+
readonly velocity?: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Defaults chosen to feel like a good UI spring: ~0.6s to settle, almost no
|
|
74
|
+
* overshoot, no perceptible acceleration delay. They are a single declaration so
|
|
75
|
+
* the runtime value and the type cannot drift apart.
|
|
76
|
+
*/
|
|
77
|
+
declare const DEFAULT_SPRING: Required<SpringConfig>;
|
|
78
|
+
/**
|
|
79
|
+
* A solved spring. Every method is a pure function of its argument: two calls
|
|
80
|
+
* with the same timestamp return the same number, in any order, on any frame rate.
|
|
81
|
+
*/
|
|
82
|
+
interface SpringSolution {
|
|
83
|
+
/** Value at `seconds` after release; negative times clamp to the release value. */
|
|
84
|
+
at(seconds: number): number;
|
|
85
|
+
/** Rate of change at `seconds`, in value units per second. */
|
|
86
|
+
velocityAt(seconds: number): number;
|
|
87
|
+
/**
|
|
88
|
+
* First time both the displacement and the velocity are within `epsilon` of
|
|
89
|
+
* rest, in seconds. Finite for every accepted config, and never increasing as
|
|
90
|
+
* damping rises towards critical (past critical, more damping genuinely makes a
|
|
91
|
+
* spring slower; see {@link solveSpring}).
|
|
92
|
+
*/
|
|
93
|
+
duration(epsilon?: number): number;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Solves a spring once, in closed form, for the underdamped, critically damped
|
|
97
|
+
* and overdamped cases, and returns a sampler for the result.
|
|
98
|
+
*
|
|
99
|
+
* Throws a RangeError for non-finite values or a non-positive stiffness, mass or
|
|
100
|
+
* damping: those configs have no finite settling time, and a silent NaN would
|
|
101
|
+
* surface as an element frozen somewhere between two values.
|
|
102
|
+
*/
|
|
103
|
+
declare function solveSpring(config?: SpringConfig): SpringSolution;
|
|
104
|
+
/**
|
|
105
|
+
* A spring as an easing over 0..1, for APIs that take an Easing (keyframes, CSS
|
|
106
|
+
* interpolation). The motion is the spring's: sampled at progress * duration().
|
|
107
|
+
*
|
|
108
|
+
* `from` and `to` in the config are ignored: an easing's output range is fixed at
|
|
109
|
+
* 0..1 by definition, and normalising to a caller's arbitrary range here would
|
|
110
|
+
* make the same config mean different things in different call sites.
|
|
111
|
+
*/
|
|
112
|
+
declare function springEasing(config?: SpringConfig): Easing;
|
|
113
|
+
declare function springFromDuration(durationMs: number, bounce?: number): {
|
|
114
|
+
readonly stiffness: number;
|
|
115
|
+
readonly damping: number;
|
|
116
|
+
readonly mass: number;
|
|
117
|
+
};
|
|
118
|
+
export { linear as C, easeSpringSettle as S, easeOutBack as _, springEasing as a, easeOutQuint as b, cubicBezier as c, easeExit as d, easeIn as f, easeOut as g, easeMove as h, solveSpring as i, easeEmphasis as l, easeInOutCubic as m, SpringConfig as n, springFromDuration as o, easeInOut as p, SpringSolution as r, Easing as s, DEFAULT_SPRING as t, easeEnter as u, easeOutCubic as v, linearEasing as w, easePop as x, easeOutExpo as y };
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
let _bevelo_runtime = require("@bevelo/runtime");
|
|
2
|
+
// src/spring.ts
|
|
3
|
+
/**
|
|
4
|
+
* Defaults chosen to feel like a good UI spring: ~0.6s to settle, almost no
|
|
5
|
+
* overshoot, no perceptible acceleration delay. They are a single declaration so
|
|
6
|
+
* the runtime value and the type cannot drift apart.
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_SPRING = {
|
|
9
|
+
stiffness: 170,
|
|
10
|
+
damping: 26,
|
|
11
|
+
mass: 1,
|
|
12
|
+
from: 0,
|
|
13
|
+
to: 1,
|
|
14
|
+
velocity: 0
|
|
15
|
+
};
|
|
16
|
+
/** Default tolerance for {@link SpringSolution.duration}: a tenth of a pixel at 100dpi. */
|
|
17
|
+
const DEFAULT_EPSILON = .001;
|
|
18
|
+
/** Bisection depth used to refine the first settling time inside a sampled step. */
|
|
19
|
+
const BISECTION_ITERATIONS = 60;
|
|
20
|
+
function requireFinite(name, value) {
|
|
21
|
+
if (!Number.isFinite(value)) throw new RangeError(`spring ${name} must be finite, received ${value}`);
|
|
22
|
+
}
|
|
23
|
+
function requirePositive(name, value) {
|
|
24
|
+
requireFinite(name, value);
|
|
25
|
+
if (value <= 0) throw new RangeError(`spring ${name} must be greater than 0, received ${value}`);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Solves a spring once, in closed form, for the underdamped, critically damped
|
|
29
|
+
* and overdamped cases, and returns a sampler for the result.
|
|
30
|
+
*
|
|
31
|
+
* Throws a RangeError for non-finite values or a non-positive stiffness, mass or
|
|
32
|
+
* damping: those configs have no finite settling time, and a silent NaN would
|
|
33
|
+
* surface as an element frozen somewhere between two values.
|
|
34
|
+
*/
|
|
35
|
+
function solveSpring(config = {}) {
|
|
36
|
+
const { stiffness = DEFAULT_SPRING.stiffness, damping = DEFAULT_SPRING.damping, mass = DEFAULT_SPRING.mass, from = DEFAULT_SPRING.from, to = DEFAULT_SPRING.to, velocity = DEFAULT_SPRING.velocity } = config;
|
|
37
|
+
requirePositive("stiffness", stiffness);
|
|
38
|
+
requirePositive("mass", mass);
|
|
39
|
+
requirePositive("damping", damping);
|
|
40
|
+
requireFinite("from", from);
|
|
41
|
+
requireFinite("to", to);
|
|
42
|
+
requireFinite("velocity", velocity);
|
|
43
|
+
const displacement = from - to;
|
|
44
|
+
const omega = Math.sqrt(stiffness / mass);
|
|
45
|
+
const discriminant = damping * damping - 4 * mass * stiffness;
|
|
46
|
+
const criticalScale = Math.max(1, damping * damping, 4 * mass * stiffness);
|
|
47
|
+
const isCritical = Math.abs(discriminant) <= 1e-12 * criticalScale;
|
|
48
|
+
let at;
|
|
49
|
+
let velocityAt;
|
|
50
|
+
let decay;
|
|
51
|
+
let omegaD = 0;
|
|
52
|
+
if (isCritical) {
|
|
53
|
+
const rate = omega;
|
|
54
|
+
const linear = velocity + rate * displacement;
|
|
55
|
+
const vBase = -rate * displacement + linear;
|
|
56
|
+
const vSlope = rate * linear;
|
|
57
|
+
at = (seconds) => seconds <= 0 ? from : to + (displacement + linear * seconds) * Math.exp(-rate * seconds);
|
|
58
|
+
velocityAt = (seconds) => seconds <= 0 ? velocity : Math.exp(-rate * seconds) * (vBase - vSlope * seconds);
|
|
59
|
+
decay = rate;
|
|
60
|
+
} else if (discriminant < 0) {
|
|
61
|
+
const alpha = damping / (2 * mass);
|
|
62
|
+
const damped = omega * Math.sqrt(1 - (damping / (2 * Math.sqrt(stiffness * mass))) ** 2);
|
|
63
|
+
const sine = (velocity + alpha * displacement) / damped;
|
|
64
|
+
const vCos = -alpha * displacement + damped * sine;
|
|
65
|
+
const vSin = -alpha * sine - damped * displacement;
|
|
66
|
+
omegaD = damped;
|
|
67
|
+
at = (seconds) => seconds <= 0 ? from : to + Math.exp(-alpha * seconds) * (displacement * Math.cos(damped * seconds) + sine * Math.sin(damped * seconds));
|
|
68
|
+
velocityAt = (seconds) => {
|
|
69
|
+
if (seconds <= 0) return velocity;
|
|
70
|
+
const angle = damped * seconds;
|
|
71
|
+
return Math.exp(-alpha * seconds) * (vCos * Math.cos(angle) + vSin * Math.sin(angle));
|
|
72
|
+
};
|
|
73
|
+
decay = alpha;
|
|
74
|
+
} else {
|
|
75
|
+
const root = Math.sqrt(discriminant);
|
|
76
|
+
const slow = (-damping + root) / (2 * mass);
|
|
77
|
+
const fast = (-damping - root) / (2 * mass);
|
|
78
|
+
const slowCoeff = (velocity - fast * displacement) / (slow - fast);
|
|
79
|
+
const fastCoeff = displacement - slowCoeff;
|
|
80
|
+
const slowVel = slowCoeff * slow;
|
|
81
|
+
const fastVel = fastCoeff * fast;
|
|
82
|
+
at = (seconds) => seconds <= 0 ? from : to + slowCoeff * Math.exp(slow * seconds) + fastCoeff * Math.exp(fast * seconds);
|
|
83
|
+
velocityAt = (seconds) => seconds <= 0 ? velocity : slowVel * Math.exp(slow * seconds) + fastVel * Math.exp(fast * seconds);
|
|
84
|
+
decay = -slow;
|
|
85
|
+
}
|
|
86
|
+
const pointSettled = (s, epsilon) => Math.abs(at(s) - to) <= epsilon && Math.abs(velocityAt(s)) <= epsilon;
|
|
87
|
+
const lookahead = omegaD > 0 ? Math.PI / (2 * omegaD) : 0;
|
|
88
|
+
const settled = (s, epsilon) => pointSettled(s, epsilon) && (lookahead <= 0 || pointSettled(s + lookahead, epsilon));
|
|
89
|
+
let defaultDuration = -1;
|
|
90
|
+
let durationCache;
|
|
91
|
+
const duration = (epsilon = DEFAULT_EPSILON) => {
|
|
92
|
+
if (!Number.isFinite(epsilon) || !(epsilon > 0)) throw new RangeError(`spring duration epsilon must be a finite number > 0, received ${epsilon}`);
|
|
93
|
+
if (epsilon === DEFAULT_EPSILON) {
|
|
94
|
+
if (defaultDuration >= 0) return defaultDuration;
|
|
95
|
+
} else if (durationCache !== void 0) {
|
|
96
|
+
const cached = durationCache.get(epsilon);
|
|
97
|
+
if (cached !== void 0) return cached;
|
|
98
|
+
}
|
|
99
|
+
let result = 0;
|
|
100
|
+
if (!settled(0, epsilon)) {
|
|
101
|
+
const amplitude = Math.max(1, Math.abs(displacement), Math.abs(velocity) / decay);
|
|
102
|
+
let horizon = Math.log(amplitude / epsilon) / decay + 1;
|
|
103
|
+
let guard = 0;
|
|
104
|
+
while (!settled(horizon, epsilon) && guard < 64) {
|
|
105
|
+
horizon *= 2;
|
|
106
|
+
guard += 1;
|
|
107
|
+
}
|
|
108
|
+
const samples = 1024;
|
|
109
|
+
const step = horizon / samples;
|
|
110
|
+
let previous = 0;
|
|
111
|
+
result = horizon;
|
|
112
|
+
for (let index = 1; index <= samples; index += 1) {
|
|
113
|
+
const time = index * step;
|
|
114
|
+
if (settled(time, epsilon)) {
|
|
115
|
+
let low = previous;
|
|
116
|
+
let high = time;
|
|
117
|
+
for (let iteration = 0; iteration < BISECTION_ITERATIONS; iteration += 1) {
|
|
118
|
+
const mid = (low + high) / 2;
|
|
119
|
+
if (settled(mid, epsilon)) high = mid;
|
|
120
|
+
else low = mid;
|
|
121
|
+
}
|
|
122
|
+
result = high;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
previous = time;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (epsilon === DEFAULT_EPSILON) defaultDuration = result;
|
|
129
|
+
else (durationCache ??= /* @__PURE__ */ new Map()).set(epsilon, result);
|
|
130
|
+
return result;
|
|
131
|
+
};
|
|
132
|
+
return {
|
|
133
|
+
at,
|
|
134
|
+
velocityAt,
|
|
135
|
+
duration
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* A spring as an easing over 0..1, for APIs that take an Easing (keyframes, CSS
|
|
140
|
+
* interpolation). The motion is the spring's: sampled at progress * duration().
|
|
141
|
+
*
|
|
142
|
+
* `from` and `to` in the config are ignored: an easing's output range is fixed at
|
|
143
|
+
* 0..1 by definition, and normalising to a caller's arbitrary range here would
|
|
144
|
+
* make the same config mean different things in different call sites.
|
|
145
|
+
*/
|
|
146
|
+
function springEasing(config = {}) {
|
|
147
|
+
const solution = solveSpring({
|
|
148
|
+
...config,
|
|
149
|
+
from: 0,
|
|
150
|
+
to: 1
|
|
151
|
+
});
|
|
152
|
+
const total = solution.duration();
|
|
153
|
+
return (progress) => {
|
|
154
|
+
if (!(progress > 0)) return 0;
|
|
155
|
+
if (progress >= 1) return 1;
|
|
156
|
+
return solution.at(progress * total);
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* A spring described by how long it takes and how much it overshoots —
|
|
161
|
+
* the duration-and-bounce shape a designer actually thinks in.
|
|
162
|
+
*
|
|
163
|
+
* bounce maps to a damping ratio through the underdamped overshoot curve
|
|
164
|
+
* (overshoot = e^(-pi*zeta/sqrt(1-zeta^2)), inverted); the stiffness is then
|
|
165
|
+
* solved so the spring's own settle time lands on `durationMs`. Explicit
|
|
166
|
+
* stiffness/damping fields on a transition still win over this conversion.
|
|
167
|
+
*/
|
|
168
|
+
const DURATION_SPRING_CACHE = /* @__PURE__ */ new Map();
|
|
169
|
+
const DURATION_SPRING_CACHE_MAX = 256;
|
|
170
|
+
function springFromDuration(durationMs, bounce = .25) {
|
|
171
|
+
if (!Number.isFinite(durationMs) || durationMs <= 0) throw new RangeError(`springFromDuration durationMs must be positive, received ${durationMs}`);
|
|
172
|
+
const clamped = Math.min(1, Math.max(0, bounce));
|
|
173
|
+
const key = `${durationMs}:${clamped}`;
|
|
174
|
+
const hit = DURATION_SPRING_CACHE.get(key);
|
|
175
|
+
if (hit !== void 0) return { ...hit };
|
|
176
|
+
const log = clamped <= 0 ? Number.POSITIVE_INFINITY : Math.log(clamped);
|
|
177
|
+
const zeta = clamped <= 0 ? 1 : Math.min(1, Math.max(.05, Math.abs(log) / Math.sqrt(Math.PI ** 2 + log * log)));
|
|
178
|
+
const mass = 1;
|
|
179
|
+
const targetSeconds = durationMs / 1e3;
|
|
180
|
+
let lo = 1;
|
|
181
|
+
let hi = 1e5;
|
|
182
|
+
for (let i = 0; i < 40; i += 1) {
|
|
183
|
+
const mid = Math.sqrt(lo * hi);
|
|
184
|
+
if (solveSpring({
|
|
185
|
+
stiffness: mid,
|
|
186
|
+
damping: 2 * zeta * Math.sqrt(mid * mass),
|
|
187
|
+
mass,
|
|
188
|
+
from: 0,
|
|
189
|
+
to: 1
|
|
190
|
+
}).duration() < targetSeconds) hi = mid;
|
|
191
|
+
else lo = mid;
|
|
192
|
+
}
|
|
193
|
+
const stiffness = Math.sqrt(lo * hi);
|
|
194
|
+
const solved = {
|
|
195
|
+
stiffness,
|
|
196
|
+
damping: 2 * zeta * Math.sqrt(stiffness * mass),
|
|
197
|
+
mass
|
|
198
|
+
};
|
|
199
|
+
if (DURATION_SPRING_CACHE.size >= DURATION_SPRING_CACHE_MAX) {
|
|
200
|
+
const oldest = DURATION_SPRING_CACHE.keys().next().value;
|
|
201
|
+
if (oldest !== void 0) DURATION_SPRING_CACHE.delete(oldest);
|
|
202
|
+
}
|
|
203
|
+
DURATION_SPRING_CACHE.set(key, solved);
|
|
204
|
+
return { ...solved };
|
|
205
|
+
}
|
|
206
|
+
// src/live/spring-value.ts
|
|
207
|
+
/** Samples the analytic solveSpring in the compute pass; onUpdate runs in the write pass. Retarget keeps velocity. */
|
|
208
|
+
function createSpringValue(initial, options) {
|
|
209
|
+
const frame = options.frame ?? (0, _bevelo_runtime.frameLoopOf)();
|
|
210
|
+
let value = initial;
|
|
211
|
+
let velocity = 0;
|
|
212
|
+
let run = null;
|
|
213
|
+
let dirty = false;
|
|
214
|
+
let jobs = null;
|
|
215
|
+
/** Frame time of the last sample while moving; a retarget starts from it so no frame is held. */
|
|
216
|
+
let lastSample = null;
|
|
217
|
+
function compute(now) {
|
|
218
|
+
if (run === null) return;
|
|
219
|
+
lastSample = now;
|
|
220
|
+
if (run.start === null) run.start = now;
|
|
221
|
+
const seconds = (now - run.start) / 1e3;
|
|
222
|
+
dirty = true;
|
|
223
|
+
if (seconds >= run.duration) {
|
|
224
|
+
value = run.target;
|
|
225
|
+
velocity = 0;
|
|
226
|
+
const finished = run;
|
|
227
|
+
run = null;
|
|
228
|
+
finished.resolve(true);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
value = run.solution.at(seconds);
|
|
232
|
+
velocity = run.solution.velocityAt(seconds);
|
|
233
|
+
}
|
|
234
|
+
function write() {
|
|
235
|
+
if (dirty) {
|
|
236
|
+
dirty = false;
|
|
237
|
+
options.onUpdate(value);
|
|
238
|
+
}
|
|
239
|
+
if (run === null) sleep();
|
|
240
|
+
}
|
|
241
|
+
/** Registers the frame jobs and holds the loop awake while there is work. */
|
|
242
|
+
function wake() {
|
|
243
|
+
if (jobs !== null) return;
|
|
244
|
+
const release = frame.keepAlive();
|
|
245
|
+
const offCompute = frame.job({
|
|
246
|
+
kind: "compute",
|
|
247
|
+
run: compute,
|
|
248
|
+
name: "spring-value"
|
|
249
|
+
});
|
|
250
|
+
const offWrite = frame.job({
|
|
251
|
+
kind: "write",
|
|
252
|
+
run: write,
|
|
253
|
+
name: "spring-value"
|
|
254
|
+
});
|
|
255
|
+
jobs = () => {
|
|
256
|
+
release();
|
|
257
|
+
offCompute();
|
|
258
|
+
offWrite();
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function sleep() {
|
|
262
|
+
jobs?.();
|
|
263
|
+
jobs = null;
|
|
264
|
+
}
|
|
265
|
+
function interrupt() {
|
|
266
|
+
const previous = run;
|
|
267
|
+
run = null;
|
|
268
|
+
previous?.resolve(false);
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
get value() {
|
|
272
|
+
return value;
|
|
273
|
+
},
|
|
274
|
+
get velocity() {
|
|
275
|
+
return velocity;
|
|
276
|
+
},
|
|
277
|
+
to(target, spring) {
|
|
278
|
+
const moving = run !== null && run.start !== null;
|
|
279
|
+
interrupt();
|
|
280
|
+
if (target === value && velocity === 0) return Promise.resolve(true);
|
|
281
|
+
const solution = solveSpring({
|
|
282
|
+
...spring ?? options.spring ?? {},
|
|
283
|
+
from: value,
|
|
284
|
+
to: target,
|
|
285
|
+
velocity
|
|
286
|
+
});
|
|
287
|
+
const epsilon = Math.max(1e-4, Math.abs(target - value) * .001);
|
|
288
|
+
return new Promise((resolve) => {
|
|
289
|
+
run = {
|
|
290
|
+
solution,
|
|
291
|
+
target,
|
|
292
|
+
duration: solution.duration(epsilon),
|
|
293
|
+
resolve,
|
|
294
|
+
start: moving ? lastSample : null
|
|
295
|
+
};
|
|
296
|
+
wake();
|
|
297
|
+
});
|
|
298
|
+
},
|
|
299
|
+
jump(next) {
|
|
300
|
+
interrupt();
|
|
301
|
+
value = next;
|
|
302
|
+
velocity = 0;
|
|
303
|
+
dirty = true;
|
|
304
|
+
wake();
|
|
305
|
+
},
|
|
306
|
+
stop() {
|
|
307
|
+
interrupt();
|
|
308
|
+
velocity = 0;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
/** progress 0 -> 1 on enter, 1 -> 0 on exit, through `write`. Reduced motion: jumps. */
|
|
313
|
+
function springRunner(options) {
|
|
314
|
+
const springs = /* @__PURE__ */ new WeakMap();
|
|
315
|
+
return (el, phase, signal) => {
|
|
316
|
+
const target = phase === "entering" ? 1 : 0;
|
|
317
|
+
const doc = el.ownerDocument;
|
|
318
|
+
let spring = springs.get(el);
|
|
319
|
+
if (spring === void 0) {
|
|
320
|
+
spring = createSpringValue(1 - target, {
|
|
321
|
+
onUpdate: (progress) => options.write(el, progress),
|
|
322
|
+
spring: options.spring,
|
|
323
|
+
frame: options.frame ?? (0, _bevelo_runtime.frameLoopOf)(doc)
|
|
324
|
+
});
|
|
325
|
+
springs.set(el, spring);
|
|
326
|
+
}
|
|
327
|
+
if ((0, _bevelo_runtime.environmentOf)(doc).reducedMotion()) {
|
|
328
|
+
spring.jump(target);
|
|
329
|
+
return Promise.resolve();
|
|
330
|
+
}
|
|
331
|
+
const running = spring.to(target);
|
|
332
|
+
return new Promise((resolve) => {
|
|
333
|
+
signal.addEventListener("abort", () => resolve(), { once: true });
|
|
334
|
+
running.then(() => resolve());
|
|
335
|
+
});
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
Object.defineProperty(exports, "DEFAULT_SPRING", {
|
|
339
|
+
enumerable: true,
|
|
340
|
+
get: function() {
|
|
341
|
+
return DEFAULT_SPRING;
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
Object.defineProperty(exports, "createSpringValue", {
|
|
345
|
+
enumerable: true,
|
|
346
|
+
get: function() {
|
|
347
|
+
return createSpringValue;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
Object.defineProperty(exports, "solveSpring", {
|
|
351
|
+
enumerable: true,
|
|
352
|
+
get: function() {
|
|
353
|
+
return solveSpring;
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
Object.defineProperty(exports, "springEasing", {
|
|
357
|
+
enumerable: true,
|
|
358
|
+
get: function() {
|
|
359
|
+
return springEasing;
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
Object.defineProperty(exports, "springFromDuration", {
|
|
363
|
+
enumerable: true,
|
|
364
|
+
get: function() {
|
|
365
|
+
return springFromDuration;
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
Object.defineProperty(exports, "springRunner", {
|
|
369
|
+
enumerable: true,
|
|
370
|
+
get: function() {
|
|
371
|
+
return springRunner;
|
|
372
|
+
}
|
|
373
|
+
});
|