-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollingTests.html
259 lines (233 loc) · 8.19 KB
/
ScrollingTests.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html onpointermove="onpointermovehandler(event);">
<head>
<style>
body {
background-image:linear-gradient(0deg, transparent 50%, #aaa 50%),
linear-gradient(90deg, #aaa 50%, #ccc 50%);
background-size:45px 45px,45px 45px;
}
#expander {
width: 100px;
height: 17px;
border-top: 1px solid red;
-webkit-animation: expandline 2s infinite;
}
#rotator {
width: 100px;
height: 100px;
border-top: 1px solid red;
-webkit-animation: rotation 4s infinite linear;
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(359deg);
}
}
@-webkit-keyframes expandline{
0% { width: 25%; }
50% { width: 75%; }
100% { width: 25%; }
}
canvas {
margin: auto;
}
</style>
<script>
var hangTime = 1234;
var hangInterval = 500;
function onpointermovehandler(event) {
var pageX = Math.round(event.pageX);
var pageY = Math.round(event.pageY);
document.getElementById("x").innerHTML = pageX;
document.getElementById("y").innerHTML = pageY;
}
function onscrollhandler(event) {
document.getElementById("scrolly").innerHTML = window.scrollY;
document.getElementById("scrollx").innerHTML = window.scrollX;
}
var rotate_deg = 0;
function rotateImage() {
var r = document.getElementById('rotatorJS');
r.style.transform = 'rotate('+((rotate_deg++)%360)+'deg)';
window.requestAnimationFrame(rotateImage);
}
function fireJSAnimations() {
window.requestAnimationFrame(rotateImage);
window.requestAnimationFrame(calculateFPS);
main();
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame =
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame;
}
var t = [];
function calculateFPS(now) {
t.unshift(now);
if (t.length > 10) {
var t0 = t.pop();
var fps = Math.floor(1000 * 10 / (now - t0));
document.getElementById('fps').innerHTML = fps;
}
window.requestAnimationFrame(calculateFPS);
};
function hang() {
var now = (new Date()).getTime();
for (;;) {
if((new Date()).getTime() - now > hangTime) {
break;
}
}
}
// Hang for "hangTime" after every "hangInterval"
var g_intervalHandle;
function stopHangs() {
clearInterval(g_intervalHandle);
}
function startIntermittentHangs() {
stopHangs();
g_intervalHandle = setInterval(hang, hangInterval);
}
function togglefpscounter() {
var fps_div = document.getElementById("fpscounter");
fps_div.style.display = fps_div.style.display === "none" ? "block" : "none";
}
function toggleanimations() {
var animations_div = document.getElementById("animations");
if (animations_div.style.display === "none") {
animations_div.style.display = "block";
} else {
animations_div.style.display = "none";
}
}
</script>
<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
<script src="https://webglfundamentals.org/webgl/resources/webgl-lessons-helper.js"></script>
<script src="https://webglfundamentals.org/webgl/lessons/resources/3d-math.js"></script>
<!-- vertex shader -->
<script id="2d-vertex-shader" type="x-shader/x-vertex">
attribute vec4 a_position;
uniform mat4 u_worldViewProjection;
void main() {
gl_Position = u_worldViewProjection * a_position;
}
</script>
<!-- fragment shader -->
<script id="2d-fragment-shader" type="x-shader/x-fragment">
void main() {
gl_FragColor = vec4(0,0,0,1);
}
</script>
<script>
"use strict";
var m = ThreeDMath;
function main() {
var cubeVertices = [
-1, -1, -1,
1, -1, -1,
1, 1, -1,
-1, 1, -1,
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1,
];
var indices = [
0, 1,
1, 2,
2, 3,
3, 0,
4, 5,
5, 6,
6, 7,
7, 4,
0, 4,
1, 5,
2, 6,
3, 7,
];
var canvas = document.getElementById("c");
var gl = canvas.getContext("webgl");
if (!gl) {
alert("no webgl");
return;
}
var program = webglUtils.createProgramFromScripts(
gl, ["2d-vertex-shader", "2d-fragment-shader"]);
gl.useProgram(program);
var positionLoc = gl.getAttribLocation(program, "a_position");
var worldViewProjectionLoc =
gl.getUniformLocation(program, "u_worldViewProjection");
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(cubeVertices),
gl.STATIC_DRAW);
gl.enableVertexAttribArray(positionLoc);
gl.vertexAttribPointer(positionLoc, 3, gl.FLOAT, false, 0, 0);
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
gl.bufferData(
gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(indices),
gl.STATIC_DRAW);
function render(clock) {
clock *= 0.001;
var scale = 4;
webglUtils.resizeCanvasToDisplaySize(gl.canvas, window.devicePixelRatio);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT);
var fieldOfView = Math.PI * 0.25;
var aspect = canvas.clientWidth / canvas.clientHeight;
var projection = m.perspective(fieldOfView, aspect, 0.0001, 500);
var radius = 5;
var eye = [
Math.sin(clock) * radius,
1,
Math.cos(clock) * radius,
];
var target = [0, 0, 0];
var up = [0, 1, 0];
var view = m.lookAt(eye, target, up);
var worldViewProjection = m.multiplyMatrix(view, projection);
gl.uniformMatrix4fv(worldViewProjectionLoc, false, worldViewProjection);
gl.drawElements(gl.LINES, indices.length, gl.UNSIGNED_SHORT, 0);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
</script>
</head>
<body onscroll="onscrollhandler(event);" onload="fireJSAnimations();">
<div style="position: fixed; left:0px; right:0px; padding: 10px;">
<button onclick="startIntermittentHangs()">Induce main thread jank.</button>
<button onclick="stopHangs()">Stop jank.</button><br />
<button onclick="toggleanimations()">Toggle animations visibility.</button>
<button onclick="togglefpscounter()">Toggle FPS counter.</button><br /><br />
<div style="width:300px; height:100px;">
Pointer co-ordinates: (<span id="x">0</span>, <span id="y">0</span>)<br />
ScrollTop: <span id="scrolly">0</span><br />
ScrollLeft: <span id="scrollx">0</span><br />
<span id="fpscounter">FPS: <span id="fps">0</span></span><br />
</div>
<hr style="height:5px; background-color:#000" />
<div id="animations">
CSS (Main thread) animation.
<div id="expander" style="height:100px;width:150px;border: 5px solid;"></div><br /><br />
CSS (CC thread) animation.
<div id="rotator" style="height:100px;width:100px;border: 5px solid;"></div><br /><br />
JS (Main thread) animation.
<div id="rotatorJS" style="height:100px;width:100px;border: 5px solid;" onclick="rotateImage()"></div><br /><br />
WebGL 2d canvas.<br />
<canvas id="c" style="height:200px;width:200px;border: 5px solid;"></canvas>
</div>
</div>
<div style="width:500vw; height:100px"></div>
<div style="width:100px; height:500vh"></div>
</body>
</html>