Skip to content

Commit

Permalink
Support for OffscreenCanvas in Web Workers #2481
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfranzl committed Jul 14, 2024
1 parent e5afe4e commit 957af98
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"TWEEN": "readonly",
"twgsl": "readonly",
"webkitAudioContext": "readonly",
"WorkerGlobalScope": "readonly",
"XRRay": "readonly",
"XRRigidTransform": "readonly",
"XRWebGLLayer": "readonly"
Expand Down
14 changes: 12 additions & 2 deletions src/core/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const detectPassiveEvents = () => {
};

const ua = (typeof navigator !== 'undefined') ? navigator.userAgent : '';
const environment = typeof window !== 'undefined' ? 'browser' :
const environment =
typeof window !== 'undefined' ? 'browser' :
typeof WorkerGlobalScope !== 'undefined' ? 'webworker' :

Check failure on line 22 in src/core/platform.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 8 spaces but found 4
typeof global !== 'undefined' ? 'node' : 'worker';

Check failure on line 23 in src/core/platform.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 12 spaces but found 4

// detect platform
Expand Down Expand Up @@ -78,7 +80,8 @@ const platform = {
global: (typeof globalThis !== 'undefined' && globalThis) ??
(environment === 'browser' && window) ??
(environment === 'node' && global) ??
(environment === 'worker' && self),
(environment === 'worker' && self) ??
(environment === 'webworker' && WorkerGlobalScope),

/**
* Convenience boolean indicating whether we're running in the browser.
Expand All @@ -87,6 +90,13 @@ const platform = {
*/
browser: environment === 'browser',

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

/**
* 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.webworker ? 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,16 @@ class GraphicsDevice extends EventHandler {
flags: CLEARFLAG_COLOR | CLEARFLAG_DEPTH
};

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

static EVENT_RESIZE = 'resizecanvas';

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

updateClientRect() {
this.clientRect = this.canvas.getBoundingClientRect();
if (platform.browser) {
const rect = this.canvas.getBoundingClientRect();
this.clientRect.width = rect.width;
this.clientRect.height = rect.height;

} else if (platform.webworker) {
// Web Workers don't do page layout, so getBoundingClientRect is not available

Check failure on line 771 in src/platform/graphics/graphics-device.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected tab character
this.clientRect.width = this.canvas.width;
this.clientRect.height = this.canvas.height;
}
}

/**
Expand Down

0 comments on commit 957af98

Please sign in to comment.