Skip to content

Commit

Permalink
Skip rendering to a canvas with zero dimensions (#805)
Browse files Browse the repository at this point in the history
If the width or height is 0, the element likely isn't mounted into the
DOM. This usually isn't a problem, but if we end up resizing the layout,
we would resize it based on a size of 0, which would break the entire
layout. This happened here:

LiveSplit/LiveSplitOne#881

Rendering the layout with a size of 0 is also a waste of time, so this
ends up benefiting us in multiple ways.
  • Loading branch information
CryZe authored May 18, 2024
1 parent 808df28 commit 99fbe17
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/rendering/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,25 @@ impl Renderer {

let ratio = self.allocator.window.device_pixel_ratio();
let bounding_rect = self.canvas_bottom.get_bounding_client_rect();
let [bounding_width, bounding_height] = [bounding_rect.width(), bounding_rect.height()];

// If the width or height is 0, the element likely isn't mounted into
// the DOM. This usually isn't a problem, but if we end up resizing the
// layout, we would resize it based on a size of 0, which would break
// the entire layout. This happened here:
//
// https://github.com/LiveSplit/LiveSplitOne/issues/881
//
// Rendering the layout with a size of 0 is also a waste of time, so
// this ends up benefiting us in multiple ways.
if bounding_width == 0.0 || bounding_height == 0.0 {
return None;
}

let (width, height) = (
(ratio * bounding_rect.width()).round(),
(ratio * bounding_rect.height()).round(),
);
let [width, height] = [
(ratio * bounding_width).round(),
(ratio * bounding_height).round(),
];

if (self.canvas_bottom.width(), self.canvas_bottom.height()) != (width as _, height as _) {
self.canvas_bottom.set_width(width as _);
Expand Down

0 comments on commit 99fbe17

Please sign in to comment.