This opensource tiny (1.5KB, < 40 lines) jQuery plugin turns any DIV into an hexagonal grid widget, for prototyping hex-grid based games in HTML. Here are the key features:
Initialise the grid by calling hexGridWidget:
$('#container').hexGridWidget(radius, columns, rows, cssClass)
Use the hex-row and hex-column DOM attributes to select an individual hex in the grid. For example:
$('#container [hex-row=2][hex-column=3]')
Capture clicks by listening to a hexclick event on the grid container DOM element.
$('#container').on('hexclick',
function (e) {
console.log('clicked [' + e.column + ',' + e.row +']' +
' hex with center at [' + e.center.x + ',' + e.center.y + ']');
}
);
Alternatively, just assign a normal click handler to individual hex field,
or even a group of hexfields.
$('#container .hexfield').click(function () {
this.classList.toggle('clicked');
});