Loading remote Inkscape SVG images into Raphael

128 views
Skip to first unread message

Brother Erryn

unread,
Nov 9, 2009, 8:08:31 PM11/9/09
to Raphaël
I'd seen a couple of posts from people wanting something like this,
and I came up with something that seems to work pretty well, and
thought I'd share. It currently renders drawings made in Inkscape 0.46
as "Inkscape SVG" and "Plain SVG". 0.47 has a couple of other formats
that don't render correctly because of the transform attribute they
add.

My code uses jQuery to handle some of the tedious bits (here's hoping
the formatting doesn't get destroyed). I'm sure there's plenty of room
for improvement.

$.get('demo.svg', null, function(doc){
var root = $(doc).find('svg')[0],
vb = root.getAttribute('viewBox'), dims = vb ? vb.split(' ') :
[0,0,root.getAttribute('width'),root.getAttribute('height')];

var img = Raphael(parseFloat(dims[0]), parseFloat(dims[1]), parseFloat
(dims[2]), parseFloat(dims[3]));

$(root).find('path').each(function(){
var node = this, path = img.path($(this).attr('d'));
$(['stroke-linejoin','stroke','stroke-miterlimit','stroke-
width','fill','stroke-linecap']).each(function(){
if($(node).attr(this.toString()))
path.attr(this, $(node).attr(this.toString()));
});

if($(node).attr('style')) {
var atts = $(node).attr('style').split(';');
for(var i=0;i<atts.length;i++) {
var bits = atts[i].split(':');
path.attr(bits[0],bits[1]);
}
}
});
}, 'xml');
Reply all
Reply to author
Forward
0 new messages