-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
260 lines (226 loc) · 6.35 KB
/
script.js
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
260
document.body.onload = init;
var MAX_LINES = 500 * 4 * (7);
var vec2 = window.vec2;
var vec4 = window.vec4;
var canvas;
var boundingClientRect;
var gl;
var shaderProgram;
var dots = [];
var dotArrayBuffer;
var color = vec4.create();
var numDots = 4;
var dotSize = 5.0;
var mousePressed = false;
var touchIdentifier = null;
var mouseX = 0;
var mouseY = 0;
var oldMouseX = -1;
var oldMouseY = -1;
var target = vec2.create();
var resolutionMatrix = vec2.create();
var lines = [];
var vertices = new Float32Array(numDots * 4);
function init() {
canvas = document.getElementById('swarm-canvas');
gl = initWebGL(canvas);
if (!gl) {
return;
}
randomizeColor();
canvas.addEventListener('mousedown', startClick);
canvas.addEventListener('touchstart', startTouch);
canvas.addEventListener('mouseup', endClick);
canvas.addEventListener('touchend', endTouch);
canvas.addEventListener('mousemove', moveMouse);
canvas.addEventListener('touchmove', moveTouch);
initShaders();
initBuffers();
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
gl.lineWidth(dotSize);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
bufferDots();
requestAnimationFrame(tick);
}
function tick() {
drawScene();
requestAnimationFrame(tick);
}
function initWebGL(canvas) {
var options = {
alpha: false,
antialias: false,
depth: false,
preserveDrawingBuffer: true
};
gl = null;
gl = canvas.getContext('webgl', options) || canvas.getContext('experimental-webgl', options);
if (!gl) {
alert('Your browser does not support WebGL :(');
}
return gl;
}
function initShaders() {
var vertShader = getShader(gl, 'shader-vert');
var fragShader = getShader(gl, 'shader-frag');
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertShader);
gl.attachShader(shaderProgram, fragShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert('Unable to initialize shaders!');
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, 'a_Position');
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, 'a_Color');
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
}
function initBuffers() {
dotArrayBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, dotArrayBuffer);
}
function bufferDots() {
vertices = Float32Array.from(lines);
gl.bindBuffer(gl.ARRAY_BUFFER, dotArrayBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.DYNAMIC_DRAW);
}
function drawScene() {
bufferDots();
if(lines.length) {
gl.clear(gl.COLOR_BUFFER_BIT);
gl.bindBuffer(gl.ARRAY_BUFFER, dotArrayBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, 2, gl.FLOAT, false, 20, 0);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, 3, gl.FLOAT, false, 20, 8);
gl.drawArrays(gl.LINES, 0, lines.length / 5);
}
}
function randomizeColor() {
var r = Math.random() * .9 + .1;
var g = Math.random() * .9 + .1;
var b = Math.random() * .9 + .1;
vec4.set(color, r, g, b, 1.0);
}
function addLines(startX, startY, endX, endY) {
startX = startX / canvas.width;
startY = startY / canvas.height;
endX = endX / canvas.width;
endY = endY / canvas.height;
lines.push(startX, startY,
color[0], color[1], color[2],
endX, endY,
color[0], color[1], color[2],
1-startX, startY,
color[0], color[1], color[2],
1-endX, endY,
color[0], color[1], color[2],
1-startX, 1-startY,
color[0], color[1], color[2],
1-endX, 1-endY,
color[0], color[1], color[2],
startX, 1-startY,
color[0], color[1], color[2],
endX, 1-endY,
color[0], color[1], color[2]
);
if(lines.length > MAX_LINES) {
lines.splice(0, lines.length - MAX_LINES);
}
}
function getShader(gl, scriptId) {
var scriptElement = document.getElementById(scriptId);
if (!scriptElement) {
return null;
}
var script = '';
var node = scriptElement.firstChild;
while (node) {
if (node.nodeType == Node.TEXT_NODE) {
script += node.textContent;
}
node = node.nextSibling;
}
var shader;
if (scriptElement.type === 'x-shader/x-fragment') {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (scriptElement.type === 'x-shader/x-vertex') {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, script);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function resizeCanvas() {
var width = canvas.clientWidth;
var height = canvas.clientHeight;
if (canvas.width != width || canvas.height != height) {
canvas.width = width;
canvas.height = height;
}
gl.viewport(0, 0, canvas.width, canvas.height);
vec2.set(resolutionMatrix, canvas.width, canvas.height);
boundingClientRect = canvas.getBoundingClientRect();
gl.clear(gl.COLOR_BUFFER_BIT);
}
function startClick(e) {
e.preventDefault();
mousePressed = true;
mouseX = e.clientX;
mouseY = e.clientY;
randomizeColor();
}
function startTouch(e) {
e.preventDefault();
if (!touchIdentifier) {
randomizeColor();
touchIdentifier = e.touches[0].identifier;
mousePressed = true;
mouseX = e.touches[0].pageX;
mouseY = e.touches[0].pageY;
}
}
function moveMouse(e) {
e.preventDefault();
moveTarget(e.clientX, e.clientY);
}
function moveTouch(e) {
e.preventDefault();
moveTarget(e.touches[0].pageX, e.touches[0].pageY);
}
function endClick(e) {
e.preventDefault();
mousePressed = false;
oldMouseX = -1;
oldMouseY = -1;
}
function endTouch(e) {
e.preventDefault();
for (var i = 0, len = e.touches.length; i < len; i++) {
if (e.touches[i].identifier === touchIdentifier) {
// The original touch is still active.
return;
}
}
// The original touch was not found.
mousePressed = false;
touchIdentifier = null;
oldMouseX = -1;
oldMouseY = -1;
}
function moveTarget(x, y) {
if(mousePressed) {
oldMouseX = mouseX;
oldMouseY = mouseY;
mouseX = x;
mouseY = y;
addLines(oldMouseX, oldMouseY, mouseX, mouseY);
}
}