-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfaders.js
78 lines (69 loc) · 2.34 KB
/
faders.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
// An extremely simple demo of the flowing points visualization.
// public domain method demo by Terry Brown, [email protected]
// get canvas element and size
var canv = document.getElementById('fade_canv');
var width = parseFloat(canv.width);
var height = parseFloat(canv.height);
var faders_n = 250; // faders to show
var faders = []; // list of active faders
var fader_age = 200; // max. age of a fader
function new_fader() {
// return a new fader
return {
// position across canvas, 0-1
pos: Math.random(),
// vertical offset
offs: (0.5-Math.random())*height*0.2,
age: 0,
speed: 0.5
};
}
// create initial faders
for (var i=0; i<faders_n; i++) {
faders.push(new_fader());
// randomize ages so they're not synchronized
faders[faders.length-1].age = Math.random()*fader_age;
}
var ctx = canv.getContext('2d');
function fade() {
for (var f=0; f<faders_n; f++) {
var fader = faders[f];
var x = fader.pos * width;
// convert canvas position to angle in range pi to 3*pi
var a = fader.pos*2*Math.PI + Math.PI;
var y = fader.offs + Math.sin(a) * height*0.3 + height/2;
// red brightness relative to speed
var c = parseInt(256*fader.speed*1.2);
// opacity also relative to speed
var o = fader.speed
// draw a colored dot
ctx.fillStyle = "rgba("+c+",255,255,"+o+")"
ctx.fillRect(x, y, 1, 1);
// move the fader along, wrap if needed
fader.pos += fader.speed / width;
if (fader.pos > 1) {
fader.pos -= 1;
}
// accelerate by cosine
fader.speed += 0.002 * Math.cos(a);
// replace fader if aged out
fader.age += 1;
if (fader.age > fader_age) {
faders[f] = new_fader();
}
}
// decrease opacity across the image
var imageData = ctx.getImageData(0, 0, width, height);
data = imageData.data;
for(var i = 0; i < data.length; i += 4) {
// data[i], [i+1], [i+2], [i+3], are R, G, B, and alpha
// either
data[i + 3] = Math.max(0, data[i + 3]-1);
// or (this never fades to clear)
// data[i + 3] *= 0.97;
}
ctx.putImageData(imageData, 0, 0);
// use timeout to loop, otherwise display doesn't update
setTimeout(fade, 0);
}
fade();