Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow running web runtime in background windows #793

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

majaha
Copy link
Contributor

@majaha majaha commented Dec 20, 2024

This patch makes update timing more regular in various environments, including when the browser window is in the background. This allows audio and netplay to continue if the user e.g. minimises the browser window. This is achieved by switching to a setTimeout() based timing solution if the observed requestAnimationFrame rate is not a multiple of 60.

Also included are a few other relevant fixes and improvements.

This commit reworks the frame timing of the runtime ensuring a regular
update on every frame on 60 fps monitors, while still supporting other
framerates, both higher and lower. It achieves this by measuring the
framerate continuously, and performing updates in a
requestAnimationFrame() callback when the framerate is close to 60, but
switching to a setTimeout() timing scheme otherwise.

This keeps animation smooth on a 60 fps screen, but keeps updates both
full-speed and at a regular 60Hz on e.g. a 30 fps screen, important for
keeping tick-based audio smooth. This also has the effect of allowing
you to run the runtime in a minimised or background browser window. This
really improves the audio experience, allowing you to run games or music
carts in the background. This also improves the netplay experience,
as the game won't lag or halt any time a player clicks out of the
browser window or changes tab.
The web runtime doesn't run on NodeJS, but was using node types. This
was causing issues because some node types override the DOM types,
e.g. `setTimeout()`.

Adding `"types": []` to tsconfig.json filters out the types in
`node_modules/@types` and prevents them from being used, including
`node_modules/@types/node`, which is installed because it is a
dependency of our dependencies.
Setting these security headers grants us access to higher precision time. This
improves our ability to pace frame timings and make accurate performance
measurements. I hope to take advantage of this to improve the devtools in the future.
Previously favicon.ico was being requested from wasm4.org when
developing locally. This won't work if the user is offline or the
website goes down, etc. It also doesn't work with secure CORS headers,
as it is served from another domain.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant