Skip to content

Instantly share code, notes, and snippets.

@vip3r011
Forked from SeanZoR/detectWebGL.js
Created September 25, 2018 21:05
Show Gist options
  • Save vip3r011/8f3c21e6c56497447650d6e3e6718676 to your computer and use it in GitHub Desktop.
Save vip3r011/8f3c21e6c56497447650d6e3e6718676 to your computer and use it in GitHub Desktop.
Detect WebGL with JS in browser
/**
* Detects if WebGL is enabled.
* Inspired from http://www.browserleaks.com/webgl#howto-detect-webgl
*
* @return { number } -1 for not Supported,
* 0 for disabled
* 1 for enabled
*/
function detectWebGL()
{
// Check for the WebGL rendering context
if ( !! window.WebGLRenderingContext) {
var canvas = document.createElement("canvas"),
names = ["webgl", "experimental-webgl", "moz-webgl", "webkit-3d"],
context = false;
for (var i in names) {
try {
context = canvas.getContext(names[i]);
if (context && typeof context.getParameter === "function") {
// WebGL is enabled.
return 1;
}
} catch (e) {}
}
// WebGL is supported, but disabled.
return 0;
}
// WebGL not supported.
return -1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment