-
Notifications
You must be signed in to change notification settings - Fork 8k
/
index.md
50 lines (33 loc) · 1.36 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
---
title: WEBGL_lose_context.loseContext()
slug: Web/API/WEBGL_lose_context/loseContext
---
{{APIRef("WebGL")}}
**WEBGL_lose_context.loseContext()**属于 [WebGL API](/zh-CN/docs/Web/API/WebGL_API),一般用来模拟 {{domxref("WebGLRenderingContext")}} 的上下文丢失。
这个方法会触发 WebGL 规范中上下文丢失的相关事件。通过这个方法丢失的上下文可以通过 {{domxref("WEBGL_lose_context.restoreContext()")}} 恢复。
## 语法
```plain
gl.getExtension('WEBGL_lose_context').loseContext();
```
## 示例
你可以用这个方法模拟 [`webglcontextlost`](/zh-CN/docs/Web/API/HTMLCanvasElement/webglcontextlost_event) 事件:
```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 事件被触发。
```
## 规范
{{Specifications}}
## 浏览器兼容性
{{Compat}}
## 相关链接
- {{domxref("WebGLRenderingContext.isContextLost()")}}
- 事件: [`webglcontextlost`](/zh-CN/docs/Web/API/HTMLCanvasElement/webglcontextlost_event), [`webglcontextrestored`](/zh-CN/docs/Web/API/HTMLCanvasElement/webglcontextrestored_event), [`webglcontextcreationerror`](/zh-CN/docs/Web/API/HTMLCanvasElement/webglcontextcreationerror_event)