Proper way to detect WebGL support?

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

    From MDN:

    // Run everything inside window load event handler, to make sure
    // DOM is fully loaded and styled before trying to manipulate it.
    window.addEventListener("load", function() {
      var paragraph = document.querySelector("p"),
        button = document.querySelector("button");
      // Adding click event handler to button.
      button.addEventListener("click", detectWebGLContext, false);
      function detectWebGLContext () {
        // Create canvas element. The canvas is not added to the
        // document itself, so it is never displayed in the
        // browser window.
        var canvas = document.createElement("canvas");
        // Get WebGLRenderingContext from canvas element.
        var gl = canvas.getContext("webgl")
          || canvas.getContext("experimental-webgl");
        // Report the result.
        if (gl && gl instanceof WebGLRenderingContext) {
          paragraph.innerHTML =
            "Congratulations! Your browser supports WebGL.";
        } else {
          paragraph.innerHTML = "Failed to get WebGL context. "
            + "Your browser or device may not support WebGL.";
        }
      }
    }, false);
    body {
      text-align : center;
    }
    button {
      display : block;
      font-size : inherit;
      margin : auto;
      padding : 0.6em;
    }

    [ Here would go the result of WebGL feature detection ]

提交回复
热议问题