-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
352 lines (306 loc) · 13.1 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sylvester/0.1.3/sylvester.min.js"></script>
<script src="spriteAnimation.js"></script>
<script src="convolution.glsl.js"></script>
<link rel="stylesheet" type="text/css" href="sliderStyle.css">
<style>
body {
font-size: 14px;
}
select {
background: #696969;
color: #00FF00;
}
</style>
</head>
<body bgcolor="#000000">
<div id="content"></div>
<script type="text/javascript">
var FPS = 32;
var blur = 0.0;
class GL {
constructor(canvas){
var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
if (!gl) {
alert("Unable to initialize WebGL. Your browser may not support it.");
gl = null;
}
this.gl = gl;
this.initShaders(blurFragmentShaderSrc, diag5VertexShaderSrc);
// init texture coordinates
this.texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array([
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0]),
gl.STATIC_DRAW);
// create position buffer
this.positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
//create texture
this.texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this.texture);
//normalize image to powers of two
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
//set resolution
gl.uniform2f(this.resolutionLocation, canvas.width, canvas.height);
//set blur ammount
gl.uniform1f(this.widthLocation, 0.0);
}
initShaders(fShader, vShader) {
var gl = this.gl;
if(this.shaderProgram) {
gl.deleteProgram(shaderProgram);
}
var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fShader);
gl.compileShader(fragmentShader);
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vShader);
gl.compileShader(vertexShader);
var shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("An error occurred linking the shaders: " + gl.getProgramInfoLog(shaderProgram));
return;
}
gl.useProgram(shaderProgram);
this.shaderProgram = shaderProgram;
this.positionLocation = gl.getAttribLocation(shaderProgram, "position");
this.texCoordLocation = gl.getAttribLocation(shaderProgram, "texcoord");
this.resolutionLocation = gl.getUniformLocation(shaderProgram, "resolution");
this.widthLocation = gl.getUniformLocation(shaderProgram, "width");
}
render(bufferCanvas, x, y, blurWidth) {
var gl = this.gl;
gl.useProgram(this.shaderProgram);
//texture coordinates
gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer);
gl.enableVertexAttribArray(this.texCoordLocation);
gl.vertexAttribPointer(this.texCoordLocation, 2, gl.FLOAT, false, 8, 0);
//load buffer
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
gl.enableVertexAttribArray(this.positionLocation);
gl.vertexAttribPointer(this.positionLocation, 2, gl.FLOAT, false, 8, 0);
//draw size and position
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
x, y,
x + bufferCanvas.width, y,
x, y + bufferCanvas.height,
x, y + bufferCanvas.height,
x+ bufferCanvas.width, y,
x+ bufferCanvas.width, y + bufferCanvas.height]), gl.STATIC_DRAW);
//load texture from 2d canvas
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texImage2D(gl.TEXTURE_2D,
0,
gl.RGBA,
gl.RGBA,
gl.UNSIGNED_BYTE,
bufferCanvas);
//set resolution
gl.uniform2f(this.resolutionLocation, gl.canvas.width, gl.canvas.height);
//update blur ammount
gl.uniform1f(this.widthLocation, blurWidth);
//draw
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
clear() {
var gl = this.gl;
gl.clear(gl.COLOR_BUFFER_BIT);
}
}
const { Component } = React;
const { render } = ReactDOM;
class CanvasAnimation extends Component {
constructor(){
super();
this.state = {
data: [
{
"id": "fan",
"width": 360,
"height": 240,
"left": 0,
"top": 0,
"spriteSheetURL": "fan.jpg",
"rows": 6,
"columns": 5,
"totalFrames": 30
}
]
};
this.shaders = [
{name:"BLUR", shader:"blurFragmentShaderSrc_diag5VertexShaderSrc"},
{name:"RADIAL BLUR", shader:"radialblurFragmentShaderSrc_passthruVertexShaderSrc"},
{name:"DILATE", shader:"dilateFragmentShaderSrc_box8VertexShaderSrc"},
{name:"ERODE", shader:"erodeFragmentShaderSrc_box8VertexShaderSrc"},
{name:"MEDIAN", shader:"medianFragmentShaderSrc_medianVertexShaderSrc"},
{name:"SHARPEN", shader:"sharpenFragmentShaderSrc_diag5VertexShaderSrc"},
{name:"EMBOSS", shader:"embossFragmentShaderSrc_embossVertexShaderSrc"},
{name:"LAPLACE", shader:"laplaceFragmentShaderSrc_laplaceVertexShaderSrc"}
]
}
componentDidMount() {
function animation(json) {
Promise.all(json.map( (data) => {
return(
new CanvasSprite(
document.getElementById(data.id),
data.width,
data.height,
data.spriteSheetURL,
data.rows,
data.columns,
data.totalFrames)
)
})).then( (canvasSprites) => {
this.animation = canvasSprites;
this.animation.canvasGL = new GL(document.getElementById('main-canvas'));
this.loop(this);
})
};
animation.call(this, this.state.data);
}
loop(scope) {
var frame = 0;
var previousTime = performance.now();
var ellapsedTime;
var looper = function(time) {
ellapsedTime = time - previousTime;
previousTime = time;
frame += (ellapsedTime / (1000 / FPS) ) / ellapsedTime;
if(frame >= 1) {
frame = 0;
}
scope.animation[0].setFrame(frame);
scope.animation.canvasGL.render(document.getElementById(scope.state.data[0].id),
scope.state.data[0].left,
scope.state.data[0].top,
blur);
requestAnimationFrame(looper);
}
looper(performance.now());
}
shaderChanger(selectedIndex) {
this.animation.canvasGL.initShaders(
window[document.getElementById("menu").value.split("_")[0]],
window[document.getElementById("menu").value.split("_")[1]]);
}
render() {
var canvases = this.state.data.map( (data) => {
return (
React.createElement('canvas',
{id : data.id,
width : data.width,
height : data.height,
left: data.left,
top: data.top,
style: {display:'none'}})
)
});
var mainCanvas = React.createElement('canvas',
{id: 'main-canvas',
width : "360px",
height : "240px",
left: "0px",
top: "0px"});
var vertexMenu = React.createElement('select',
{id: "menu",
style: {width: "353px",
left: "10px",
top: "280px",
position: "absolute"},
onChange : (selectedIndex) =>
this.shaderChanger(selectedIndex)},
this.shaders.map( (shaders) => {
return (React.createElement('option',
{value: shaders.shader,
label: shaders.name}))}));
return(
React.createElement('div', null, mainCanvas,
...canvases,
vertexMenu));
}
}
class sliderReactBlur extends Component {
componentDidMount() {
document.getElementById('sliderBlur').value = 0.0;
}
sliderChanged(event) {
blur = event.target.value;
}
render() {
var caption = React.createElement('span',
{style: {left: "10px",
top: "317px",
color: "#00FF00",
position: "absolute"}},
'Convolution');
var slider = React.createElement('input',
{id: 'sliderBlur',
type: 'range',
min: 0.0,
max: 0.5,
step: 0.01,
style: {width: "270px",
left: "85px",
top: "320px",
position: "absolute"},
onChange : (event) => this.sliderChanged(event)});
return( React.createElement('div', null, caption, slider) );
}
}
class sliderReactFPS extends Component {
componentDidMount() {
document.getElementById('sliderFPS').value = 3;
}
sliderChanged(event) {
FPS = Math.pow(event.target.value, 2);
}
render() {
var caption = React.createElement('span',
{style: {left: "10px",
top: "347px",
color: "#00FF00",
position: "absolute"}},
'FPS');
var slider = React.createElement('input',
{id: 'sliderFPS',
type: 'range',
min: 1,
max: 6,
step: 0.1,
style: {width: "270px",
left: "85px",
top: "350px",
position: "absolute"},
onChange : (event) => this.sliderChanged(event)});
return( React.createElement('div', null, caption, slider) );
}
}
render(
React.createElement('div', null,
React.createElement(CanvasAnimation),
React.createElement(sliderReactBlur),
React.createElement(sliderReactFPS)),
document.getElementById('content')
);
</script>
</body>
</html>