← Glossary

What Is WebGL?

WebGL (Web Graphics Library) is a JavaScript API for rendering GPU-accelerated 2D and 3D graphics inside an HTML canvas, based on OpenGL ES and programmed with shaders, without plug-ins.

Updated:

WebGL, the Web Graphics Library, is a JavaScript API for rendering interactive 2D and 3D graphics inside an HTML canvas element without plug-ins. It gives web pages access to the GPU through an interface that closely follows OpenGL ES: WebGL 1 corresponds to OpenGL ES 2.0 and WebGL 2 adds most of OpenGL ES 3.0. The specifications are maintained by the Khronos Group, and the API is broadly implemented in major browsers, subject to device and policy support.

Unlike the drawing commands of the 2D canvas, WebGL exposes a programmable pipeline. The page uploads geometry and textures to GPU memory, writes small programs called shaders that run on the GPU, and issues draw calls. This article explains contexts, shaders and the rendering flow, how capabilities and browser implementations vary, how WebGL compares with Canvas 2D and WebGPU, and where its limits are.

Contexts, Shaders and Rendering

Getting a Rendering Context

Everything starts with a WebGL rendering context obtained from a canvas: calling getContext with the string webgl returns a WebGLRenderingContext, and webgl2 returns a WebGL2RenderingContext. The call returns null if the context type is unsupported or if the canvas already holds a different kind of context, since a canvas can have only one context mode. If the drawing buffer cannot be created, the browser fires a context creation error event on the canvas.

The second argument carries context attributes. Among them, antialias asks for edge smoothing, preserveDrawingBuffer keeps the previous frame instead of clearing it, powerPreference hints whether a high-performance or low-power GPU should be chosen, and failIfMajorPerformanceCaveat asks the browser to reject a context if it would have a major performance caveat. A page that would rather fall back to a simpler renderer than run slowly sets that last flag.

Shaders and the Pipeline

A WebGL program consists of two shaders written in GLSL ES, the OpenGL ES Shading Language. Vertex processing computes positions and associated values; fragment processing determines colours and other outputs for rasterised fragments. A fragment is not necessarily one final displayed pixel. The page compiles both, links them into a program, places vertex data in buffers and binds textures. Each draw call sends the geometry through the vertex shader, the implementation assembles primitives such as triangles and rasterises them, and the fragment shader colours the resulting pixels into the framebuffer, which is the canvas or an offscreen target.

Because the GPU is a shared and sometimes unstable resource, a context can be lost, for example when a driver resets or the system switches graphics adapters. The browser fires a context lost event, all resources become invalid, and the page must rebuild them after the context restored event. Robust applications treat this as a normal event rather than an error.

Capabilities and Browser Implementations

Not every context offers the same features. WebGL 2 adds 3D textures, uniform buffer objects, transform feedback, instanced drawing and multiple render targets, several of which were optional extensions in WebGL 1. Extensions are requested with getExtension and vary by hardware: anisotropic filtering, compressed texture formats and floating-point textures are common examples, and MDN notes that some compressed formats appear only on particular devices. Limits such as maximum texture size are read with getParameter.

Browsers can map WebGL to a native graphics API through a layer such as ANGLE; the exact backend varies by platform and configuration. Software implementations can also provide rendering, but automatic fallback is not guaranteed. SwiftShader implements graphics on the CPU and can work beneath ANGLE. Chromium has deprecated automatic SwiftShader WebGL fallback for security reasons, so an unavailable hardware path can result in context creation failing. The performance-caveat attribute lets an application decline a context the browser considers seriously disadvantaged; it is not a universal software-renderer detector.

Implementations also differ in what they disclose. The WEBGL_debug_renderer_info extension exposes the driver's vendor and renderer strings for debugging. MDN states that, depending on privacy settings, the extension may be restricted to privileged contexts or unavailable, and that Firefox disables it when the resist-fingerprinting preference is on. The observable differences between implementations are the raw material of WebGL fingerprinting, which is a separate topic from the API itself.

WebGL vs Canvas 2D and WebGPU

Three canvas-based graphics APIs coexist in browsers.

AspectCanvas 2DWebGLWebGPU
Context name2dwebgl, webgl2webgpu
Programming modelImmediate drawing commands for paths, text and imagesProgrammable pipeline with GLSL shadersPipelines with WGSL shaders and explicit command encoding
ComputeNoneLimited, through rendering workaroundsFirst-class compute shaders
Based onBrowser-defined APIOpenGL ES 2.0 and 3.0Vulkan, Metal and Direct3D 12 concepts
AvailabilityBroadly supportedBroad support; runtime checks still neededLimited availability, not yet Baseline per MDN

Canvas 2D is the simplest: the page calls a fill or draw-image method and the browser does the rest, which suits charts, image editing and simple animation. WebGL trades that simplicity for control over the GPU. WebGPU, described by MDN as the successor to WebGL, maps to modern native APIs, adds compute pipelines for general-purpose GPU work such as machine learning, and lowers CPU overhead. The APIs coexist: a new WebGPU feature does not remove WebGL support or make an existing renderer obsolete. Library support, device coverage and workload matter more than choosing an API solely because it is newer.

Common Uses

WebGL powers 3D product viewers, browser games, map rendering, scientific and financial visualisation, and image or video processing where per-pixel work is parallelised on the GPU. Libraries such as three.js, Babylon.js and PixiJS can hide much of the raw graphics API. Some 2D libraries use WebGL under the surface to batch large numbers of sprites efficiently, with the actual gain depending on the scene.

Limitations and Considerations

WebGL output depends on the hardware and driver underneath, so the same scene can render with subtle differences in colour, precision or antialiasing on different machines, and performance ranges from smooth to unusable. Contexts can be lost at any time. The specification restricts what shaders can read, requires textures and buffers to be initialised, and forbids uploading cross-origin images as textures unless the server permits it through CORS, because otherwise a page could read pixels it is not allowed to see. These rules protect users but constrain developers working with third-party media. Finally, WebGL is a graphics API: it reports what the GPU can do so that pages can render correctly, and that same openness is why its parameters are measured in fingerprinting, a subject with its own glossary entry.

Frequently asked questions

Is WebGL the same as a GPU?

No. A GPU is hardware; WebGL is a JavaScript API through which a web page asks the browser to render graphics. The browser translates WebGL calls to the native graphics API and driver, which in turn use the GPU. On systems without a usable GPU, WebGL may still run through a CPU-based renderer.

Can WebGL work through software rendering?

It can, through a software graphics backend where the browser and configuration support one. That does not guarantee automatic fallback: Chromium has deprecated automatic SwiftShader WebGL fallback. Applications must handle a null context or context loss. failIfMajorPerformanceCaveat can reject a disadvantaged context, but does not identify every software backend.

How does WebGL differ from canvas 2D?

Canvas2D provides drawing operations for paths, text and images. WebGL asks the application to supply geometry, shaders and rendering state for a programmable pipeline. It offers more control for complex graphics, but does not automatically make every task faster; implementation, batching and scene complexity determine the result.

Is WebGL still supported now that WebGPU exists?

Yes. WebGL remains broadly available and is used by many graphics libraries. WebGPU offers a newer model with compute shaders and more explicit resource management, but support depends on browser, device and driver. Applications should check the needed capabilities at runtime and choose an appropriate fallback.

Can a website tell which graphics card I have through WebGL?

Sometimes. The WEBGL_debug_renderer_info extension can reveal the driver's vendor and renderer strings, and other capability queries hint at the hardware class. Browsers limit this: Firefox disables the extension under resist-fingerprinting, and it may be unavailable depending on privacy settings. Exact disclosure depends on browser and configuration.