Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to give existing canvas element as winit window #515

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ uuid = { version = "0.8", features = ["v4", "serde"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
uuid = { version = "0.8", features = ["wasm-bindgen"] }
web-sys = "0.3"
8 changes: 8 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub struct Window {
pub vsync: bool,
pub resizable: bool,
pub mode: WindowMode,
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
}

/// Defines the way a window is displayed
Expand All @@ -64,6 +66,8 @@ impl Window {
vsync: window_descriptor.vsync,
resizable: window_descriptor.resizable,
mode: window_descriptor.mode,
#[cfg(target_arch = "wasm32")]
canvas: window_descriptor.canvas.clone(),
}
}
}
Expand All @@ -77,6 +81,8 @@ pub struct WindowDescriptor {
pub vsync: bool,
pub resizable: bool,
pub mode: WindowMode,
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,

// this is a manual implementation of the non exhaustive pattern,
// especially made to allow ..Default::default()
Expand All @@ -93,6 +99,8 @@ impl Default for WindowDescriptor {
vsync: true,
resizable: true,
mode: WindowMode::Windowed,
#[cfg(target_arch = "wasm32")]
canvas: None,
__non_exhaustive: (),
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ log = { version = "0.4", features = ["release_max_level_info"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
winit = { version = "0.22.2", package = "cart-tmp-winit", features = ["web-sys"] }
wasm-bindgen = { version = "0.2" }
web-sys = "0.3"
42 changes: 32 additions & 10 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,30 @@ impl WinitWindows {
.with_resizable(window.resizable),
};

let winit_window = winit_window_builder
.with_title(&window.title)
.build(&event_loop)
.unwrap();
#[allow(unused_mut)]
let mut winit_window_builder = winit_window_builder.with_title(&window.title);

#[cfg(target_arch = "wasm32")]
{
use wasm_bindgen::JsCast;
use winit::platform::web::WindowBuilderExtWebSys;

if let Some(selector) = &window.canvas {
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let canvas = document
.query_selector(&selector)
.expect("Cannot query for canvas element");
if let Some(canvas) = canvas {
let canvas = canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok();
winit_window_builder = winit_window_builder.with_canvas(canvas);
} else {
panic!("Cannot find element: {}", selector);
}
}
}

let winit_window = winit_window_builder.build(&event_loop).unwrap();

self.window_id_to_winit.insert(window.id, winit_window.id());
self.winit_to_window_id.insert(winit_window.id(), window.id);
Expand All @@ -50,14 +70,16 @@ impl WinitWindows {
{
use winit::platform::web::WindowExtWebSys;

let canvas = winit_window.canvas();
if window.canvas.is_none() {
let canvas = winit_window.canvas();

let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let body = document.body().unwrap();
let window = web_sys::window().unwrap();
let document = window.document().unwrap();
let body = document.body().unwrap();

body.append_child(&canvas)
.expect("Append canvas to HTML body");
body.append_child(&canvas)
.expect("Append canvas to HTML body");
}
}

self.windows.insert(winit_window.id(), winit_window);
Expand Down