Fix wasm examples (#4639)

This commit is contained in:
Connor Fitzgerald authored and GitHub committed 2023-11-06 03:55:45 +00:00
1 parent 873f19dec6
commit bc7f4beedb
2 files changed
+11 -8

No files matched your search

+3 -1
View File
@@ -166,6 +166,8 @@ impl SurfaceWrapper {
fn resume(&mut self, context: &ExampleContext, window: &Window, srgb: bool) {
// Window size is only actually valid after we enter the event loop.
let window_size = window.inner_size();
let width = window_size.width.max(1);
let height = window_size.height.max(1);
log::info!("Surface resume {window_size:?}");
@@ -180,7 +182,7 @@ impl SurfaceWrapper {
// Get the default configuration,
let mut config = surface
.get_default_config(&context.adapter, window_size.width, window_size.height)
.get_default_config(&context.adapter, width, height)
.expect("Surface isn't supported by the adapter.");
if srgb {
// Not all platforms (WebGPU) support sRGB swapchains, so we need to use view formats
+8 -7
View File
@@ -6,7 +6,9 @@ use winit::{
};
async fn run(event_loop: EventLoop<()>, window: Window) {
let size = window.inner_size();
let mut size = window.inner_size();
size.width = size.width.max(1);
size.height = size.height.max(1);
let instance = wgpu::Instance::default();
@@ -97,8 +99,8 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
match event {
WindowEvent::Resized(new_size) => {
// Reconfigure the surface with the new size
config.width = new_size.width;
config.height = new_size.height;
config.width = new_size.width.max(1);
config.height = new_size.height.max(1);
surface.configure(&device, &config);
// On macos the window needs to be redrawn manually after resizing
window.request_redraw();
@@ -158,14 +160,13 @@ fn main() {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init().expect("could not initialize logger");
use winit::platform::web::WindowExtWebSys;
let canvas = window.canvas().expect("Couldn't get canvas");
canvas.style().set_css_text("height: 100%; width: 100%;");
// On wasm, append the canvas to the document body
web_sys::window()
.and_then(|win| win.document())
.and_then(|doc| doc.body())
.and_then(|body| {
body.append_child(&web_sys::Element::from(window.canvas().unwrap()))
.ok()
})
.and_then(|body| body.append_child(&canvas).ok())
.expect("couldn't append canvas to document body");
wasm_bindgen_futures::spawn_local(run(event_loop, window));
}