Using web-sys
Add web-sys
as a dependency to your Cargo.toml
[dependencies]
wasm-bindgen = "0.2"
[dependencies.web-sys]
version = "0.3"
features = [
]
Enable the cargo features for the APIs you're using
To keep build times super speedy, web-sys
gates each Web interface behind a
cargo feature. Find the type or method you want to use
in the API documentation; it will list the features that must be enabled
to access that API.
For example, if we're looking for the window.resizeTo
function, we would search for resizeTo
in the API
documentation. We would find the
web_sys::Window::resize_to
function, which requires the
Window
feature. To get access to that function, we enable the Window
feature
in Cargo.toml
:
[dependencies.web-sys]
version = "0.3"
features = [
"Window"
]
Call the method!
#![allow(unused)] fn main() { use wasm_bindgen::prelude::*; use web_sys::Window; #[wasm_bindgen] pub fn make_the_window_small() { // Resize the window to 500px by 500px. let window = web_sys::window().unwrap(); window.resize_to(500, 500) .expect("could not resize the window"); } }