animation: Extract value_at() and fix animations off difference
Browse files at this commit37840a418aeba33cfba196d2ce7dd7227369dd85Parent: 4a4c9721 changed file +16 -3
- src/animation/mod.rs +16 -3
src/animation/mod.rsmodified+16 -3
| @@ -242,12 +242,21 @@ impl Animation { | |||
| 242 | self.clock.now() >= self.start_time + self.clamped_duration | 242 | self.clock.now() >= self.start_time + self.clamped_duration |
| 243 | } | 243 | } |
| 244 | 244 | ||
| 245 | - pub fn value(&self) -> f64 { | 245 | + pub fn value_at(&self, at: Duration) -> f64 { |
| 246 | - if self.is_done() { | 246 | + if at <= self.start_time { |
| 247 | + // Return from when at == start_time so that when the animations are off, the behavior | ||
| 248 | + // within a single event loop cycle (i.e. no time had passed since the start of an | ||
| 249 | + // animation) matches the behavior when the animations are on. | ||
| 250 | + return self.from; | ||
| 251 | + } else if self.start_time + self.duration <= at { | ||
| 252 | + return self.to; | ||
| 253 | + } | ||
| 254 | + | ||
| 255 | + if self.clock.should_complete_instantly() { | ||
| 247 | return self.to; | 256 | return self.to; |
| 248 | } | 257 | } |
| 249 | 258 | ||
| 250 | - let passed = self.clock.now().saturating_sub(self.start_time); | 259 | + let passed = at.saturating_sub(self.start_time); |
| 251 | 260 | ||
| 252 | match self.kind { | 261 | match self.kind { |
| 253 | Kind::Easing { curve } => { | 262 | Kind::Easing { curve } => { |
| @@ -280,6 +289,10 @@ impl Animation { | |||
| 280 | } | 289 | } |
| 281 | } | 290 | } |
| 282 | 291 | ||
| 292 | + pub fn value(&self) -> f64 { | ||
| 293 | + self.value_at(self.clock.now()) | ||
| 294 | + } | ||
| 295 | + | ||
| 283 | /// Returns a value that stops at the target value after first reaching it. | 296 | /// Returns a value that stops at the target value after first reaching it. |
| 284 | /// | 297 | /// |
| 285 | /// Best effort; not always exactly precise. | 298 | /// Best effort; not always exactly precise. |