add timer

This commit is contained in:
histausse 2022-02-13 14:48:00 +01:00
parent a6f89ab707
commit 8333df9474
Signed by: histausse
GPG key ID: 67486F107F62E9E9
2 changed files with 45 additions and 8 deletions

View file

@ -20,7 +20,7 @@
transform: translate(-50%, -50%); transform: translate(-50%, -50%);
} }
</style> </style>
<canvas id="circle_canvas" height="150" width="150"></canvas> <canvas id="circle_canvas" height="800" width="800"></canvas>
</body> </body>
</html> </html>

View file

@ -1,4 +1,7 @@
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
use js_sys::Date;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
mod drawing; mod drawing;
@ -8,6 +11,15 @@ mod circle;
use shapes::*; use shapes::*;
use circle::CircleShapes; 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)] #[wasm_bindgen(start)]
pub fn start() { pub fn start() {
let canvas_id = "circle_canvas"; let canvas_id = "circle_canvas";
@ -25,11 +37,36 @@ pub fn start() {
.dyn_into::<web_sys::CanvasRenderingContext2d>() .dyn_into::<web_sys::CanvasRenderingContext2d>()
.unwrap(); .unwrap();
context.begin_path();
let iter = CircleShapes::new(100, 13, Circle {center: Point {x: 75.0, y: 75.0}, radius: 70.0}); 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 { for shape in iter {
drawing::draw_shape(shape, &context); drawing::draw_shape(shape, &context);
} }
context.stroke(); 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());
} }