Public
Edited
Apr 11, 2023
7 stars
Insert cell
Insert cell
{
const context = DOM.context2d(width, height);

function redraw(transform) {
context.clearRect(0, 0, width, height);
context.save();
if (transform) {
context.translate(transform.x, transform.y);
context.scale(transform.k, transform.k);
}
context.beginPath();
context.stroke(path2D);
context.restore();
}

d3.select(context.canvas).call(
d3
.zoom()
.scaleExtent([0.1, 8])
.on("zoom", function(evt) {
redraw(evt.transform);
})
);

path(topojson.mesh(us)); // Draw the mesh to the path2D object
redraw();

return context.canvas;
}
Insert cell
path2D = new Path2D() //To precompute the path
Insert cell
path = d3.geoPath().context(path2D)
Insert cell
us = d3.json("https://unpkg.com/us-atlas@1/us/10m.json")
Insert cell
height = 600
Insert cell
d3 = require("d3@6")
Insert cell
topojson = require("topojson-client@3")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more