Last active
July 8, 2021 03:20
-
-
Save lemmih/037a23f8391390df8560a2043a14121e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use gloo_events::EventListener; | |
use once_cell::sync::Lazy; | |
use std::sync::atomic::{Ordering, AtomicUsize}; | |
use std::sync::{Mutex, Once}; | |
use wasm_bindgen::JsCast; | |
use rgeometry::algorithms::convex_hull; | |
fn ui_range(min: usize, max: usize) { | |
let corners = N_CORNERS.load(Ordering::Relaxed); | |
let document = web_sys::window().unwrap().document().unwrap(); | |
let overlay = document.get_element_by_id("ui-overlay").unwrap(); | |
let val = document | |
.create_element("input") | |
.unwrap() | |
.dyn_into::<web_sys::HtmlInputElement>() | |
.unwrap(); | |
val.set_attribute("type", "range").unwrap(); | |
val.set_attribute("value", &corners.to_string()).unwrap(); | |
val.set_attribute("min", &min.to_string()).unwrap(); | |
val.set_attribute("max", &max.to_string()).unwrap(); | |
overlay.append_child(&val).unwrap(); | |
let ev = EventListener::new(&val, "input", move |event| { | |
let target = event.target().unwrap(); | |
let target = target.dyn_into::<web_sys::HtmlInputElement>().unwrap(); | |
let n = target.value().parse::<usize>().unwrap_or(3); | |
N_CORNERS.store(n, Ordering::Relaxed); | |
{ | |
let mut p = POLYGON.lock().unwrap(); | |
*p = gen_convex(); | |
} | |
clear_screen(); | |
main(); | |
}); | |
ev.forget(); | |
} | |
static N_CORNERS: AtomicUsize = AtomicUsize::new(5); | |
static POLYGON: Lazy<Mutex<PolygonConvex<Num>>> = Lazy::new(|| Mutex::new(gen_convex())); | |
fn gen_convex() -> PolygonConvex<Num> { | |
let n = N_CORNERS.load(Ordering::Relaxed); | |
PolygonConvex::<i32>::random(n, &mut rand::thread_rng()) | |
.float() | |
.normalize() | |
} | |
pub fn main() { | |
static START: Once = Once::new(); | |
START.call_once(|| { | |
ui_range(3, 20); | |
on_canvas_click(|| { | |
{ | |
let mut p = POLYGON.lock().unwrap(); | |
*p = gen_convex(); | |
} | |
clear_screen(); | |
main(); | |
}); | |
}); | |
let p = POLYGON.lock().unwrap(); | |
render_polygon(&p); | |
set_fill_style("grey"); | |
fill(); | |
for pt in p.iter_boundary() { | |
render_fixed_point(pt.point()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment