Check & report if browser supports this renderer
`RenderWebGL`'s constructor will now throw if TWGL init fails. There's also a new static method to check whether or not we expect TWGL init to succeed.
This commit is contained in:
parent
d7c7a56e25
commit
07a6ae57be
@ -58,6 +58,31 @@ const FENCE_WIDTH = 15;
|
||||
|
||||
|
||||
class RenderWebGL extends EventEmitter {
|
||||
/**
|
||||
* Check if this environment appears to support this renderer before attempting to create an instance.
|
||||
* Catching an exception from the constructor is also a valid way to test for (lack of) support.
|
||||
* @param {canvas} [optCanvas] - An optional canvas to use for the test. Otherwise a temporary canvas will be used.
|
||||
* @returns {boolean} - True if this environment appears to support this renderer, false otherwise.
|
||||
*/
|
||||
static isSupported (optCanvas) {
|
||||
try {
|
||||
// Create the context the same way that the constructor will: attributes may make the difference.
|
||||
return !!RenderWebGL._getContext(optCanvas || document.createElement('canvas'));
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask TWGL to create a rendering context with the attributes used by this renderer.
|
||||
* @param {canvas} canvas - attach the context to this canvas.
|
||||
* @returns {WebGLRenderingContext} - a TWGL rendering context (backed by either WebGL 1.0 or 2.0).
|
||||
* @private
|
||||
*/
|
||||
static _getContext (canvas) {
|
||||
return twgl.getContext(canvas, {alpha: false, stencil: true});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a renderer for drawing Scratch sprites to a canvas using WebGL.
|
||||
* Coordinates will default to Scratch 2.0 values if unspecified.
|
||||
@ -77,6 +102,12 @@ class RenderWebGL extends EventEmitter {
|
||||
constructor (canvas, xLeft, xRight, yBottom, yTop) {
|
||||
super();
|
||||
|
||||
/** @type {WebGLRenderingContext} */
|
||||
const gl = this._gl = RenderWebGL._getContext(canvas);
|
||||
if (!gl) {
|
||||
throw new Error('Could not get WebGL context: this browser or environment may not support WebGL.');
|
||||
}
|
||||
|
||||
/** @type {Drawable[]} */
|
||||
this._allDrawables = [];
|
||||
|
||||
@ -86,9 +117,6 @@ class RenderWebGL extends EventEmitter {
|
||||
/** @type {Array<int>} */
|
||||
this._drawList = [];
|
||||
|
||||
/** @type {WebGLRenderingContext} */
|
||||
const gl = this._gl = twgl.getWebGLContext(canvas, {alpha: false, stencil: true});
|
||||
|
||||
/** @type {int} */
|
||||
this._nextDrawableId = RenderConstants.ID_NONE + 1;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user