diff --git a/package.json b/package.json index 504bd3c..9663410 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sdf-2d", - "version": "0.1.1-alpha", + "version": "0.1.2-alpha", "description": "Graphics framework for efficiently rendering 2D signed distance fields.", "keywords": [ "webgl", diff --git a/src/graphics/graphics-library/universal-rendering-context.ts b/src/graphics/graphics-library/universal-rendering-context.ts index 3b299eb..ace3e99 100644 --- a/src/graphics/graphics-library/universal-rendering-context.ts +++ b/src/graphics/graphics-library/universal-rendering-context.ts @@ -7,28 +7,27 @@ export const getUniversalRenderingContext = ( canvas: HTMLCanvasElement, ignoreWebGL2 = false ): UniversalRenderingContext => { - let context: WebGL2RenderingContext | WebGLRenderingContext | null = ignoreWebGL2 + const context: WebGL2RenderingContext | WebGLRenderingContext | null = ignoreWebGL2 ? null : canvas.getContext('webgl2'); - let webgl2Support = true; + let result = context as UniversalRenderingContext; - if (!context) { - context = canvas.getContext('webgl'); - webgl2Support = false; + if (context) { + if (!Object.prototype.hasOwnProperty.call(context, 'isWebGL2')) { + result.isWebGL2 = true; + } + } else { + result = (canvas.getContext('webgl') || + canvas.getContext('experimental-webgl')) as UniversalRenderingContext; + + if (!result) { + throw new Error('Neither WebGL or WebGL2 is supported'); + } + + result.isWebGL2 = false; } - if (!context) { - context = canvas.getContext('experimental-webgl') as WebGLRenderingContext; - } - - if (!context) { - throw new Error('Neither WebGL or WebGL2 is supported'); - } - - Insights.setValue('using WebGL2', webgl2Support); - - const result = context as UniversalRenderingContext; - result.isWebGL2 = webgl2Support; + Insights.setValue('using WebGL2', result.isWebGL2); return result; };