-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgl_graph.html
309 lines (280 loc) · 11.6 KB
/
webgl_graph.html
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!doctype html>
<!-- Simple oscilloscope trace plot using WebGL
Copyright (c) Jeremy P Bentham 2021. See http://iosoft.blog for details
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
v0.17 JPB 13/1/21 Removed duplicate init_graph
-->
<html>
<body onload=start_graph()>
<canvas id = "graph_canvas"></canvas>
<button id="single_btn" onclick="run_single(this)">Single</button>
<button id="run_stop_btn" onclick="run_stop(this)" >Run</button>
<select id="sel_nchans" onchange="sel_nchans()"></select>
<select id="sel_srce" onchange="sel_srce()" ></select>
<pre id="status" style="font-size: 14px; margin: 8px"></pre>
<script>
const WEBGL2 = true;
const NORM_XMIN=-1.0, NORM_XMAX=1.0, NORM_YMIN=-1.0, NORM_YMAX=1.0;
const XMARGIN=20, YMARGIN=90, MIN_CHANS=1, MAX_CHANS=16, NCHANS=2;
var data_srce = sim_srce = "sim", fifo_srce = "fifo";
var canvas = document.getElementById('graph_canvas');
var gl = canvas.getContext(WEBGL2 ? 'webgl2' : 'experimental-webgl');
var clear_colour = [0.82, 0.87, 0.82, 1.0];
var trace_colours = [[0.6, 0.7, 0.6, 1],
colr(0x000000), colr(0x800000), colr(0xff0000), colr(0xff9900),
colr(0xffff00), colr(0x00ff00), colr(0x0000ff), colr(0xff00ff),
colr(0x969696), colr(0xffffcc), colr(0x000000), colr(0x800000),
colr(0xff0000), colr(0xff9900), colr(0xffff00), colr(0x00ff00)];
var program, running=false, trace_ymax = 2.0, num_chans = NCHANS;
var trace_scoffs = [], grid_vertices = [], trace_vertices = [];
var frag_code, vert_code;
//Fragment shader for WebGL 2.0 and 1.0 (GLES 3.0 and 2.0)
if (WEBGL2)
frag_code = `#version 300 es
precision mediump float;
in vec4 v_colour;
out vec4 o_colour;
void main() {
o_colour = v_colour;
}`;
else
frag_code = `
precision mediump float;
varying vec4 v_colour;
void main(void) {
gl_FragColor = v_colour;
}`;
// Vertex shader for WebGL 2.0 and 1.0
if (WEBGL2)
vert_code = `#version 300 es
in vec3 a_coords;
out vec4 v_colour;
`;
else
vert_code = `
attribute vec3 a_coords;
varying vec4 v_colour;
`;
vert_code +=
`#define MAX_CHANS ${MAX_CHANS}
uniform vec4 u_colours[MAX_CHANS];
uniform vec2 u_scoffs[MAX_CHANS];
vec2 scoff;
void main(void) {
int zint = int(a_coords.z);
scoff = u_scoffs[zint];
gl_Position = vec4(a_coords.x, a_coords.y*scoff.x + scoff.y, 0, 1);
v_colour = u_colours[zint];
}`;
// Start creating display
function start_graph() {
resize_canvas();
var sel = document.getElementById("sel_nchans");
for (var n=MIN_CHANS; n<=MAX_CHANS; n++)
sel.options.add(new Option(n+" channel"+(n>1?"s":""), value=n));
sel.selectedIndex = NCHANS-1;
var sel = document.getElementById("sel_srce");
sel.options.add(new Option("simulated", value=sim_srce));
sel.options.add(new Option("from "+fifo_srce, value=fifo_srce));
try {
init_graph();
} catch (e) {
alert("Error: "+e);
}
window.addEventListener("resize", resize_canvas);
draw_test_graph();
redraw_graph();
}
// Initialise graph
function init_graph() {
var vertex_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
var frag_shader = compile_shader(gl.FRAGMENT_SHADER, frag_code);
var vert_shader = compile_shader(gl.VERTEX_SHADER, vert_code);
program = gl.createProgram();
gl.attachShader(program, vert_shader);
gl.attachShader(program, frag_shader);
gl.linkProgram(program);
gl.useProgram(program);
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
var coord = gl.getAttribLocation(program, "a_coords");
gl.vertexAttribPointer(coord, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(coord);
var clrs = gl.getUniformLocation(program, 'u_colours');
gl.uniform4fv(clrs, new Float32Array(trace_colours.flat()));
disp_status(location.host ? "Loaded from "+location.host :
"Loaded from filesystem, no data available");
}
// Compile a shader
function compile_shader(typ, source) {
var s = gl.createShader(typ);
gl.shaderSource(s, source);
gl.compileShader(s);
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS))
throw "Could not compile " +
(typ==gl.VERTEX_SHADER ? "vertex" : "fragment") +
" shader:\n\n"+gl.getShaderInfoLog(s);
return(s);
}
// Set point in line
function set_point(vts, x, y, z) {
vts.push(x, y, z);
}
// Draw a line
function draw_line(vts, x1, y1, x2, y2, z) {
vts.push(x1, y1, z, x2, y2, z);
}
// Draw a scope trace, given single-channel data
function draw_trace(vts, xmin, xmax, vals, zval) {
var x, y, np = vals.length;
for (var n=0; n<np; n++) {
if (n > 1)
set_point(vts, x, y, zval);
x = xmin + (xmax-xmin) * n / (np - 1);
y = vals[n];
set_point(vts, x, y, zval);
}
}
// Draw scope traces, given interleaved multi-channel data
function draw_traces(vts, vals) {
var x, y, zval, np = vals.length / num_chans;
for (var chan=0; chan<num_chans; chan++) {
zval = chan + 1;
for (var n=0; n<np; n++) {
if (n > 1)
set_point(vts, x, y, zval);
x = NORM_XMIN + (NORM_XMAX-NORM_XMIN) * n / (np - 1);
y = vals[chan + n*num_chans];
set_point(vts, x, y, zval);
}
}
}
// Draw a test trace
function draw_test_trace(vts, np, zval) {
var vals = [];
for (var i=0; i<np; i++)
vals.push((Math.sin(i / 10.0 - Math.PI / 2) + 1) *
trace_ymax / (2.0 + i/100.0));
draw_trace(vts, NORM_XMIN, NORM_XMAX, vals, zval);
}
// Draw initial graph with test data
function draw_test_graph() {
gl.clearColor(...clear_colour);
init_scale_offset(trace_ymax);
draw_grid(grid_vertices, 10, 8, 0);
draw_test_trace(trace_vertices, 1000, 1);
draw_test_trace(trace_vertices, 1000, 2);
redraw_data();
}
// Prepare the data for a graph redraw
function redraw_data() {
var graph_vertices = [];
graph_vertices.push(...grid_vertices);
graph_vertices.push(...trace_vertices);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(graph_vertices), gl.STATIC_DRAW);
}
// Redraw the graph
function redraw_graph() {
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT);
if (trace_vertices.length)
gl.drawArrays(gl.LINES, 0, (grid_vertices.length + trace_vertices.length) / 3);
if (running)
get_data(data_srce)
}
// Draw grid
function draw_grid(vts, nx, ny, z) {
for (var i=0; i<=nx; i++) {
var x = NORM_XMIN + (NORM_XMAX-NORM_XMIN) * i / nx;
draw_line(vts, x, NORM_YMIN, x, NORM_YMAX, z);
}
for (var i=0; i<=ny; i++) {
var y = NORM_YMIN + (NORM_YMAX-NORM_YMIN) * i / ny;
draw_line(vts, NORM_XMIN, y, NORM_XMAX, y, z);
}
}
// Initialise the scale & offset values for channels
function init_scale_offset(ymax) {
trace_scoffs = [1.0,0.0];
for (var n=0; n<num_chans; n++) {
trace_scoffs.push((NORM_YMAX-NORM_YMIN) / (ymax*num_chans));
trace_scoffs.push(NORM_YMIN + n * (NORM_YMAX-NORM_YMIN) / num_chans);
}
var u = gl.getUniformLocation(program, 'u_scoffs');
gl.uniform2fv(u, new Float32Array(trace_scoffs));
}
// Get data from server, redraw graph
function get_data(fname) {
fetch(fname).then((response) => {
return response.ok ? response.text() : ""
})
.then(data => {
var vals = csv_decode(data);
disp_status(data ? vals.length+" samples" : "No data");
trace_vertices = [];
draw_traces(trace_vertices, vals);
redraw_data();
redraw_graph();
})
.catch(error => {
disp_status("Can't load data");
});
}
// Handle 'single' button press
function run_single(btn) {
if (running)
run_stop(elem('run_stop_btn'));
var vals = get_data(data_srce);
}
// Handle 'run/stop' button press
function run_stop(btn) {
running = !running;
btn.innerText = running ? "Stop" : "Run";
if (running)
window.requestAnimationFrame(redraw_graph);
}
// Update the status display
function disp_status(s) {
elem('status').innerHTML = s.trim();
}
// Handle resizing of the canvas
function resize_canvas() {
canvas.width = window.innerWidth - XMARGIN;
canvas.height = window.innerHeight - YMARGIN;
redraw_graph();
}
// Change number of channels
function sel_nchans() {
var sel = document.getElementById("sel_nchans");
num_chans = sel.options[sel.selectedIndex].value;
init_scale_offset(trace_ymax);
}
// Select data source (simulation or FIFO)
function sel_srce() {
var sel = elem("sel_srce");
data_srce = sel.options[sel.selectedIndex].value;
}
// Decode CSV string into floating-point array
function csv_decode(s) {
data = s.trim().split(',');
return data.map(x => parseFloat(x));
}
// Convert hex colour to normalised RGB values
function colr(x) {
return([(x>>16&255)/255.0, (x>>8&255)/255.0, (x&255)/255.0, 1.0]);
}
// Return a document element, given ID
function elem(id) {
return document.getElementById(id);
}
</script>
</body>
</html>