Skip to content

web: follow the browser's pixel ratio as it changes - #710

Open
benface wants to merge 1 commit into
not-fl3:masterfrom
benface:web-live-dpi-scale
Open

benface wants to merge 1 commit into
not-fl3:masterfrom
benface:web-live-dpi-scale

Conversation

@benface

@benface benface commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

On the web, dpi_scale was read once at startup, while gl.js sizes the canvas and places the mouse and touches by the live window.devicePixelRatio. Once the ratio changes in a running page, by zooming it or moving its window to a display with another scale, the two disagree: screen_width() and screen_height() in points, and mouse_position() and touch positions, are off by the change. A board laid out from screen_width() is drawn bigger and off-centre, and clicks land elsewhere.

  • resize on the wasm side reads dpi_scale() again, so every resize brings the scale up to date.
  • gl.js's resize reports a change of ratio even when the canvas keeps its size in pixels, as zooming to 200% does when the canvas fills the window: half the CSS pixels at twice the ratio.
  • gl.js listens for the ratio changing without the window resizing, as on a move to another display, with a (resolution: …dppx) media query, re-armed on each change.

Checked with cargo check --target wasm32-unknown-unknown. In headless Chromium, with an app that fits a board to screen_width() and screen_height() and reports where it put it: zooming a running page to 125% and 200% (chrome.tabs.setZoom) now puts the board exactly where a page loaded at that zoom puts it, and a click at a cell's middle reaches that cell. Before, at 125%, the board was 57% bigger and its left edge 68 CSS pixels further right. The same holds when Chrome DevTools' device emulation changes the ratio together with the page's size.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant