Proper way to detect WebGL support?

后端 未结 7 1125
悲&欢浪女
悲&欢浪女 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

    // this code will detect WebGL version until WebGL Version maxVersionTest 
    var
    maxVersionTest = 5,
    canvas = document.createElement('canvas'),
    webglVersion = (canvas.getContext('webgl') || canvas.getContext('experimental-webgl')) ? 1 : null,
    canvas = null; // free context
    
    // range: if maxVersionTest = 5 makes [5, 4, 3, 2]
    Array.apply(null, Array(maxVersionTest - 1))
    .map(function (_, idx) {return idx + 2;})
    .reverse()
    .some(function(version){
        // cant reuse canvas, potential to exceed contexts or mem limit *
        if (document.createElement('canvas').getContext('webgl'+version))
            return !!(webglVersion = version);
    });
    
    console.log(webglVersion);
    

    * re "potential to exceed contexts or mem limit" see https://bugs.chromium.org/p/chromium/issues/detail?id=226868

提交回复
热议问题