Published
Edited
Oct 14, 2019
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const canvas = d3.create('canvas')
.attr('width', '360px')
.attr('height', '360px')
.node();
// 2 3
//
// 0 1
const vertices = [
0, 0,
300, 0,
0, 300,
300, 300,
];
const indices = [0, 2, 3, 0, 3, 1];
const colors = [
0,0,
1,0,
0,1,
1,1,
];
// vertex shader source code
const vertCode = `
attribute vec2 coordinates;
attribute vec3 color;
varying vec3 vColor;

void main(void) {
gl_Position = vec4(coordinates, 0.0, 1.0);
vColor = color;
}
`;
// fragment shader source code
const fragCode = `
precision mediump float;
varying vec3 vColor;

void main(void) {
gl_FragColor = vec4(vColor, 1.);
}
`;
DrawColor(canvas, 30, 30, vertices, indices, colors, 2, vertCode, fragCode);
return canvas;
}
Insert cell
Insert cell
DrawColor = (canvas, xTranslation, yTranslation, vertices, indices, colors, ColorBytes, vertCode, fragCode) => {
/*============= Creating a canvas ==================*/
const gl = canvas.getContext('experimental-webgl');
const localVertices = new Array(vertices.length);
// Transform from world space
for (let i = 0; i < vertices.length; i += 2) {
localVertices[i] = (2 * (vertices[i] + xTranslation) / canvas.width) - 1;
localVertices[i + 1] = (2 * (vertices[i + 1] + yTranslation) / canvas.height) - 1;
}
/*========== Defining and storing the geometry ==========*/
// Create an empty buffer object and store vertex data
const vertex_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(localVertices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);

// Create an empty buffer object and store Index data
const Index_Buffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

// Create an empty buffer object and store color data
const color_buffer = gl.createBuffer ();
gl.bindBuffer(gl.ARRAY_BUFFER, color_buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);

/*======================= Shaders =======================*/
// Create a vertex shader object
const vertShader = gl.createShader(gl.VERTEX_SHADER);

// Attach vertex shader source code
gl.shaderSource(vertShader, vertCode);

// Compile the vertex shader
gl.compileShader(vertShader);
// Create fragment shader object
const fragShader = gl.createShader(gl.FRAGMENT_SHADER);

// Attach fragment shader source code
gl.shaderSource(fragShader, fragCode);

// Compile the fragmentt shader
gl.compileShader(fragShader);

// Create a shader program object to
// store the combined shader program
const shaderProgram = gl.createProgram();

// Attach a vertex shader
gl.attachShader(shaderProgram, vertShader);

// Attach a fragment shader
gl.attachShader(shaderProgram, fragShader);

// Link both the programs
gl.linkProgram(shaderProgram);

// Use the combined shader program object
gl.useProgram(shaderProgram);

/* ======== Associating shaders to buffer objects =======*/

// Bind vertex buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);

// Bind index buffer object
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);

// Get the attribute location
const coord = gl.getAttribLocation(shaderProgram, "coordinates");

// point an attribute to the currently bound VBO
gl.vertexAttribPointer(coord, 2, gl.FLOAT, false, 0, 0);

// Enable the attribute
gl.enableVertexAttribArray(coord);

// bind the color buffer
gl.bindBuffer(gl.ARRAY_BUFFER, color_buffer);
// get the attribute location
const color = gl.getAttribLocation(shaderProgram, "color");
// point attribute to the volor buffer object
gl.vertexAttribPointer(color, ColorBytes, gl.FLOAT, false,0,0) ;
// enable the color attribute
gl.enableVertexAttribArray(color);

/*============Drawing the Quad====================*/

// Clear the canvas
gl.clearColor(1.0, 1.0, 1.0, 1.0);

// Enable the depth test
gl.enable(gl.DEPTH_TEST);

// Clear the color buffer bit
gl.clear(gl.COLOR_BUFFER_BIT);

// Set the view port
gl.viewport(0,0,canvas.width,canvas.height);

//Draw the triangle
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT,0);
}
Insert cell
Draw = (canvas, xTranslation, yTranslation, vertices, indices, vertCode, fragCode) => {
/*============= Creating a canvas ==================*/
const gl = canvas.getContext('experimental-webgl');
const localVertices = new Array(vertices.length);
// Transform from world space
for (let i = 0; i < vertices.length; i += 2) {
localVertices[i] = (2 * (vertices[i] + xTranslation) / canvas.width) - 1;
localVertices[i + 1] = (2 * (vertices[i + 1] + yTranslation) / canvas.height) - 1;
}
/*========== Defining and storing the geometry ==========*/
// Create an empty buffer object and store vertex data
const vertex_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(localVertices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);

// Create an empty buffer object and store Index data
const Index_Buffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

/*======================= Shaders =======================*/
// Create a vertex shader object
const vertShader = gl.createShader(gl.VERTEX_SHADER);

// Attach vertex shader source code
gl.shaderSource(vertShader, vertCode);

// Compile the vertex shader
gl.compileShader(vertShader);
// Create fragment shader object
const fragShader = gl.createShader(gl.FRAGMENT_SHADER);

// Attach fragment shader source code
gl.shaderSource(fragShader, fragCode);

// Compile the fragmentt shader
gl.compileShader(fragShader);

// Create a shader program object to
// store the combined shader program
const shaderProgram = gl.createProgram();

// Attach a vertex shader
gl.attachShader(shaderProgram, vertShader);

// Attach a fragment shader
gl.attachShader(shaderProgram, fragShader);

// Link both the programs
gl.linkProgram(shaderProgram);

// Use the combined shader program object
gl.useProgram(shaderProgram);

/* ======== Associating shaders to buffer objects =======*/

// Bind vertex buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);

// Bind index buffer object
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);

// Get the attribute location
const coord = gl.getAttribLocation(shaderProgram, "coordinates");

// point an attribute to the currently bound VBO
gl.vertexAttribPointer(coord, 2, gl.FLOAT, false, 0, 0);

// Enable the attribute
gl.enableVertexAttribArray(coord);
// get the attribute location
const color = gl.getAttribLocation(shaderProgram, "color");

/*============Drawing the Quad====================*/

// Clear the canvas
gl.clearColor(1.0, 1.0, 1.0, 1.0);

// Enable the depth test
gl.enable(gl.DEPTH_TEST);

// Clear the color buffer bit
gl.clear(gl.COLOR_BUFFER_BIT);

// Set the view port
gl.viewport(0,0,canvas.width,canvas.height);

//Draw the triangle
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT,0);
}
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more