-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.html
90 lines (76 loc) · 2.52 KB
/
4.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
}
</style>
</head>
<body>
<script type="shader-source" id="vertexShader">
precision mediump float;
attribute vec2 a_Position;
attribute vec2 a_Canvas_Size;
void main(){
vec2 position = (a_Position / a_Canvas_Size) * 2.0 - 1.0;
position = position * vec2(1.0,-1.0);
gl_Position = vec4(position,0,1);
}
</script>
<script type="shader-source" id="fragmentShader">
precision mediump float;
uniform vec4 u_Color;
void main(){
vec4 color = u_Color/ vec4(255,255,255,1);
gl_FragColor = color;
}
</script>
<script src="../util.js"></script>
<script>
const canvas = document.createElement('canvas')
const wWidth = window.innerWidth;
const wHeight = window.innerHeight;
canvas.width = wWidth;
canvas.height = wHeight;
document.body.appendChild(canvas)
const gl = canvas.getContext('webgl');
const program = createSimpleProgram(gl);
gl.useProgram(program)
//设置清空画布颜色为黑色。
gl.clearColor(0.0, 0.0, 0.0, 1.0);
//用上一步设置的清空画布颜色清空画布。
gl.clear(gl.COLOR_BUFFER_BIT);
const u_Color = gl.getUniformLocation(program, 'u_Color')
const a_Position = gl.getAttribLocation(program, 'a_Position')
const a_Canvas_Size = gl.getAttribLocation(program, 'a_Canvas_Size')
gl.vertexAttrib2f(program, wWidth, wHeight);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
gl.enableVertexAttribArray(a_Position)
const size = 2;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.vertexAttribPointer(a_Position, size, type, normalize, stride, offset)
const positionData = [0, 0];
canvas.onclick = function (e) {
const color = randomColor();
gl.uniform4f(u_Color, color.r, color.g, color.b, color.a);
positionData.push(e.x)
positionData.push(e.y)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positionData), gl.STATIC_DRAW)
//设置清空画布颜色为黑色。
gl.clearColor(0.0, 0.0, 0.0, 1.0);
//用上一步设置的清空画布颜色清空画布。
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.LINE_STRIP, 0, positionData.length / 2)
}
</script>
</body>
</html>