Hex Grid
Vanduo Hex Grid is a standalone, canvas-rendered axial hex grid, installed separately from the framework. Pan, zoom, select hexes, and attach terrain or custom data. It reads Vanduo theme tokens, ships a pure @vanduo-oss/vd3-cbun/hex-grid/hex-math subexport, and an optional Vue 3 binding (@vanduo-oss/vd3-cbun/hex-grid) used here.
Grid Controls
30px
15
10
0°
Terrain & pathfinding
API Reference
Install
pnpm add @vanduo-oss/vd3-cbunUsage
<script setup lang="ts">
import { ref } from 'vue';
import { VdHexGrid } from '@vanduo-oss/vd3-cbun/hex-grid';
import { TerrainType } from '@vanduo-oss/vd3-cbun/hex-grid/hex-math';
const size = ref(30);
const width = ref(15);
const height = ref(10);
const rotationDeg = ref(0);
let grid = null;
const onReady = (instance) => { grid = instance; };
const onSelect = (hex) => console.log('Selected', hex.q, hex.r);
</script>
<template>
<div style="height: 420px;">
<VdHexGrid
:size="size"
:width="width"
:height="height"
:rotation="rotationDeg * Math.PI / 180"
@ready="onReady"
@select="onSelect"
@zoom="({ scale }) => console.log('Zoom', scale)"
/>
</div>
</template>Component API
| Prop / event | Description |
|---|---|
:size | Hexagon size in px (default 30). |
:width / :height | Grid columns / rows in hexes (default 10). |
:rotation | Grid rotation in radians (default 0). |
@select / @zoom / @pan | Forwarded interaction events. |
@ready | Emitted once with the underlying VdHexGrid instance. |
getInstance() | Template ref expose — call imperative API methods. |
Events
| Event | Description |
|---|---|
select | A hex was clicked; payload is the hex (with q, r). |
zoom | Zoom changed; payload { scale }. |
pan | Pan reset; payload { x, y } (emitted on resetView). |
Pure math (/hex-math)
Axial coordinate math and terrain tables, importable without a canvas or the DOM — handy for game logic, tests, or Node.
// Pure axial math — no canvas, no DOM, no Vue.
import { hexDistance, getAdjacentHexes, TerrainType } from '@vanduo-oss/vd3-cbun/hex-grid/hex-math';
hexDistance(0, 0, 2, -1); // → 2
getAdjacentHexes(0, 0); // → 6 neighbors