Skip to content

Pause the animated background while the window is unfocused - #153

Merged
spuder merged 1 commit into
mainfrom
fix/pause-background-when-unfocused
Nov 17, 2026
Merged

spuder merged 1 commit into
mainfrom
fix/pause-background-when-unfocused

Conversation

@spuder

@spuder spuder commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Fixes #151.

What

AnimatedBackground.svelte stopped animating only when document.hidden was true. In the Tauri/WebKitGTK shell that never happens for a window on another workspace or behind other windows, and WebKit keeps firing requestAnimationFrame at vblank rate. So the backdrop kept rendering while nobody could see it. On Linux, with the window out of sight, that measured about 33% CPU across the UI process, the web process and the compositor.

Now the background animates only while the document is visible and focused:

  • Waves / Particles: the rAF loop continues only while !document.hidden && document.hasFocus(). focus and blur restart or stop it, alongside visibilitychange. On blur, the current frame is drawn and the loop stops. On focus, it resumes where it left off.
  • Aurora: a paused class sets animation-play-state: paused on the blobs under the same rule.

Trade-off: a window that is visible but unfocused (tiled side by side, for example) shows a still backdrop until it's focused. This matches how reduced motion already freezes it, and fits the "keep the UI calm" convention.

Verification

  • npm test passes (252 tests) and npm run check reports 0 errors.

  • Ran the dev build in headless Chromium through Playwright, faking document.hasFocus() and dispatching blur / focus:

    focused blurred refocused
    Waves (canvas draws per second) 30 0 30
    Aurora (animation-play-state) running paused running
  • I haven't tested this in the real Linux desktop build, where the focus events come from GTK. To check: switch workspaces, and top -H on the WebKitWebProcess should show its main thread and ThreadedCompositor near 0%.

🤖 Generated with Claude Code

WebKitGTK keeps document.hidden false and fires rAF at vblank rate when the
desktop window is on another workspace or covered, so the Waves/Particles
loop and the Aurora CSS animation never stopped (~33% CPU measured on Linux
with the window out of sight). Run the canvas loop only while the document
is visible and focused, and pause Aurora's animation under the same rule.

Fixes #151

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@spuder
spuder enabled auto-merge November 17, 2026 23:15
@spuder
spuder merged commit 37fae6f into main Nov 17, 2026
12 checks passed
@spuder spuder mentioned this pull request Nov 17, 2026
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.

Animated background keeps rendering when the window isn't visible (rAF/CSS loop never pauses in WebKitGTK)

1 participant