From da320031c94d4360d10b8f57bb5fe399c0deb47e Mon Sep 17 00:00:00 2001 From: ghostly_zsh Date: Wed, 19 Mar 2025 21:14:02 -0500 Subject: [PATCH] resize canvas from wasm, not html --- crates/client/Cargo.toml | 2 +- crates/client/index.html | 4 +--- crates/client/src/rendering/mod.rs | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 57275532aeea99ee95da21f7450ead500ad72105..77bb2ba2c7b98a8dd0610181e128319e60ec1d88 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -32,7 +32,7 @@ bytemuck = "1.22.0" tracing-web = "0.1" # Log output console_error_panic_hook = "0.1" # Give useful information in the panic response, other than the useless "entered unreachable code" wasm-bindgen = "0.2" -web-sys = { version = "0.3", features = ["Window", "Location", "WebSocket", "MessageEvent"] } +web-sys = { version = "0.3", features = ["Window", "Location", "WebSocket", "MessageEvent", "HtmlCanvasElement"] } wasm-bindgen-futures = "0.4" ehttp = "0.5.0" poll-promise = { version = "0.3.0", features = ["web"] } diff --git a/crates/client/index.html b/crates/client/index.html index af3bfac5df36fadeb5bc69804ec9429cef95d126..1aa8a2f17c5a4be882641fac26ecdf9eb03470ca 100644 --- a/crates/client/index.html +++ b/crates/client/index.html @@ -22,8 +22,6 @@ position: absolute; top: 0; left: 0; - width: 100%; - height: 100%; } @@ -35,6 +33,6 @@ } window.addEventListener('load', run); - + diff --git a/crates/client/src/rendering/mod.rs b/crates/client/src/rendering/mod.rs index eb5edbf39d2e50f9493913f7b66e8a27ef35339f..dcd1d39125cfb05cb726477102d60247127fec13 100644 --- a/crates/client/src/rendering/mod.rs +++ b/crates/client/src/rendering/mod.rs @@ -1,5 +1,6 @@ use glow::HasContext; -use wasm_bindgen::JsCast; +use wasm_bindgen::{prelude::Closure, JsCast}; +use web_sys::{Event, HtmlCanvasElement}; use winit::{application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent, event_loop::ActiveEventLoop, platform::web::{WindowAttributesExtWebSys, WindowExtWebSys}, raw_window_handle::HasWindowHandle, window::{Window, WindowAttributes}}; pub mod init; @@ -27,6 +28,8 @@ impl ApplicationHandler for App { let canvas = document.get_element_by_id("canvas").unwrap(); let canvas = canvas.dyn_into::() .map_err(|_| ()).unwrap(); + canvas.set_width(web_sys::window().unwrap().inner_width().unwrap().as_f64().unwrap() as u32); + canvas.set_height(web_sys::window().unwrap().inner_height().unwrap().as_f64().unwrap() as u32); Window::default_attributes() .with_title("StarKingdoms.TK") .with_canvas(Some(canvas)) @@ -86,11 +89,21 @@ impl ApplicationHandler for App { gl.enable_vertex_attrib_array(0); gl.clear_color(1.0, 1.0, 1.0, 1.0); + gl.viewport(0, 0, window.inner_size().width as i32, window.inner_size().height as i32); self.program = program; self.vertex_array = vertex_array; self.vertex_buffer = vertex_buffer; } + #[cfg(target_arch = "wasm32")] + web_sys::window().unwrap().set_onresize(Some(Closure::::new(move |_| { + let document = web_sys::window().unwrap().document().unwrap(); + let canvas = document.get_element_by_id("canvas").unwrap(); + let canvas = canvas.dyn_into::() + .map_err(|_| ()).unwrap(); + canvas.set_width(web_sys::window().unwrap().inner_width().unwrap().as_f64().unwrap() as u32); + canvas.set_height(web_sys::window().unwrap().inner_height().unwrap().as_f64().unwrap() as u32); + }).into_js_value().as_ref().unchecked_ref())); self.gl = Some(gl); } fn window_event( @@ -101,6 +114,9 @@ impl ApplicationHandler for App { ) { match event { WindowEvent::Resized(size) => { + unsafe { + self.gl.as_ref().unwrap().viewport(0, 0, size.width as i32, size.height as i32); + } } WindowEvent::RedrawRequested => { let window = self.window.as_ref().unwrap();