pub struct BouncingSimulation { /* private fields */ }Expand description
Flutter BouncingScrollSimulation — the iOS UIScrollView fling.
While the content is inside [min, max] it coasts on exponential decay.
The moment the coast would cross a bound, the simulation hands off to a
spring anchored at that side’s rest target, so the content overshoots and is
pulled back. A release that starts outside the range skips the coast and
springs immediately.
§The two pairs of bounds
min/max and leading/trailing are different questions, and conflating
them is the reason both are parameters:
min/max— the scroll extent: the offsets past which the content is overscrolled. This is where the coast gives way to the spring.leading/trailing— the spring’s rest targets: where overscrolled content comes to rest. Normallyleading == minandtrailing == max, which is whatwithinfills in. They differ when the resting place is not the crossing place — content that snaps to a sticky header, or to a paged boundary, past the edge it crossed.
use std::time::Duration;
use teksilo_core::kinetic::{BouncingSimulation, ScrollSimulation};
// Released mid-range with plenty of room: pure exponential decay.
let sim = BouncingSimulation::within(0.0, 1000.0, -1.0e6, 1.0e6);
// Flutter FrictionSimulation: x(1s) = v·(0.135 − 1)/ln(0.135).
assert!((sim.position(Duration::from_secs(1)) - 431.96).abs() < 0.5);Implementations§
Source§impl BouncingSimulation
impl BouncingSimulation
Sourcepub fn within(position: f32, velocity: f32, min: f32, max: f32) -> Self
pub fn within(position: f32, velocity: f32, min: f32, max: f32) -> Self
The common case: the spring rests exactly at the bound it crossed.
Sourcepub fn new(
position: f32,
velocity: f32,
min: f32,
max: f32,
leading: f32,
trailing: f32,
) -> Self
pub fn new( position: f32, velocity: f32, min: f32, max: f32, leading: f32, trailing: f32, ) -> Self
A bouncing fling with explicit spring rest targets, using the shipped physics tokens. See the type docs for what the two pairs mean.
Sourcepub fn with_tokens(
position: f32,
velocity: f32,
min: f32,
max: f32,
leading: f32,
trailing: f32,
tokens: &ScrollPhysicsTokens,
) -> Self
pub fn with_tokens( position: f32, velocity: f32, min: f32, max: f32, leading: f32, trailing: f32, tokens: &ScrollPhysicsTokens, ) -> Self
new against a theme’s own physics tokens.
Sourcepub fn spring_time(&self) -> f32
pub fn spring_time(&self) -> f32
When the spring takes over, in seconds; INFINITY when it never does.
Trait Implementations§
Source§impl Clone for BouncingSimulation
impl Clone for BouncingSimulation
Source§fn clone(&self) -> BouncingSimulation
fn clone(&self) -> BouncingSimulation
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more