Skip to content

Commit dca8de2

Browse files
committed
Add initial support for OffcreenCanvas
1 parent a55ad11 commit dca8de2

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/core/utils-core.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,23 @@ class UtilsCore {
3535
* @memberOf UtilsCore
3636
*
3737
*
38-
* @desc Return TRUE, on a valid DOM canvas object
38+
* @desc Return TRUE, on a valid DOM canvas or OffscreenCanvas object
3939
*
4040
* Note: This does just a VERY simply sanity check. And may give false positives.
4141
*
4242
* @param {CanvasDOMObject} canvasObj - Object to validate
4343
*
44-
* @returns {Boolean} TRUE if the object is a DOM canvas
44+
* @returns {Boolean} TRUE if the object is a DOM canvas or OffscreenCanvas
4545
*
4646
*/
4747
static isCanvas(canvasObj) {
4848
return (
4949
canvasObj !== null &&
50-
canvasObj.nodeName &&
51-
canvasObj.getContext &&
52-
canvasObj.nodeName.toUpperCase() === 'CANVAS'
50+
((canvasObj.nodeName &&
51+
canvasObj.getContext &&
52+
canvasObj.nodeName.toUpperCase() === 'CANVAS') ||
53+
(typeof OffscreenCanvas !== 'undefined' &&
54+
canvasObj instanceof OffscreenCanvas))
5355
);
5456
}
5557

@@ -87,7 +89,7 @@ class UtilsCore {
8789
}
8890

8991
// Create a new canvas DOM
90-
const canvas = document.createElement('canvas');
92+
const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : new OffscreenCanvas(0, 0);
9193

9294
// Default width and height, to fix webgl issue in safari
9395
canvas.width = 2;
@@ -217,10 +219,21 @@ class UtilsCore {
217219
}
218220

219221
// Create a new canvas DOM
220-
const webGl = (
221-
canvasObj.getContext('experimental-webgl', UtilsCore.initWebGlDefaultOptions()) ||
222-
canvasObj.getContext('webgl', UtilsCore.initWebGlDefaultOptions())
223-
);
222+
let webGl = null;
223+
224+
try {
225+
webGl = canvasObj.getContext('experimental-webgl', UtilsCore.initWebGlDefaultOptions());
226+
} catch (e) {
227+
// 'experimental-webgl' is not a supported context type
228+
// fallback to 'webgl2' or 'webgl' below
229+
}
230+
231+
if (webGl === null) {
232+
webGl = (
233+
canvasObj.getContext('webgl2', UtilsCore.initWebGlDefaultOptions()) ||
234+
canvasObj.getContext('webgl', UtilsCore.initWebGlDefaultOptions())
235+
);
236+
}
224237

225238
if (webGl) {
226239
// Get the extension that is needed
@@ -287,7 +300,7 @@ class UtilsCore {
287300
//
288301
//-----------------------------------------------------------------------------
289302

290-
const _isCanvasSupported = typeof document !== 'undefined' ? UtilsCore.isCanvas(document.createElement('canvas')) : false;
303+
const _isCanvasSupported = typeof document !== 'undefined' ? UtilsCore.isCanvas(document.createElement('canvas')) : typeof OffscreenCanvas !== 'undefined';
291304
const _testingWebGl = UtilsCore.initWebGl(UtilsCore.initCanvas());
292305
const _testingWebGl2 = UtilsCore.initWebGl2(UtilsCore.initCanvas());
293306
const _isWebGlSupported = UtilsCore.isWebGl(_testingWebGl);

0 commit comments

Comments
 (0)