Skip to content

Commit 3f2008b

Browse files
committed
[egui_web] Always use an even canvas size
Reportedly fixes #103
1 parent 2cbea02 commit 3f2008b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

egui_web/src/lib.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,29 @@ pub fn resize_canvas_to_screen_size(canvas_id: &str) -> Option<()> {
135135
let canvas_size_pixels = canvas_size_pixels.min(max_size_pixels);
136136
let canvas_size_points = canvas_size_pixels / pixels_per_point;
137137

138+
// Make sure that the height and width are always even numbers.
139+
// otherwise, the page renders blurry on some platforms.
140+
// See https://github.com/emilk/egui/issues/103
141+
fn round_to_even(v: f32) -> f32 {
142+
(v / 2.0).round() * 2.0
143+
}
144+
138145
canvas
139146
.style()
140-
.set_property("width", &format!("{}px", canvas_size_points.x))
147+
.set_property(
148+
"width",
149+
&format!("{}px", round_to_even(canvas_size_points.x)),
150+
)
141151
.ok()?;
142152
canvas
143153
.style()
144-
.set_property("height", &format!("{}px", canvas_size_points.y))
154+
.set_property(
155+
"height",
156+
&format!("{}px", round_to_even(canvas_size_points.y)),
157+
)
145158
.ok()?;
146-
canvas.set_width(canvas_size_pixels.x.round() as u32);
147-
canvas.set_height(canvas_size_pixels.y.round() as u32);
159+
canvas.set_width(round_to_even(canvas_size_pixels.x) as u32);
160+
canvas.set_height(round_to_even(canvas_size_pixels.y) as u32);
148161

149162
Some(())
150163
}

0 commit comments

Comments
 (0)