M crates/client/Cargo.toml => crates/client/Cargo.toml +1 -1
@@ 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"] }
M crates/client/index.html => crates/client/index.html +1 -3
@@ 22,8 22,6 @@
position: absolute;
top: 0;
left: 0;
- width: 100%;
- height: 100%;
}
</style>
</head>
@@ 35,6 33,6 @@
}
window.addEventListener('load', run);
</script>
- <canvas id="canvas"></canvas>
+ <canvas id="canvas" width=100 height=100></canvas>
</body>
</html>
M crates/client/src/rendering/mod.rs => crates/client/src/rendering/mod.rs +17 -1
@@ 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::<web_sys::HtmlCanvasElement>()
.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::<dyn Fn(Event)>::new(move |_| {
+ let document = web_sys::window().unwrap().document().unwrap();
+ let canvas = document.get_element_by_id("canvas").unwrap();
+ let canvas = canvas.dyn_into::<web_sys::HtmlCanvasElement>()
+ .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();