Skip to content

Instantly share code, notes, and snippets.

@dan-diaz
Created August 21, 2014 18:01
Show Gist options
  • Select an option

  • Save dan-diaz/a264274251f1557116c0 to your computer and use it in GitHub Desktop.

Select an option

Save dan-diaz/a264274251f1557116c0 to your computer and use it in GitHub Desktop.
draw canvas hexagon
var dxCanvas = document.getElementById('dxCanvas');
var ctx = dxCanvas.getContext('2d');
function drawHex(size,x,y) {
ctx.beginPath();
ctx.moveTo(x,y);
var x2 = Math.sqrt((size*size) - ((size/2)*(size/2)))+x,
y2 = y-(size/2),
x3 = (Math.sqrt((size*size) - ((size/2)*(size/2)))*2)+x,
y3 = y,
x4 = x3,
y4 = y+size,
x5 = x2,
y5 = (size*1.5)+y,
x6 = x,
y6 = y4;
ctx.lineTo(x2,y2);
ctx.lineTo(x3,y3);
ctx.lineTo(x4,y4);
ctx.lineTo(x5,y5);
ctx.lineTo(x6,y6);
ctx.closePath();
ctx.strokeStyle = 'red';
ctx.stroke();
}
drawHex(100,300,300);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment