-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpectrumView.js
50 lines (44 loc) · 1.42 KB
/
SpectrumView.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
var SpectrumView = function(canvas)
{
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.fftSize;
this.feedFrequencyData = function (data) {};
window.requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element){
window.setTimeout(callback, 1000 / 60);
};
})();
};
SpectrumView.prototype = {
/**
* ele canvas HTML Element
* ctx canvas context 2d
* data Time Damain Data
*/
render : function(ele, ctx, data) {
ctx.clearRect(0, 0, ele.width, ele.height);
var value;
ctx.beginPath();
ctx.strokeStyle = 'rgba(98, 149, 211, 0.5)';
for (var i = 0; i < data.length; ++i){
value = 1.0 - (parseInt(data[i]) / 256);
ctx.moveTo(i, ele.height);
ctx.lineTo(i, value * ele.height);
}
ctx.closePath();
ctx.stroke();
},
animationLoop : function(){
var data = new Uint8Array(this.fftSize);
this.feedFrequencyData(data);
this.render(this.canvas, this.ctx, data);
var self = this;
requestAnimationFrame(function () { self.animationLoop() });
},
};