You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.9 KiB
Rust
73 lines
1.9 KiB
Rust
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<dyn FnMut()>) {
|
|
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::<web_sys::HtmlCanvasElement>()
|
|
.map_err(|_| ())
|
|
.unwrap();
|
|
|
|
let context = canvas
|
|
.get_context("2d")
|
|
.unwrap()
|
|
.unwrap()
|
|
.dyn_into::<web_sys::CanvasRenderingContext2d>()
|
|
.unwrap();
|
|
|
|
|
|
let f = Rc::new(RefCell::new(None));
|
|
let g = f.clone();
|
|
|
|
let mut timestamp: Option<f64> = 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<dyn FnMut()>));
|
|
|
|
request_animation_frame(g.borrow().as_ref().unwrap());
|
|
}
|