@vanduo-oss/hex-grid
v1.0.0
Published
Standalone VdHexGrid package for canvas-based hex grids
Downloads
81
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';License
MIT
