|
@ -92,25 +92,43 @@ pub fn pos_from_touch_event(event: &web_sys::TouchEvent) -> egui::Pos2 { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn canvas_size_in_points(canvas_id: &str) -> egui::Vec2 { |
|
|
|
|
|
let canvas = canvas_element(canvas_id).unwrap(); |
|
|
|
|
|
let pixels_per_point = native_pixels_per_point(); |
|
|
|
|
|
egui::vec2( |
|
|
|
|
|
canvas.width() as f32 / pixels_per_point, |
|
|
|
|
|
canvas.height() as f32 / pixels_per_point, |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
pub fn resize_canvas_to_screen_size(canvas_id: &str) -> Option<()> { |
|
|
pub fn resize_canvas_to_screen_size(canvas_id: &str) -> Option<()> { |
|
|
let canvas = canvas_element(canvas_id)?; |
|
|
let canvas = canvas_element(canvas_id)?; |
|
|
|
|
|
|
|
|
let screen_size = screen_size_in_native_points()?; |
|
|
let screen_size_points = screen_size_in_native_points()?; |
|
|
let pixels_per_point = native_pixels_per_point(); |
|
|
let pixels_per_point = native_pixels_per_point(); |
|
|
|
|
|
|
|
|
|
|
|
let canvas_size_pixels = pixels_per_point * screen_size_points; |
|
|
|
|
|
// Some browsers get slow with huge WebGL canvases, so we limit the size:
|
|
|
|
|
|
let max_size_pixels = egui::vec2(2048.0, 4096.0); |
|
|
|
|
|
let canvas_size_pixels = canvas_size_pixels.min(max_size_pixels); |
|
|
|
|
|
let canvas_size_points = canvas_size_pixels / pixels_per_point; |
|
|
|
|
|
|
|
|
canvas |
|
|
canvas |
|
|
.style() |
|
|
.style() |
|
|
.set_property("width", &format!("{}px", screen_size.x)) |
|
|
.set_property("width", &format!("{}px", canvas_size_points.x)) |
|
|
.ok()?; |
|
|
.ok()?; |
|
|
canvas |
|
|
canvas |
|
|
.style() |
|
|
.style() |
|
|
.set_property("height", &format!("{}px", screen_size.y)) |
|
|
.set_property("height", &format!("{}px", canvas_size_points.y)) |
|
|
.ok()?; |
|
|
.ok()?; |
|
|
canvas.set_width((screen_size.x * pixels_per_point).round() as u32); |
|
|
canvas.set_width(canvas_size_pixels.x.round() as u32); |
|
|
canvas.set_height((screen_size.y * pixels_per_point).round() as u32); |
|
|
canvas.set_height(canvas_size_pixels.y.round() as u32); |
|
|
|
|
|
|
|
|
Some(()) |
|
|
Some(()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
pub fn local_storage() -> Option<web_sys::Storage> { |
|
|
pub fn local_storage() -> Option<web_sys::Storage> { |
|
|
web_sys::window()?.local_storage().ok()? |
|
|
web_sys::window()?.local_storage().ok()? |
|
|
} |
|
|
} |
|
|