1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use crate::dom::window;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::{JsCast, JsValue};

/// request animation frame handle
pub struct AnimationFrameHandle {
    handle: i32,
    _closure: Closure<dyn FnMut()>,
}
impl Drop for AnimationFrameHandle {
    fn drop(&mut self) {
        window()
            .cancel_animation_frame(self.handle)
            .expect("cancel animation handle")
    }
}

/// utility function which a closure in request animation frame
pub fn request_animation_frame<F>(f: F) -> Result<AnimationFrameHandle, JsValue>
where
    F: FnMut() + 'static,
{
    let closure: Closure<dyn FnMut() + 'static> = Closure::once(f);
    let handle = window().request_animation_frame(closure.as_ref().unchecked_ref())?;
    Ok(AnimationFrameHandle {
        handle,
        _closure: closure,
    })
}