【问题标题】:Reactjs - create manual webGl check since pixi is not using fallbackReactjs - 创建手动 webGl 检查,因为 pixi 没有使用回退
【发布时间】:2021-01-14 02:37:26
【问题描述】:

我目前正在使用 pixi.js 来处理 webGl,因此我需要一个后备。

我已经在使用旧版:

import * as PIXI from 'pixi.js-legacy'

但不知何故,当不支持 webGl 说我需要使用旧版本时,它仍然会引发错误。

由于这不起作用,我需要在其他地方检查 webGl。我已经制定了以下代码,它适用于 webGl 被禁用的情况但如果它不受支持则不起作用。我该如何检查呢?

const canvas = document.createElement("canvas");
const gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
return gl && gl instanceof WebGLRenderingContext;

【问题讨论】:

  • 不知道为什么需要gl instanceof WebGLRenderingContext。否则不确定你在问什么。这应该足以测试期间。
  • 测试一下webGl是否被禁用就够了,但不支持就不行了

标签: javascript webgl pixi.js


【解决方案1】:

检查浏览器是否太旧以至于不支持 WebGL 的唯一方法是检查 WebGLRenderingContext 是否存在。注意:如果浏览器太旧,则不能使用const,因为浏览器只会将其视为语法错误。

if (typeof WebGLRenderingContext === 'undefined') {
  // this browser doesn't even know what WebGL is
} else {
  var gl = document.createElement('canvas').getContext('webgl');
  if (!gl) {
     // webgl failed to initialize for any number of reasons
     // including it's turned off, the browser blacklisted the drivers,
     // it's out of memory, other.
  }
}

检查experimental-webgl 基本上意味着检查有错误的webgl,所以如果你关心使用有已知错误的webgl 版本,那么你可以添加它。

【讨论】:

  • 谢谢,我会将此标记为答案 - 但是,由于不支持 webGl,在 lambdatest.com 上对其进行测试仍会返回 ...please use pixi.js-legacy 以作为后备。它以某种方式检测到 webGl 何时在 f.e. 中被禁用。火狐。
猜你喜欢
  • 1970-01-01
  • 2022-08-03
  • 2014-08-31
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
  • 2013-06-03
  • 2018-12-23
相关资源
最近更新 更多