Created
July 23, 2011 12:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // <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