Skip to content

Instantly share code, notes, and snippets.

@danieljpgo
Created July 19, 2023 17:48
Show Gist options
  • Save danieljpgo/21475f803f37e0c62c7ed980e74c54c0 to your computer and use it in GitHub Desktop.
Save danieljpgo/21475f803f37e0c62c7ed980e74c54c0 to your computer and use it in GitHub Desktop.
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