-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
78 lines (69 loc) · 2.07 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
<html lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width">
<title>WebGL</title>
<style>
html, body {
margin: 0;
padding: 0;
overflow: hidden;
width: 100%;
height: 100%;
}
canvas {
width: 100%;
height: 100%;
display: block;
}
</style>
</head>
<body>
<script type="text/javascript" src="https://rawgit.com/patriciogonzalezvivo/glslCanvas/master/dist/GlslCanvas.js"></script>
<canvas id="canvas"></canvas>
<script>
const canvas = document.querySelector('#canvas');
const sandbox = new GlslCanvas(canvas);
const dpr = window.devicePixelRatio || 1;
function resizeCanvas() {
canvas.width = window.innerWidth * dpr;
canvas.height = window.innerHeight * dpr;
sandbox.setUniform('u_resolution', canvas.width, canvas.height);
}
document.addEventListener('DOMContentLoaded', () => {
fetch(`/14/shader.frag`)
.then(res => res.text())
.then(fragmentText => {
sandbox.load(fragmentText);
});
});
function handleMove(event) {
let x, y;
const rect = canvas.getBoundingClientRect();
if(event.touches) {
const touch = event.touches[0];
x = (touch.clientX - rect.left) * dpr;
y = (touch.clientY - rect.top) * dpr;
} else {
x = (event.clientX - rect.left) * dpr;
y = (event.clientY - rect.top) * dpr;
}
sandbox.setUniform('u_pointer', x, y);
}
function fullscreen(event) {
const d = document.documentElement;
if (d.requestFullscreen) {
d.requestFullscreen();
} else if (d.webkitRequestFullScreen) {
d.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
}
};
document.body.addEventListener("dblclick", fullscreen);
canvas.addEventListener('mousemove', handleMove);
canvas.addEventListener('touchstart', handleMove);
canvas.addEventListener('touchmove', handleMove);
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
</script>
</body>
</html>