Proper way to detect WebGL support?

后端 未结 7 1134
悲&欢浪女
悲&欢浪女 2020-11-30 05:15

I am attempting to detect WebGL support across multiple browsers and I\'ve encountered the following scenario. The current version of Firefox appears to report positive supp

7条回答
  •  感情败类
    2020-11-30 06:03

    [Oct 2014] I've updated modernizrs example to match their current implementation, which is a cleaned up version from http://get.webgl.org/ further below.

    Modernizr does,

    var canvas;
    var ctx;
    var exts;
    
    try {
      canvas = createElement('canvas');
      ctx = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
      exts = ctx.getSupportedExtensions();
    }
    catch (e) {
      return;
    }
    
    if (ctx !== undefined) {
      Modernizr.webglextensions = new Boolean(true);
    }
    
    for (var i = -1, len = exts.length; ++i < len; ){
      Modernizr.webglextensions[exts[i]] = true;
    }
    
    canvas = undefined;
    

    Chromium points to http://get.webgl.org/ for the canonical support implementation,

    try { gl = canvas.getContext("webgl"); }
    catch (x) { gl = null; }
    
    if (gl == null) {
        try { gl = canvas.getContext("experimental-webgl"); experimental = true; }
        catch (x) { gl = null; }
    }
    

提交回复
热议问题