pub struct Spring { /* private fields */ }Expand description
A damped harmonic oscillator producing physically-based motion.
The spring interpolates from an initial position toward a target, with configurable stiffness and damping.
§Example
use std::time::Duration;
use ftui_core::animation::spring::Spring;
let mut spring = Spring::new(0.0, 1.0)
.with_stiffness(170.0)
.with_damping(26.0);
// Simulate at 60fps
for _ in 0..120 {
spring.tick(Duration::from_millis(16));
}
assert!((spring.position() - 1.0).abs() < 0.01);Implementations§
Source§impl Spring
impl Spring
Sourcepub fn new(initial: f64, target: f64) -> Self
pub fn new(initial: f64, target: f64) -> Self
Create a spring starting at initial and targeting target.
Default parameters: stiffness = 170.0, damping = 26.0 (slightly underdamped, producing a subtle bounce).
Sourcepub fn normalized() -> Self
pub fn normalized() -> Self
Create a spring animating from 0.0 to 1.0 (normalized).
Sourcepub fn with_stiffness(self, k: f64) -> Self
pub fn with_stiffness(self, k: f64) -> Self
Set stiffness (builder pattern). Clamped to minimum 0.1.
Sourcepub fn with_damping(self, c: f64) -> Self
pub fn with_damping(self, c: f64) -> Self
Set damping (builder pattern). Clamped to minimum 0.0.
Sourcepub fn with_rest_threshold(self, threshold: f64) -> Self
pub fn with_rest_threshold(self, threshold: f64) -> Self
Set rest threshold (builder pattern).
Sourcepub fn with_velocity_threshold(self, threshold: f64) -> Self
pub fn with_velocity_threshold(self, threshold: f64) -> Self
Set velocity threshold (builder pattern).
Sourcepub fn set_target(&mut self, target: f64)
pub fn set_target(&mut self, target: f64)
Change the target. Wakes the spring if it was at rest.
Sourcepub fn impulse(&mut self, velocity_delta: f64)
pub fn impulse(&mut self, velocity_delta: f64)
Apply an impulse (add to velocity). Wakes the spring.
Sourcepub fn is_at_rest(&self) -> bool
pub fn is_at_rest(&self) -> bool
Whether the spring has settled at the target.
Sourcepub fn critical_damping(&self) -> f64
pub fn critical_damping(&self) -> f64
Compute the critical damping coefficient for the current stiffness.
At critical damping, the spring converges as fast as possible without oscillating.
Trait Implementations§
Source§impl Animation for Spring
impl Animation for Spring
Source§fn value(&self) -> f32
fn value(&self) -> f32
Returns the spring position clamped to [0.0, 1.0].
For springs with targets outside [0, 1], use position()
directly.