use wasm_bindgen::prelude::*; use std::cell::RefCell; use std::rc::Rc; use js_sys::Date; use wasm_bindgen::JsCast; mod drawing; mod shapes; mod circle; use shapes::*; use circle::CircleShapes; fn request_animation_frame(f: &Closure) { web_sys::window() .unwrap() .request_animation_frame( f.as_ref() .unchecked_ref() ).unwrap(); } #[wasm_bindgen(start)] pub fn start() { let canvas_id = "circle_canvas"; let document = web_sys::window().unwrap().document().unwrap(); let canvas = document.get_element_by_id(canvas_id).unwrap(); let canvas: web_sys::HtmlCanvasElement = canvas .dyn_into::() .map_err(|_| ()) .unwrap(); let context = canvas .get_context("2d") .unwrap() .unwrap() .dyn_into::() .unwrap(); let f = Rc::new(RefCell::new(None)); let g = f.clone(); let mut timestamp: Option = None; let mut factor = 2; let mut modulo = 10; *g.borrow_mut() = Some(Closure::wrap(Box::new(move || { match timestamp { Some(date) if (Date::now() - date) < 250. => (), _ => { timestamp = Some(Date::now()); context.begin_path(); let iter = CircleShapes::new(modulo, factor, Circle {center: Point {x: 400.0, y: 400.0}, radius: 400.0}); for shape in iter { drawing::draw_shape(shape, &context); } context.stroke(); factor += 1; if factor == modulo { factor = 2; modulo += 1; } } } request_animation_frame(f.borrow().as_ref().unwrap()); }) as Box)); request_animation_frame(g.borrow().as_ref().unwrap()); }