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.
- Define your geometry as buffers of vertex coordinates.
- The vertex shader transforms each point's position (for camera angle, perspective, movement).
- The GPU rasterizes the shape into candidate pixels.
- The fragment shader colors each of those pixels (for lighting, texture, transparency).
- 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.
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.