Created
July 19, 2023 17:48
-
-
Save danieljpgo/21475f803f37e0c62c7ed980e74c54c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function useSupportWebGL() { | |
const [hasWebGLSupport, setHasWebGLSupport] = useState<boolean>(); | |
useEffect(() => { | |
const canvas = window.document.createElement('canvas'); | |
try { | |
const gl = canvas.getContext('webgl'); | |
if (gl === null) throw new Error('not supported'); | |
gl.getSupportedExtensions(); | |
setHasWebGLSupport(true); | |
} catch (e) { | |
const glExperimental = canvas.getContext('experimental-webgl'); | |
if (glExperimental !== null && 'getSupportedExtensions' in glExperimental) { | |
glExperimental.getSupportedExtensions(); | |
setHasWebGLSupport(true); | |
return; | |
} | |
// WebGL isn't properly supported | |
alert('WebGL not supported'); | |
setHasWebGLSupport(false); | |
return; | |
} | |
}, []); | |
return hasWebGLSupport; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment