use existing canvas

This commit is contained in:
Quaternions 2025-01-03 20:53:06 -08:00
parent 275373d9bc
commit f8583702be
4 changed files with 34 additions and 1 deletions

2
Cargo.lock generated
View File

@ -2284,7 +2284,9 @@ dependencies = [
"strafesnet_deferred_loader", "strafesnet_deferred_loader",
"strafesnet_rbx_loader", "strafesnet_rbx_loader",
"strafesnet_snf", "strafesnet_snf",
"wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"web-sys",
"wgpu", "wgpu",
"winit", "winit",
] ]

View File

@ -28,6 +28,8 @@ strafesnet_common = { path = "../lib/common", registry = "strafesnet" }
strafesnet_deferred_loader = { path = "../lib/deferred_loader", features = ["legacy"], registry = "strafesnet", optional = true } strafesnet_deferred_loader = { path = "../lib/deferred_loader", features = ["legacy"], registry = "strafesnet", optional = true }
strafesnet_rbx_loader = { path = "../lib/rbx_loader", registry = "strafesnet", optional = true } strafesnet_rbx_loader = { path = "../lib/rbx_loader", registry = "strafesnet", optional = true }
strafesnet_snf = { path = "../lib/snf", registry = "strafesnet", optional = true } strafesnet_snf = { path = "../lib/snf", registry = "strafesnet", optional = true }
wasm-bindgen = "0.2.99"
wasm-bindgen-futures = "0.4.49" wasm-bindgen-futures = "0.4.49"
web-sys = { version = "0.3.76", features = ["console"] }
wgpu = "23.0.1" wgpu = "23.0.1"
winit = "0.30.7" winit = "0.30.7"

View File

@ -34,8 +34,13 @@ fn create_window(title:&str,event_loop:&winit::event_loop::EventLoop<()>)->Resul
} }
#[cfg(target_arch="wasm32")] #[cfg(target_arch="wasm32")]
{ {
use wasm_bindgen::JsCast;
use winit::platform::web::WindowAttributesExtWebSys; use winit::platform::web::WindowAttributesExtWebSys;
attr=attr.with_append(true); let canvas=web_sys::window().unwrap()
.document().unwrap()
.get_element_by_id("canvas").unwrap()
.dyn_into::<web_sys::HtmlCanvasElement>().unwrap();
attr=attr.with_canvas(Some(canvas));
} }
event_loop.create_window(attr) event_loop.create_window(attr)
} }

View File

@ -6,8 +6,32 @@
<title>Strafe Client</title> <title>Strafe Client</title>
<base data-trunk-public-url /> <base data-trunk-public-url />
<style type="text/css">
body {
margin: 0px;
background: #fff;
width: 100%;
height: 100%;
}
.root {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.main-canvas {
margin: 0;
/* This allows the flexbox to grow to max size, this is needed for WebGPU */
flex: 1;
/* This forces CSS to ignore the width/height of the canvas, this is needed for WebGL */
contain: size;
}
</style>
</head> </head>
<body> <body>
<div class="root">
<canvas class="main-canvas" id="canvas"></canvas>
</div>
<link data-trunk rel="rust" href="../strafe-client/Cargo.toml"/> <link data-trunk rel="rust" href="../strafe-client/Cargo.toml"/>
</body> </body>
</html> </html>