animation: Implement clamped value and duration
Browse files at this commit0c686090637f51caee2dd2fea2223a3c5bc4995bParent: 6cd3f962 changed files +51 -0
- src/animation/mod.rs +20 -0
- src/animation/spring.rs +31 -0
src/animation/mod.rsmodified+20 -0
| @@ -16,6 +16,8 @@ pub struct Animation { | |||
| 16 | from: f64, | 16 | from: f64, |
| 17 | to: f64, | 17 | to: f64, |
| 18 | duration: Duration, | 18 | duration: Duration, |
| 19 | + /// Time until the animation first reaches `to`. | ||
| 20 | + clamped_duration: Duration, | ||
| 19 | start_time: Duration, | 21 | start_time: Duration, |
| 20 | current_time: Duration, | 22 | current_time: Duration, |
| 21 | kind: Kind, | 23 | kind: Kind, |
| @@ -106,6 +108,8 @@ impl Animation { | |||
| 106 | from, | 108 | from, |
| 107 | to, | 109 | to, |
| 108 | duration, | 110 | duration, |
| 111 | + // Our current curves never overshoot. | ||
| 112 | + clamped_duration: duration, | ||
| 109 | start_time: now, | 113 | start_time: now, |
| 110 | current_time: now, | 114 | current_time: now, |
| 111 | kind, | 115 | kind, |
| @@ -118,12 +122,14 @@ impl Animation { | |||
| 118 | let now = get_monotonic_time(); | 122 | let now = get_monotonic_time(); |
| 119 | 123 | ||
| 120 | let duration = spring.duration(); | 124 | let duration = spring.duration(); |
| 125 | + let clamped_duration = spring.clamped_duration(); | ||
| 121 | let kind = Kind::Spring(spring); | 126 | let kind = Kind::Spring(spring); |
| 122 | 127 | ||
| 123 | Self { | 128 | Self { |
| 124 | from: spring.from, | 129 | from: spring.from, |
| 125 | to: spring.to, | 130 | to: spring.to, |
| 126 | duration, | 131 | duration, |
| 132 | + clamped_duration, | ||
| 127 | start_time: now, | 133 | start_time: now, |
| 128 | current_time: now, | 134 | current_time: now, |
| 129 | kind, | 135 | kind, |
| @@ -159,6 +165,7 @@ impl Animation { | |||
| 159 | from, | 165 | from, |
| 160 | to, | 166 | to, |
| 161 | duration, | 167 | duration, |
| 168 | + clamped_duration: duration, | ||
| 162 | start_time: now, | 169 | start_time: now, |
| 163 | current_time: now, | 170 | current_time: now, |
| 164 | kind, | 171 | kind, |
| @@ -228,6 +235,10 @@ impl Animation { | |||
| 228 | self.current_time >= self.start_time + self.duration | 235 | self.current_time >= self.start_time + self.duration |
| 229 | } | 236 | } |
| 230 | 237 | ||
| 238 | + pub fn is_clamped_done(&self) -> bool { | ||
| 239 | + self.current_time >= self.start_time + self.clamped_duration | ||
| 240 | + } | ||
| 241 | + | ||
| 231 | pub fn value(&self) -> f64 { | 242 | pub fn value(&self) -> f64 { |
| 232 | if self.is_done() { | 243 | if self.is_done() { |
| 233 | return self.to; | 244 | return self.to; |
| @@ -254,6 +265,15 @@ impl Animation { | |||
| 254 | } | 265 | } |
| 255 | } | 266 | } |
| 256 | 267 | ||
| 268 | + /// Returns a value that stops at the target value after first reaching it. | ||
| 269 | + pub fn clamped_value(&self) -> f64 { | ||
| 270 | + if self.is_clamped_done() { | ||
| 271 | + return self.to; | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + self.value() | ||
| 275 | + } | ||
| 276 | + | ||
| 257 | pub fn to(&self) -> f64 { | 277 | pub fn to(&self) -> f64 { |
| 258 | self.to | 278 | self.to |
| 259 | } | 279 | } |
src/animation/spring.rsmodified+31 -0
| @@ -96,6 +96,37 @@ impl Spring { | |||
| 96 | Duration::from_secs_f64(x1) | 96 | Duration::from_secs_f64(x1) |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | + /// Computes and returns the duration until the spring reaches its target position. | ||
| 100 | + pub fn clamped_duration(&self) -> Duration { | ||
| 101 | + let beta = self.params.damping / (2. * self.params.mass); | ||
| 102 | + | ||
| 103 | + if beta.abs() <= f64::EPSILON || beta < 0. { | ||
| 104 | + return Duration::MAX; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + if (self.to - self.from).abs() <= f64::EPSILON { | ||
| 108 | + return Duration::ZERO; | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + // The first frame is not that important and we avoid finding the trivial 0 for in-place | ||
| 112 | + // animations. | ||
| 113 | + let mut i = 1u16; | ||
| 114 | + let mut y = self.oscillate(f64::from(i) / 1000.); | ||
| 115 | + | ||
| 116 | + while (self.to - self.from > f64::EPSILON && self.to - y > self.params.epsilon) | ||
| 117 | + || (self.from - self.to > f64::EPSILON && y - self.to > self.params.epsilon) | ||
| 118 | + { | ||
| 119 | + if i > 1000 { | ||
| 120 | + return Duration::ZERO; | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + i += 1; | ||
| 124 | + y = self.oscillate(f64::from(i) / 1000.); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + Duration::from_millis(u64::from(i)) | ||
| 128 | + } | ||
| 129 | + | ||
| 99 | /// Returns the spring position at a given time in seconds. | 130 | /// Returns the spring position at a given time in seconds. |
| 100 | fn oscillate(&self, t: f64) -> f64 { | 131 | fn oscillate(&self, t: f64) -> f64 { |
| 101 | let b = self.params.damping; | 132 | let b = self.params.damping; |