Skip to content

Commit a4d8a41

Browse files
fix web viewport scaling
Set canvas to physical pixels with devicePixelRatio for crisp rendering, use pixels_per_point=1.0 on web for compact UI, and override screen_rect with correct logical dimensions
1 parent c543e43 commit a4d8a41

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
.main-canvas {
3131
display: block;
32+
width: 100%;
33+
height: 100%;
3234
}
3335
</style>
3436
</head>
@@ -41,8 +43,14 @@
4143
<script>
4244
function resizeCanvas() {
4345
const canvas = document.getElementById("canvas");
44-
canvas.width = window.innerWidth;
45-
canvas.height = window.innerHeight;
46+
const dpr = window.devicePixelRatio || 1;
47+
const width = window.innerWidth;
48+
const height = window.innerHeight;
49+
50+
canvas.width = Math.round(width * dpr);
51+
canvas.height = Math.round(height * dpr);
52+
canvas.style.width = width + "px";
53+
canvas.style.height = height + "px";
4654
}
4755
resizeCanvas();
4856
window.addEventListener("resize", resizeCanvas);

src/lib.rs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl ApplicationHandler for App {
104104

105105
#[cfg(target_arch = "wasm32")]
106106
{
107-
gui_context.set_pixels_per_point(window_handle.scale_factor() as f32);
107+
gui_context.set_pixels_per_point(1.0);
108108
}
109109

110110
#[cfg(target_os = "android")]
@@ -182,6 +182,9 @@ impl ApplicationHandler for App {
182182
}
183183
if renderer_received {
184184
self.renderer_receiver = None;
185+
if let Some(window) = self.window.as_ref() {
186+
window.request_redraw();
187+
}
185188
}
186189
}
187190

@@ -211,18 +214,13 @@ impl ApplicationHandler for App {
211214
event_loop.exit();
212215
}
213216
}
214-
WindowEvent::ScaleFactorChanged {
215-
#[cfg(not(target_os = "android"))]
216-
scale_factor,
217-
..
218-
} => {
219-
#[cfg(not(target_os = "android"))]
217+
WindowEvent::ScaleFactorChanged { .. } => {
218+
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
220219
{
221-
gui_state
222-
.egui_ctx()
223-
.set_pixels_per_point(scale_factor as f32);
220+
let scale_factor = window.scale_factor() as f32;
221+
gui_state.egui_ctx().set_pixels_per_point(scale_factor);
224222
}
225-
#[cfg(target_os = "android")]
223+
#[cfg(any(target_arch = "wasm32", target_os = "android"))]
226224
{
227225
gui_state.egui_ctx().set_pixels_per_point(1.0);
228226
}
@@ -236,12 +234,12 @@ impl ApplicationHandler for App {
236234
renderer.resize(width, height);
237235
self.last_size = (width, height);
238236

239-
#[cfg(not(target_os = "android"))]
237+
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
240238
{
241239
let scale_factor = window.scale_factor() as f32;
242240
gui_state.egui_ctx().set_pixels_per_point(scale_factor);
243241
}
244-
#[cfg(target_os = "android")]
242+
#[cfg(any(target_arch = "wasm32", target_os = "android"))]
245243
{
246244
gui_state.egui_ctx().set_pixels_per_point(1.0);
247245
}
@@ -251,11 +249,54 @@ impl ApplicationHandler for App {
251249
event_loop.exit();
252250
}
253251
WindowEvent::RedrawRequested => {
252+
#[cfg(target_arch = "wasm32")]
253+
{
254+
let canvas = wgpu::web_sys::window()
255+
.unwrap()
256+
.document()
257+
.unwrap()
258+
.get_element_by_id("canvas")
259+
.unwrap()
260+
.dyn_into::<wgpu::web_sys::HtmlCanvasElement>()
261+
.unwrap();
262+
let new_width = canvas.width();
263+
let new_height = canvas.height();
264+
if (new_width, new_height) != self.last_size && new_width > 0 && new_height > 0
265+
{
266+
log::info!("Canvas resized to: ({new_width}, {new_height})");
267+
renderer.resize(new_width, new_height);
268+
self.last_size = (new_width, new_height);
269+
}
270+
}
271+
254272
let now = Instant::now();
255273
let delta_time = now - *last_render_time;
256274
*last_render_time = now;
257275

276+
#[cfg(target_arch = "wasm32")]
277+
let mut gui_input = gui_state.take_egui_input(window);
278+
#[cfg(not(target_arch = "wasm32"))]
258279
let gui_input = gui_state.take_egui_input(window);
280+
281+
#[cfg(target_arch = "wasm32")]
282+
{
283+
let canvas = wgpu::web_sys::window()
284+
.unwrap()
285+
.document()
286+
.unwrap()
287+
.get_element_by_id("canvas")
288+
.unwrap()
289+
.dyn_into::<wgpu::web_sys::HtmlCanvasElement>()
290+
.unwrap();
291+
let pixels_per_point = gui_state.egui_ctx().pixels_per_point();
292+
let canvas_size = egui::vec2(
293+
canvas.width() as f32 / pixels_per_point,
294+
canvas.height() as f32 / pixels_per_point,
295+
);
296+
gui_input.screen_rect =
297+
Some(egui::Rect::from_min_size(egui::Pos2::ZERO, canvas_size));
298+
}
299+
259300
gui_state.egui_ctx().begin_pass(gui_input);
260301

261302
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]

0 commit comments

Comments
 (0)