-
Notifications
You must be signed in to change notification settings - Fork 1
/
effects.js
344 lines (285 loc) · 11.2 KB
/
effects.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
(function() {
"use strict";
var app = window.app || {};
window.app = app;
// Keep the contexts separate
function Effect(base) {
this.name = base.name;
this.author = base.author;
this.load = base.load;
this.draw = base.draw;
this.update = base.update;
this.resize = base.resize;
this.renderer = base.renderer;
this.shaders = base.shaders;
}
app.Effect = Effect;
app.effects = {
deflt: new Effect({
name: "Default",
author: "nucular",
draw: function(w, h, s) {
api.octx.globalCompositeOperation = "lighter";
// Clear the offscreen canvas
api.octx.clearRect(0, 0, w, h);
var fcount = app.analyser.frequencyBinCount;
var tcount = app.analyser.fftSize;
// Frequency average used for rotation later
var avg = 0;
for (var i = 0; i < fcount; i++) {
var f = api.freqdata[i] / 255;
var t = api.timedata[Math.floor((i / fcount) * tcount)] / 255;
if (i < fcount / 2)
avg += f * f;
else
avg -= f * f;
// Draw the rectangle
api.octx.fillStyle = "hsl(" + (48+(f * 164)) + ", 100%, " + (t * 100) + "%)";
api.octx.fillRect((i / fcount) * w, h - (f * (h / 4)), w / fcount, f * (h / 4));
api.octx.fillRect(w - (i / fcount) * w, f * (h / 4), w / fcount, -f * (h / 4));
}
// Translate the contents of the screen, leave behind a trail
api.ctx.save();
api.ctx.translate(w/2, h/2);
api.ctx.rotate((avg / (fcount / 2)) / 10);
api.ctx.scale(0.99, 0.99);
api.ctx.translate(-w/2, -h/2);
api.ctx.drawImage(api.canvas, 0, 0);
api.ctx.restore();
// Draw the off-screen canvas untranslated to the screen
api.ctx.drawImage(api.ocanvas, 0, 0);
}
}),
ristovski: new Effect({
name: "Effect Jesus",
author: "Ristovski (effect jesus)",
draw: function(w, h, s) {
api.octx.globalCompositeOperation = "lighter";
api.octx.clearRect(0, 0, w, h);
var fcount = app.analyser.frequencyBinCount;
var tcount = app.analyser.fftSize;
for (var i = 0; i < fcount; i++) {
var f = api.freqdata[i] / 255;
var t = api.timedata[Math.floor((i / fcount) * tcount)] / 255;
api.octx.fillStyle = "hsl(" + (360-Math.exp(f * 9)) + ", 100%," + (t * 100) + "%)";
api.octx.fillRect((i / fcount) * w, h - (f * (h)), w / fcount, f * (h / 40));
api.octx.fillRect(w - (i / fcount) * w, f * (h), w / fcount, -f * (h / 40));
api.octx.fillRect(w - (i / fcount) / w, f * (h), w / fcount, -f * (h / 40));
}
api.ctx.save();
api.ctx.translate(w/2, h/2);
api.ctx.scale(0.98, 0.98);
api.ctx.translate(-w/2, -h/2);
api.ctx.drawImage(api.canvas, 0, 0);
api.ctx.restore();
api.ctx.fillStyle = 'rgba(0, 0, 0, 0.3)';
api.ctx.fillRect(0, 0, w, h);
api.ctx.drawImage(api.ocanvas, 0, 0);
}
}),
simple: new Effect({
name: "Simple",
author: "nucular",
draw: function(w, h, s) {
api.ctx.clearRect(0, 0, w, h);
var fcount = app.analyser.frequencyBinCount;
var tcount = app.analyser.fftSize;
api.ctx.strokeStyle = "#fff";
api.ctx.beginPath();
api.ctx.moveTo(0, h / 2);
for (var i = 0; i < tcount; i++) {
var f = api.timedata[i] / 255;
api.ctx.lineTo((i / tcount) * w, f * h);
}
api.ctx.stroke();
}
}),
freq3d: new Effect({
name: "Freq3D",
author: "nucular",
draw: function(w, h, s) {
api.octx.clearRect(0, 0, w, h);
api.octx.translate(w / 2, h / 2);
var fcount = app.analyser.frequencyBinCount;
var tcount = app.analyser.fftSize;
var side = Math.sqrt(app.analyser.frequencyBinCount);
var mfcount = Math.floor(fcount / side) * side;
for (var i = 0; i < mfcount; i++) {
var f = api.freqdata[i] / 255;
var t = api.timedata[Math.floor((i / fcount) * tcount)] / 255;
var x = (i % side) / side;
var y = f;
var z = 1 - (Math.floor(i / side) / side);
var rw = (1.5 - z) * 5;
// Translate a bit
x = (x - 0.5) / 2;
y = (y - 0.5) / 2 + (z / 4);
z = (z + 0.5) * 2;
// Project
var px = (x * w) / z;
var py = (-y * h) / z;
api.octx.fillStyle = "hsl(" + (48+(i/fcount * 164)) + ", 100%, " + (t * 100) + "%)";
api.octx.fillRect(px, py, rw, rw / 2);
}
api.octx.translate(-w / 2, -h / 2);
api.ctx.save();
api.ctx.translate(w/2, h/2);
api.ctx.scale(0.99, 0.99);
api.ctx.translate(-w/2, -h/2);
api.ctx.drawImage(api.canvas, 0, 0);
api.ctx.restore();
api.ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
api.ctx.fillRect(0, 0, w, h);
api.ctx.drawImage(api.ocanvas, 0, 0);
}
}),
deadrose: new Effect({
name: "Dead Rose",
author: "nucular",
draw: function(w, h, s) {
api.octx.globalCompositeOperation = "lighter";
api.octx.clearRect(0, 0, w, h);
s = s / 2;
var fcount = app.analyser.frequencyBinCount;
var tcount = app.analyser.fftSize;
api.octx.translate(w / 2, h / 2);
var avg = 0;
for (var i = 0; i < fcount; i++) {
var f = api.freqdata[i] / 255;
var t = api.timedata[Math.floor((i / fcount) * tcount)] / 255;
if (i < fcount / 3)
avg += f * f;
else
avg -= f * f;
var x = Math.cos(i / fcount * 200) * f;
var y = Math.sin(i / fcount * 200) * f;
if (!(x == 0 && y == 0)) {
api.octx.fillStyle = "hsla(" + (300+(i/fcount)*80) + ", 100%," + (t * 100) + "%, " + (f+0.6) + ")";
api.octx.fillRect(x * s, y * s,
(0.1 + f) * (s / 60), (0.1 + f) * (s / 60));
}
}
api.octx.translate(-w / 2, -h / 2);
api.ctx.save();
api.ctx.translate(w/2, h/2);
api.ctx.rotate((avg / (fcount / 2)) / 10);
api.ctx.scale(0.99, 0.99);
api.ctx.translate(-w/2, -h/2);
api.ctx.drawImage(api.canvas, 0, 0);
api.ctx.restore();
api.ctx.fillStyle = 'rgba(0, 0, 0, 0.03)';
api.ctx.fillRect(0, 0, w, h);
api.ctx.drawImage(api.ocanvas, 0, 0);
}
}),
gltest: new Effect({
name: "GLTest",
author: "ported from glslsandbox.com",
renderer: "webgl",
shaders: {
fragment: "precision mediump float;"
+ "uniform float time;"
+ "uniform vec2 mouse;"
+ "uniform vec2 resolution;"
+ "uniform float avg;"
+ "void main(void) {"
+ " vec2 position = (gl_FragCoord.xy / resolution.xy) + mouse / 4.0;"
+ " float color = 0.0;"
+ " float btime = (time * 5.0) + (avg * 100.0);"
+ " color += sin(position.x * cos(btime / 15.0) * 80.0) + cos(position.y * cos(btime / 15.0) * 10.0);"
+ " color += sin(position.y * sin(btime / 10.0) * 40.0) + cos(position.x * sin(btime / 25.0) * 40.0);"
+ " color += sin(position.x * sin(btime / 5.0) * 10.0) + sin(position.y * sin(btime / 35.0) * 80.0);"
+ " color *= sin(btime / 10.0) * 0.5;"
+ " gl_FragColor = vec4(vec3(color, color * 0.5, sin(color + btime / 3.0) * 0.75), 1.0);"
+ "}"
},
load: function() {
api.gl.program.avgLocation = api.gl.getUniformLocation(api.gl.program, "avg");
},
draw: function(w, h, ow, oh) {
var fcount = app.analyser.frequencyBinCount;
var avg = 0;
for (var i = 0; i < fcount; i++) {
var f = api.freqdata[i] / 255;
if (i < fcount / 3)
avg += f * f;
else
avg -= f * f;
}
avg /= fcount;
api.gl.uniform1f(api.gl.program.avgLocation, avg);
api.gl.uniform2f(api.gl.program.resolutionLocation, w, h);
api.gl.vertexAttribPointer(api.gl.program.positionLocation, 2, api.gl.FLOAT, false, 0, 0);
api.gl.clear(api.gl.COLOR_BUFFER_BIT | api.gl.DEPTH_BUFFER_BIT);
api.gl.drawArrays(api.gl.TRIANGLES, 0, 6);
}
}),
fireflies: new Effect({
name: "Fireflies",
author: "ported from glslsandbox.com/e#24765.0",
renderer: "webgl",
shaders: {
fragment: "precision mediump float;"
+ "uniform float time;"
+ "uniform vec2 resolution;"
+ "uniform float avg1;"
+ "uniform float avg2;"
+ "void main(void) {"
+ " float myTime = time / 50.0;"
+ " vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y);"
+ " p *= mat2(cos(myTime*1.0), sin(myTime*1.0), -sin(myTime*1.0), cos(myTime*1.0));"
+ " vec3 destColor = vec3(0.0);"
+ " for (float i = 2.0; i < 10.0; i++) {"
+ " float j = i * i;"
+ " float tt = myTime + 2.0;"
+ " vec2 q = p + vec2(sin(tt * j)*length(cos(tt)), cos(tt * j)*length(sin(tt)));"
+ " destColor += 0.02 * abs(atan(myTime)) / length(q) * avg1;"
+ " }"
+ " float g = destColor.r * abs(sin(myTime*5.0));"
+ " float b = destColor.r * abs(sin(myTime*3.0));"
+ " float r = destColor.r * abs(cos(myTime*0.2));"
+ " destColor = vec3(0.0);"
+ " for (float i = 2.0; i < 10.0; i++) {"
+ " float j = i * i;"
+ " float tt = myTime + 1.0;"
+ " vec2 q = p + vec2(sin(tt * j)*length(cos(tt)), cos(tt * j)*length(sin(tt)));"
+ " destColor += 0.02 * abs(atan(tt)) / length(q) * avg2;"
+ " }"
+ " g += destColor.r * abs(sin(myTime*2.0));"
+ " b += destColor.r * abs(sin(myTime*5.0));"
+ " r += destColor.r * abs(cos(myTime*0.5));"
+ " gl_FragColor = vec4(r, g, b, 1.0);"
+ "}"
},
load: function() {
this.time = 0;
api.gl.program.avg1Location = api.gl.getUniformLocation(api.gl.program, "avg1");
api.gl.program.avg2Location = api.gl.getUniformLocation(api.gl.program, "avg2");
},
update: function(dt, time) {
var fcount = app.analyser.frequencyBinCount;
var avg1 = 0, avg2 = 0;
for (var i = 0; i < fcount; i++) {
var f = api.freqdata[i] / 255;
if (i < fcount / 2) {
avg1 += f * f;
} else {
avg2 += f * f;
}
}
avg1 /= fcount;
avg2 /= fcount;
this.time += (dt * (avg1 + 0.01)) * 50;
api.gl.uniform1f(api.gl.program.timeLocation, this.time);
api.gl.uniform1f(api.gl.program.avg1Location, 0.2 + (avg1 * 5), 0);
api.gl.uniform1f(api.gl.program.avg2Location, 0.2 + (avg2 * 5), 0);
},
draw: function(w, h, ow, oh) {
api.gl.uniform2f(api.gl.program.resolutionLocation, w, h);
api.gl.vertexAttribPointer(api.gl.program.positionLocation, 2, api.gl.FLOAT, false, 0, 0);
api.gl.clear(api.gl.COLOR_BUFFER_BIT | api.gl.DEPTH_BUFFER_BIT);
api.gl.drawArrays(api.gl.TRIANGLES, 0, 6);
}
})
};
})();