aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbrian cully <bjc@spork.org>2025-12-29 14:32:30 -0500
committerbrian cully <bjc@spork.org>2025-12-29 14:32:30 -0500
commit65d1301072a7f9223ce2ffe420b6f2784b8bbe04 (patch)
treec20667f8241a0211a23d6676213ea44843bbfb44
parent316286b8b65ac5a7abf5117dc942a70b94e1be53 (diff)
downloadpolyring-65d1301072a7f9223ce2ffe420b6f2784b8bbe04.tar.gz
polyring-65d1301072a7f9223ce2ffe420b6f2784b8bbe04.zip
wasm: hook up dot count to html input
-rw-r--r--site/index.html10
-rw-r--r--src/lib.rs28
-rw-r--r--src/state.rs13
3 files changed, 42 insertions, 9 deletions
diff --git a/site/index.html b/site/index.html
index 5ef2cf6..a540c27 100644
--- a/site/index.html
+++ b/site/index.html
@@ -12,6 +12,11 @@
<p class='subst-alts'></p>
<p>benchmarking maximal convex polygon finding</p>
+ <label>
+ dots:
+ <input id='dots' value='40'>
+ </label>
+
<section class='watch'>
<h2>watch</h2>
<button>go</button>
@@ -22,7 +27,10 @@
<section class='bench'>
<h2>bench</h2>
- <input name='iters' value='1000'>
+ <label>
+ iters:
+ <input name='iters' value='1000'>
+ </label>
<button>bench</button>
<p class='results'></p>
</section>
diff --git a/src/lib.rs b/src/lib.rs
index 8e650e5..71f0afd 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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);