Published
Edited
Feb 1, 2019
2 forks
21 stars
Insert cell
Insert cell
Insert cell
{
const offscreenContext = DOM.context2d(width, height, 1);
const offscreenPath = d3.geoPath(projection, offscreenContext);
const context = DOM.context2d(width, height);
const path = d3.geoPath(projection, context);

// Initialize the context’s path with the desired boundary.
offscreenContext.beginPath();
offscreenPath(topojson.feature(world, world.objects.land));

// Iterate over the grid and test whether points are inside.
for (let j = 0, y = j * dy; y < height + r; y += dy, ++j) {
for (let x = (j & 1) * dx / 2; x < width + dx / 2; x += dx) {
if (offscreenContext.isPointInPath(x, y)) {
context.beginPath();
hexagonPath(context, x, y, r);
context.fill();
}
}
yield context.canvas;
}
}
Insert cell
function hexagonPath(context, x, y, r) {
context.moveTo(x, y - r);
for (let a = 1; a < 6; ++a) {
const angle = a * Math.PI / 3;
context.lineTo(x + Math.sin(angle) * r, y - Math.cos(angle) * r);
}
context.closePath();
}
Insert cell
Insert cell
cells = {
const offscreenContext = DOM.context2d(width, height, 1);
const offscreenPath = d3.geoPath(projection, offscreenContext);

// Initialize the context’s path with the desired boundary.
offscreenContext.beginPath();
offscreenPath(topojson.feature(world, world.objects.land));

// Iterate over the grid and test whether points are inside.
const rings = [];
for (let j = 0, y = j * dy; y < height + r; y += dy, ++j) {
for (let x = (j & 1) * dx / 2; x < width + dx / 2; x += dx) {
if (offscreenContext.isPointInPath(x, y)) {
rings.push(hexagonPoints(x, y, r));
}
}
}

return {
type: "MultiPolygon",
coordinates: rings.map(ring => [ring])
};
}
Insert cell
function hexagonPoints(x, y, r) {
const points = [[x, y - r]];
for (let a = 1; a < 6; ++a) {
const angle = a * Math.PI / 3;
points.push([x + Math.sin(angle) * r, y - Math.cos(angle) * r]);
}
points.push([x, y - r]);
return points;
}
Insert cell
Insert cell
{
const context = DOM.context2d(width, height);
const path = d3.geoPath(null, context);
context.beginPath();
path(cells);
context.fillStyle = "#eee";
context.fill();
context.stroke();
return context.canvas;
}
Insert cell
Insert cell
topology = topojson.topology({cells}, 1e6)
Insert cell
Insert cell
merge = topojson.merge(topology, [topology.objects.cells])
Insert cell
{
const context = DOM.context2d(width, height);
const path = d3.geoPath(null, context);
context.beginPath();
path(merge);
context.fillStyle = "#eee";
context.fill();
context.stroke();
return context.canvas;
}
Insert cell
Insert cell
Insert cell
Insert cell
height = {
const [[minX, minY], [maxX, maxY]] = d3.geoPath(projection).bounds({type: "Sphere"});
return Math.round(maxY - minY + dy);
}
Insert cell
projection = d3.geoEqualEarth().fitWidth(width, {type: "Sphere"})
Insert cell
land = topojson.feature(world, world.objects.land)
Insert cell
world = d3.json("https://cdn.jsdelivr.net/npm/world-atlas@1/world/110m.json")
Insert cell
topojson = require("topojson-server@3", "topojson-client@3")
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more