Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support for OffscreenCanvas in Web Workers #5168

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/core/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ const platform = {
*/
browser: environment === 'browser',

/**
* True if running in a Web Worker.
*
* @type {boolean}
* @ignore
*/
worker: environment === 'worker',

/**
* True if running on a desktop or laptop device.
*
Expand Down
2 changes: 1 addition & 1 deletion src/framework/app-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,7 @@ const makeTick = function (_app) {
if (application.xr?.session) {
application.frameRequestId = application.xr.session.requestAnimationFrame(application.tick);
} else {
application.frameRequestId = platform.browser ? window.requestAnimationFrame(application.tick) : null;
application.frameRequestId = platform.browser || platform.worker ? requestAnimationFrame(application.tick) : null;
}

if (application.graphicsDevice.contextLost)
Expand Down
2 changes: 1 addition & 1 deletion src/framework/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Application extends AppBase {
* - sound ({@link SoundComponentSystem})
* - sprite ({@link SpriteComponentSystem})
*
* @param {HTMLCanvasElement} canvas - The canvas element.
* @param {HTMLCanvasElement | OffscreenCanvas} canvas - The canvas element.
* @param {object} [options] - The options object to configure the Application.
* @param {ElementInput} [options.elementInput] - Input handler for {@link ElementComponent}s.
* @param {Keyboard} [options.keyboard] - Keyboard handler for input.
Expand Down
21 changes: 20 additions & 1 deletion src/platform/graphics/graphics-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,17 @@ class GraphicsDevice extends EventHandler {
flags: CLEARFLAG_COLOR | CLEARFLAG_DEPTH
};

/**
* The current client rect.
*
* @type {{ width: number, height: number }}
* @ignore
*/
clientRect = {
width: 0,
height: 0
};

static EVENT_RESIZE = 'resizecanvas';

constructor(canvas, options) {
Expand Down Expand Up @@ -752,7 +763,15 @@ class GraphicsDevice extends EventHandler {
}

updateClientRect() {
this.clientRect = this.canvas.getBoundingClientRect();
if (platform.worker) {
// Web Workers don't do page layout, so getBoundingClientRect is not available
this.clientRect.width = this.canvas.width;
this.clientRect.height = this.canvas.height;
} else {
const rect = this.canvas.getBoundingClientRect();
this.clientRect.width = rect.width;
this.clientRect.height = rect.height;
}
}

/**
Expand Down