Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Drawing a hexagonal grid with HTML canvas
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Canvas Hexagonal Map</title>
<style type="text/css">
canvas {
border:0;
display:block;
margin:0 auto;
}
</style>
</head>
<body>
<canvas id="hexmap" width="660" height="624"></canvas>
<script src="hexagons.js"></script>
</body>
</html>
(function(){
var canvas = document.getElementById('hexmap');
var hexHeight,
hexRadius,
hexRectangleHeight,
hexRectangleWidth,
hexagonAngle = 0.523598776, // 30 degrees in radians
sideLength = 36,
boardWidth = 10,
boardHeight = 10;
hexHeight = Math.sin(hexagonAngle) * sideLength;
hexRadius = Math.cos(hexagonAngle) * sideLength;
hexRectangleHeight = sideLength + 2 * hexHeight;
hexRectangleWidth = 2 * hexRadius;
if (canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.fillStyle = "#000000";
ctx.strokeStyle = "#CCCCCC";
ctx.lineWidth = 1;
drawBoard(ctx, boardWidth, boardHeight);
canvas.addEventListener("mousemove", function(eventInfo) {
var x,
y,
hexX,
hexY,
screenX,
screenY;
x = eventInfo.offsetX || eventInfo.layerX;
y = eventInfo.offsetY || eventInfo.layerY;
hexY = Math.floor(y / (hexHeight + sideLength));
hexX = Math.floor((x - (hexY % 2) * hexRadius) / hexRectangleWidth);
screenX = hexX * hexRectangleWidth + ((hexY % 2) * hexRadius);
screenY = hexY * (hexHeight + sideLength);
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBoard(ctx, boardWidth, boardHeight);
// Check if the mouse's coords are on the board
if(hexX >= 0 && hexX < boardWidth) {
if(hexY >= 0 && hexY < boardHeight) {
ctx.fillStyle = "#000000";
drawHexagon(ctx, screenX, screenY, true);
}
}
});
}
function drawBoard(canvasContext, width, height) {
var i,
j;
for(i = 0; i < width; ++i) {
for(j = 0; j < height; ++j) {
drawHexagon(
ctx,
i * hexRectangleWidth + ((j % 2) * hexRadius),
j * (sideLength + hexHeight),
false
);
}
}
}
function drawHexagon(canvasContext, x, y, fill) {
var fill = fill || false;
canvasContext.beginPath();
canvasContext.moveTo(x + hexRadius, y);
canvasContext.lineTo(x + hexRectangleWidth, y + hexHeight);
canvasContext.lineTo(x + hexRectangleWidth, y + hexHeight + sideLength);
canvasContext.lineTo(x + hexRadius, y + hexRectangleHeight);
canvasContext.lineTo(x, y + sideLength + hexHeight);
canvasContext.lineTo(x, y + hexHeight);
canvasContext.closePath();
if(fill) {
canvasContext.fill();
} else {
canvasContext.stroke();
}
}
})();
@zackthehuman

This comment has been minimized.

Show comment Hide comment
@zackthehuman

zackthehuman Feb 20, 2012

Works in Chrome but my mouse position gather is wrong for Firefox. Working on a fix...

Owner

zackthehuman commented Feb 20, 2012

Works in Chrome but my mouse position gather is wrong for Firefox. Working on a fix...

@Apreche

This comment has been minimized.

Show comment Hide comment
@Apreche

Apreche Nov 25, 2012

I'm thinking of making a version of this that scales with window size. Either more hexes will be visible with a larger window, or the map will have a fixed number of hexes that will get bigger and easier to see.

Apreche commented Nov 25, 2012

I'm thinking of making a version of this that scales with window size. Either more hexes will be visible with a larger window, or the map will have a fixed number of hexes that will get bigger and easier to see.

@matthewisabel

This comment has been minimized.

Show comment Hide comment
@matthewisabel

matthewisabel Dec 8, 2012

Forked and added a few lines to fix the mouse positioning in FF. Using getBoundingClientRect(), I see the correct behavior in Chrome, FF, IE9, and Opera on Win 7.

Forked and added a few lines to fix the mouse positioning in FF. Using getBoundingClientRect(), I see the correct behavior in Chrome, FF, IE9, and Opera on Win 7.

@ArneBab

This comment has been minimized.

Show comment Hide comment
@ArneBab

ArneBab Mar 22, 2015

forked to experiment with conways life in hexagon. Would you allow me to use this under a license which is compatible with GPL?

ArneBab commented Mar 22, 2015

forked to experiment with conways life in hexagon. Would you allow me to use this under a license which is compatible with GPL?

@JohnRayson

This comment has been minimized.

Show comment Hide comment
@JohnRayson

JohnRayson Jan 23, 2016

Great work: I've incorporated your core hex drawing logic into my own project here: https://github.com/JohnRayson/Hexagons_Rebooted
You are currently referenced in the MD file, is there a specific way you would like me to attribute the work to yourself?

Great work: I've incorporated your core hex drawing logic into my own project here: https://github.com/JohnRayson/Hexagons_Rebooted
You are currently referenced in the MD file, is there a specific way you would like me to attribute the work to yourself?

@Krischeto

This comment has been minimized.

Show comment Hide comment
@Krischeto

Krischeto May 26, 2017

Awesome

Awesome

@Krischeto

This comment has been minimized.

Show comment Hide comment
@Krischeto

Krischeto May 26, 2017

I search for rotated hexagon grid 1 hour

I search for rotated hexagon grid 1 hour

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment