/
shapes.js
executable file
·323 lines (267 loc) · 7.87 KB
/
shapes.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
function main(){
// 1. Retrieve canvas
// 2. Get context
// 3. Define and Init shaders
// 4. Set color for clearing - Tool for converting hex to pd color - http://sherryumlah.com/git/pdcolors.htm
// 5. Clear canvas
// 6. Draw
var vec1 = 0.0;
var vec2 = 0.0;
var vec3 = 0.0;
var vec4 = 1.0;
var R = 0.0;
var G = 0.0;
var B = 0.0;
var A = 1.0;
var ptSize = '10.0';
var currentShape = 'point';
var animate = false;
var animation;
var rotation = 90.0;
// GET CANVAS
var canvas = document.getElementById('webgl');
// GET CONTEXT
var gl = getWebGLContext(canvas);
if (!gl){
console.log('Failed to render context');
return;
}
var FSHADER_SOURCE =
'void main(){\n'+
'gl_FragColor = vec4(' + R +',' + G + ',' + B + ',' + A + ');\n' +
'}\n';
var VSHADER_SOURCE =
'void main(){\n' +
'gl_Position=vec4(' + vec1 +',' + vec2 + ',' + vec3 + ',' + vec4 + ');\n' +
'gl_PointSize = ' + ptSize +';\n' +
'}\n';
// Translation distance for x, y, z
var Tx = 0.0, Ty = 0.0, Tz = 0.0;
function set_FSHADER_SOURCE(R, G, B, A){
FSHADER_SOURCE =
'void main(){\n'+
'gl_FragColor = vec4(' + R +',' + G + ',' + B + ',' + A + ');\n' +
'}\n';
}
function set_VSHADER_SOURCE(vec1, vec2, vec3, vec4, ptSize){
VSHADER_SOURCE =
'void main(){\n' +
'gl_Position=vec4(' + vec1 +',' + vec2 + ',' + vec3 + ',' + vec4 + ');\n' +
'gl_PointSize = ' + ptSize +';\n' +
'}\n';
}
function set_Tri_VSHADER_SOURCE(){
VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'uniform vec4 u_Translation;\n'+
'void main() {\n' +
'gl_Position = a_Position + u_Translation;\n' +
'}\n';
}
function shaders(VSHADER_SOURCE, FSHADER_SOURCE){
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}
}
function initVertexBuffers(gl){
var p1x = $('#p1x').val();
var p1y = $('#p1y').val();
var p2x = $('#p2x').val();
var p2y = $('#p2y').val();
var p3x = $('#p3x').val();
var p3y = $('#p3y').val();
var vertices = new Float32Array([
p1x, p1y, p2x, p2y, p3x, p3y
]);
var n=3;
// Create a buffer object
var vertexBuffer = gl.createBuffer();
if (!vertexBuffer) {
console.log('Failed to create the buffer object');
return -1;
}
// Bind the buffer object to target
// target tells WebGL what type of data buffer contains
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// Write date into the buffer object
// allocates storage and writes data to buffer
// writes vertices data into gl.ARRAY_BUFFER
// gl.bufferData (target, data, usage)
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < 0){
console.log('Failed to get the storage location of a_Position');
return -1;
}
// Assign the buffer object to a_Position variable
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
// Enable the assignment to a_Position variable
gl.enableVertexAttribArray(a_Position);
return n;
}
function clearCanvas(){
// Specify the color for clearing <canvas>
gl.clearColor(1.0, 1.0, 1.0, 1);
// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);
}
function createTriangle(R,G,B,A){
set_FSHADER_SOURCE(R,G,B,A);
set_Tri_VSHADER_SOURCE();
// Initialize shaders
shaders(VSHADER_SOURCE, FSHADER_SOURCE);
// Write the positions of vertices to a vertex shader
var n = initVertexBuffers(gl);
if (n < 0) {
console.log('Failed to set the positions of the vertices');
return;
}
// Pass the translation distance to the vertex shader
var u_Translation = gl.getUniformLocation(gl.program, 'u_Translation');
if (!u_Translation) {
console.log('Failed to get the storage location of u_Translation');
return;
}
gl.uniform4f(u_Translation, Tx, Ty, Tz, 0.0);
gl.drawArrays(gl.TRIANGLES, 0, n);
}
function drawTriangle(){}
function createPoint(vec1, vec2, vec3, vec4, R, G, B, A, ptSize){
/* DEFINE SHADERS */
// vertex shader
// gl_Position: type vec4 (vector of 4 floating point numbers, XYZW converted using vec4())
// specifies position of a vertex. 1.0 as W makes 4D objects 3D by dividing all by 1 x/w, y/w, z/w
// gl_PointSize: type float (0.0) specifies the size of a point (in pixels) defaults to 1.0
// Fragment shader program
set_FSHADER_SOURCE(R, G, B, A);
set_VSHADER_SOURCE(vec1, vec2, vec3, vec4, ptSize);
// Initialize shaders
shaders(VSHADER_SOURCE, FSHADER_SOURCE);
clearCanvas();
// Draw - (mode, first, count)
gl.drawArrays(gl.POINTS, 0, 1);
}
function clearAndDraw(R, G, B, A, ptSize){
if (R==''){var R='1.0'};
if (G==''){var G='1.0'};
if (B==''){var B='1.0'};
if (A==''){var A='1.0'};
createPoint(vec1, vec2, vec3, vec4, R, G, B, A, ptSize);
}
function stopAnimation(){
animate = false;
clearInterval(animation);
$('.animateTri').text("Animate Triangle");
}
function drawPoint(R, G, B, A){
if (R=='0'){
R='0.0';
}
if (G=='0'){
G='0.0';
}
if (B=='0'){
B='0.0';
}
currentShape="point";
ptSize = ($('#ptSize').val());
ptSize = parseFloat(ptSize);
ptSize = ptSize.toFixed(2);
shaders(VSHADER_SOURCE, FSHADER_SOURCE);
clearAndDraw(R, G, B, A, ptSize);
}
function animateTri(){
var min = -0.25;
var max = 0.25;
var randomX = Math.random() * (max - min + 0.25) + min;
var randomY = Math.random() * (max - min + 0.25) + min;
Tx = randomX;
Ty = randomY;
$('.movex').val(Tx);
$('.movey').val(Ty);
currentShape="triangle";
createTriangle(R,G,B,A);
};
// EVENT HANDLERS
$('.tripoint').on("change", function(e){
e.preventDefault();
currentShape="triangle";
createTriangle(R,G,B,A);
});
$('.create').on("click", function(e){
e.preventDefault();
stopAnimation();
currentShape="triangle";
createTriangle(R,G,B,A);
});
$('.point').on("click", function(e){
e.preventDefault();
stopAnimation();
drawPoint(R, G, B, A);
})
/* CHANGE COLORS */
$("#picked").on("change", function(e){
R=($('#picked').val());
G=($('#picked').val());
B=($('#picked').val());
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7) : h}
function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
R=hexToR(R)/255;
G=hexToG(G)/255;
B=hexToB(B)/255;
$('#R').val(R);
$('#G').val(G);
$('#B').val(B);
set_FSHADER_SOURCE(R, G, B, A);
if (currentShape=="point"){
shaders(VSHADER_SOURCE, FSHADER_SOURCE);
clearAndDraw(R, G, B, A, ptSize);
} else if (currentShape=="triangle"){
set_Tri_VSHADER_SOURCE();
shaders(VSHADER_SOURCE, FSHADER_SOURCE);
createTriangle(R,G,B,A);
}
});
$("#ptSize").on("change", function(e){
e.preventDefault();
drawPoint(R, G, B, A);
});
$(".animateTri").on("click", function(e){
e.preventDefault();
if (animate == false){
animate = true;
$('.animateTri').text("Stop Animation");
animation = setInterval(function(){animateTri()}, 250);
} else {
stopAnimation();
}
});
$(".move").on("change", function(){
var moveX = $(".movex").val();
var moveY = $(".movey").val();
Ty = parseFloat(moveY);
Tx = parseFloat(moveX);
if (Ty < -0.5){
Ty = -0.5;
$(".movey").val(Ty);
}
if (Ty > 0.5){
Ty = 0.5;
$(".movey").val(Ty);
}
if (Tx < -0.5){
Tx = -0.5;
$(".movex").val(Tx);
}
if (Tx > 0.5){
Tx = 0.5;
$(".movex").val(Tx);
}
currentShape="triangle";
createTriangle(R,G,B,A);
});
createTriangle('0.0','0.0','0.0','1.0');
}