Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smoother animations #4787

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading