25 May 2015

How to detect WebGL support

Detect if browser supports WebGL.

function webglDetect() {
  var canvas = document.createElement('canvas');
  var contextNames = ['webgl','experimental-webgl','moz-webgl','webkit-3d'];
  var context;

  if (navigator.userAgent.indexOf('MSIE') > -1) {
    try {
      context = WebGLHelper.CreateGLContext(canvas, 'canvas');
    } catch(e) {}
  } else {
    for (var i = 0; i < contextNames.length; i++) {
      try {
        context = canvas.getContext(contextNames[i]);
        if (context) {
          break;
        }
      } catch(e) {}
    }
  }

  return !!context;
}

Usage:

if (webglDetect()) {
    // WebGL is supported
}

On github at miguelmota/webgl-detect.