WebGL Concepts — Creative Web

WebGL 2 (OpenGL ES 3.0, getContext('webgl2'), check for null) draws with the GPU through shaders you write in GLSL ES 3.00. The pipeline: vertex buffer →…

Raka wants buyers to turn the coffee-print mug around in 3D in his shop. For 3D, the browser needs the help of the GPU, the graphics chip in a laptop or phone. The way in is WebGL, a JavaScript API for drawing on a <canvas> with the GPU.

Think of how Raka screen-prints: one pull of the squeegee pushes ink through the screen onto thousands of points of fabric at once. The screen decides where the ink goes, the ink decides what colour. A GPU works like that: one draw command, then the same small program runs for thousands of pixels together. That small program is called a shader, and in WebGL you write it. Before three.js, let's see what happens on the GPU with the most basic shape: one triangle.

WebGL 2 and the GPU

A GPU has thousands of small cores that run the same program on lots of data at once. WebGL 2 is OpenGL ES 3.0 for the browser:

const gl = canvas.getContext('webgl2');
if (!gl) throw new Error('This browser does not support WebGL 2 yet');

WebGL 2 has been around since Chrome 56, Firefox 51 and Safari 15, about 96% of users (caniuse, October 2026). getContext returns null when it isn't supported or the GPU is blocked, so always check.

Canvas 2D WebGL 2
How you draw call fillRect, arc, the browser does the work write shaders yourself, the GPU runs them
Each pixel's colour is decided by the browser your fragment shader
3D none yes, you compute the positions yourself
Code for one shape 3 lines dozens of lines

Canvas 2D is a high-level 2D API. The browser may well use the GPU behind it (Chrome and Safari often do), but you can't write a program for the GPU. In WebGL you can, and that is what makes 3D and heavy effects possible.

The pipeline: from points to pixels

One gl.drawArrays(gl.TRIANGLES, 0, 3) on a 300 × 300 canvas goes through four stages:

Stage Runs Its job
1. Vertex buffer filled once holds each point's data (position, colour) in GPU memory
2. Vertex shader 3 times, once per point decides that point's final position
3. Rasteriser once, automatically finds which pixels lie inside the triangle: 14,850 pixels
4. Fragment shader 14,850 times, once per pixel decides that pixel's colour

The candidate pixels the rasteriser produces are called fragments. 14,850 isn't a guess: the triangle covers 16.5% of the canvas, and 16.5% × 90,000 = 14,850, the same as the readPixels count in the Lab. On a 600 × 600 canvas (a DPR 2 screen) the fragment shader runs 59,400 times, four times as many. The rasteriser is the only stage you don't write.

Clip space

The vertex shader doesn't talk in pixels. It fills gl_Position in clip space: x and y from -1 to 1, (0, 0) in the middle of the canvas, and y pointing up, the opposite of Canvas 2D. Turning it into pixels:

pixel x = (x + 1) / 2 × width
pixel y = (1 - y) / 2 × height

The triangle's top point (0, 0.6) on a 300 × 300 canvas lands on pixel (150, 60). A point outside -1 to 1 is no error: the part of the triangle that sticks out is cut off (clipping), and the rest is still drawn.

The first triangle, complete code

<canvas id="gpu" width="300" height="300"></canvas>
const canvas = document.getElementById('gpu');
const gl = canvas.getContext('webgl2', { antialias: false });
if (!gl) throw new Error('This browser does not support WebGL 2 yet');

// Vertex shader: runs once per point
const vsSource = `#version 300 es
in vec2 aPosition;
in vec3 aColor;
out vec3 vColor;
void main() {
  vColor = aColor;
  gl_Position = vec4(aPosition, 0.0, 1.0);
}`;

// Fragment shader: runs once per pixel
const fsSource = `#version 300 es
precision highp float;
in vec3 vColor;
out vec4 outColor;
void main() {
  outColor = vec4(vColor, 1.0);
}`;

function makeShader(type, source) {
  const shader = gl.createShader(type);
  gl.shaderSource(shader, source);
  gl.compileShader(shader);
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
    throw new Error(gl.getShaderInfoLog(shader));
  }
  return shader;
}

const program = gl.createProgram();
gl.attachShader(program, makeShader(gl.VERTEX_SHADER, vsSource));
gl.attachShader(program, makeShader(gl.FRAGMENT_SHADER, fsSource));
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
  throw new Error(gl.getProgramInfoLog(program));
}

// Each point: x, y (clip space), then r, g, b (0 to 1)
const data = new Float32Array([
   0.0,  0.6,   0.769, 0.478, 0.239, // top: BROWN #c47a3d
  -0.6, -0.5,   0.961, 0.902, 0.784, // bottom left: CREAM #f5e6c8
   0.6, -0.5,   0.498, 0.690, 0.412, // bottom right: GREEN #7fb069
]);
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);

const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const positionLoc = gl.getAttribLocation(program, 'aPosition');
gl.enableVertexAttribArray(positionLoc);
gl.vertexAttribPointer(positionLoc, 2, gl.FLOAT, false, 20, 0);
const colorLoc = gl.getAttribLocation(program, 'aColor');
gl.enableVertexAttribArray(colorLoc);
gl.vertexAttribPointer(colorLoc, 3, gl.FLOAT, false, 20, 8);

gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0.11, 0.098, 0.09, 1); // shirt black #1c1917
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(program);
gl.drawArrays(gl.TRIANGLES, 0, 3);

Attribute, uniform, varying

Data reaches a shader in three ways:

