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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
---
title: 检测WebGL
slug: Web/API/WebGL_API/By_example/Detect_WebGL
translation_of: Web/API/WebGL_API/By_example/Detect_WebGL
---
<div>{{IncludeSubnav("/en-US/Learn")}}</div>
<p>{{PreviousNext("Learn/WebGL/By_example","Learn/WebGL/By_example/Clearing_with_colors")}}</p>
<div id="detect-webgl">
<div class="summary">
<p>这个例子演示了如何通过渲染上下文来检测{{Glossary("WebGL")}},并将结果报告给用户。</p>
</div>
<p>{{EmbedLiveSample("detect-webgl-source",660,150)}}</p>
<div id="detect-webgl-intro">
<h3 id="WebGL特性检测">WebGL特性检测</h3>
<p>在第一个例子中,我们将检查浏览器是否支持{{Glossary("WebGL")}}。为此,我们将尝试从{{domxref("HTMLCanvasElement","canvas")}}元素获取{{domxref("WebGLRenderingContext","WebGL渲染的上下文","",1)}} 。{{domxref("WebGLRenderingContext","WebGL渲染的上下文", "", 1)}}是一个接口,通过它你可以设置和查询绘图器的状态,发送数据到WebGL,执行绘制命令。</p>
<p>在单个上下文接口中保存绘图器的状态并不是{{Glossary("WebGL")}}独有的。这在其他绘图技术里也是存在的{{Glossary("API")}},比如{{domxref("CanvasRenderingContext2D","2D渲染上下文的canvas", "", 1)}}。然而,您可以调整的属性和变量对于每个{{Glossary("API")}}来说都是不同的。</p>
</div>
<div id="detect-webgl-source">
<pre class="brush: html"><p>[ Here would go the result of WebGL feature detection ]</p>
<button>Press here to detect WebGLRenderingContext</button>
</pre>
<pre class="brush: css">body {
text-align : center;
}
button {
display : block;
font-size : inherit;
margin : auto;
padding : 0.6em;
}
</pre>
<pre class="brush: js">// Run everything inside window load event handler, to make sure
// DOM is fully loaded and styled before trying to manipulate it.
window.addEventListener("load", function() {
var paragraph = document.querySelector("p"),
button = document.querySelector("button");
// Adding click event handler to button.
button.addEventListener("click", detectWebGLContext, false);
function detectWebGLContext () {
// Create canvas element. The canvas is not added to the
// document itself, so it is never displayed in the
// browser window.
var canvas = document.createElement("canvas");
// Get WebGLRenderingContext from canvas element.
var gl = canvas.getContext("webgl")
|| canvas.getContext("experimental-webgl");
// Report the result.
if (gl && gl instanceof WebGLRenderingContext) {
paragraph.innerHTML =
"Congratulations! Your browser supports WebGL.";
} else {
paragraph.innerHTML = "Failed to get WebGL context. "
+ "Your browser or device may not support WebGL.";
}
}
}, false);
</pre>
<p>这个例子的源代码可以在<a href="https://github.com/idofilin/webgl-by-example/tree/master/detect-webgl">GitHub</a>上获取。</p>
</div>
</div>
<p>{{PreviousNext("Learn/WebGL/By_example","Learn/WebGL/By_example/Clearing_with_colors")}}</p>
|