Skip to content

Web: sharp rendering on HiDPI screens and pause while the page is hidden - #146

Merged
eduardodoria merged 1 commit into
doriaxengine:mainfrom
brunolinsalves:fix/web-hidpi-pause
Nov 23, 2026
Merged

eduardodoria merged 1 commit into
doriaxengine:mainfrom
brunolinsalves:fix/web-hidpi-pause

Conversation

@brunolinsalves

Copy link
Copy Markdown
Contributor

Problem

I published a Doriax 0.8.5 game on itch.io for the Doriax Flash Jam 2026 and found two problems on phones:

  1. Text is jagged and blurry on high-density screens. DoriaxWeb::renderLoop sizes the framebuffer with the CSS size of the canvas. On a phone with 800×360 CSS pixels at 3 device pixels per CSS pixel, the framebuffer is 800×360, a 1280×720 letterboxed game is drawn in 640×360, and the browser stretches it 3×. Text suffers most.
  2. Music keeps playing after the browser is minimized, the tab changes or the phone locks. Browsers stop requestAnimationFrame on a hidden page, but SoLoud mixes in its own audio callback. Engine::systemPause exists, but nothing calls it for page visibility on the web.

Fix

HiDPI framebuffer

DoriaxWeb::updateFramebufferSize replaces the sizing block in renderLoop. Each frame:

pixelRatio  = min(devicePixelRatio × min(visualViewport.scale, 1), DORIAX_WEB_MAX_PIXEL_RATIO)
framebuffer = round(canvas CSS size × pixelRatio)
  • Canvas without a CSS size: its CSS size follows the width/height attributes, so multiplying it would grow the canvas every frame. After a resize the CSS size is measured again and, if it followed the attributes, it is fixed at its previous size.
  • Border: the CSS size is measured without the border (a bordered canvas already grew a little every frame, even at density 1).
  • Zoomed-out pages (no viewport meta tag): visualViewport.scale keeps the game from drawing more pixels than the screen has.
  • Input: mouse, touch and pointer-lock movement are multiplied by pixelRatio, so a captured cursor moves at the same speed at every density.
  • Full screen uses EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE, and the render loop sizes the framebuffer like any other change.
  • changeCanvasSize (exported to the page) now sets the canvas CSS size; the framebuffer follows on the next frame.

Pause while hidden

  • DoriaxWeb::setPaused(PauseReason, bool) is the only place the web platform pauses and resumes the engine. Reasons: AD, PORTAL, HIDDEN, CONTEXT_LOST; the game resumes when none is left. The two flags WebPortals.cpp kept for ads and portals moved into it, and the WebGL context callbacks use it too, so a tab switch during an ad no longer resumes the game early.
  • A visibilitychange callback sets HIDDEN. When the page hides, a pending IndexedDB sync runs at once, so a file saved in onPause is not lost if the tab is closed.
  • The callback is removed in unregisterCallbacks (it would otherwise reach a closed Lua state after System::quit).
  • YouTube Playables forbids the Page Visibility API, so the registration is compiled out with #ifndef DORIAX_WEB_PORTAL_YOUTUBE.

Audio pause (all platforms)

Engine::systemPause/systemResume used AudioSystem::pauseAll/resumeAll, and SoLoud's resume-all also restarts sounds the game had paused itself (already an issue on Android and iOS when returning from background). New private AudioSystem::systemPause/systemResume (reached through friend class Engine) remember the voices that were playing and resume only those. Engine::systemPause now calls onPause before pausing audio, so a game that calls AudioSystem.pauseAll() in onPause stays silent after resuming. pauseAll/resumeAll keep their meaning for games.

Build option and editor

  • engine/CMakeLists.txt: cache variable DORIAX_WEB_MAX_PIXEL_RATIO (default 2), passed to DoriaxWeb.cpp only. The portal definitions now also reach DoriaxWeb.cpp, for the YouTube guard.
  • Editor: Project Settings → Platforms → Web → Max Pixel Ratio (web.maxPixelRatio in project.yaml, minimum 1), exported to the generated CMakeLists.txt. The AI/MCP tool set_project_settings accepts web_max_pixel_ratio.

Behaviour changes

  • High-density screens draw up to 4× the pixels they did before. A heavy game can set Max Pixel Ratio to 1 to get the old cost back.
  • With NATIVE scaling on the web, canvas units are device pixels (up to the limit).
  • System::getScreenWidth/getScreenHeight return the framebuffer size, which is no longer the CSS size on those screens.
  • onPause runs just before audio pauses, not after.
  • A page calling changeCanvasSize sees the new size one frame later.

Testing

  • Chrome 158 (headless), with a small test game and a browser driver: input at densities 1–3, pointer lock, full screen, changeCanvasSize from the page, density changes while running, pages without a viewport tag, an iframe, real tab switches with the audio output measured, and every pair of pause reasons — 156 checks pass.
  • Firefox 157 at densities 1, 1.5 and 2: sizing, tab switch and clicks pass.
  • Native: Engine::systemPause/systemResume against libdoriax.so with three voices, including 4000 quick toggles against the audio thread.
  • A real export of TappyPlane with the rebuilt editor is stable at densities 1, 2 and 3.
  • A YouTube Playables build compiles and its JavaScript has no visibility code.
  • Not tested: Safari and physical phones.

Not changed

  • Screen wake lock.
  • Sound::isPlaying() returns false for a looping sound after its first pass.
  • The Fullscreen button of the default Emscripten page blanks a canvas with no CSS size.
  • The unused resize_callback in DoriaxWeb.cpp still passes framebuffer pixels to changeCanvasSize (it is never registered).

🤖 Generated with Claude Code

The web framebuffer follows devicePixelRatio, up to the new
DORIAX_WEB_MAX_PIXEL_RATIO (2 by default, Max Pixel Ratio in the editor),
and input is scaled to framebuffer pixels. The game pauses while the page
is hidden, through one pause control shared with ads, portals and WebGL
context loss. A system pause resumes only the sounds it paused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eduardodoria
eduardodoria merged commit 3687278 into doriaxengine:main Nov 23, 2026
3 checks passed
@eduardodoria

Copy link
Copy Markdown
Contributor

Thanks!

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.

2 participants