【问题标题】:webglcontextrestored event not firing after context lostwebglcontextrestored 事件在上下文丢失后未触发
【发布时间】:2016-05-13 16:57:41
【问题描述】:

我正在编写一个 webgl 应用程序,并试图在 webgl 上下文丢失后恢复它,但上下文恢复事件似乎没有触发。

我正在运行以下代码,并希望根据WebGL specificationWebGL wiki 上的文档记录“上下文丢失”和“上下文恢复”。当我在 Chrome (50.0.2661.102 m) 和 Firefox (46.0.1) 的 jsfiddle 中运行下面的代码时,我看到记录了“上下文丢失”但没有“恢复上下文”,并且我在电子应用程序中看到了相同的行为.

var canvas = document.createElement( 'canvas' )
var gl = canvas.getContext("webgl");
var WEBGL_lose_context = gl.getExtension('WEBGL_lose_context');

canvas.addEventListener("webglcontextlost", function(e) {
    log("context lost");
    e.preventDefault();
}, false);

canvas.addEventListener("webglcontextrestored", function() {
    log("context restored");
}, false);

WEBGL_lose_context.loseContext();

function log(msg) {
  var div = document.createElement("pre");
  div.appendChild(document.createTextNode(msg));
  document.body.appendChild(div);
}

我需要做任何额外的事情来触发上下文恢复事件吗?我是否误解了 WebGL 规范?

【问题讨论】:

    标签: webgl


    【解决方案1】:

    如果使用WEBGL_lose_context,您必须致电WEBGL_lose_context.restoreContext()WEBGL_lose_context 仅用于测试。在正常情况下,由浏览器决定何时恢复上下文。例如,如果您的选项卡不是前面的选项卡,而另一个选项卡需要所有 WebGL 内存,则您的选项卡可能会丢失上下文事件。稍后,当您将选项卡设为活动选项卡时,您最终将获得一个恢复上下文事件。

    See conformance test here

    【讨论】:

    • 旁注:在丢失上下文后调用getExtension('WEBGL_lose_context') 不再起作用,因此您需要在某处缓存扩展。
    猜你喜欢
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2016-03-18
    • 1970-01-01
    • 2011-12-05
    • 2019-05-09
    相关资源
    最近更新 更多