diff --git a/packages/utils/src/browser/isWebGLSupported.js b/packages/utils/src/browser/isWebGLSupported.js index 1a2b9e2..bc23866 100644 --- a/packages/utils/src/browser/isWebGLSupported.js +++ b/packages/utils/src/browser/isWebGLSupported.js @@ -1,3 +1,5 @@ +let supported; + /** * Helper for checking for WebGL support. * @@ -7,37 +9,45 @@ */ export function isWebGLSupported() { - const contextOptions = { stencil: true, failIfMajorPerformanceCaveat: true }; - - try + if (typeof supported === 'undefined') { - if (!window.WebGLRenderingContext) + supported = (function supported() { - return false; - } + const contextOptions = { stencil: true, failIfMajorPerformanceCaveat: true }; - const canvas = document.createElement('canvas'); - let gl = canvas.getContext('webgl', contextOptions) || canvas.getContext('experimental-webgl', contextOptions); - - const success = !!(gl && gl.getContextAttributes().stencil); - - if (gl) - { - const loseContext = gl.getExtension('WEBGL_lose_context'); - - if (loseContext) + try { - loseContext.loseContext(); + if (!window.WebGLRenderingContext) + { + return false; + } + + const canvas = document.createElement('canvas'); + let gl = canvas.getContext('webgl', contextOptions) + || canvas.getContext('experimental-webgl', contextOptions); + + const success = !!(gl && gl.getContextAttributes().stencil); + + if (gl) + { + const loseContext = gl.getExtension('WEBGL_lose_context'); + + if (loseContext) + { + loseContext.loseContext(); + } + } + + gl = null; + + return success; } - } - - gl = null; - - return success; + catch (e) + { + return false; + } + })(); } - catch (e) - { - return false; - } + + return supported; } -