Skip to content

Commit

Permalink
Smoother animations (#4787)
Browse files Browse the repository at this point in the history
This makes animations slightly smoother, especially in reactive mode
  • Loading branch information
emilk authored Jul 5, 2024
1 parent 1059e0e commit 0be4450
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions crates/egui/src/animation_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ impl AnimationManager {
Some(anim) => {
let time_since_toggle = (input.time - anim.toggle_time) as f32;
// On the frame we toggle we don't want to return the old value,
// so we extrapolate forwards:
let time_since_toggle = time_since_toggle + input.predicted_dt;
// so we extrapolate forwards by half a frame:
let time_since_toggle = time_since_toggle + input.predicted_dt / 2.0;
let current_value = remap_clamp(
time_since_toggle,
0.0..=animation_time,
Expand Down
5 changes: 3 additions & 2 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,10 @@ impl Prepared {

if self.fade_in {
if let Some(last_became_visible_at) = self.state.last_became_visible_at {
let age = ctx.input(|i| (i.time - last_became_visible_at) as f32 + i.predicted_dt);
let age =
ctx.input(|i| (i.time - last_became_visible_at) as f32 + i.predicted_dt / 2.0);
let opacity = crate::remap_clamp(age, 0.0..=ctx.style().animation_time, 0.0..=1.0);
let opacity = emath::easing::cubic_out(opacity); // slow fade-out = quick fade-in
let opacity = emath::easing::quadratic_out(opacity); // slow fade-out = quick fade-in
ui.multiply_opacity(opacity);
if opacity < 1.0 {
ctx.request_repaint();
Expand Down
7 changes: 6 additions & 1 deletion crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl ContextImpl {

fn request_repaint_after(
&mut self,
delay: Duration,
mut delay: Duration,
viewport_id: ViewportId,
cause: RepaintCause,
) {
Expand All @@ -163,6 +163,11 @@ impl ContextImpl {
// Hovering a tooltip is a good example of a case where we want to repaint after a delay.
}

if let Ok(predicted_frame_time) = Duration::try_from_secs_f32(viewport.input.predicted_dt) {
// Make it less likely we over-shoot the target:
delay = delay.saturating_sub(predicted_frame_time);
}

viewport.repaint.causes.push(cause);

// We save some CPU time by only calling the callback if we need to.
Expand Down

0 comments on commit 0be4450

Please sign in to comment.