function hexgonTile(opts = {}) {
const { id, r, strokeWidth, spokes, drawBorder, background } = Object.assign(
{
r: 1,
strokeWidth: 1,
spokes: [0, 2, 4],
drawBorder: true,
background: false
},
opts
);
const vertices = 6;
const dAngle = (Math.PI * 2) / vertices;
const angleOffset = -dAngle / 2;
const points = d3.range(6).map((i) => {
const angle = i * dAngle;
return [
r * Math.cos(angle + angleOffset),
r * Math.sin(angle + angleOffset)
];
});
const [x0, y0] = [0, 0];
function drawSpokes() {
return points
.map((p, i) => {
if (spokes.includes(i)) {
return svg`<line x1=${x0} y1=${y0} x2=${p[0]} y2=${p[1]}></line>`;
}
})
.filter(Boolean);
}
return svg`<g
id=${id}
stroke="${palette.fg}"
stroke-width=${strokeWidth}
stroke-linecap="round"
>
<path
d="${d3.line()(points)}z"
fill=${background ? palette.bg : "none"}
stroke=${drawBorder ? palette.fg : "none"}
></path>
${drawSpokes()}
</g>`;
}