Implement hidpi for web platform (#1233)
* fix: use a 'static lifetime for the web backend's `Event` types * implement hidpi for stdweb (web-sys wip?) * fix: make all canvas resizes go through backend::set_canvas_size * update Window docs for web, make `inner/outer_position` return the position in the viewport
This commit is contained in:
parent
28a20aec10
commit
777d9edeaa
11 changed files with 137 additions and 91 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use super::event;
|
||||
use crate::dpi::{LogicalPosition, LogicalSize};
|
||||
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
|
||||
use crate::error::OsError as RootOE;
|
||||
use crate::event::{ModifiersState, MouseButton, MouseScrollDelta, ScanCode, VirtualKeyCode};
|
||||
use crate::platform_impl::OsError;
|
||||
|
|
@ -11,6 +11,7 @@ use wasm_bindgen::{closure::Closure, JsCast};
|
|||
use web_sys::{Event, FocusEvent, HtmlCanvasElement, KeyboardEvent, PointerEvent, WheelEvent};
|
||||
|
||||
pub struct Canvas {
|
||||
/// Note: resizing the HTMLCanvasElement should go through `backend::set_canvas_size` to ensure the DPI factor is maintained.
|
||||
raw: HtmlCanvasElement,
|
||||
on_focus: Option<Closure<dyn FnMut(FocusEvent)>>,
|
||||
on_blur: Option<Closure<dyn FnMut(FocusEvent)>>,
|
||||
|
|
@ -80,23 +81,20 @@ impl Canvas {
|
|||
.expect(&format!("Set attribute: {}", attribute));
|
||||
}
|
||||
|
||||
pub fn position(&self) -> (f64, f64) {
|
||||
pub fn position(&self) -> LogicalPosition<f64> {
|
||||
let bounds = self.raw.get_bounding_client_rect();
|
||||
|
||||
(bounds.x(), bounds.y())
|
||||
LogicalPosition {
|
||||
x: bounds.x(),
|
||||
y: bounds.y(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width(&self) -> f64 {
|
||||
self.raw.width() as f64
|
||||
}
|
||||
|
||||
pub fn height(&self) -> f64 {
|
||||
self.raw.height() as f64
|
||||
}
|
||||
|
||||
pub fn set_size(&self, size: LogicalSize<f64>) {
|
||||
self.raw.set_width(size.width as u32);
|
||||
self.raw.set_height(size.height as u32);
|
||||
pub fn size(&self) -> PhysicalSize<u32> {
|
||||
PhysicalSize {
|
||||
width: self.raw.width(),
|
||||
height: self.raw.height(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> &HtmlCanvasElement {
|
||||
|
|
@ -218,12 +216,12 @@ impl Canvas {
|
|||
|
||||
pub fn on_cursor_move<F>(&mut self, mut handler: F)
|
||||
where
|
||||
F: 'static + FnMut(i32, LogicalPosition, ModifiersState),
|
||||
F: 'static + FnMut(i32, PhysicalPosition<i32>, ModifiersState),
|
||||
{
|
||||
self.on_cursor_move = Some(self.add_event("pointermove", move |event: PointerEvent| {
|
||||
handler(
|
||||
event.pointer_id(),
|
||||
event::mouse_position(&event),
|
||||
event::mouse_position(&event).to_physical(super::hidpi_factor()),
|
||||
event::mouse_modifiers(&event),
|
||||
);
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ mod timeout;
|
|||
pub use self::canvas::Canvas;
|
||||
pub use self::timeout::Timeout;
|
||||
|
||||
use crate::dpi::LogicalSize;
|
||||
use crate::dpi::{LogicalSize, Size};
|
||||
use crate::platform::web::WindowExtWebSys;
|
||||
use crate::window::Window;
|
||||
use wasm_bindgen::{closure::Closure, JsCast};
|
||||
|
|
@ -56,6 +56,29 @@ pub fn window_size() -> LogicalSize<f64> {
|
|||
LogicalSize { width, height }
|
||||
}
|
||||
|
||||
pub fn hidpi_factor() -> f64 {
|
||||
let window = web_sys::window().expect("Failed to obtain window");
|
||||
window.device_pixel_ratio()
|
||||
}
|
||||
|
||||
pub fn set_canvas_size(raw: &HtmlCanvasElement, size: Size) {
|
||||
let hidpi_factor = hidpi_factor();
|
||||
|
||||
let physical_size = size.to_physical::<u32>(hidpi_factor);
|
||||
let logical_size = size.to_logical::<f64>(hidpi_factor);
|
||||
|
||||
raw.set_width(physical_size.width);
|
||||
raw.set_height(physical_size.height);
|
||||
|
||||
let style = raw.style();
|
||||
style
|
||||
.set_property("width", &format!("{}px", logical_size.width))
|
||||
.expect("Failed to set canvas width");
|
||||
style
|
||||
.set_property("height", &format!("{}px", logical_size.height))
|
||||
.expect("Failed to set canvas height");
|
||||
}
|
||||
|
||||
pub fn is_fullscreen(canvas: &HtmlCanvasElement) -> bool {
|
||||
let window = window().expect("Failed to obtain window");
|
||||
let document = window.document().expect("Failed to obtain document");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue