Graphics WebGL Introduction

WebGL is a JavaScript API that hands drawing work directly to the GPU using small programs called shaders, making it a lower-level, far more powerful (and more complex) alternative to the Canvas 2D context.

Talking Directly to the Graphics Card

The Canvas 2D context you may already know gives you friendly, high-level commands — draw a rectangle, draw an arc, draw this image — and the browser handles the rest on the CPU. WebGL is a different rendering context you can request from the same canvas element, and it exposes the GPU's parallel rendering pipeline almost directly, based on the OpenGL ES standard used across desktop and mobile GPUs.

Shaders: Tiny Programs the GPU Runs in Parallel

WebGL has no built-in shape or drawing commands at all. Instead, you supply raw geometry (lists of point coordinates) and two small programs written in GLSL: a vertex shader, which the GPU runs once per point to decide where it lands on screen, and a fragment shader, which the GPU runs once per pixel to decide what color it should be. Because a GPU can run thousands of these tiny programs at once, WebGL can render far more geometry and far more complex effects per frame than CPU-based drawing ever could.

  1. Define your geometry as buffers of vertex coordinates.
  2. The vertex shader transforms each point's position (for camera angle, perspective, movement).
  3. The GPU rasterizes the shape into candidate pixels.
  4. The fragment shader colors each of those pixels (for lighting, texture, transparency).
  5. The result is composited onto the canvas.

Why It's Lower-Level Than Canvas 2D

Canvas 2D hides all of this behind commands like fillRect or drawImage; WebGL gives you none of that — there's no built-in circle, no built-in text, not even a built-in concept of a 'shape' beyond raw points and the shaders you write yourself. That's a steep jump in complexity, but it's also where the performance and flexibility come from: you're not limited to whatever drawing operations the browser decided to expose.

AspectCanvas 2DWebGL
Abstraction levelHigh — built-in shapes, text, imagesLow — raw vertices and hand-written shaders
Where work happensMostly CPU-driven drawing commandsGPU pipeline, run in parallel
Typical useCharts, simple games, everyday drawing3D scenes, particle effects, custom shader effects, GPU-heavy 2D
Learning curveGentleSteep — requires understanding the GPU pipeline

In Practice, You Rarely Write Raw WebGL

Because raw WebGL is verbose and low-level, most real projects use a library that wraps it in friendlier, higher-level concepts — three.js and Babylon.js expose objects like scenes, cameras, meshes, materials, and lights, letting you build 3D content without writing GLSL shaders or managing vertex buffers by hand for common cases.

Note: WebGL isn't only for 3D — it's also used for GPU-accelerated 2D effects, like custom image/video filters or generative art, whenever Canvas 2D's built-in operations aren't fast or flexible enough.
Note: WebGPU is a newer browser API positioned as WebGL's eventual successor — it exposes the GPU even more directly and is designed around how modern graphics hardware actually works, though WebGL remains the more universally supported option today.