Skip to content

@mapequation/d3gl

GPU-accelerated rendering for d3 — especially maps with GeoJSON and grid-cell data, with switchable SVG / Canvas2D / WebGL2 backends.

d3’s generators (d3-geo’s geoPath, d3-shape, d3-hierarchy links) don’t draw; they emit path commands to a context. d3gl implements that same context across SVG / Canvas2D / WebGL2, so any context-driven d3 generator can render on the GPU unchanged. Geometry is projected & tessellated once; pan/zoom is one transform-matrix uniform and recolor/show-hide is one texture write.

Terminal window
npm i @mapequation/d3gl
# React components also need: npm i react react-dom (optional peer deps)
import { Scene } from "@mapequation/d3gl"; // core (root entry)
import { geoMap, plot } from "@mapequation/d3gl/map"; // interactive engines
import { fitProjection } from "@mapequation/d3gl/geo"; // project-once primitives
import { D3GL } from "@mapequation/d3gl/react"; // React component
// also: /webgl /canvas /svg /labels
SubpathResponsibility
(root)Scene, PathContext, tessellation, stroke expansion, hit-testing
/canvasCanvas2D backend
/webglluma.gl v9 WebGL2 backend (palette-texture color, GPU picking, PNG export)
/svgSVG backend + vector export
/geoprojection + GeoJSON project-once helpers, inverse projection
/labelsHTML label overlay with culling
/mapgeoMap + plot engines with d3-zoom wiring
/reactheadless MapController + <D3GL> component

See the repository for full docs, examples, and the live demo.

MIT © Daniel Edler

ModuleDescription
canvasThe Canvas2D backend. CanvasBackend renders render layers to a 2D canvas context (honouring per-layer clipping and world/screen size modes), and CanvasContext is a passthrough core!PathContext that draws directly to a CanvasRenderingContext2D.
coreThe backend-agnostic scene model: a retained Scene that records vector geometry through a PathContext, flattens curves, groups rings, tessellates fills, expands strokes, and packs everything into GPU-ready buffers with per-drawable color/flag side-tables. HitIndex provides CPU hit-testing over the same data.
geoGeographic projection plus a project-once GeoJSON pipeline. fitProjection fits any d3 projection to a feature collection and viewport, featureGroup projects features into a core!Scene group exactly once, and viewTransform / lonLatFromScreen / referenceFromScreen map between screen, reference, and lon/lat space.
labelsHTML label overlay. LabelLayer keeps geometry on the GPU and renders only the visible labels into the DOM, positioning anchors (reference-space) under a core!ViewTransform; cullLabels does the viewport + collision culling.
mapThe high-level engines that wire a core!Scene, a backend, and d3-zoom together: geoMap (a project-once map engine over GeoJSON) and plot (a generic 2D engine driven by per-datum draw callbacks). createBackend selects the rendering backend by BackendType.
network-
reactReact bindings. The D3GL component mounts a canvas, builds it from initial groups + a core!ViewTransform, renders, and hands back the headless MapController; GeoMap wraps the project-once map engine.
svgThe SVG backend and publication-quality vector export. SvgBackend renders render layers as SVG, SvgPathContext is a core!PathContext that records path d strings, and svgDocument / svgFromLayers serialize geometry to a standalone SVG document.
webglThe luma.gl v9 WebGL2 backend. WebGLBackend draws render layers via a GroupRenderer (palette-texture color keyed by drawableId, a mat3 transform uniform, texture-write recolor), with GPU picking, clipFromView view math, and toPNG export.