-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch1_GL_Attributes.html
102 lines (85 loc) · 2.22 KB
/
ch1_GL_Attributes.html
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>WebGL Beginner's Guide - Setting WebGL context attributes</title>
</head>
<body onLoad="initWebGL()">
<canvas id="canvas-element-id" width="800" height="600">
Your browser does not support the HTML5 canvas element.
</canvas>
<style type="text/css">
canvas {
border: 2px dotted blue;
}
</style>
<script>
var gl = null;
var c_width = 0;
var c_height = 0;
window.onkeydown = checkKey;
function checkKey(ev) {
switch (ev.keyCode) {
case 49: { // 1
gl.clearColor(0.3, 0.7, 0.2, 1.0);
clear(gl);
break;
}
case 50: { // 2
gl.clearColor(0.3, 0.2, 0.7, 1.0);
clear(gl);
break;
}
case 51: { // 3
var color = gl.getParameter(gl.COLOR_CLEAR_VALUE);
// Don't get confused with the following line. It
// basically rounds up the numbers to one decimalcipher
// just for visualization purposes
alert('clearColor = (' +
Math.round(color[0] * 10) / 10 +
',' + Math.round(color[1] * 10) / 10 +
',' + Math.round(color[2] * 10) / 10 + ')');
window.focus();
break;
}
}
}
function getGLContext() {
var canvas = document.getElementById("canvas-element-id");
if (canvas == null) {
alert("there is no canvas on this page");
return;
}
var names = [
"webgl",
"experimental-webgl",
"webkit-3d",
"moz-webgl"
];
var ctx = null;
for (var i = 0; i < names.length; ++i) {
try {
ctx = canvas.getContext(names[i]);
}
catch (e) { }
if (ctx) break;
}
if (ctx == null) {
alert("WebGL is not available");
}
else {
return ctx;
}
}
function clear(ctx) {
ctx.clear(ctx.COLOR_BUFFER_BIT);
ctx.viewport(0, 0, c_width, c_height);
}
function initWebGL() {
gl = getGLContext();
}
</script>
</body>
</html>