大网数据广告活动
Web 标准与前端开发

HTMLCanvasElement: webglcontextlost event

· 52CE 我爱测

如果浏览器检测到与 WebGLRenderingContext对象关联的图形缓冲区已丢失,则会触发WebGL API 中的 **`webglcontextlost`** 事件。

如果浏览器检测到与 WebGLRenderingContext对象关联的图形缓冲区已丢失,则会触发WebGL API 中的 webglcontextlost 事件。

<table class="properties"> <tbody> <tr> <th scope="row">冒泡</th> <td>Yes</td> </tr> <tr> <th scope="row">可取消</th> <td>Yes</td> </tr> <tr> <th scope="row">继承</th> <td>WebGLContextEvent</td> </tr> <tr> <th scope="row">事件处理程序属性</th> <td>none</td> </tr> </tbody> </table>

例子

在 WEBGL_lose_context 扩展的帮助下,你可以模拟 webglcontextlost 事件:

const canvas = document.getElementById("canvas");
const gl = canvas.getContext("webgl");

canvas.addEventListener("webglcontextlost", (event) => {
  console.log(event);
});

//WEBGL_lose_context 是 webgl 是属于 WebGLAPI 的一个扩展 API,它提供一组方法用来模拟一个 WebGLRenderingContext 上下文的丢失和恢复。
gl.getExtension("WEBGL_lose_context").loseContext();

// "webglcontextlost" event is logged.

规范

浏览器兼容性

请参阅

  • WebGLContextEvent
  • WebGLRenderingContext.isContextLost()
  • WEBGL_lose_context, WEBGL_lose_context.loseContext(), WEBGL_lose_context.restoreContext()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。