Skip to content

Commit 0b06b3f

Browse files
committed
Fix slide puzzle not resizing with the html canvas
Improve detection of explicitly sized canvas elements, by looking for css rules that applie (!= "auto" in computed style) or explicit inline style width/height.
1 parent d320e6e commit 0b06b3f

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

internal/backends/winit/winitwindowadapter.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -906,18 +906,17 @@ impl WinitWindowAdapter {
906906
);
907907

908908
#[cfg(target_arch = "wasm32")]
909-
if let Some(html_canvas) =
910-
winit_window.canvas().filter(|html_canvas| !is_preferred_sized_canvas(html_canvas))
911-
{
912-
let existing_canvas_size = winit::dpi::LogicalSize::new(
913-
html_canvas.client_width() as f32,
914-
html_canvas.client_height() as f32,
915-
);
909+
if let Some(html_canvas) = winit_window.canvas() {
916910
// Try to maintain the existing size of the canvas element, if any
917-
if existing_canvas_size.width > 0. {
911+
if !is_preferred_sized_canvas(&html_canvas)
912+
&& !canvas_has_explicit_size_set(&html_canvas)
913+
{
914+
let existing_canvas_size = winit::dpi::LogicalSize::new(
915+
html_canvas.client_width() as f32,
916+
html_canvas.client_height() as f32,
917+
);
918918
preferred_size.width = existing_canvas_size.width;
919-
}
920-
if existing_canvas_size.height > 0. {
919+
921920
preferred_size.height = existing_canvas_size.height;
922921
}
923922
}
@@ -1526,3 +1525,23 @@ fn is_preferred_sized_canvas(canvas: &web_sys::HtmlCanvasElement) -> bool {
15261525
.and_then(|val_str| val_str.parse::<bool>().ok())
15271526
.unwrap_or_default()
15281527
}
1528+
1529+
#[cfg(target_family = "wasm")]
1530+
fn canvas_has_explicit_size_set(canvas: &web_sys::HtmlCanvasElement) -> bool {
1531+
let style = canvas.style();
1532+
if !style.get_property_value("width").unwrap_or_default().is_empty()
1533+
|| !style.get_property_value("height").unwrap_or_default().is_empty()
1534+
{
1535+
return true;
1536+
}
1537+
1538+
let Some(window) = web_sys::window() else {
1539+
return false;
1540+
};
1541+
let Some(computed_style) = window.get_computed_style(&canvas).ok().flatten() else {
1542+
return false;
1543+
};
1544+
1545+
computed_style.get_property_value("width").ok().as_deref() != Some("auto")
1546+
|| computed_style.get_property_value("height").ok().as_deref() != Some("auto")
1547+
}

0 commit comments

Comments
 (0)