forked from WICG/html-in-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebGL.html
More file actions
193 lines (167 loc) · 5.61 KB
/
webGL.html
File metadata and controls
193 lines (167 loc) · 5.61 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>WebGL DrawElement Demo</title>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.8.1/gl-matrix-min.js"
integrity="sha512-zhHQR0/H5SEBL3Wn6yYSaTTZej12z0hVZKOv3TwCUXT1z5qeqGcXJLLrbERYRScEDDpYIJhPC1fk31gqR783iQ=="
crossorigin="anonymous"
defer>
</script>
<script src="webGLSetup.js"></script>
<style>
canvas {
border: 1px solid blue;
}
#drawElementContents {
transformf: translateY(100px) translateX(50px) rotateZ(45deg);
transform-originf: center; width: 300px;
border: 1px solid black;
}
img {
width: 40px;
height: auto;
}
</style>
</head>
<body>
<canvas id="gl-canvas" width="600" height="600" layoutsubtree=true>
<div id="drawElement" style="width: 300px; height: 300px;" id="d">
Hello world!<br>I'm multi-line, <b>formatted</b>,
rotated text with emoji (😀), RTL text
<span dir=rtl>من فارسی صحبت میکنم</span>,
vertical text,
<p style="writing-mode: vertical-rl;">
这是垂直文本
</p>
and an inline image:
<img src="https://upload.wikimedia.org/wikipedia/commons/9/9b/Gustav_chocolate.jpg">
</div>
</canvas>
<br>
<button type="button" onclick="toggleAnimation()">Stop Animation</button>
</body>
<script>
let cubeRotation = 0.0;
let currentTime = 0;
let deltaTime = 0;
let animating = true;
let render_context = null;
function toggleAnimation() {
if (animating) {
animating = false;
} else {
animating = true;
currentTime = performance.now() * 0.001
requestAnimationFrame(render);
}
}
//
// Initialize a texture and load an image.
// When the image finished loading copy it into the texture.
//
function loadTexture(gl) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
const level = 0;
const internalFormat = gl.RGBA;
const srcFormat = gl.RGBA;
const srcType = gl.UNSIGNED_BYTE;
gl.texElement2D(gl.TEXTURE_2D, level, internalFormat,
srcFormat, srcType, drawElement);
// Linear texture filtering produces better results than mipmap with text.
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
return texture;
}
// Draw the scene repeatedly
function render() {
if (animating) {
let new_time = performance.now() * 0.001; // convert to seconds
deltaTime = new_time - currentTime;
currentTime = new_time;
}
if (render_context === null) {
return;
}
drawScene(render_context.gl,
render_context.program,
render_context.buffers,
render_context.texture,
cubeRotation);
if (animating) {
cubeRotation += deltaTime;
requestAnimationFrame(render);
}
}
function main() {
const canvas = document.querySelector("#gl-canvas");
// Initialize the GL context
const gl = canvas.getContext("webgl2");
// Only continue if WebGL is available and working
if (gl === null) {
alert(
"Unable to initialize WebGL. Your browser or machine may not support it.",
);
return;
}
// Vertex shader program
const vsSource = `
attribute vec4 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
varying highp vec2 vTextureCoord;
void main(void) {
gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
vTextureCoord = aTextureCoord;
}
`;
// Fragment shader program
const fsSource = `
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTextureCoord);
}
`;
// Initialize a shader program; this is where all the lighting
// for the vertices and so forth is established.
const shaderProgram = initShaderProgram(gl, vsSource, fsSource);
// Collect all the info needed to use the shader program.
// Look up which attribute our shader program is using
// for aVertexPosition and look up uniform locations.
const programInfo = {
program: shaderProgram,
attribLocations: {
vertexPosition: gl.getAttribLocation(shaderProgram, "aVertexPosition"),
textureCoord: gl.getAttribLocation(shaderProgram, "aTextureCoord"),
},
uniformLocations: {
projectionMatrix: gl.getUniformLocation(shaderProgram, "uProjectionMatrix"),
modelViewMatrix: gl.getUniformLocation(shaderProgram, "uModelViewMatrix"),
uSampler: gl.getUniformLocation(shaderProgram, "uSampler"),
},
};
const buffers = initBuffers(gl);
// Load texture
const texture = loadTexture(gl);
// Flip image pixels into the bottom-to-top order that WebGL expects.
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
render_context = {
gl: gl,
program: programInfo,
buffers: buffers,
texture:texture,
};
if (animating) {
requestAnimationFrame(render);
} else {
render();
}
}
onload = () => main();
</script>
</html>