Skip to content

Commit d94b278

Browse files
committed
feat: replace "config.speed" with "config.frequency + config.damping"
1 parent 7a57d89 commit d94b278

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

packages/core/src/SpringValue.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,12 @@ export class SpringValue<T = any> extends AnimationValue<T> {
456456
anim.config = config = { ...BASE_CONFIG, ...config }
457457
}
458458

459-
// When "speed" is provided, we derive "tension" and "friction" from it.
460-
if (!is.und(config.speed)) {
461-
config.tension = Math.pow((2 * Math.PI) / config.speed, 2) * config.mass
462-
// Note: We treat "friction" as the *damping ratio* instead of as its coefficient.
459+
// Derive "tension" and "friction" from "frequency" and "damping".
460+
if (!is.und(config.frequency)) {
461+
const damping = is.und(config.damping) ? 1 : config.damping
462+
config.tension = Math.pow(config.frequency, 2) * config.mass
463463
config.friction =
464-
(4 * Math.PI * config.friction * config.mass) / config.speed
464+
(damping * Math.sqrt(config.tension * config.mass)) / 0.5
465465
}
466466

467467
// Cache the angular frequency in rad/ms

packages/core/src/types/animated.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,22 @@ export interface AnimationConfig {
5353
*/
5454
tension: number
5555
/**
56-
* The frequency response.
56+
* The natural frequency (in seconds), which dictates the number of bounces
57+
* per second when no damping exists.
5758
*
58-
* An alternative to `tension` that describes the speed of an undamped spring.
59+
* When defined, `tension` is derived from this, and `friction` is derived
60+
* from `tension` and `damping`.
5961
*/
60-
speed?: number
62+
frequency?: number
63+
/**
64+
* The damping ratio, which dictates how the spring slows down.
65+
*
66+
* Set to `0` to never slow down. Set to `1` to slow down without bouncing.
67+
* Between `0` and `1` is for you to explore.
68+
*
69+
* Defaults to `1` when `frequency` is defined.
70+
*/
71+
damping?: number
6172
/**
6273
* The damping ratio coefficient, or just the damping ratio when `speed` is defined.
6374
*

0 commit comments

Comments
 (0)