WEBGL_lose_context 擴充套件

Baseline 已廣泛支援

此功能已成熟,可跨多種裝置和瀏覽器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有瀏覽器中可用。

WEBGL_lose_context 擴充套件是 WebGL API 的一部分,它公開了用於模擬丟失和恢復 WebGLRenderingContext 的函式。

可以使用 WebGLRenderingContext.getExtension() 方法來訪問 WebGL 擴充套件。有關更多資訊,請參閱 WebGL 教程中的 使用擴充套件

注意: 此擴充套件同時適用於 WebGL1WebGL2 上下文。

例項方法

WEBGL_lose_context.loseContext()

模擬丟失上下文。

WEBGL_lose_context.restoreContext()

模擬恢復上下文。

示例

使用此擴充套件,您可以模擬 webglcontextlostwebglcontextrestored 事件

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

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

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

// WebGLContextEvent event with type "webglcontextlost" is logged.

規範

規範
WebGL WEBGL_lose_context Khronos 批准的擴充套件規範

瀏覽器相容性

另見