-
Notifications
You must be signed in to change notification settings - Fork 0
/
single.html
140 lines (117 loc) · 3.97 KB
/
single.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
<!DOCTYPE html>
<!-- saved from url=(0037)https://twgljs.org/examples/tiny.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--
@license twgl.js Copyright (c) 2015, Gregg Tavares All Rights Reserved.
Available via the MIT license.
see: http://github.com/greggman/twgl.js for details
-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta property="og:title" content="TWGL.js - tiny">
<meta property="og:type" content="website">
<meta property="og:image" content="http://twgljs.org/examples/screenshots/tiny.png">
<meta property="og:description" content="TWGL.js - tiny">
<meta property="og:url" content="http://twgljs.org">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@greggman">
<meta name="twitter:creator" content="@greggman">
<meta name="twitter:domain" content="twgljs.org">
<meta name="twitter:title" content="TWGL.js - tiny">
<meta name="twitter:url" content="http://twgljs.org/examples/tiny.html">
<meta name="twitter:description" content="TWGL.js - tiny">
<meta name="twitter:image:src" content="http://twgljs.org/examples/screenshots/tiny.png">
<link href="https://twgljs.org/resources/images/twgljs-icon.png" rel="shortcut icon" type="image/png">
<title>twgl.js - tiny</title>
<style>
body {
margin: 0;
font-family: monospace;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
#b {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 2;
}
</style>
</head>
<body>
<canvas id="c" width="1680" height="855"></canvas>
<div id="b"><a href="https://twgljs.org/">twgl.js</a> - tiny</div>
<script id="vs" type="notjs">
#version 300 es
// attribute vec4 position;
layout(location = 0) in vec4 position;
void main() {
gl_Position = position;
}
</script>
<script id="fs" type="notjs">
#version 300 es
precision mediump float;
uniform vec2 resolution;
uniform float time;
uniform float mx;
uniform float my;
out vec4 fragColor;
// TODO slow
float linmap(float a, float b, float c, float d, float x) {
return ((x - a) * ((d - c) / (b - a))) + c;
}
vec2 toUV(float x, float y) {
// Assumes horizontal?
float m = (resolution.x - resolution.y) / 2.0;
vec2 uv = vec2(linmap(resolution.x/2.0, resolution.x - m, 0.0, 1.0, x),
linmap(resolution.y/2.0, resolution.y , 0.0, 1.0, y));
return uv;
}
void main() {
vec2 uv = toUV(gl_FragCoord.x, gl_FragCoord.y);
vec2 mouse = toUV(mx, my);
SHAPE_ASDF
// Works
// float x = dFdx(topColor.r);
fragColor = topColor;
}
</script>
<script type="module">
let mx = 0;
let my = 0;
let canvas = document.getElementById('c');
export function myFunction(e) {
mx = e.clientX;
my = e.clientY;
// console.log(mx + " " + my);
}
canvas.addEventListener("mousemove", myFunction, false);
import * as twgl from './twgl-full.module.js';
const gl = document.querySelector("#c").getContext("webgl2");
const programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
const arrays = {
position: [-1, -1, 0, 1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 0],
};
const bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);
function render(time) {
twgl.resizeCanvasToDisplaySize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
const uniforms = {
time: time * 0.001,
resolution: [gl.canvas.width, gl.canvas.height],
mx: mx,
my: my,
};
gl.useProgram(programInfo.program);
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
twgl.setUniforms(programInfo, uniforms);
twgl.drawBufferInfo(gl, bufferInfo);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
</script>
</body></html>