检查客户端浏览器是否支持和启用 WebGL2

Nee*_*raj 3 javascript webgl webgl2

我想检查用户浏览器是否启用和支持 WebGL 2。

有很多关于 WebGL 1 的帖子,但我没有发现与 WebGL 版本 2 相关的内容。

gma*_*man 8

您应该检查的方式只是查看尝试获取webgl2上下文是成功还是失败

const gl = document.createElement('canvas').getContext('webgl2');
if (!gl) {
  console.log('your browser/OS/drivers do not support WebGL2');
} else {
  console.log('webgl2 works!');
}
  
Run Code Online (Sandbox Code Playgroud)

您还可以检查是否window.WebGL2RenderingContext存在以尝试猜测是否是不支持 WebGL2 的浏览器或用户的 OS/GPU/驱动程序。

const gl = document.createElement('canvas').getContext('webgl2');
if (!gl) {
  if (typeof WebGL2RenderingContext !== 'undefined') {
    console.log('your browser appears to support WebGL2 but it might be disabled. Try updating your OS and/or video card drivers');
  } else {
    console.log('your browser has no WebGL2 support at all'); 
  }
} else {
  console.log('webgl2 works!');
}
Run Code Online (Sandbox Code Playgroud)