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) -> Spring
pub fn new(initial: f64, target: f64) -> Spring
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() -> Spring
pub fn normalized() -> Spring
Create a spring animating from 0.0 to 1.0 (normalized).
Sourcepub fn with_stiffness(self, k: f64) -> Spring
pub fn with_stiffness(self, k: f64) -> Spring
Set stiffness (builder pattern). Clamped to minimum 0.1.
Sourcepub fn with_damping(self, c: f64) -> Spring
pub fn with_damping(self, c: f64) -> Spring
Set damping (builder pattern). Clamped to minimum 0.0.
Sourcepub fn with_rest_threshold(self, threshold: f64) -> Spring
pub fn with_rest_threshold(self, threshold: f64) -> Spring
Set rest threshold (builder pattern).
Sourcepub fn with_velocity_threshold(self, threshold: f64) -> Spring
pub fn with_velocity_threshold(self, threshold: f64) -> Spring
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.
Source§fn is_complete(&self) -> bool
fn is_complete(&self) -> bool
Auto Trait Implementations§
impl Freeze for Spring
impl RefUnwindSafe for Spring
impl Send for Spring
impl Sync for Spring
impl Unpin for Spring
impl UnsafeUnpin for Spring
impl UnwindSafe for Spring
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more