Skip to content

Commit ba0160b

Browse files
committed
Added some kind of legend
1 parent 03efcee commit ba0160b

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

plotter.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@
455455
<input type="range" id="tresholdslider" min="0" max="1" step = "0.001"/>
456456
<input type="color" id="colorinput" />
457457
<input type="range" id="alphaslider" min="0" max="255" step="1"/>
458-
458+
<br>
459+
<canvas id="legend" width="300" height="50"/>
459460
</layer>
460461
<script type="text/javascript" src="vema/vema.js"></script>
461462
<script type="text/javascript" src="shader.js"></script>

plotter.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
let meshOn = true;
7373
let clearColor = [0, 0, 0, 1];
7474

75+
let legend;
76+
let legendtex;
77+
let legendprogram;
78+
let legendvertexbuffer;
79+
let legendtexcoordsbuffer;
80+
7581
const frequencyArray = [];
7682

7783
const Vec3 = VemaLib.Vec3; // eslint-disable-line no-undef
@@ -307,6 +313,8 @@
307313
}
308314
}
309315
}
316+
317+
if(legendtex !== undefined) drawLegend(gl);
310318
checkGlError();
311319
}
312320

@@ -1011,6 +1019,7 @@
10111019
pointcount = dataLength;
10121020
histogramvisible = true;
10131021
console.log('READY OR NOT');
1022+
updateLegend(maxv);
10141023
} else if (dataType === 1) {
10151024
const testfreq = splitStringBuffer[datastart].split('\t')[1];
10161025
let k = 0;
@@ -1202,6 +1211,7 @@
12021211
histogramvisible = true;
12031212
cf = frequencyArray[0];
12041213
console.log('READY OR NOT2');
1214+
updateLegend(maxv2[0],minv2[0]);
12051215
}
12061216
};
12071217
reader.onerror = function () {
@@ -1422,6 +1432,101 @@
14221432
clearColor = [rgb.r / 255, rgb.g / 255, rgb.b / 255, alpha / 255];
14231433
};
14241434

1435+
function updateLegend(maxv) {
1436+
let ctx = legend.getContext('2d');
1437+
let h = legend.height;
1438+
let w = legend.width;
1439+
for(let i=0; i<w; i++){
1440+
const color = getPseudoColor(i/w);
1441+
ctx.strokeStyle = 'rgb('+Math.ceil(color.red*255)+','+Math.ceil(color.green*255)+','+Math.ceil(color.blue*255),+')';
1442+
ctx.beginPath();
1443+
ctx.moveTo(i,0);
1444+
ctx.lineTo(i,h);
1445+
ctx.stroke();
1446+
}
1447+
//ctx.font = "30px Comic Sans MS";
1448+
ctx.font = "8px"
1449+
ctx.fillStyle = 'white';
1450+
ctx.textAlign = "center";
1451+
ctx.rotate(-Math.PI/2);
1452+
ctx.fillText(""+maxv.toFixed(2),-h/2,w);
1453+
1454+
legendtex = makeTexture(legend);
1455+
}
1456+
1457+
function initLegend(gl){
1458+
let legendvertex=`
1459+
precision highp float;
1460+
attribute vec2 pos;
1461+
attribute vec2 texCoord;
1462+
varying vec2 v_TexCoordinate;
1463+
void main() {
1464+
v_TexCoordinate = vec2(texCoord.x,1.0-texCoord.y);
1465+
gl_Position = vec4(pos,0.0,1.0);
1466+
}
1467+
`;
1468+
let legendfragment=`
1469+
precision highp float;
1470+
uniform sampler2D uTexture0;
1471+
varying vec2 v_TexCoordinate;
1472+
void main () {
1473+
gl_FragColor = texture2D(uTexture0, v_TexCoordinate);
1474+
}
1475+
`;
1476+
1477+
legendprogram = ShaderLib.makeProgram2(gl, legendvertex, legendfragment);
1478+
gl.useProgram(legendprogram);
1479+
legendprogram.vertexCoords = gl.getAttribLocation(legendprogram, 'pos');
1480+
legendprogram.texCoords = gl.getAttribLocation(legendprogram, 'texCoord');
1481+
legendprogram.uTexture0 = gl.getUniformLocation(legendprogram, 'uTexture0');
1482+
legendvertexbuffer =gl.createBuffer();
1483+
1484+
gl.bindBuffer(gl.ARRAY_BUFFER, legendvertexbuffer);
1485+
const ratio= 16/9;
1486+
const height = 1/10;
1487+
const width = height*300/80*ratio;
1488+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([1-width, 1-height, 1, 1-height, 1-width, 1, 1, 1
1489+
]), gl.STATIC_DRAW);
1490+
1491+
legendtexcoordsbuffer =gl.createBuffer();
1492+
gl.bindBuffer(gl.ARRAY_BUFFER, legendtexcoordsbuffer);
1493+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0, 0, 1, 0, 0, 1, 1, 1
1494+
]), gl.STATIC_DRAW);
1495+
1496+
}
1497+
1498+
function drawLegend(gl){
1499+
1500+
if(legendtex === undefined) return;
1501+
gl.useProgram(legendprogram);
1502+
1503+
gl.activeTexture(gl.TEXTURE0);
1504+
gl.bindTexture(gl.TEXTURE_2D, legendtex);
1505+
gl.uniform1i(legendprogram.uTexture0, 0);
1506+
1507+
gl.bindBuffer(gl.ARRAY_BUFFER, legendvertexbuffer);
1508+
gl.enableVertexAttribArray(legendprogram.vertexCoords);
1509+
gl.vertexAttribPointer(legendprogram.vertexCoords, 2, gl.FLOAT, false, 0, 0);
1510+
1511+
gl.bindBuffer(gl.ARRAY_BUFFER, legendtexcoordsbuffer);
1512+
gl.enableVertexAttribArray(legendprogram.texCoords);
1513+
gl.vertexAttribPointer(legendprogram.texCoords, 2, gl.FLOAT, false, 0, 0);
1514+
1515+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
1516+
}
1517+
1518+
function makeTexture(textCanvas){
1519+
var textWidth = textCanvas.width;
1520+
var textHeight = textCanvas.height;
1521+
var textTex = gl.createTexture();
1522+
gl.bindTexture(gl.TEXTURE_2D, textTex);
1523+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textCanvas);
1524+
// make sure we can render it even if it's not a power of 2
1525+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
1526+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1527+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1528+
return textTex;
1529+
}
14251530

