aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs5
-rw-r--r--src/render_loop.rs16
-rw-r--r--src/state.rs12
3 files changed, 22 insertions, 11 deletions
diff --git a/src/lib.rs b/src/lib.rs
index a798872..87f31ff 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -52,8 +52,9 @@ pub fn init() -> JSResult<()> {
s.render_frame(0.0)?;
let render_loop = RenderLoop::new(move |t| {
- s.render_frame(t).expect("should render frame");
- !*paused.lock().unwrap()
+ s.render_frame(t)?;
+ s.update()?;
+ Ok(!*paused.lock().unwrap())
});
let a: HandlerClosure = Closure::new(move |e| {
diff --git a/src/render_loop.rs b/src/render_loop.rs
index 628e687..7056493 100644
--- a/src/render_loop.rs
+++ b/src/render_loop.rs
@@ -4,6 +4,8 @@ use std::rc::Rc;
use log::error;
use wasm_bindgen::prelude::*;
+use crate::JSResult;
+
/// use `window.requestAnimationFrame()` to schedule calling a
/// function as long as the function returns true.
pub struct RenderLoop {
@@ -21,7 +23,7 @@ impl RenderLoop {
/// `fun` takes a timestamp in the same space as the document
/// timeline and returns a flag specifying whether we should
/// schedule another frame..
- pub fn new<T: FnMut(f64) -> bool + 'static>(mut fun: T) -> Self {
+ pub fn new<T: FnMut(f64) -> JSResult<bool> + 'static>(mut fun: T) -> Self {
// init with stub closure because rust wants that, then change
// it later once we have our rc clone.
let inner = Rc::new(UnsafeCell::new(Closure::new(|_| {})));
@@ -29,11 +31,15 @@ impl RenderLoop {
let rloop = inner.clone();
let f = unsafe { &mut *inner.get() };
*f = Closure::new(move |t| {
- if fun(t) {
- let cl = unsafe { &*rloop.get() };
- if let Err(e) = Self::request_animation_frame(cl) {
- error!("couldn't request animation frame: {:?}", e);
+ match fun(t) {
+ Err(e) => error!("render callback error: {:?}", e),
+ Ok(true) => {
+ let cl = unsafe { &*rloop.get() };
+ if let Err(e) = Self::request_animation_frame(cl) {
+ error!("couldn't request animation frame: {:?}", e);
+ }
}
+ Ok(false) => {},
}
});
diff --git a/src/state.rs b/src/state.rs
index 61e82c9..09120fb 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -67,10 +67,6 @@ impl State {
self.canvas.height().into(),
);
self.render_points()?;
- if self.bounce_points() {
- //debug!("point bounced");
- }
- self.move_points();
// poly finding assumes sorted
self.points.sort_by(|a, b| {
@@ -100,6 +96,14 @@ impl State {
Ok(())
}
+ pub fn update(&mut self) -> JSResult<()> {
+ if self.bounce_points() {
+ //debug!("point bounced");
+ }
+ self.move_points();
+ Ok(())
+ }
+
fn render_points(&self) -> JSResult<()> {
for p in &self.points {
self.ctx.set_fill_style_str(&p.color);