Published
Edited
Sep 7, 2021
Insert cell
Insert cell
viewof gl = {
const canvas = DOM.canvas(
ewidth * devicePixelRatio,
height * devicePixelRatio
);
canvas.style.width = `${ewidth}px`;
canvas.style.height = `${height}px`;
const gl = (canvas.value = canvas.getContext("webgl"));
gl.viewport(0, 0, canvas.width, canvas.height);
return canvas;
}
Insert cell
Insert cell
vertexBuffer = {
// A non-indexed full-"screen" quad of two triangles
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array([-0.5, -0.5, 0.5, -0.5, 0, 0.5]),
gl.STATIC_DRAW
);
invalidation.then(() => gl.deleteBuffer(buffer));
return buffer;
}
Insert cell
Insert cell
Insert cell
u_background = gl.getUniformLocation(program, "background")
Insert cell
Insert cell
Insert cell
vertexShader = {
const shader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(shader, vertexShaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
throw gl.getShaderInfoLog(shader);

// Important: delete our resources when the cell is invalidated
invalidation.then(() => gl.deleteShader(shader));
return shader;
}
Insert cell
Insert cell
Insert cell
fragmentShader = {
const shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(shader, fragmentShaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
throw gl.getShaderInfoLog(shader);
invalidation.then(() => gl.deleteShader(shader));
return shader;
}
Insert cell
Insert cell
program = {
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
throw gl.getProgramInfoLog(program);
invalidation.then(() => gl.deleteProgram(program));
return program;
}
Insert cell
Insert cell
draw = {
setup;
gl.uniform3fv(u_background, hexToRGB(background));
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
Insert cell
Insert cell
setup = {
// One time setup
const positions = gl.getAttribLocation(program, "position");
gl.viewport(0, 0, viewof gl.width, viewof gl.height);
gl.useProgram(program);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(positions, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(positions);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more