Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Created December 2, 2012 20:02
Show Gist options
  • Save nicksheffield/4190758 to your computer and use it in GitHub Desktop.
Save nicksheffield/4190758 to your computer and use it in GitHub Desktop.
Raphael.js - Variable Width Hexagon
function hexagon(x,y,w,h){
return paper.path(
'M' + (x+12) + ',' + y
+ 'L' + (x+12+w) + ',' + y
+ 'L' + (x+24+w) + ',' + (y+(h/2))
+ 'L' + (x+12+w) + ',' + (y+h)
+ 'L' + (x+12) + ',' + (y+h)
+ 'L' + x + ',' + (y+(h/2))
+ 'L' + (x+12) + ',' + y
)
}
@dieresys
Copy link

Another version:

function hexagon(x, y, lineSize){
    thirtyRadians = 30 * (Math.PI / 180);
    var pathLine = '' + 
        'M' + x + ' ' + (y + lineSize / 2) + ' '
        + 'v' + lineSize + ' '
        + 'l' + (lineSize * Math.cos(thirtyRadians) * (1)) + ' ' + (lineSize / (2)) + ' '
        + 'l' + (lineSize * Math.cos(thirtyRadians) * (1)) + ' ' + (lineSize / (-2)) + ' '
        + 'v' + (lineSize * (-1)) + ' '
        + 'l' + (lineSize * Math.cos(thirtyRadians) * (-1)) + ' ' + (lineSize / (-2)) + ' '
        + 'Z';
    return paper.path(pathLine);
}

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