Skip to content

Commit e83ee79

Browse files
emilkhacknus
authored andcommitted
Smoother animations (emilk#4787)
This makes animations slightly smoother, especially in reactive mode
1 parent 0c52a2d commit e83ee79

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

crates/egui/src/animation_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ impl AnimationManager {
8787
Some(anim) => {
8888
let time_since_toggle = (input.time - anim.toggle_time) as f32;
8989
// On the frame we toggle we don't want to return the old value,
90-
// so we extrapolate forwards:
91-
let time_since_toggle = time_since_toggle + input.predicted_dt;
90+
// so we extrapolate forwards by half a frame:
91+
let time_since_toggle = time_since_toggle + input.predicted_dt / 2.0;
9292
let current_value = remap_clamp(
9393
time_since_toggle,
9494
0.0..=animation_time,

crates/egui/src/containers/area.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,10 @@ impl Prepared {
545545

546546
if self.fade_in {
547547
if let Some(last_became_visible_at) = self.state.last_became_visible_at {
548-
let age = ctx.input(|i| (i.time - last_became_visible_at) as f32 + i.predicted_dt);
548+
let age =
549+
ctx.input(|i| (i.time - last_became_visible_at) as f32 + i.predicted_dt / 2.0);
549550
let opacity = crate::remap_clamp(age, 0.0..=ctx.style().animation_time, 0.0..=1.0);
550-
let opacity = emath::easing::cubic_out(opacity); // slow fade-out = quick fade-in
551+
let opacity = emath::easing::quadratic_out(opacity); // slow fade-out = quick fade-in
551552
ui.multiply_opacity(opacity);
552553
if opacity < 1.0 {
553554
ctx.request_repaint();

crates/egui/src/context.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl ContextImpl {
146146

147147
fn request_repaint_after(
148148
&mut self,
149-
delay: Duration,
149+
mut delay: Duration,
150150
viewport_id: ViewportId,
151151
cause: RepaintCause,
152152
) {
@@ -163,6 +163,11 @@ impl ContextImpl {
163163
// Hovering a tooltip is a good example of a case where we want to repaint after a delay.
164164
}
165165

166+
if let Ok(predicted_frame_time) = Duration::try_from_secs_f32(viewport.input.predicted_dt) {
167+
// Make it less likely we over-shoot the target:
168+
delay = delay.saturating_sub(predicted_frame_time);
169+
}
170+
166171
viewport.repaint.causes.push(cause);
167172

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

0 commit comments

Comments
 (0)