From 44e63b0bbe4aaa419d69c4d2c5e1934795d5e833 Mon Sep 17 00:00:00 2001 From: Maan2003 Date: Wed, 19 May 2021 12:53:50 +0530 Subject: [PATCH] [web backend] Make sure we only request animation once per frame Earlier we call `requestAnimationFrame` every time on `invalidate_rect` or `request_anim_frame`. This lead to even `render()` being called many times per frame and hence poor performance. --- druid-shell/src/platform/web/window.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/druid-shell/src/platform/web/window.rs b/druid-shell/src/platform/web/window.rs index 4ba6237c00..28bbe28be9 100644 --- a/druid-shell/src/platform/web/window.rs +++ b/druid-shell/src/platform/web/window.rs @@ -113,6 +113,7 @@ struct WindowState { invalid: RefCell, click_counter: ClickCounter, active_text_input: Cell>, + rendering_soon: Cell, } // TODO: support custom cursors @@ -444,6 +445,7 @@ impl WindowBuilder { invalid: RefCell::new(Region::EMPTY), click_counter: ClickCounter::default(), active_text_input: Cell::new(None), + rendering_soon: Cell::new(false), }); setup_web_callbacks(&window); @@ -632,10 +634,14 @@ impl WindowHandle { fn render_soon(&self) { if let Some(s) = self.0.upgrade() { let state = s.clone(); - s.request_animation_frame(move || { - state.render(); - }) - .expect("Failed to request animation frame"); + if !state.rendering_soon.get() { + state.rendering_soon.set(true); + s.request_animation_frame(move || { + state.rendering_soon.set(false); + state.render(); + }) + .expect("Failed to request animation frame"); + } } }