WEBGL_lose_context

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

WEBGL_lose_context 是属于 WebGL API 的一个扩展 API,它提供一组方法用来模拟一个 WebGLRenderingContext 上下文的丢失和恢复。

WebGL 扩展可以通过 WebGLRenderingContext.getExtension() 方法来使用。更多信息可参阅 WebGL 教程 中的 使用 WebGL 扩展

备注: 该扩展在 WebGL1WebGL2 上下文中都是可用的。

方法

WEBGL_lose_context.loseContext()

模拟上下文丢失。

WEBGL_lose_context.restoreContext()

模拟上下文恢复。

示例

使用这个扩展,你可以模拟 webglcontextlostwebglcontextrestored 事件:

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

canvas.addEventListener(
  "webglcontextlost",
  function (e) {
    console.log(e);
  },
  false,
);

gl.getExtension("WEBGL_lose_context").loseContext();

// 打印了 "webglcontextlost" 类型的 WebGLContextEvent 事件。

规范

Specification
WebGL WEBGL_lose_context Khronos Ratified Extension Specification

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
WEBGL_lose_context
loseContext
restoreContext

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Requires a vendor prefix or different name for use.
Has more compatibility info.

参见