gl lets you create a WebGL context in Node.js without making a window or loading a full browser environment.
It aspires to fully conform to the WebGL 1.0.3 specification. On Linux and macOS, it passes 617 (99.5%) of the 620 tests in the WebGL 1.0.3 conformance suite that can run outside a browser. The other three need features that headless-gl or Node don't have. The suite's remaining 55 tests need a browser, for example for image and video elements, workers or a page to show the canvas on.
// Create context
const createGL = require('gl')
const width = 64
const height = 64
const gl = createGL(width, height)
//Clear screen to red
gl.clearColor(1, 0, 0, 1)
gl.clear(gl.COLOR_BUFFER_BIT)
//Write output as a PPM formatted image
const pixels = new Uint8Array(width * height * 4)
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
process.stdout.write(['P3\n# gl.ppm\n', width, " ", height, '\n255\n'].join(''))
for(let i = 0; i < pixels.length; i += 4) {
for(let j = 0; j < 3; ++j) {
process.stdout.write(pixels[i + j] + ' ')
}
}Installing headless-gl on a supported platform is a snap using one of the prebuilt binaries. Using npm, run the command
npm install gl
And you are good to go!
npm 12 and later don't run the install scripts of dependencies you haven't approved, and gl uses its install script to download a prebuilt binary or build one from source. If require('gl') fails with "Could not locate the bindings file", approve the script and rebuild:
npm install-scripts approve gl
npm rebuild gl
Prebuilt binaries are generally available for LTS Node.js versions (e.g. 22, 24, 26) until they reach EOL. If your system is not supported, then please see the system dependencies section on how to configure your build environment. Patches to improve support are always welcome!
When gl builds from source, it downloads the ANGLE libraries for your platform from this repository's GitHub releases, since the npm package doesn't include them. To build without access to GitHub, set HEADLESS_GL_ANGLE_MIRROR to a URL or a local folder with the same .gz files.
gl runs on Linux (including Alpine), macOS, and Windows, on x64 and arm64.
Node.js versions 22 and up are tested and supported.
headless-gl exports a function which you can use to create a WebGL context with the given context attributes, and createCanvas, which creates a canvas whose getContext creates one, as in browsers:
const createGL = require('gl')
const gl = createGL(width, height)widthis the width of the drawing bufferheightis the height of the drawing buffercontextAttributesis an optional object whose properties are the WebGL context attributes. Onlyalpha,depthandstencilchange the drawing buffer, andantialiasis always off. Nothing composites the drawing buffer in Node, so it's always preserved whateverpreserveDrawingBufferis.premultipliedAlphasays whethergl.canvas.toDataURL()takes the drawing buffer's colors to be premultiplied by alpha. The rest only change whatgetContextAttributesreports.optionsis an optional object whosecontextTypeproperty is the type of context to create, asgetContexttakes it:'webgl', the default, for WebGL1, or'webgl2'for WebGL2. Itsbackendproperty chooses what renders the context, as Backends describes.
If width or height is larger than the GPU allows, the drawing buffer is made as large as it allows, as in browsers, and gl.drawingBufferWidth and gl.drawingBufferHeight give its size. The STACKGL_resize_drawingbuffer extension does the same.
Every context has a canvas, as in browsers, which Canvases describes.
Returns A new WebGLRenderingContext object, or a WebGL2RenderingContext when options.contextType is 'webgl2'.
Throws an Error with the code HEADLESS_GL_CONTEXT_CREATION_FAILED if the context can't be created, for example when a library that ANGLE needs isn't installed, or the backend can't work on this machine. The error message gives the reason, such as the name of the missing library. Throws a RangeError if width or height is less than 1, and a TypeError if options.contextType isn't 'webgl' or 'webgl2', or the backend isn't one of the backends.
The function also has WebGLRenderingContext and WebGL2RenderingContext properties, the classes of the contexts it creates, which you can also import by name. Use them with instanceof to check a context's version:
const { WebGL2RenderingContext } = require('gl')
// or: import createGL, { WebGL2RenderingContext } from 'gl'
gl instanceof WebGL2RenderingContextThey can't be constructed directly.
createCanvas(width, height, options) creates a canvas without a context, for code that calls getContext itself, as code written for browsers, and libraries that expect a browser's canvas, do. options is optional, and its backend property chooses what renders the context, as Backends describes:
const { createCanvas } = require('gl')
// or: import { createCanvas } from 'gl'
const canvas = createCanvas(256, 256)
const gl = canvas.getContext('webgl2', { alpha: false })getContext('webgl'), or the legacy'experimental-webgl', creates a WebGL1 context with the context attributes it's given, andgetContext('webgl2')a WebGL2 one. Later calls return the same context, ornullfor the other version, and other context ids givenull, as in browsers.- When the context can't be created,
getContextfires awebglcontextcreationerrorevent at the canvas, whosestatusMessagegives the reason, and returnsnull, as browsers do, wherecreateGLthrows. The event's class is exported asWebGLContextEvent. - As on an
OffscreenCanvas,widthandheightare required, and a size that isn't an integer from 0 up throws aTypeError. A canvas 0 wide or high has a 1Γ1 drawing buffer, whilecreateGLneeds at least 1Γ1. - The canvas's class is exported as
HeadlessCanvas, forinstanceofchecks, andnew HeadlessCanvas(width, height, options)is the same ascreateCanvas(width, height, options).
Every context has a canvas, which gl.canvas returns, including contexts from createGL:
- Setting its
widthorheightresizes the drawing buffer and clears it, even when the size doesn't change. toDataURL()encodes the drawing buffer as a PNG data URL, with the top row first and alpha not premultiplied. Other image types give a PNG too, as browsers do for types they don't support. A canvas without a context gives a transparent image, and one 0 wide or high gives"data:,".toBlob(callback)calls back with the same PNG in aBlob, as a<canvas>does, andconvertToBlob()resolves to one, as anOffscreenCanvasdoes. Both take the canvas as it is when they're called and compress it without blocking the event loop, whichtoDataURLblocks. A canvas 0 wide or high calls back withnullor rejects with anIndexSizeError.- Texture uploads take the canvas as an image of its drawing buffer, as browsers do, and so does another library's canvas whose
getContextreturns a headless-gl context. Either can be another context's canvas or the uploading context's own. - Its
styleis an object that nothing reads, for code that styles a canvas as it resizes it, as three.js'ssetSizedoes. - Assigning
gl.canvasreplaces what it returns, for libraries that wrap a context in a canvas of their own. - Posting it to a worker throws a
DataCloneError, in Node 22.10 and later, since a context can't move to another thread. Create the canvas in the worker instead. - Its
[Symbol.dispose]()destroys its context, asSTACKGL_destroy_contextdoes, sousing canvas = createCanvas(width, height)frees the context when the block ends, in Node versions withusingdeclarations (24 and later). Browsers' canvases don't have it.
headless-gl includes TypeScript types for its contexts, so projects don't need TypeScript's DOM types. They follow the DOM's WebGL types, except that a context's canvas is headless-gl's own, the contexts have no color space members, and texture uploads take an object with pixel data, like ImageData, an object with a 2D context, like a node-canvas Canvas, or a headless-gl canvas, instead of DOM elements.
To pass a context to code that expects the DOM's WebGLRenderingContext, such as three.js, cast it with gl as WebGLRenderingContext. That's why gl.canvas is typed without convertToBlob, which a DOM <canvas> doesn't have; cast it to HeadlessCanvas to call that. To type every context as the DOM's, add this to your project:
declare module 'gl' {
interface CustomContextTypes {
WebGLRenderingContext: globalThis.WebGLRenderingContext
WebGL2RenderingContext: globalThis.WebGL2RenderingContext
}
}Both need the DOM types in your project.
In addition to all the usual WebGL methods, headless-gl exposes some custom extensions to make it easier to manage WebGL context resources in a server side environment:
This extension provides a mechanism to resize the drawing buffer of a WebGL context once it is created.
It sets the canvas's size, like setting gl.canvas.width and gl.canvas.height, except that it only clears the drawing buffer when its size changes, and throws for a width or height below 1.
const assert = require('assert')
const gl = require('gl')(10, 10)
assert(gl.drawingBufferHeight === 10 && gl.drawingBufferWidth === 10)
const ext = gl.getExtension('STACKGL_resize_drawingbuffer')
ext.resize(20, 5)
assert(gl.drawingBufferWidth === 20 && gl.drawingBufferHeight === 5)[NoInterfaceObject]
interface STACKGL_resize_drawingbuffer {
void resize(GLint width, GLint height);
};
Resizes the drawing buffer of a WebGL rendering context
widthis the new width of the drawing buffer for the contextheightis the new height of the drawing buffer for the context
Destroys the WebGL context immediately, reclaiming all resources associated with it.
For long-running jobs, garbage collection of contexts is often not fast enough. To prevent the system from becoming overloaded with unused contexts, you can force the system to reclaim a WebGL context immediately by calling .destroy().
const gl = require('gl')(10, 10)
const ext = gl.getExtension('STACKGL_destroy_context')
ext.destroy()[NoInterfaceObject]
interface STACKGL_destroy_context {
void destroy();
};
Immediately destroys the context and all associated resources.
To create a WebGL2 context, set the contextType property to 'webgl2' in the options argument:
const gl = createGL(width, height, {}, { contextType: 'webgl2' })Every WebGL2 method is implemented, but WebGL2 hasn't been tested against the WebGL 2.0 conformance suite yet.
Unlike in browsers, query results and sync objects are ready as soon as the GPU finishes, which can be before control returns to the event loop, and clientWaitSync can wait for up to MAX_CLIENT_WAIT_TIMEOUT_WEBGL nanoseconds, which is Number.MAX_SAFE_INTEGER. Code that waits the way browsers require, by checking again in a later callback, works the same.
headless-gl renders with ANGLE, which runs WebGL on one of several graphics APIs, its backends. The backend option chooses one, in createGL's options or createCanvas's:
const gl = createGL(width, height, {}, { backend: 'swiftshader' })
const canvas = createCanvas(width, height, { backend: 'swiftshader' })backend |
Renders with | Where |
|---|---|---|
'metal' |
Metal | macOS |
'opengl' |
the system's OpenGL | macOS, and Linux with an X server |
'd3d11' |
Direct3D 11 | Windows |
'swiftshader' |
SwiftShader, on the CPU, with no GPU or X server | Linux |
'auto' |
the first of these that works | everywhere |
'auto' tries the system's own backends, then SwiftShader where the build includes it:
- macOS: Metal, then OpenGL, for Macs without Metal.
- Linux: OpenGL, when the
DISPLAYenvironment variable is set and OpenGL works there, then SwiftShader. - Windows: Direct3D 11.
When the backend option is undefined, the HEADLESS_GL_BACKEND environment variable chooses, as it is when the canvas is created, which sets the backend for code that creates contexts itself, such as another library's tests. When the variable is unset or empty, it's 'auto'. A backend option, 'auto' included, ignores the variable.
A value that isn't one of these, from the option or the variable, throws a TypeError. A backend that can't work on this machine makes creating the context fail: createGL throws an Error with the code HEADLESS_GL_CONTEXT_CREATION_FAILED, and getContext returns null, with a message that lists the backends this build supports.
Backends differ in their limits, their extensions and the exact pixels of some operations, so choose one explicitly where results have to match from machine to machine, such as on servers, in CI and for pixel snapshots. In our tests, SwiftShader gave the same pixels on every machine with the same CPU architecture, on glibc and on musl, but slightly different ones on x64 and arm64. Its limits are lower than most GPUs': textures and renderbuffers up to 8192 pixels, 6 draw buffers and 1-pixel-wide lines, and it has no WEBGL_provoking_vertex.
A context's renderer string tells which backend it got:
const info = gl.getExtension('WEBGL_debug_renderer_info')
gl.getParameter(info.UNMASKED_RENDERER_WEBGL)
// 'ANGLE (Apple, ANGLE Metal Renderer: Apple M2 Pro, Unspecified Version)'
// 'ANGLE (Google, Vulkan 1.3.0 (SwiftShader Device (Subzero) (0x0000C0DE)), SwiftShader driver)'Contexts on different backends work side by side in one process.
Builds for macOS and Windows don't include SwiftShader, but copying its files next to the addon, in build/Release, adds it: libvk_swiftshader.dylib and vk_swiftshader_icd.json on macOS, and vk_swiftshader.dll, vk_swiftshader_icd.json and vulkan-1.dll on Windows, which is untested. Electron ships them. Rebuilding or reinstalling headless-gl replaces build/Release, so copy them again afterwards.
In most cases installing headless-gl from npm should just work. However, if you run into problems you might need to adjust your system configuration and make sure all your dependencies are up to date. For general information on building native modules, see the node-gyp documentation.
Prebuilt binaries (arm64 and x64) need macOS 12 or later, and nothing else. To build from source, install Python 3 and the Xcode Command Line Tools, with xcode-select --install.
Prebuilt binaries for glibc 2.34 and later (x64 and arm64), such as Debian 12, Ubuntu 22.04, RHEL 9 and Amazon Linux 2023, need these libraries at runtime:
$ sudo apt-get install -y libx11-6 libxext6 libxcb1
On RHEL, Fedora and Amazon Linux, install libX11 libXext libxcb with dnf. If one is missing, createGL throws an error naming the library it couldn't load.
To build from source, also install build-essential and python3. For headless use, see below.
Prebuilt binaries for Alpine 3.22 and later (x64 and arm64) need libX11 at runtime:
$ apk add libx11
To build from source, also install build-base and python3. SwiftShader renders without an X server, so headless use needs nothing else. The 'opengl' backend needs an X server and an OpenGL driver (see below), such as Xvfb and Mesa, with apk add xvfb-run mesa-gl mesa-dri-gallium.
Prebuilt binaries (x64 and arm64) need nothing else. ANGLE's Direct3D 11 backend compiles shaders with d3dcompiler_47.dll, which Windows 10 and later include.
To build from source, install Python 3 and Visual Studio with the "Desktop development with C++" workload, and on arm64 its ARM64 build tools.
headless-gl should work out of the box, as it does on GitHub Actions' Linux, macOS and Windows runners. On Linux without an X server, it renders with SwiftShader. To test on Linux's OpenGL backend instead, run under Xvfb with Mesa, as described below. For an example, see the ci workflow, which runs this repository's tests on GitHub Actions on both backends.
On a Linux machine without an X server, such as a cloud instance or a container, headless-gl renders with SwiftShader, on the CPU, which the Linux builds include. It needs only the runtime libraries listed under System dependencies.
The 'opengl' backend, which renders with the system's OpenGL, as headless-gl 9 and earlier did, needs an X server and an OpenGL driver, which such a machine usually doesn't have. Two packages provide them:
- Xvfb is a lightweight X11 server which provides a back buffer for displaying X11 applications offscreen and reading back the pixels which were drawn offscreen. It is typically used in continuous integration systems.
- Mesa is the reference open source implementation of OpenGL. Its llvmpipe driver renders in software, which a machine without a GPU needs.
Installing Xvfb installs Mesa too:
- Debian and Ubuntu:
apt-get install -y xvfb xauth - RHEL, Fedora and Amazon Linux:
dnf install -y xorg-x11-server-Xvfb - Alpine:
apk add xvfb-run mesa-gl mesa-dri-gallium
Interacting with Xvfb requires you to start it in the background and to execute your node program with the DISPLAY environment variable set to whatever was configured when running Xvfb (the default being :99). If you want to do that reliably you'll have to start Xvfb as a service at boot time, which is extra configuration burden. Fortunately there is a wrapper script shipped with Xvfb known as xvfb-run which can start Xvfb on the fly, execute your Node.js program and finally shut Xvfb down. Here's how to run it:
xvfb-run -s "-ac -screen 0 1280x1024x24" <node program>
In a Docker container, start the container with docker run --init, or init: true in Docker Compose, since xvfb-run waits forever when it's the container's first process.
Yes, with browserify. The STACKGL_destroy_context and STACKGL_resize_drawingbuffer extensions are emulated as well.
In a browser, createGL creates a canvas, or an OffscreenCanvas in a worker, and calls its getContext, and throws the same errors as in Node. The reason in the error message comes from the browser, which often gives little or no detail.
createCanvas returns the same kind of canvas without a context, and WebGLContextEvent is the browser's. These are the browser's own canvases, not HeadlessCanvases, which the browser build doesn't have. So a <canvas> has no convertToBlob, and an OffscreenCanvas has no toDataURL or toBlob. Their sizes follow the browser's rules, so an invalid size becomes 0 on a <canvas>.
They aren't. Texture uploads take a typed array, an object with pixel data like ImageData, an object with a 2D context like a node-canvas Canvas, or a canvas with a headless-gl context. The RGBA pixels of the last three are converted to the upload's format and type, as browsers convert images. In WebGL2 contexts, an upload's width and height, with UNPACK_SKIP_PIXELS and UNPACK_SKIP_ROWS, select a rectangle of the image, and 3D uploads take slices of it, as in browsers. To help reading and saving images, you should check out the following modules:
As in browsers, texImage2D, texSubImage2D, texImage3D, texSubImage3D and readPixels take pixel data in the kind of typed array that their type needs, and set INVALID_OPERATION for any other kind, or for a DataView:
type |
Typed array |
|---|---|
UNSIGNED_BYTE |
Uint8Array, such as a Node Buffer, or Uint8ClampedArray |
FLOAT |
Float32Array |
HALF_FLOAT and HALF_FLOAT_OES |
Uint16Array, with each half float's 16 bits |
UNSIGNED_SHORT and UNSIGNED_SHORT_* |
Uint16Array |
UNSIGNED_INT and UNSIGNED_INT_* |
Uint32Array |
BYTE, SHORT and INT (WebGL2) |
Int8Array, Int16Array and Int32Array |
Anything else, such as an ArrayBuffer or a plain array, throws a TypeError. The exception is WebGL2's forms without a srcOffset or dstOffset argument, which convert it to a byte offset into the bound pixel buffer.
Supported extensions by WebGL version. "Built in" means WebGL2 has the feature itself, so getExtension returns null for that extension on a WebGL2 context.
* Not on macOS, where ANGLE's OpenGL backend has no BPTC textures.
Float textures are built into WebGL2, but rendering to them still needs EXT_color_buffer_float, or EXT_color_buffer_half_float for 16-bit float textures.
After you have your system dependencies installed, do the following:
- Clone this repo:
git clone git@github.com:stackgl/headless-gl.git - Switch to the headless gl directory:
cd headless-gl - Install npm dependencies:
npm install - Run node-gyp to generate build scripts:
npm run rebuild
Once this is done, you should be good to go! A few more things:
- To run the test cases, use the command
npm test, or execute specific tests by just running them usingnode. - On a Unix-like platform, you can do incremental rebuilds by going into the
build/directory and runningmake. This is way faster than runningnpm run rebuildeach time you make a change. webgl.d.tsholds WebGL declarations copied from TypeScript's DOM types. After updating thetypescriptdev dependency, runnode scripts/webgl-types.jsto copy them again.- When you replace the ANGLE libraries in
deps/, runnode scripts/angle-assets.js <release tag> <folder>with a new release tag, and commit the updatedsrc/angle.jsonwith the libraries. When the change reaches master, the ANGLE release workflow creates that release for source builds to download from.
Note: the @stackgl/gl-conformance dev dependency is hosted on GitHub Packages, which needs a token to install any package, even a public one. Create a personal access token (classic) with the read:packages scope, and add it to the .npmrc file in your home directory:
//npm.pkg.github.com/:_authToken=<your token>
To debug with RenderDoc, you can set the RENDERDOC_ENABLED define in the src/native/webgl.cc file.
You'll need to also install RenderDoc and attach it to the gl process when you launch your program. The easiest way to attach to the running process is to uncomment the MessageBoxA call in the src/native/webgl.cc file.
See LICENSES