use crate::dom::window;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::{JsCast, JsValue};
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")
}
}
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,
})
}