Skip to content

Instantly share code, notes, and snippets.

@mklabs
Created July 23, 2011 12:57
Show Gist options
  • Select an option

  • Save mklabs/1101393 to your computer and use it in GitHub Desktop.

Select an option

Save mklabs/1101393 to your computer and use it in GitHub Desktop.
svg to raphael helper, get an array of path object {path, attr} from an svg file
// <script type="text/someunknowntype" id="logo-svg"><svg>...</svg></script>
var svg = $('#logo-svg')[0].innerHTML
// >< whitespace cleanr ><
.replace(/>[\s]+</g, '><')
.replace(/[\s]+/g, ' ')
// get an array of path hash object, with path/attr property
.match(/<(path[^\/]+)\/>/g)
.map(function(el) {
var m = el.match(/<path d="([^"]+).+style="([^"]+).+/),
attr = {},
am = m[2].split(';').forEach(function(el) {
var m = el.split(':');
attr[m[0]] = m[1];
});
return {
path: m[1],
attr: attr
}
});
// draw the svg
var paper = new Raphael($('#container')[0], width, height);
svg.forEach(function(node) {
node.attr.stroke = 'transparent';
paper.path(node.path).attr(node.attr);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment