diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 1d08f68d77c11..d05679532806f 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -206,6 +206,14 @@ pub struct Window { /// /// This value has no effect on non-web platforms. pub canvas: Option, + /// Whether or not to fit the canvas element's size to its parent element's size. + /// + /// **Warning**: this will not behave as expected for parents that set their size according to the size of their + /// children. This creates a "feedback loop" that will result in the canvas growing on each resize. When using this + /// feature, ensure the parent's size is not affected by its children. + /// + /// This value has no effect on non-web platforms. + pub fit_canvas_to_parent: bool, /// Whether or not to stop events from propagating out of the canvas element /// /// When `true`, this will prevent common browser hotkeys like F5, F12, Ctrl+R, tab, etc. @@ -274,6 +282,7 @@ impl Default for Window { transparent: false, focused: true, window_level: Default::default(), + fit_canvas_to_parent: false, prevent_default_event_handling: true, canvas: None, window_theme: None, diff --git a/crates/bevy_winit/Cargo.toml b/crates/bevy_winit/Cargo.toml index 5574b67438d71..c2d4efbedd2ea 100644 --- a/crates/bevy_winit/Cargo.toml +++ b/crates/bevy_winit/Cargo.toml @@ -45,7 +45,6 @@ winit = { version = "0.29", default-features = false, features = [ [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = { version = "0.2" } web-sys = "0.3" - crossbeam-channel = "0.5" [package.metadata.docs.rs] diff --git a/crates/bevy_winit/src/system.rs b/crates/bevy_winit/src/system.rs index 48ccc1e02f62e..44bcc43e6c76e 100644 --- a/crates/bevy_winit/src/system.rs +++ b/crates/bevy_winit/src/system.rs @@ -17,6 +17,9 @@ use winit::{ event_loop::EventLoopWindowTarget, }; +#[cfg(target_arch = "wasm32")] +use winit::platform::web::WindowExtWebSys; + use crate::{ converters::{ self, convert_enabled_buttons, convert_window_level, convert_window_theme, @@ -80,6 +83,17 @@ pub fn create_windows( window: window.clone(), }); + #[cfg(target_arch = "wasm32")] + { + if window.fit_canvas_to_parent { + let canvas = winit_window + .canvas() + .expect("window.canvas() can only be called in main thread."); + let style = canvas.style(); + style.set_property("width", "100%").unwrap(); + style.set_property("height", "100%").unwrap(); + } + } window_created_events.send(WindowCreated { window: entity }); } } diff --git a/examples/wasm/index.html b/examples/wasm/index.html index 0fa46de41877f..1688962669241 100644 --- a/examples/wasm/index.html +++ b/examples/wasm/index.html @@ -15,8 +15,6 @@ background-size: 20px 20px; } canvas { - width: 100%; - height: 100%; background-color: white; } diff --git a/examples/window/window_settings.rs b/examples/window/window_settings.rs index 31ea347e64db6..2e509e2fba7eb 100644 --- a/examples/window/window_settings.rs +++ b/examples/window/window_settings.rs @@ -17,6 +17,8 @@ fn main() { name: Some("bevy.app".into()), resolution: (500., 300.).into(), present_mode: PresentMode::AutoVsync, + // Tells wasm to resize the window according to the available canvas + fit_canvas_to_parent: true, // Tells wasm not to override default event handling, like F5, Ctrl+R etc. prevent_default_event_handling: false, window_theme: Some(WindowTheme::Dark), diff --git a/tools/example-showcase/window-settings-wasm.patch b/tools/example-showcase/window-settings-wasm.patch index 02e8965901869..5fcb1fba9556c 100644 --- a/tools/example-showcase/window-settings-wasm.patch +++ b/tools/example-showcase/window-settings-wasm.patch @@ -1,11 +1,13 @@ diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs -index 7b5c75d38..8e9404b93 100644 +index 87cdfb050..1d87a0bf5 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs -@@ -245,8 +245,8 @@ impl Default for Window { +@@ -266,9 +266,9 @@ impl Default for Window { transparent: false, focused: true, window_level: Default::default(), +- fit_canvas_to_parent: false, ++ fit_canvas_to_parent: true, prevent_default_event_handling: true, - canvas: None, + canvas: Some("#bevy".to_string()),