2016-05-12 40 views
1

我正在寫一個webgl應用程序,並試圖恢復webgl上下文後,它已經丟失,但上下文恢復事件似乎並沒有被解僱。webglcontextrestored事件不發射後上下文

我正在運行以下代碼,根據WebGL specificationWebGL wiki上的文檔,預計將記錄「上下文丟失」和「上下文已恢復」。當我在jsfiddle中同時運行Chrome(50.0.2661.102 m)和Firefox(46.0.1)中的代碼時,我看到「上下文丟失」記錄,但沒有「上下文恢復」,我在電子應用程序中看到了相同的行爲。

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規範嗎?

回答

1

在使用WEBGL_lose_context的情況下,您必須致電WEBGL_lose_context.restoreContext()WEBGL_lose_context僅用於測試。在正常情況下,由瀏覽器決定何時恢復上下文。例如,如果您的選項卡不是前面的選項卡,而另一個選項卡需要所有的WebGL內存,您的選項卡可能會丟失上下文事件。稍後,當您將選項卡設置爲活動選項卡時,您最終將獲得恢復上下文事件。

See conformance test here

+0

邊注:調用'getExtension(「WEBGL_lose_context」)丟失上下文之後'不再起作用,所以你需要在某處緩存擴展名。 – Thomas