Proper way to detect WebGL support?

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

    In order to detect browsers that support WebGL, but leaving out older browsers with may not support it well (as needed in WebGL detected as supported when it is actually not for ruling out Android 4.4.2 devices), I am adding a tighter, though unrelated check:

    function hasWebGL() {
        var supported;
    
        try {
            var canvas = document.createElement('canvas');
            supported = !! window.WebGLRenderingContext && (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'));
        } catch(e) { supported = false; }
    
        try {
            // let is by no means required, but will help us rule out some old browsers/devices with potentially buggy implementations: http://caniuse.com/#feat=let
            eval('let foo = 123;');
        } catch (e) { supported = false; }
    
        if (supported === false) {
            console.log("WebGL is not supported");
        }
    
        canvas = undefined;
    
        return supported;
    },
    

提交回复
热议问题