javascript 抑制“老鼠!WebGL 遇到了障碍”。谷歌浏览器中的错误栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18361596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suppressing the "Rats! WebGL hit a snag." error bar in Google Chrome
提问by Facebook Staff are Complicit
I've recently started using WebGL on a site I'm developing. It's being used as an enhancement, and the site falls back to a canvas rendering if WebGL is not supported or has an error.
我最近开始在我正在开发的网站上使用 WebGL。它被用作增强功能,如果 WebGL 不受支持或有错误,站点将退回到画布渲染。
Unfortunately when a WebGL exception occurs in Google Chrome, an error message bar appears. This bar does not disappear until the user interacts with it. If they reload or navigate to a different page, the message will re-appear the next time the site tries to use WebGL.
不幸的是,当 Google Chrome 中出现 WebGL 异常时,会出现错误消息栏。在用户与之交互之前,此栏不会消失。如果他们重新加载或导航到不同的页面,则该消息将在该站点下次尝试使用 WebGL 时重新出现。
In the case of my site, this means that the WebGL error message will never go away because each page attempts to use WebGL. Once an error has occurred Chrome will not use WebGL again on the same site until the user tells it to Reload, so the continuous error message doesn't actually indicate continuous errors, just continuous attempts to use WebGL.
就我的站点而言,这意味着 WebGL 错误消息永远不会消失,因为每个页面都尝试使用 WebGL。一旦发生错误,Chrome 将不会在同一站点上再次使用 WebGL,直到用户告诉它Reload,因此连续错误消息实际上并不表示连续错误,只是连续尝试使用 WebGL。
Once a WebGL error occurs, this dialog can be reproduced on that site just by running:
一旦发生 WebGL 错误,只需运行以下命令即可在该站点上重现此对话框:
document.createElement('canvas').getContext('experimental-webgl');
This does not raise any exception, and my .onerror
method on the canvas element wasn't called.
这不会引发任何异常,并且我.onerror
在画布元素上的方法没有被调用。
I haven't been able to investigate this too deeply because I have not been able to reliably reproduce a WebGL error. (Even if I could reproduce one on my computer, it may not be reproducible on others.)
我无法对此进行深入调查,因为我无法可靠地重现 WebGL 错误。(即使我可以在我的计算机上复制一个,也可能无法在其他人上复制。)
This behaviour would be reasonable for a site that relied on WebGL, but mine does not, so the message is just distracting and confusing to users.
对于依赖 WebGL 的站点而言,这种行为是合理的,但我的站点并非如此,因此该消息只会分散用户的注意力并让用户感到困惑。
Is there any way to suppress this error message? I don't mind falling back to the Ignorebehaviour of having WebGL disabled once an error occurs.
有什么办法可以抑制这个错误信息?一旦发生错误,我不介意退回到禁用 WebGL的忽略行为。
回答by Gero3
If you want to ignore the exception, then the following should be enough.
如果您想忽略异常,那么以下内容应该足够了。
canvas.addEventListener("webglcontextlost", function(e) { e.preventDefault(); Stopdrawing(); }, false);
If you want to retry loading the webgl context
如果你想重试加载 webgl 上下文
canvas.addEventListener("webglcontextrestored", function (event) {initializeResources(); }, false);
EDIT:
编辑:
If you want to test if the context get handled correctly then make use of the WEBGL_lose_context extension on the webgl context.
如果您想测试上下文是否得到正确处理,请使用 webgl 上下文上的 WEBGL_lose_context 扩展。
gl.getExtension("WEBGL_lose_context").loseContext()
gl.getExtension("WEBGL_lose_context").restoreContext()
回答by Chris O
Took me ages to solve this problem - it seems that the Rats! error came about when resizing a canvas which had a 3D context based on it when using Chrome. The solution was to catch the resize via a listener and recreate the canvas and context at that point. Hope that helps.
花了我很长时间才解决这个问题 - 似乎是老鼠!在使用 Chrome 时调整具有基于它的 3D 上下文的画布的大小时出现错误。解决方案是通过侦听器捕获调整大小并在此时重新创建画布和上下文。希望有帮助。