use std::time::{SystemTime, UNIX_EPOCH};
use wasm_bindgen::prelude::*;
pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
pub fn warn(s: &str);
pub fn error(s: &str);
pub fn debug(s: &str);
pub fn info(s: &str);
}
#[macro_export]
macro_rules! log {
( $( $t:tt )* ) => {
$crate::utils::log(&format!( $( $t )* ));
}
}
#[macro_export]
macro_rules! info {
( $( $t:tt )* ) => {
$crate::utils::info(&format!( $( $t )* ));
}
}
#[macro_export]
macro_rules! warn {
( $( $t:tt )* ) => {
$crate::utils::warn(&format!( $( $t )* ));
}
}
#[macro_export]
macro_rules! error {
( $( $t:tt )* ) => {
$crate::utils::error(&format!( $( $t )* ));
}
}
#[macro_export]
macro_rules! debug {
( $( $t:tt )* ) => {
$crate::utils::debug(&format!( $( $t )* ));
}
}
pub fn get_font_with_limit(ctx: &web_sys::CanvasRenderingContext2d, text: &str, size: f64, font: &str) -> u32 {
let mut px = 5;
if text.trim().len() < 1 {
return px;
}
let mut style: String;
let mut res: Result<web_sys::TextMetrics, JsValue>;
for _ in 0..1000 {
style = format!("{}px {}", px, font);
ctx.set_font(&style);
res = ctx.measure_text(text);
if res.unwrap().width() >= size {
return px;
}
px += 2;
}
px
}
#[allow(dead_code)]
#[inline]
pub fn now_ms() -> u128 {
web_sys::window().unwrap().performance().unwrap().now() as u128
}