diff options
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 42 |
1 files changed, 4 insertions, 38 deletions
@@ -1,45 +1,17 @@ -use std::cell::RefCell; -use std::sync::{Mutex}; use std::rc::Rc; +use std::sync::Mutex; -use log::{Level, info, debug}; +use log::{Level, debug, info}; use wasm_bindgen::prelude::*; use state::State; +use render_loop::RenderLoop; mod line; mod point; +mod render_loop; mod state; -struct RenderLoop { - inner: Rc<RefCell<Option<Closure<dyn FnMut(f64)>>>>, -} - -impl RenderLoop { - fn new<T: FnMut(f64) -> bool + 'static>(mut fun: T) -> Self { - let inner = Rc::new(RefCell::new(None)); - let rloop = inner.clone(); - - *inner.borrow_mut() = Some(Closure::new(move |t| { - if fun(t) { - request_animation_frame(rloop.borrow().as_ref().expect("can borrow rloop")); - } - })); - Self { inner } - } - - fn start(&self) -> Result<(), JsValue> { - request_animation_frame(self.inner.borrow().as_ref().ok_or("closure exists")?); - Ok(()) - } -} - -impl Clone for RenderLoop { - fn clone(&self) -> Self { - Self { inner: self.inner.clone() } - } -} - fn window() -> web_sys::Window { web_sys::window().expect("no window") } @@ -69,12 +41,6 @@ fn go() -> Result<web_sys::HtmlElement, JsValue> { Ok(x.dyn_into::<web_sys::HtmlElement>()?) } -fn request_animation_frame(f: &Closure<dyn FnMut(f64)>) { - window() - .request_animation_frame(f.as_ref().unchecked_ref()) - .expect("should register `requestAnimationFrame` OK"); -} - #[wasm_bindgen(start)] pub fn init() -> Result<(), JsValue> { console_log::init_with_level(Level::Debug).expect("couldn't init console log"); |
