Skip to content

Commit b966371

Browse files
committed
examples/pong: Fix animation granularity
Using too large of a unit was causing the animations to jump and/or seemingly animate at different rates. This was because the animation speed was defined using Seconds, so any granularity less than seconds was being dropped. Using per_millis was the fix.
1 parent 41d5a06 commit b966371

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

examples/pong/src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,19 @@ impl Pong {
204204
// animation chain. Meaning that you would have an animation start
205205
// at the previous animations's interupted location, animate to elsewhere,
206206
// then go back to that spot!
207+
//
208+
// Also notice the speed here is per_millis! This is important.
209+
// The animation is only as granular as your definition in the chain.
210+
// If you animation time is not in exact seconds, I highly recommend
211+
// using a smaller unit.
207212
.link(keyframes::Space::lazy(Duration::ZERO))
208213
.link(
209-
keyframes::Space::new(Speed::per_secs(100.))
214+
keyframes::Space::new(Speed::per_millis(0.3))
210215
.height(self.window.height - 100.),
211216
),
212217
Direction::Up => cosmic_time::space::Chain::new(PADDLE_LEFT.clone())
213218
.link(keyframes::Space::lazy(Duration::ZERO))
214-
.link(keyframes::Space::new(Speed::per_secs(100.)).height(0.)),
219+
.link(keyframes::Space::new(Speed::per_millis(0.3)).height(0.)),
215220
})
216221
} else {
217222
None
@@ -225,12 +230,12 @@ impl Pong {
225230
Direction::Down => cosmic_time::space::Chain::new(PADDLE_RIGHT.clone())
226231
.link(keyframes::Space::lazy(Duration::ZERO))
227232
.link(
228-
keyframes::Space::new(Speed::per_secs(100.))
233+
keyframes::Space::new(Speed::per_millis(0.3))
229234
.height(self.window.height - 100.),
230235
),
231236
Direction::Up => cosmic_time::space::Chain::new(PADDLE_RIGHT.clone())
232237
.link(keyframes::Space::lazy(Duration::ZERO))
233-
.link(keyframes::Space::new(Speed::per_secs(100.)).height(0.)),
238+
.link(keyframes::Space::new(Speed::per_millis(0.3)).height(0.)),
234239
})
235240
} else {
236241
None

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ impl Speed {
5151
fn calc_duration(self, first: f32, second: f32) -> Duration {
5252
match self {
5353
Speed::PerSecond(speed) => {
54-
((first - second).abs() / speed).round() as u32 * Duration::from_secs(1)
54+
((first - second).abs() / speed).round() as u32 * Duration::from_nanos(1e9 as u64)
5555
}
5656
Speed::PerMillis(speed) => {
57-
((first - second).abs() / speed).round() as u32 * Duration::from_millis(1)
57+
((first - second).abs() / speed).round() as u32 * Duration::from_nanos(1e6 as u64)
5858
}
5959
Speed::PerMicros(speed) => {
60-
((first - second).abs() / speed).round() as u32 * Duration::from_micros(1)
60+
((first - second).abs() / speed).round() as u32 * Duration::from_nanos(1000)
6161
}
6262
Speed::PerNanoSe(speed) => {
6363
((first - second).abs() / speed).round() as u32 * Duration::from_nanos(1)

0 commit comments

Comments
 (0)