Kind In GLSL ES 3.00 Its value Example
attribute in in the vertex shader different at each point, from the buffer aPosition, aColor
uniform uniform the same for every point and pixel in one draw a single colour, the time, the camera matrix
varying out in the vertex shader, in in the fragment shader blended (interpolated) by the GPU for each pixel vColor

WebGL 1 writes them attribute and varying; GLSL ES 3.00 uses in and out.

Before reading on, guess: that triangle is brown #c47a3d at the top, cream #f5e6c8 at bottom left, green #7fb069 at bottom right. What colour is the pixel right in its middle (the triangle's centroid)?

The rasteriser blends a varying by the pixel's distance to each point. At the centroid all three points weigh the same, 1/3: red (196 + 245 + 127) / 3 = 189, green (122 + 230 + 176) / 3 = 176, blue (61 + 200 + 105) / 3 = 122. The middle pixel is #bdb07a, a brownish cream, and Chrome, Firefox and Safari agree.

Want one colour for the whole triangle? That's a uniform:

// fragment shader: uniform vec3 uColor;  ...  outColor = vec4(uColor, 1.0);
gl.useProgram(program);
gl.uniform3f(gl.getUniformLocation(program, 'uColor'), 0.769, 0.478, 0.239);

Why three.js exists

The code above is 55 lines (not counting blank lines and comments) for one flat triangle. The three.js version:

import * as THREE from 'three';

const canvas = document.getElementById('gpu');
const renderer = new THREE.WebGLRenderer({ canvas });
const scene = new THREE.Scene();
scene.background = new THREE.Color('#1c1917');
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, -1, 1);

const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute([0, 0.6, 0, -0.6, -0.5, 0, 0.6, -0.5, 0], 3));
const colors = ['#c47a3d', '#f5e6c8', '#7fb069'].flatMap((hex) => new THREE.Color(hex).toArray());
geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
scene.add(new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ vertexColors: true })));

renderer.render(scene, camera);

12 lines, and three.js (version 0.186) makes the shaders, buffers, VAO and program for you. The triangle covers the same 14,850 pixels; its middle is a little lighter (#c5b78a) because three.js blends colours in a linear colour space, covered in the Geometry & Material lesson. The gap widens for a 3D mug: a perspective camera, rotation matrices, surface normals, a lighting formula and textures. In raw WebGL that's hundreds of lines; in three.js a few lines each.

WebGPU today

WebGPU is WebGL's successor: an API closer to modern GPUs, with shaders in WGSL (WebGPU Shading Language), not GLSL. Its status as of October 2026 (the gpuweb implementation wiki):

Browser On without any setting
Chrome and Edge Windows, macOS, ChromeOS since 113; Android 12+ since 121. Linux since 144 for Intel Gen12+ GPUs, since 147 for NVIDIA on Wayland, other GPUs still behind a flag
Firefox Windows since 141; Apple Silicon macOS since 145 (macOS 26 only), every macOS version on Apple Silicon since 147. Linux, Intel Macs and Android still in development
Safari macOS, iOS, iPadOS and visionOS 26

About 86% of users (caniuse), fewer than WebGL 2. three.js has a WebGPURenderer that falls back to WebGL 2 automatically when WebGPU is missing. The concepts are the same: buffers, vertex shader, rasteriser, fragment shader.

⚠️ An empty canvas, or a triangle in the wrong place?

TL;DR: WebGL 2 (OpenGL ES 3.0, getContext('webgl2'), check for null) draws with the GPU through shaders you write in GLSL ES 3.00. The pipeline: vertex buffer → vertex shader (once per point, fills gl_Position in clip space -1 to 1, y up) → rasteriser (finds the pixels inside the shape) → fragment shader (once per pixel, decides the colour). A triangle on a 300 × 300 canvas: the vertex shader runs 3 times, the fragment shader 14,850 times. An attribute differs per point, a uniform is the same for all, a varying is blended per pixel (the middle of a brown, cream, green triangle = #bdb07a). One triangle takes 55 lines of WebGL or 12 lines of three.js. WebGPU (WGSL) is on in Chrome, Edge, Safari 26 and Firefox on Windows and Apple Silicon Macs, not yet on every Linux.

⚡ Into the GPU through WebGL 2

Write:

const gl = canvas.getContext('webgl2');
if (!gl) throw new Error('This browser does not support WebGL 2 yet');

Meaning: WebGL draws on a <canvas> with the GPU. What decides positions and colours is a shader: a small program you write, which the GPU runs for thousands of points and pixels at once. getContext can return null, so always check.

⚡ From points to pixels

Guess first: one triangle on a 300 × 300 canvas. How many times does the fragment shader run?

14,850 times, once per pixel inside the triangle. The vertex shader runs only 3 times, once per point.

Meaning: The pipeline: vertex buffer, vertex shader (each point's position), rasteriser (finds the pixels inside the triangle), then fragment shader (each pixel's colour). The rasteriser is the only stage you don't write.

⚡ Clip space: y points up

Write:

pixel x = (x + 1) / 2 × width
pixel y = (1 - y) / 2 × height

Meaning: gl_Position is filled in clip space: -1 to 1, (0, 0) in the middle, and y pointing up, the opposite of Canvas 2D. The top point (0, 0.6) on a 300 × 300 canvas lands on pixel (150, 60).

⚡ Attribute, uniform, varying

Meaning: An attribute differs at each point and comes from the buffer. A uniform is the same for every point and pixel in one draw. A varying is blended by the GPU for each pixel, so the middle of a brown, cream, green triangle is #bdb07a.

What you will learn