checkWEBGL(检测webgl)

2022-2-18
/**
 * Detects if WebGL is enabled.
 * @param {Object} av Autodesk.Viewing;
 * @return { number } -1 for not Supported,
 *                    0 for disabled
 *                    1 for enabled
 */
function detectWebGL(av) {
  // Check for the webgl rendering context
  if (av.getGlobal().WebGLRenderingContext) {
    let canvas = av.getGlobal().document.createElement('canvas'),
      names = ['webgl', 'experimental-webgl', 'moz-webgl', 'webkit-3d'],
      context = false;

    for (let i = 0; i < 4; i++) {
      try {
        context = canvas.getContext(names[i]);
        if (context && typeof context.getParameter === 'function') {
          // WebGL is enabled.
          return 1;
        }
      } catch (e) {
        console.log(e);
      }
    }

    // WebGL is supported, but disabled.
    return 0;
  }

  // WebGL not supported.
  return -1;
}

export default detectWebGL;