Skip to main content

External WebGL Contexts with MapLibre

This guide shows how to connect luma.gl to a WebGL context that is created and owned by MapLibre GL JS. It uses the webgl2Adapter.attach API to wrap the map's context in a WebGLDevice and keep a WebGLCanvasContext synchronized with MapLibre's canvas.

Install dependencies​

Add MapLibre GL JS alongside luma.gl:

npm install @luma.gl/core @luma.gl/engine maplibre-gl

This example uses OpenFreeMap basemaps so you do not need an API token, but you can still supply a Mapbox token if you point MapLibre at a Mapbox-hosted style elsewhere in your application.

Attach luma.gl to MapLibre​

Configure the MapLibre worker before creating the map. This example uses a Vite worker URL import. For other bundlers, serve the MapLibre worker module and its shared module together and pass the worker URL to setWorkerUrl. Then attach the luma.gl device to the WebGL context created by the map.

import {Map, setWorkerUrl} from 'maplibre-gl'
import maplibreWorkerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url'
import {Matrix4} from '@math.gl/core/matrix'
import {webgl2Adapter} from '@luma.gl/core/webgl'
import {Model} from '@luma.gl/engine'

setWorkerUrl(maplibreWorkerUrl)

const map = new Map({
container: 'map',
style: 'https://tiles.openfreemap.org/styles/positron',
canvasContextAttributes: {antialias: true},
pitch: 60,
zoom: 12
})

map.on('load', async () => {
const webglContext = map.getCanvas().getContext('webgl2') as WebGL2RenderingContext
const device = await webgl2Adapter.attach(webglContext, {
createCanvasContext: {autoResize: false}
})

// Keep the WebGLCanvasContext aligned with MapLibre's drawing buffer
device.canvasContext.resize({width: webglContext.drawingBufferWidth, height: webglContext.drawingBufferHeight})

const modelMatrix = new Matrix4()
const viewProjection = new Matrix4()

const overlay = new Model(device, {
id: 'maplibre-overlay',
vs: `...`,
fs: `...`,
shaderLayout: {
attributes: [
{name: 'positions', location: 0, format: 'float32x3'}
],
bindings: [{name: 'app', type: 'uniform', location: 0}]
},
attributes: {
positions: new Float32Array([...])
},
vertexCount: 6,
bindings: {
app: /* uniform buffer */
}
})

map.addLayer({
id: 'luma-gl-overlay',
type: 'custom',
renderingMode: '3d',
render: (_, matrix) => {
viewProjection.fromArray(matrix as number[])
// Update uniforms and draw without clearing the map's buffers
const renderPass = device.beginRenderPass({clearColor: false, clearDepth: false})
overlay.draw(renderPass)
renderPass.end()
map.triggerRepaint()
}
})
})

Handle map resizes​

Because the context comes from MapLibre, luma.gl cannot resize it automatically. Listen for MapLibre resize events and keep the WebGLCanvasContext in sync:

map.on('resize', () => {
const webglContext = map.getCanvas().getContext('webgl2') as WebGL2RenderingContext
device.canvasContext.resize({
width: webglContext.drawingBufferWidth,
height: webglContext.drawingBufferHeight
})
})

If you are matching an overlaid map canvas such as deck.gl MapboxOverlay or a MapLibre custom overlay that still sizes its drawing buffer with legacy Math.round(css * devicePixelRatio) logic, opt into compatibility sizing when creating the canvas context:

const device = await webgl2Adapter.attach(webglContext, {
createCanvasContext: {
autoResize: false,
pixelSizeSource: 'css-dpr'
}
})

Keep the default 'exact' mode when you want luma.gl to track the browser's precise device-pixel content box.

See it in action​

The revived External WebGL Context example renders a luma.gl overlay driven by MapLibre's WebGL render loop and uses the webgl2Adapter.attach API to keep both frameworks in sync.