Skip to content

Instantly share code, notes, and snippets.

@benshimmin
Created January 20, 2013 17:46
Show Gist options
  • Save benshimmin/4580195 to your computer and use it in GitHub Desktop.
Save benshimmin/4580195 to your computer and use it in GitHub Desktop.
Convert an SVG polygon into a path (useful for rendering actual SVGs with Raphaël)
# Largely inspired by: http://stackoverflow.com/questions/9690241/rendering-svg-polygons-in-raphael-javascript-library
# You have, for example, this:
# <polygon fill="#00B8C0" stroke="#00FFFF" stroke-miterlimit="10" points="146.5,196.326 137.326,205.5 155.674,205.5 "/>
# You would like this:
# M147,196L137,206L156,206Z
# Assuming `xml` is your <polygon> node...
polygonToPath = (xml) ->
p = xml.getAttribute "points"
path = p.replace(/(\-?[0-9.]+)[\W|,](\-?[0-9.]+)/g, ($0, x, y) ->
"L" + Math.round(x) + "," + Math.round(y)
).replace(/^L/, "M").replace(/$/, "Z").replace(/\s/g, "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment