Browse Source

Smoother animations (#4787)

This makes animations slightly smoother, especially in reactive mode
pull/4789/head
Emil Ernerfeldt 4 months ago
committed by GitHub
parent
commit
0be4450e3d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      crates/egui/src/animation_manager.rs
  2. 5
      crates/egui/src/containers/area.rs
  3. 7
      crates/egui/src/context.rs

4
crates/egui/src/animation_manager.rs

@ -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,

5
crates/egui/src/containers/area.rs

@ -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();

7
crates/egui/src/context.rs

@ -146,7 +146,7 @@ impl ContextImpl {
fn request_repaint_after(
&mut self,
delay: Duration,
mut delay: Duration,
viewport_id: ViewportId,
cause: RepaintCause,
) {
@ -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.

Loading…
Cancel
Save