From b83f6670c80ffe152eafb89afd9a67283ffbaf80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Tue, 24 Nov 2026 17:35:22 -0500 Subject: [PATCH] web: follow the browser's pixel ratio as it changes dpi_scale was read once at startup, while gl.js sized the canvas and placed the pointer by the live ratio. Once the ratio changed, by zooming the page or moving its window to another display, the app's screen size in points and its mouse and touch positions were off by the change. The wasm side's resize now reads dpi_scale again. gl.js calls it when only the ratio changes, which zooming can do while keeping the canvas's size in pixels, and listens for the ratio changing without the window resizing. --- js/gl.js | 23 ++++++++++++++++++++++- src/native/wasm.rs | 3 +++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/js/gl.js b/js/gl.js index df0116ac9..6126fff8c 100644 --- a/js/gl.js +++ b/js/gl.js @@ -420,20 +420,38 @@ function _webglGet(name_, p, type) { var Module; var wasm_exports; +// The `dpi_scale` the canvas was last sized at, which the wasm side reads +// again on every `resize`. +var sized_dpi_scale; + function resize(canvas, on_resize) { var dpr = dpi_scale(); var displayWidth = canvas.clientWidth * dpr; var displayHeight = canvas.clientHeight * dpr; + // Zooming changes the ratio and the canvas's size in CSS pixels + // together, and can leave its size in pixels as it was. if (canvas.width != displayWidth || - canvas.height != displayHeight) { + canvas.height != displayHeight || + dpr != sized_dpi_scale) { canvas.width = displayWidth; canvas.height = displayHeight; + sized_dpi_scale = dpr; if (on_resize != undefined) on_resize(Math.floor(displayWidth), Math.floor(displayHeight)) } } +// Moving the window to a display with another pixel ratio changes it +// without resizing the window. +function on_device_pixel_ratio_change(f) { + var query = window.matchMedia("(resolution: " + window.devicePixelRatio + "dppx)"); + query.addEventListener("change", function () { + f(); + on_device_pixel_ratio_change(f); + }, { once: true }); +} + function animation() { wasm_exports.frame(); if (!window.blocking_event_loop) { @@ -1308,6 +1326,9 @@ var importObject = { window.onresize = function () { resize(canvas, wasm_exports.resize); }; + on_device_pixel_ratio_change(function () { + resize(canvas, wasm_exports.resize); + }); window.addEventListener("copy", function (e) { if (clipboard != null) { event.clipboardData.setData('text/plain', clipboard); diff --git a/src/native/wasm.rs b/src/native/wasm.rs index c79bb9ce8..fca2213b3 100644 --- a/src/native/wasm.rs +++ b/src/native/wasm.rs @@ -316,6 +316,9 @@ pub extern "C" fn resize(width: i32, height: i32) { let mut d = crate::native_display().lock().unwrap(); d.screen_width = width as _; d.screen_height = height as _; + // The browser's pixel ratio changes as the page zooms or moves to + // another display. + d.dpi_scale = unsafe { dpi_scale() }; } tl_event_handler(|event_handler| { event_handler.resize_event(width as _, height as _);