1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 14:20:04 -04:00

Add some helpful wrapper around web_sys

This commit is contained in:
Emil Ernerfeldt
2019-02-11 20:27:32 +01:00
parent eb589757a8
commit 68db833a3a
5 changed files with 40 additions and 30 deletions

View File

@@ -18,10 +18,12 @@ emigui = { path = "../emigui" }
[dependencies.web-sys]
version = "0.3"
features = [
'console',
'Document',
'Element',
'HtmlCanvasElement',
'Performance',
'Storage',
'WebGlBuffer',
'WebGlProgram',
'WebGlRenderingContext',

View File

@@ -5,3 +5,35 @@ extern crate wasm_bindgen;
extern crate emigui;
pub mod webgl;
// ----------------------------------------------------------------------------
// Helpers to hide some of the verbosity of web_sys
pub fn console_log(s: String) {
web_sys::console::log_1(&s.into());
}
pub fn now_sec() -> f64 {
web_sys::window()
.expect("should have a Window")
.performance()
.expect("should have a Performance")
.now()
/ 1000.0
}
pub fn local_storage() -> Option<web_sys::Storage> {
web_sys::window()?.local_storage().ok()?
}
pub fn local_storage_get(key: &str) -> Option<String> {
local_storage().map(|storage| storage.get_item(key).ok())??
}
pub fn local_storage_set(key: &str, value: &str) {
local_storage().map(|storage| storage.set_item(key, value));
}
pub fn local_storage_remove(key: &str) {
local_storage().map(|storage| storage.remove_item(key));
}