How do I make a hexagon world map as SVG?
Create a DottedMap from @setkernel/dotted-map-next and call getSVG({ shape: 'hexagon' }): it returns the world as an SVG string of hexagons. Pass countries to the map for one country, and radius to getSVG() to size the hexagons.
npm install @setkernel/dotted-map-next
The world in hexagons
height is the number of rows of the grid. The map's
title is what a screen reader says: without one, the map is
hidden from it as decoration.
world.mjsimport { DottedMap } from '@setkernel/dotted-map-next';
const map = new DottedMap({ height: 40 });
const svg = map.getSVG({ shape: 'hexagon', title: 'The world in hexagons' });
world.mjs when this page was built.One country
countries takes ISO 3166-1 alpha-3 codes, such as
'AUS', and the grid fits what you asked for. At a
radius of 0.5, the hexagons touch edge to edge.
country.mjsimport { DottedMap } from '@setkernel/dotted-map-next';
const map = new DottedMap({ height: 40, countries: ['AUS'] });
const svg = map.getSVG({
shape: 'hexagon',
radius: 0.5,
title: 'Australia in hexagons',
});
country.mjs when this page was built.What radius means for a hexagon
radius is in grid units: one unit is the distance between two
neighbouring dots in a row. For a hexagon it is the distance from the centre
to the middle of a side, so a hexagon is 2 × radius wide and
about 2.31 × radius tall, and wraps exactly around the circle of
the same radius.
-
0.3, the default, leaves a gap of 0.4 between two hexagons in a row. -
0.5makes them tile edge to edge on the diagonal grid, the default grid. -
A pin on a hexagon map is a hexagon too, and its
svgOptions.radiusmeans the same.
Coming from 1.x, which drew a hexagon √3 times as large for the same
radius: multiply the old radius by 1.732 to keep the
same picture.