invaders/src/utils.rs

24 lines
773 B
Rust

use wasm_bindgen::prelude::*;
use web_sys::window;
/// set the console error panic hook if the respecive feature is enabled
pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
/// display an alert with an error message passed as a JsValue.
pub fn alert_err(msg: JsValue) {
let mut s = String::from("ERROR! --- ");
s.push_str(&msg.as_string().unwrap_or_else(|| String::from("unknown")));
window().unwrap().alert_with_message(&s).unwrap();
}
/// helper function for using javascript's `window.requestAnimationFrame()`
pub fn request_animation_frame(f: &Closure<dyn FnMut(f32)>) {
window()
.unwrap()
.request_animation_frame(f.as_ref().unchecked_ref())
.unwrap();
}