14261531
PlotterLib.startdice = function startdice() {
14271532
gravity = new Vec3(0, -9.81, 0);
@@ -1435,6 +1540,8 @@
14351540
}
14361541

14371542
canvas = document.getElementById('glcanvas'); // eslint-disable-line no-undef
1543+
legend = document.getElementById('legend');
1544+
14381545
const tresholdslider = document.getElementById('tresholdslider'); // eslint-disable-line no-undef
14391546
const colorinput = document.getElementById('colorinput'); // eslint-disable-line no-undef
14401547
const alphaslider = document.getElementById('alphaslider'); // eslint-disable-line no-undef
@@ -1575,6 +1682,8 @@
15751682
// Initialize the GL context
15761683
gl = initWebGL(canvas);
15771684

1685+
initLegend(gl);
1686+
15781687
// stream = canvas.captureStream(); // frames per second
15791688

15801689
modelTrans.translate(modelPosition.f[0], modelPosition.f[1], modelPosition.f[2]);

shader.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@
3535
return program;
3636
};
3737

38+
ShaderLib.makeProgram2 = function makeProgram(gl, vertexshader, fragmentshader) {
39+
const vb = vertexshader;
40+
const fb = fragmentshader;
41+
42+
const vs = gl.createShader(gl.VERTEX_SHADER);
43+
gl.shaderSource(vs, vb);
44+
gl.compileShader(vs);
45+
46+
const fs = gl.createShader(gl.FRAGMENT_SHADER);
47+
gl.shaderSource(fs, fb);
48+
gl.compileShader(fs);
49+
50+
const program = gl.createProgram();
51+
gl.attachShader(program, vs);
52+
gl.attachShader(program, fs);
53+
gl.linkProgram(program);
54+
if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) {
55+
console.log(gl.getShaderInfoLog(vs)); // eslint-disable-line no-console
56+
}
57+
58+
if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) {
59+
console.log(gl.getShaderInfoLog(fs)); // eslint-disable-line no-console
60+
}
61+
62+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
63+
console.log(gl.getProgramInfoLog(program)); // eslint-disable-line no-console
64+
}
65+
return program;
66+
};
67+
3868
return ShaderLib;
3969
}
4070
if (typeof (ShaderLib) === 'undefined') window.ShaderLib = defineLibrary(); // eslint-disable-line no-param-reassign, no-undef

0 commit comments

Comments
 (0)