Skip to content

Fix device constructor crash when canvas lacks getBoundingClientRect#9000

Merged
mvaligursky merged 1 commit into
mainfrom
mv-null-device-client-rect
Jul 2, 2026
Merged

Fix device constructor crash when canvas lacks getBoundingClientRect#9000
mvaligursky merged 1 commit into
mainfrom
mv-null-device-client-rect

Conversation

@mvaligursky

Copy link
Copy Markdown
Contributor

Fixes #8999

Since v2.20.0 (#8950), the base GraphicsDevice constructor calls updateClientRect(), which unconditionally calls this.canvas.getBoundingClientRect() outside of Web Workers. This broke the documented headless path — constructing a NullGraphicsDevice with a mock canvas (e.g. { id: 'mock' }, as @playcanvas/react does for SSR) now throws, which broke static site generation on developer.playcanvas.com.

Changes:

  • GraphicsDevice#updateClientRect now uses a capability check (typeof this.canvas.getBoundingClientRect === 'function') instead of the platform.worker environment check. When the method is missing (OffscreenCanvas in workers, mock canvases in headless environments), it falls back to this.canvas.width ?? 0 / height ?? 0, so a bare mock object yields clientRect = { width: 0, height: 0 } rather than throwing or producing NaN.
  • Worker behavior is unchanged (OffscreenCanvas has no getBoundingClientRect either), and real DOM canvases still get a valid clientRect at construction time, preserving the intent of Fix camera.screenToWorld returning NaN when called in initialize #8950.
  • Added unit tests covering a mock canvas without getBoundingClientRect, a mock with width/height, and a DOM canvas using getBoundingClientRect.

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

Build size report

This PR changes the size of the minified bundles.

Bundle Minified Gzip Brotli
playcanvas.min.js 2270.6 KB (+0.0 KB, +0.00%) 584.5 KB (+0.0 KB, +0.00%) 454.0 KB (−0.2 KB, −0.04%)
playcanvas.min.mjs 2268.1 KB (+0.0 KB, +0.00%) 583.6 KB (+0.0 KB, +0.00%) 453.7 KB (+0.1 KB, +0.03%)

@mvaligursky mvaligursky merged commit 3fbd89b into main Jul 2, 2026
10 checks passed
@mvaligursky mvaligursky deleted the mv-null-device-client-rect branch July 2, 2026 08:51
mvaligursky added a commit that referenced this pull request Jul 2, 2026
…9000)

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
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.

NullGraphicsDevice throws in constructor with mock canvas since v2.20.0 (getBoundingClientRect regression from #8950)

1 participant