【问题标题】:Deleting WebGL contexts after testing for WebGL context在测试 WebGL 上下文后删除 WebGL 上下文
【发布时间】:2018-05-31 01:29:41
【问题描述】:

出于好奇,我想使用以下代码查看各种访问者支持哪些 WebGL 上下文:

var e = document.createElement('canvas');
var c = ['webgl','ms-webgl','experimental-webgl','moz-webgl']; 
for (var y = -1, len = c.length; ++y < len;)
{
 try {if (e.getContext(c[y])) {/*do-a-jig*/ break;}} catch(e) {}
}

不幸的是,这造成了一个非常难以重现的在 Web 开发者控制台中报告的错误:

WebGL 警告:此主体的实时 WebGL 上下文超过 16 个, 丢失最近最少使用的。

所以问题很简单,在测试了上下文之后,如何删除该上下文以避免产生此错误?

【问题讨论】:

  • for (var y=0; y&lt;c.length; y++) 有什么问题。
  • moz-webglms-webgl 不存在

标签: html5-canvas webgl


【解决方案1】:

您可以尝试以下方法:(未经测试)

var e = document.createElement('canvas');
var c = ['webgl','ms-webgl','experimental-webgl','moz-webgl']; 

var found = '';
for (var y=0; y<c.length; y++)
{
    try {
        var ctx = e.getContext(c[y]);
        if (ctx) {
            found = c[y];
            ctx.getExtension('WEBGL_lose_context').loseContext();
            break;
        }

    } catch(e) { }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多