Debian Codegraph @

.cargo/vendor/wasm-bindgen/guide/src/web-sys/using-web-sys.md

raw
1 # Using `web-sys`
2
3 ## Add `web-sys` as a dependency to your `Cargo.toml`
4
5 ```toml
6 [dependencies]
7 wasm-bindgen = "0.2"
8
9 [dependencies.web-sys]
10 version = "0.3"
11 features = [
12 ]
13 ```
14
15 ## Enable the cargo features for the APIs you're using
16
17 To keep build times super speedy, [`web-sys` gates each Web interface behind a
18 cargo feature](./cargo-features.html). Find the type or method you want to use
19 in the [API documentation][api]; it will list the features that must be enabled
20 to access that API.
21
22 For example, if we're looking for [the `window.resizeTo`
23 function][js-resize-to], we would [search for `resizeTo` in the API
24 documentation][search-resize-to]. We would find [the
25 `web_sys::Window::resize_to` function][rust-resize-to], which requires the
26 `Window` feature. To get access to that function, we enable the `Window` feature
27 in `Cargo.toml`:
28
29 ```toml
30 [dependencies.web-sys]
31 version = "0.3"
32 features = [
33 "Window"
34 ]
35 ```
36
37 ## Call the method!
38
39 ```rust
40 use wasm_bindgen::prelude::*;
41 use web_sys::Window;
42
43 #[wasm_bindgen]
44 pub fn make_the_window_small() {
45 // Resize the window to 500px by 500px.
46 let window = web_sys::window().unwrap();
47 window.resize_to(500, 500)
48 .expect("could not resize the window");
49 }
50 ```
51
52 [api]: https://rustwasm.github.io/wasm-bindgen/api/web_sys/
53 [js-resize-to]: https://developer.mozilla.org/en-US/docs/Web/API/window/resizeTo
54 [search-resize-to]: https://rustwasm.github.io/wasm-bindgen/api/web_sys/?search=resizeTo
55 [rust-resize-to]: https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.Window.html#method.resize_to