-
Notifications
You must be signed in to change notification settings - Fork 2
/
visualisation.js
184 lines (162 loc) · 3.92 KB
/
visualisation.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
const analyser = require('./master').analyser;
const canvas = document.getElementById('visualisation');
const ctx = canvas.getContext('2d');
const JSCONF_HUE = 300;
const INITIAL_DELAY = 120;
const INITIAL_HEIGHT_FACTOR = 2;
let bars = Array(300);
let barCount = 60;
let forward = true;
let lineWidth = 6;
let lineGap = 5;
let heightFactor = INITIAL_HEIGHT_FACTOR;
let delay = INITIAL_DELAY;
let animate = 'auto';
let animateSwitch = 1000;
let hue = JSCONF_HUE;
let width, height;
const render = () => {
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
var average = getAverageVolume(array);
average *= heightFactor;
bars[0] = average;
average *= 0.8;
for (var i = 1; i < barCount; i++) {
average = average - Math.sqrt(average) + 1;
if (average < 0) {
average = 0;
}
(function( i, average ) {
setTimeout( function() {
bars[i] = average;
}, delay * ( forward ? i : 60 - i ) );
})(i, average);
}
draw();
requestAnimationFrame(render);
};
const draw = () => {
// clear the current state
ctx.clearRect( 0, 0, width, height );
// set the fill style
var average = bars[ 0 ];
var color = getColor( average );
rect( ( width / 2 ) - ( lineWidth / 2 ), ( height / 2 ) - ( average / 2 ), lineWidth, average, color );
for ( var i = 1; i < barCount; i++ ) {
average = bars[ i ];
color = getColor( average );
if ( average === undefined || average <= 0 ) {
average = 0;
} else {
rect( ( width / 2 ) - ( lineWidth / 2 ) + ( ( lineWidth + lineGap ) * i ), ( height / 2 ) - ( average / 2 ), lineWidth, average, color );
rect( ( width / 2 ) - ( lineWidth / 2 ) - ( ( lineWidth + lineGap ) * i ), ( height / 2 ) - ( average / 2 ), lineWidth, average, color );
}
}
};
let colors = [
'#111',
'purple',
'magenta',
'pink',
'red',
'orange',
'yellow',
'green',
'cyan',
'blue'
];
const getColor = (val) => {
if ( hue == 0 ) {
colors = [
'#111',
'purple',
'magenta',
'pink',
'red',
'orange',
'yellow',
'green',
'cyan',
'blue'
];
for ( var i = 0; i < hue; i++ ) {
colors.unshift( colors.pop() );
}
var whiteIndex = colors.indexOf( '#111' );
colors.splice( whiteIndex, 1 );
colors.unshift( '#111' );
} else {
colors = Array( 10 );
colors[ 0 ] = '#111';
var lightness = 79;
for ( var j = 9; j >= 1; j-- ) {
colors[ j ] = 'hsl(' + hue + ', 100%, ' + lightness + '%)';
lightness -= 5;
}
}
var colorIndex = Math.floor( val / ( 10 * heightFactor ) );
if ( colorIndex > 9 ) {
colorIndex = 9;
} else if ( colorIndex < 0 ) {
colorIndex = 0;
}
return colors[ colorIndex ];
};
const rect = ( x, y, width, height, color ) => {
ctx.save();
ctx.beginPath();
ctx.rect( x, y, width, height );
ctx.stroke();
ctx.clip();
ctx.fillStyle = color;
ctx.fillRect( 0,0,canvas.width,canvas.height );
ctx.restore();
};
const flip = () => {
if ( animate == 'auto' ) {
if ( forward ) {
forward = false;
} else {
forward = true;
}
}
setTimeout( flip, animateSwitch );
};
const getAverageVolume = (array) => {
var values = 0;
var average;
var length = array.length;
// get all the frequency amplitudes
for (var i = 0; i < length; i++) {
values += array[i];
}
average = values / length;
return average;
};
const fullScreen = () => {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
barCount = (width / (lineWidth * 2)) / 2;
};
module.exports = {
start () {
fullScreen();
flip();
requestAnimationFrame(render);
},
makeColorful () {
hue = 0;
delay = 50;
},
makeJSConfColors () {
hue = JSCONF_HUE;
delay = INITIAL_DELAY;
},
makeBig () {
heightFactor = 3.5;
},
makeNormalSize () {
heightFactor = INITIAL_HEIGHT_FACTOR;
}
};