-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
43 lines (33 loc) · 1006 Bytes
/
popup.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
canvas = document.getElementById('visualizer');
canvasCtx = canvas.getContext("2d");
var WIDTH = 300;
var HEIGHT = 300;
var port = chrome.extension.connect();
port.postMessage({action: 'start'});
port.onMessage.addListener(function(msg) {
var dataArray = msg.data;
var bufferLength = msg.bufferLength;
canvasCtx.fillStyle = 'rgb(242, 242, 242)';
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
canvasCtx.lineWidth = 2;
canvasCtx.beginPath();
var sliceWidth = WIDTH * 1.0 / bufferLength;
var x = 0;
for(var i = 0; i < bufferLength; i++) {
var data = dataArray[i];
var v = data / 128.0;
var y = v * HEIGHT/2;
var r = data + 120 ;
var g = 255 - data ;
var b = data / 3;
canvasCtx.strokeStyle = 'rgb(' + r + ', ' + g + ', ' + b + ')';
if(i === 0) {
canvasCtx.moveTo(x, y);
} else {
canvasCtx.lineTo(x, y);
}
x += sliceWidth;
}
canvasCtx.lineTo(canvas.width, canvas.height/2);
canvasCtx.stroke();
});