Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use wasm_bindgen::JsCast;
- use wasm_bindgen::closure::Closure;
- use web_sys::{Document, HtmlCanvasElement, HtmlElement, Window};
- fn window() -> Window {
- web_sys::window().expect("no global `window` exists")
- }
- fn document() -> Document {
- window()
- .document()
- .expect("should have a document on window")
- }
- fn body() -> HtmlElement {
- document().body().expect("document should have a body")
- }
- fn request_animation_frame(f: &Closure<dyn FnMut()>) {
- window()
- .request_animation_frame(f.as_ref().unchecked_ref())
- .expect("should register `requestAnimationFrame` OK");
- }
- fn foo() {
- web_sys::console::log_1(&"hello wasm".into());
- let f: Rc<RefCell<Option<Closure<dyn FnMut()>>>> = Rc::new(RefCell::new(None));
- let g = f.clone();
- let h = g.clone();
- let mut i = 0;
- let closure = Closure::wrap(Box::new(move || {
- if i > 300 {
- body().set_text_content(Some("All done!"));
- return;
- }
- // Set the body's text content to how many times this
- // requestAnimationFrame callback has fired.
- i += 1;
- request_animation_frame(h.borrow().as_ref().unwrap());
- }) as Box<dyn FnMut()>);
- *g.borrow_mut() = Some(closure);
- request_animation_frame(f.borrow().as_ref().unwrap());
- }
Advertisement
Add Comment
Please, Sign In to add comment