@vanduo-oss/hex-grid
v1.1.0
Published
Standalone VdHexGrid package for canvas-based hex grids
Maintainers
Readme
@vanduo-oss/hex-grid
Standalone VdHexGrid package for canvas-based hex grids.
Install
pnpm add @vanduo-oss/hex-gridUsage
import { VdHexGrid } from '@vanduo-oss/hex-grid';
const grid = new VdHexGrid({
element: document.getElementById('hex-demo-container'),
canvas: document.getElementById('hex-demo'),
size: 30,
width: 15,
height: 10,
rotation: 0
});Optional Utility Import
import { hexToPixel, pixelToHex, hexDistance } from '@vanduo-oss/hex-grid/hex-math';API
Constructor
new VdHexGrid({ element, canvas, size, width, height, rotation })| Option | Type | Default | Description |
|--------|------|---------|-------------|
| element | HTMLElement | required | Container element |
| canvas | HTMLCanvasElement | auto-created | Canvas element |
| size | number | 30 | Hex radius in pixels |
| width | number | 10 | Grid width in hexes |
| height | number | 10 | Grid height in hexes |
| rotation | number | 0 | Grid rotation in radians |
Methods
on(event, callback)— Subscribe to events (select,zoom,pan)setSize(size)— Change hex radiussetDimensions(width, height)— Change grid dimensionssetRotation(rotation)— Rotate the gridreset()— Reset to defaultsresetView()— Reset pan and zoomzoomIn()/zoomOut()— Adjust zoomfillRandom()— Fill hexes with random colorsgetHex(q, r)— Get a hex by coordinatesgetAllHexes()— Get all hexessetHexFill(q, r, color)— Set hex fill colorsetHexTerrain(q, r, terrainType)— Set hex terraingetHexTerrain(q, r)— Get hex terraingenerateRandomTerrain()— Randomize all terrainsgetValidMoves(q, r, movementPoints)— Get reachable hexesgetPath(startQ, startR, endQ, endR)— BFS pathfindinghexDistance(q1, r1, q2, r2)— Distance between hexessetHexData(q, r, data)/getHexData(q, r)/clearHexData(q, r)— Custom dataexportTerrainData()/importTerrainData(data)— SerializationsetCustomRender(callback)/clearCustomRender()— Per-hex rendering hooks
Terrain Types
GRASSLAND, PLAINS, DESERT, TUNDRA, SNOW, MOUNTAIN, OCEAN, COAST
Hex Math Utilities
import {
hexToPixel, pixelToHex, axialRound, rotatePoint, unrotatePoint,
getHexCorners, getAdjacentHexes, hexDistance, isPassable,
getMovementCost, getTerrainYields, getTerrainColor,
TerrainType, TERRAIN_COLORS, DEFAULT_TERRAIN_COLOR,
TERRAIN_YIELDS, TERRAIN_MOVEMENT_COSTS
} from '@vanduo-oss/hex-grid/hex-math';Vue 3
An optional Vue 3 component ships at @vanduo-oss/hex-grid/vue. vue is an optional peer dependency — needed only when you import this subpath, so vanilla/CDN consumers are unaffected. The component is SSR-safe (the canvas grid is created on mount into a plain container the server can pre-render).
<script setup>
import { VdHexGrid } from '@vanduo-oss/hex-grid/vue';
function onSelect(hex) {
console.log('selected', hex);
}
</script>
<template>
<VdHexGrid :size="30" :width="15" :height="10" @select="onSelect" />
</template>| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| size | number | 30 | Hexagon size in px. |
| width | number | 10 | Grid columns (number of hexes). |
| height | number | 10 | Grid rows (number of hexes). |
| rotation | number | 0 | Grid rotation in radians. |
Emits select, zoom, pan (forwarded from the grid) plus ready (the grid instance, on mount). Prop changes are driven through the instance setters (setSize, setDimensions, setRotation) — no recreate. The component exposes { getInstance() } via template ref for the full imperative API. Types ship with the subpath (dist/vue.d.ts).
License
MIT
