Proper way to detect WebGL support?

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

    As seen in http://www.browserleaks.com/webgl#howto-detect-webgl

    This is a proper javascript function to detect WebGL support, with all kind of experimental WebGL context names and with checking of special cases, such as blocking WebGL functions by NoScript or TorBrowser.

    It will report one of the three WebGL capability states:

    • WebGL is enabled — return TRUE, or return
    • WebGL object, if the first argument was passed
    • WebGL is disabled — return FALSE, you can change it if you need>
    • WebGL is not implimented — return FALSE
    function webgl_detect(return_context)
    {
        if (!!window.WebGLRenderingContext) {
            var canvas = document.createElement("canvas"),
                 names = ["webgl2", "webgl", "experimental-webgl", "moz-webgl", "webkit-3d"],
               context = false;
    
            for(var i=0;i< names.length;i++) {
                try {
                    context = canvas.getContext(names[i]);
                    if (context && typeof context.getParameter == "function") {
                        // WebGL is enabled
                        if (return_context) {
                            // return WebGL object if the function's argument is present
                            return {name:names[i], gl:context};
                        }
                        // else, return just true
                        return true;
                    }
                } catch(e) {}
            }
    
            // WebGL is supported, but disabled
            return false;
        }
    
        // WebGL not supported
        return false;
    }
    

提交回复
热议问题