This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
106 lines (96 loc) · 2.58 KB
/
index.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
103
104
105
106
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL</title>
<style>
body { margin: 0; }
canvas { width: 100vw; height: 100vh; display: block; }
</style>
<script src="utils.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
var gl;
var program;
var buf;
var then;
// each of these functions calls the next
// when it's done, so we only need to call start()
function start() {
var canvas = document.getElementById("c");
gl = canvas.getContext("webgl");
if(!gl) {
console.log("No WebGL.");
return;
}
loadShaders();
}
function loadShaders() {
var count = 2; // how many shaders still to load?
var vs;
var fs;
function callback(sh, shader) {
if(sh == "vs") {
vs = shader;
} else {
fs = shader;
}
count -= 1;
if(count == 0) { // done loading
program = createProgram(gl, vs, fs);
gl.useProgram(program);
setupBuffer();
}
}
loadShader(gl, gl.VERTEX_SHADER, "basic.vert", (shader) => { callback("vs", shader); });
loadShader(gl, gl.FRAGMENT_SHADER, "basic.frag", (shader) => { callback("fs", shader); });
}
function setupBuffer() {
buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf);
var positions = [-0.3, -0.3,
0.3, -0.3,
-0.3, 0.3,
0.3, 0.3];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
setupVertexAttributes();
}
function setupVertexAttributes() {
var location = gl.getAttribLocation(program, "position");
gl.enableVertexAttribArray(location);
gl.vertexAttribPointer(location,
2, // size
gl.FLOAT, // type
false, // normalize?
0, // stride
0); // offset
remainder();
}
function remainder() {
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
then = window.performance.now();
requestAnimationFrame(loop); // start loop
}
// main loop
function loop(now) {
var dt = now - then; // timestep is available here (for demonstration purposes; it's unused)
then = now;
resize(gl);
gl.clearColor(0.392 + 0.4*Math.sin(0.0004 * now), 0.584 + 0.3*Math.cos(0.00051*now), 0.829 + 0.15*Math.sin(0.00002*now), 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLE_STRIP,
0, // offset
4); // count
//console.log(dt);
requestAnimationFrame(loop);
}
start();
});
</script>
<canvas id="c">
Canvas!
</canvas>
</body>
</html>