-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualise.js
147 lines (120 loc) · 4 KB
/
visualise.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
function makeCanvas(width, height) {
var c = document.createElement('CANVAS');
c.width = width || window.innerWidth;
c.height = height || 200;
return c;
}
function Visualiser(name, canvas) {
this.name = name || "default";
if (canvas) {
this.width = canvas.width || 0;
this.height = canvas.height || 0;
this.ctx = canvas.getContext('2d') || null;
var grd = this.ctx.createLinearGradient(0, 0, 0, this.height);
grd.addColorStop(0, "red");
grd.addColorStop(1, "blue");
this.ctx.fillStyle = grd;
this.ctx.strokeStyle = "#ffffff";
this.clear = function() {this.ctx.clearRect(0, 0, this.width, this.height);}
}
}
// data :: Uint8Array. Plots as bar graph
function BarGraph(name, canvas, data) {
Visualiser.call(this, name, canvas);
this.data = data;
this.bins = data.length;
this.binWidth = this.width / this.bins;
this.draw = function() {
var bins = this.bins;
var height = this.height;
var ctx = this.ctx;
for (var i = 0; i < bins; i++) {
var amp = this.data[i] / 255;
ctx.fillRect( this.binWidth * i, height * (1 - amp), this.binWidth, height );
}
}
}
BarGraph.prototype = new Visualiser;
// data :: Uint8Array. Plots as Line graph
function LineGraph(name, canvas, data) {
Visualiser.call(this, name, canvas);
this.data = data;
this.bins = data.length;
this.binWidth = this.width / this.bins;
this.draw = function() {
var bins = this.bins;
var height = this.height;
var ctx = this.ctx;
ctx.beginPath();
ctx.moveTo(0, height / 2);
for (var i = 0; i < bins; i++) {
var amp = this.data[i] / 255;
ctx.lineTo( this.binWidth * (i+0.5), height * (1 - amp));
}
ctx.stroke();
}
}
LineGraph.prototype = new Visualiser;
function DynamicLine(name, canvas, timescale, initY) {
Visualiser.call(this, name, canvas);
this.scale = this.width / timescale;
this.ctx.beginPath();
this.ctx.moveTo(0, this.height * (1 - initY));
var date = new Date();
this.prevTime = date.getTime();
this.draw = function(d) {
var height = this.height;
var ctx = this.ctx;
var date = new Date();
var currTime = date.getTime();
var t = ( currTime - this.prevTime ) * 0.001;
var x = ( t * this.scale );
var y = (1 - d/255) * height;
ctx.lineTo( x, y );
ctx.stroke();
if ( x > this.width ) {
this.clear();
ctx.beginPath();
ctx.moveTo( 0, y );
this.prevTime = currTime;
}
}
}
function HilbertImage(name, canvas, data, order) {
Visualiser.call(this, name, canvas);
this.data = data;
this.bins = data.length;
this.order = order;
this.mapping = hilbert_mapping(order);
var dimension = Math.sqrt(this.mapping.length);
this.scaleX = this.width / dimension;
this.scaleY = this.height / dimension;
this.draw = function() {
this.clear();
for (var i = 0; i < this.bins; i++) {
var fAmp = this.data[i];
var pos = this.mapping[i];
this.ctx.fillStyle = "rgba("+fAmp+","+0+","+(255-fAmp)+","+(fAmp/255)+")";
this.ctx.fillRect(pos.x * this.scaleX,
pos.y * this.scaleY,
this.scaleX,
this.scaleY);
}
}
}
// used in render loop
// higher the n, the smoother the FPS reading.
function FpsMonitor(n) {
this.n = n;
this.meanFPS = 0;
var date = new Date();
this.prevTime = date.getTime();
this.getFPS = function() {
var date = new Date();
var t = date.getTime();
var n = this.n;
this.meanFPS = (this.meanFPS*(n-1) + 1000/(t - this.prevTime))/n;
this.prevTime = t;
return Math.round(this.meanFPS);
}
}