const gl = document.querySelector('canvas').getContext('webgl');
const vs = `
attribute vec2 v;
varying vec2 v_v;
void main() {
gl_PointSize = 150.0;
gl_Position = vec4(0, 0, 0, 1);
v_v = v;
}
`;
const fs = `
precision highp float;
varying vec2 v_v;
float getNthBit(vec2 v, int bit) {
float t = bit < 16 ? v[0] : v[1];
int b = bit < 16 ? bit : bit - 16;
return mod(floor((t + 0.5) / pow(2.0, float(b))), 2.0);
}
void main() {
int bit = 31 - int(gl_FragCoord.x / 4.0);
float nthBit = getNthBit(v_v, bit);
gl_FragColor = vec4(0, nthBit > 0.0 ? 1.0 : 0.0, 0, 1);
}
`;
const prg = twgl.createProgram(gl, [vs, fs]);
const vLoc = gl.getAttribLocation(prg, 'v');
const buf = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buf, gl.STATIC_DRAW);
gl.enableVertexAttribArray(vLoc);
gl.vertexAttribPointer(vLoc, 2, gl.UNSIGNED_SHORT, false, 0, 0);
gl.useProgram(prg);
const data = new Float32Array(1);
const bits = new Uint32Array(data.buffer);
const inputElem = document.querySelector('input');
const codeElem = document.querySelector('pre');
function render() {
data[0] = inputElem.value;
codeElem.textContent = bits[0].toString(2).padStart(32, '0');
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
gl.drawArrays(gl.POINTS, 0, 1); // draw 1 point
}
render();
inputElem.addEventListener('input', render);
<script src="https://twgljs.org/dist/4.x/twgl-full.min.js"></script>
<canvas
width="128"
height="4"
style="
width: 256px;
height: 8px;
image-rendering: pixelated;
border: 1px solid black;
display: block;
"></canvas>
<pre></pre>
<input type="number" value="13445">