Skip to content

Commit b7e7755

Browse files
authored
Improve web example (#2115)
* Improve web example * Implement basic logger into the example webpage * Repace bash script with xtask * replace wasm-bindgen-cli with wasm-bindgen-cli-support * refactor * Move logic into external crate. * Remove CI changes * Review feedback
1 parent 40f48cb commit b7e7755

File tree

6 files changed

+63
-13
lines changed

6 files changed

+63
-13
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[alias]
2+
run-wasm = ["run", "--release", "--package", "run-wasm", "--"]

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,8 @@ version = "0.2.45"
125125

126126
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
127127
console_log = "0.2"
128+
129+
[workspace]
130+
members = [
131+
"run-wasm",
132+
]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Winit provides the following features, which can be enabled in your `Cargo.toml`
7171

7272
#### WebAssembly
7373

74+
To run the web example: `cargo run-wasm --example web`
75+
7476
Winit supports compiling to the `wasm32-unknown-unknown` target with `web-sys`.
7577

7678
On the web platform, a Winit window is backed by a `<canvas>` element. You can

examples/web.rs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,13 @@ pub fn main() {
1313
.unwrap();
1414

1515
#[cfg(target_arch = "wasm32")]
16-
{
17-
use winit::platform::web::WindowExtWebSys;
18-
19-
let canvas = window.canvas();
20-
21-
let window = web_sys::window().unwrap();
22-
let document = window.document().unwrap();
23-
let body = document.body().unwrap();
24-
25-
body.append_child(&canvas)
26-
.expect("Append canvas to HTML body");
27-
}
16+
let log_list = wasm::create_log_list(&window);
2817

2918
event_loop.run(move |event, _, control_flow| {
3019
*control_flow = ControlFlow::Wait;
3120

3221
#[cfg(target_arch = "wasm32")]
33-
log::debug!("{:?}", event);
22+
wasm::log_event(&log_list, &event);
3423

3524
match event {
3625
Event::WindowEvent {
@@ -48,11 +37,51 @@ pub fn main() {
4837
#[cfg(target_arch = "wasm32")]
4938
mod wasm {
5039
use wasm_bindgen::prelude::*;
40+
use winit::{event::Event, window::Window};
5141

5242
#[wasm_bindgen(start)]
5343
pub fn run() {
5444
console_log::init_with_level(log::Level::Debug).expect("error initializing logger");
5545

5646
super::main();
5747
}
48+
49+
pub fn create_log_list(window: &Window) -> web_sys::Element {
50+
use winit::platform::web::WindowExtWebSys;
51+
52+
let canvas = window.canvas();
53+
54+
let window = web_sys::window().unwrap();
55+
let document = window.document().unwrap();
56+
let body = document.body().unwrap();
57+
58+
// Set a background color for the canvas to make it easier to tell the where the canvas is for debugging purposes.
59+
canvas.style().set_css_text("background-color: crimson;");
60+
body.append_child(&canvas).unwrap();
61+
62+
let log_header = document.create_element("h2").unwrap();
63+
log_header.set_text_content(Some("Event Log"));
64+
body.append_child(&log_header).unwrap();
65+
66+
let log_list = document.create_element("ul").unwrap();
67+
body.append_child(&log_list).unwrap();
68+
log_list
69+
}
70+
71+
pub fn log_event(log_list: &web_sys::Element, event: &Event<()>) {
72+
log::debug!("{:?}", event);
73+
74+
// Getting access to browser logs requires a lot of setup on mobile devices.
75+
// So we implement this basic logging system into the page to give developers an easy alternative.
76+
// As a bonus its also kind of handy on desktop.
77+
if let Event::WindowEvent { event, .. } = &event {
78+
let window = web_sys::window().unwrap();
79+
let document = window.document().unwrap();
80+
let log = document.create_element("li").unwrap();
81+
log.set_text_content(Some(&format!("{:?}", event)));
82+
log_list
83+
.insert_before(&log, log_list.first_child().as_ref())
84+
.unwrap();
85+
}
86+
}
5887
}

run-wasm/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "run-wasm"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
cargo-run-wasm = "0.1.0"

run-wasm/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
cargo_run_wasm::run_wasm();
3+
}

0 commit comments

Comments
 (0)