Skip to content

Commit 26d95fd

Browse files
committed
Append Canvas to <body> when targetting Wasm in iced_winit
1 parent 825c774 commit 26d95fd

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

winit/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ path = "../futures"
3737

3838
[target.'cfg(target_os = "windows")'.dependencies.winapi]
3939
version = "0.3.6"
40+
41+
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
42+
version = "0.3"
43+
features = ["Document", "Window"]

winit/src/application.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ where
148148
.build(&event_loop)
149149
.map_err(Error::WindowCreationFailed)?;
150150

151+
#[cfg(target_arch = "wasm32")]
152+
{
153+
use winit::platform::web::WindowExtWebSys;
154+
155+
let canvas = window.canvas();
156+
157+
let window = web_sys::window().unwrap();
158+
let document = window.document().unwrap();
159+
let body = document.body().unwrap();
160+
161+
let _ = body
162+
.append_child(&canvas)
163+
.expect("Append canvas to HTML body");
164+
}
165+
151166
let mut clipboard = Clipboard::connect(&window);
152167

153168
run_command(

0 commit comments

Comments
 (0)