diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 28 | ||||
| -rw-r--r-- | src/state.rs | 13 |
2 files changed, 33 insertions, 8 deletions
@@ -66,6 +66,17 @@ fn results() -> JSResult<web_sys::HtmlElement> { Ok(x.dyn_into()?) } +fn dots() -> JSResult<web_sys::HtmlInputElement> { + let x = document() + .query_selector("#dots")? + .ok_or("no dots element")?; + Ok(x.dyn_into()?) +} + +fn dots_val() -> JSResult<usize> { + Ok(dots()?.value().parse().map_err(|_| "dots isn't int")?) +} + #[wasm_bindgen(start)] pub fn init() -> JSResult<()> { console_log::init_with_level(Level::Debug) @@ -76,10 +87,12 @@ pub fn init() -> JSResult<()> { let p1 = paused.clone(); let mut s = State::new(canvas()?, fps()?)?; + s.set_dot_count(dots_val()?); s.render_frame(0.0)?; let shared_s = Rc::new(RefCell::new(s)); let s1 = shared_s.clone(); + let s2 = shared_s.clone(); let render_loop = RenderLoop::new(move |t| { let mut s = shared_s.borrow_mut(); @@ -88,6 +101,18 @@ pub fn init() -> JSResult<()> { Ok(!*paused.lock().unwrap()) }); + let dot_count_handler: HandlerClosure = Closure::new(move |e| { + debug!("dot count changed {:?} - {}", e, dots_val()?); + let mut s = s2.borrow_mut(); + s.set_dot_count(dots_val()?); + s.render_frame(0.0)?; + Ok(()) + }); + dots()?.set_onchange(Some(dot_count_handler.as_ref().unchecked_ref())); + // otherwise it gets dropped after we're done, invalidating the + // handler. + dot_count_handler.forget(); + let go_handler: HandlerClosure = Closure::new(move |e| { debug!("go clicked: {:?}", e); let mut p = p1.lock().unwrap(); @@ -102,8 +127,7 @@ pub fn init() -> JSResult<()> { Ok(()) }); go()?.set_onclick(Some(go_handler.as_ref().unchecked_ref())); - // otherwise it gets dropped after we're done, invalidating the - // handler. + // ibid. go_handler.forget(); let bench_handler: HandlerClosure = Closure::new(move |e| { diff --git a/src/state.rs b/src/state.rs index 592f26e..1793388 100644 --- a/src/state.rs +++ b/src/state.rs @@ -11,7 +11,6 @@ use crate::point::Point; pub const TAU: f64 = PI * 2.0; pub const MAX_SPEED: f64 = 0.01; -const NUM_POINTS: usize = 40; const VELVEC_SCALE: f64 = 3.0; #[derive(Debug)] @@ -33,15 +32,11 @@ impl State { .dyn_into()?; ctx.scale(canvas.width().into(), canvas.height().into())?; - let points = (0..NUM_POINTS) - .map(|_| Point::new(fastrand::f64(), fastrand::f64())) - .collect(); - Ok(Self { canvas, ctx, fps, - points, + points: vec![], last_time: None, inter_count: 1, }) @@ -104,6 +99,12 @@ impl State { Ok(()) } + pub fn set_dot_count(&mut self, n: usize) { + self.points = (0..n) + .map(|_| Point::new(fastrand::f64(), fastrand::f64())) + .collect(); + } + fn render_points(&self) -> JSResult<()> { for p in &self.points { self.ctx.set_fill_style_str(&p.color); |
