@@ -35,21 +35,23 @@ class UtilsCore {
35
35
* @memberOf UtilsCore
36
36
*
37
37
*
38
- * @desc Return TRUE, on a valid DOM canvas object
38
+ * @desc Return TRUE, on a valid DOM canvas or OffscreenCanvas object
39
39
*
40
40
* Note: This does just a VERY simply sanity check. And may give false positives.
41
41
*
42
42
* @param {CanvasDOMObject } canvasObj - Object to validate
43
43
*
44
- * @returns {Boolean } TRUE if the object is a DOM canvas
44
+ * @returns {Boolean } TRUE if the object is a DOM canvas or OffscreenCanvas
45
45
*
46
46
*/
47
47
static isCanvas ( canvasObj ) {
48
48
return (
49
49
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 ) )
53
55
) ;
54
56
}
55
57
@@ -87,7 +89,7 @@ class UtilsCore {
87
89
}
88
90
89
91
// Create a new canvas DOM
90
- const canvas = document . createElement ( 'canvas' ) ;
92
+ const canvas = typeof document !== 'undefined' ? document . createElement ( 'canvas' ) : new OffscreenCanvas ( 0 , 0 ) ;
91
93
92
94
// Default width and height, to fix webgl issue in safari
93
95
canvas . width = 2 ;
@@ -217,10 +219,21 @@ class UtilsCore {
217
219
}
218
220
219
221
// 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
+ }
224
237
225
238
if ( webGl ) {
226
239
// Get the extension that is needed
@@ -287,7 +300,7 @@ class UtilsCore {
287
300
//
288
301
//-----------------------------------------------------------------------------
289
302
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' ;
291
304
const _testingWebGl = UtilsCore . initWebGl ( UtilsCore . initCanvas ( ) ) ;
292
305
const _testingWebGl2 = UtilsCore . initWebGl2 ( UtilsCore . initCanvas ( ) ) ;
293
306
const _isWebGlSupported = UtilsCore . isWebGl ( _testingWebGl ) ;
0 commit comments