canvas = {
const map = usAlbersCountiesGeo
const height = 600
const fillDefault = '#cdb4db'
const fillSelected = '#231942'
const ctx = DOM.context2d(width, height)
const scale = Math.max(2, window.devicePixelRatio)
ctx.canvas.style.width = `${width}px`
ctx.canvas.style.height = `${height}px`
ctx.canvas.width = width * scale
ctx.canvas.height = height * scale
ctx.scale(scale, scale)
const projection = d3.geoAlbersUsa()
.translate([width/2, height/2])
const path = d3.geoPath()
.projection(projection)
const mapData = map.features.map((d, i) => ({
d: path(d),
p: new Path2D(path(d)),
centroid: path.centroid(d),
fill: fillDefault,
stroke: 'white'
}))
const drawMap = (ctx) => {
for (const { p, fill, stroke } of mapData) {
ctx.fillStyle = fill
ctx.strokeStyle = stroke
ctx.stroke(p)
ctx.fill(p)
}
}
drawMap(ctx)
ctx.canvas.addEventListener("mousemove", (event) => {
mapData.forEach(({ p, fill, stroke }) => {
const isPointInPath = ctx.isPointInPath(p, event.offsetX*scale, event.offsetY*scale)
ctx.fillStyle = isPointInPath ? fillSelected : fill
ctx.stroke(p)
ctx.fill(p)
})
});
return ctx.canvas;
}