Skip to content

feat(renderer): track devicePixelRatio changes at runtime#19

Merged
sauyon merged 1 commit into
mainfrom
sauyon/hidpi-dpr-tracking
May 18, 2026
Merged

feat(renderer): track devicePixelRatio changes at runtime#19
sauyon merged 1 commit into
mainfrom
sauyon/hidpi-dpr-tracking

Conversation

@sauyon

@sauyon sauyon commented May 18, 2026

Copy link
Copy Markdown
Owner

The renderer captured window.devicePixelRatio once in its constructor and never re-checked it. Browser zoom, window drags between monitors with different scales, and OS scale changes all leave the canvas backing store sized to the old ratio — blurry one way, oversharp the other.

Subscribe to a matchMedia query pinned to the current DPR and update the field on change. Since each MediaQueryList is bound to one ratio, re-create the query on every change. The render loop's needsResize check picks up the new DPR and forces a full resize+redraw on the next frame, so no explicit render call is needed.

Skip the subscription when the caller pinned a DPR via options — they opted out of browser-driven changes.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces automatic tracking of the device pixel ratio (DPR) in the CanvasRenderer to handle browser-driven changes like zooming or monitor dragging. It adds methods to observe and unobserve DPR changes using matchMedia, along with comprehensive unit tests to verify the behavior. The reviewer suggested improving the robustness of the observeDevicePixelRatio method by ensuring existing listeners are cleaned up before re-subscription and adding a guard to prevent late-firing events after disposal, providing a code snippet to implement these safety checks.

Comment thread lib/renderer.ts
@sauyon
sauyon force-pushed the sauyon/hidpi-dpr-tracking branch from 091a780 to 440a7cb Compare May 18, 2026 07:40
The renderer captured window.devicePixelRatio once in its constructor
and never re-checked it. Browser zoom, window drags between monitors
with different scales, and OS scale changes all leave the canvas
backing store sized to the old ratio — blurry one way, oversharp the
other.

Subscribe to a matchMedia query pinned to the current DPR and update
the field on change. Since each MediaQueryList is bound to one ratio,
re-create the query on every change. The render loop's needsResize
check picks up the new DPR and forces a full resize+redraw on the
next frame, so no explicit render call is needed.

Skip the subscription when the caller pinned a DPR via options — they
opted out of browser-driven changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sauyon
sauyon force-pushed the sauyon/hidpi-dpr-tracking branch from 440a7cb to c07710b Compare May 18, 2026 08:04
@sauyon
sauyon merged commit b2245fe into main May 18, 2026
5 checks passed
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