gl lets you create a WebGL 2 context in Node.js without making a window or loading a full browser environment.
It is built on ANGLE, the same graphics abstraction layer used by Chrome, using the Metal backend on macOS and SwiftShader (software Vulkan) for headless/CI environments.
const createGL = require('gl')
const width = 64
const height = 64
const gl = createGL(width, height, { createWebGL2Context: true })
// Clear screen to red
gl.clearColor(1, 0, 0, 1)
gl.clear(gl.COLOR_BUFFER_BIT)
// Read pixels
const pixels = new Uint8Array(width * height * 4)
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
// Clean up
gl.getExtension('STACKGL_destroy_context').destroy()npm install gl
const createGL = require('gl')
const gl = createGL(width, height, contextAttributes)widthβ width of the drawing bufferheightβ height of the drawing buffercontextAttributesβ optional object:createWebGL2Contextβ set totruefor a WebGL 2 context (default:false)alpha,depth,stencil,premultipliedAlpha,preserveDrawingBufferβ standard WebGL context attributesuseSwiftShaderβ force software rendering via SwiftShader (default:false, also settable viaUSE_SWIFTSHADER=1env var)
Returns a WebGL rendering context backed by ANGLE, or null on failure.
All extensions supported by ANGLE are available via gl.getExtension(). Additionally, two custom extensions are provided:
Destroys the context and reclaims all resources immediately.
gl.getExtension('STACKGL_destroy_context').destroy()Updates drawingBufferWidth/drawingBufferHeight and the viewport.
gl.getExtension('STACKGL_resize_drawingbuffer').resize(newWidth, newHeight)- macOS: Metal (via ANGLE)
- Linux/Windows: Native GPU or SwiftShader (software Vulkan)
- SwiftShader: Force with
useSwiftShader: trueorUSE_SWIFTSHADER=1env var. Useful for CI/headless environments without a GPU.
In most cases npm install gl should just work using prebuilt binaries.
For building from source:
- macOS: Xcode, Python 3
- Linux:
build-essential, Python 3 - Windows: Visual Studio, Python 3
- Clone:
git clone <repo-url> - Install:
npm install - Build:
npm run rebuild - Test:
npx tape test/*.js
See LICENSES