Skip to content

Instantly share code, notes, and snippets.

@simonwoo
Forked from benbarnett/raphael-svg-buildjson
Created August 5, 2014 07:53
Show Gist options
  • Save simonwoo/d86a0a8423ee309a79a6 to your computer and use it in GitHub Desktop.
Save simonwoo/d86a0a8423ee309a79a6 to your computer and use it in GitHub Desktop.
// loop through the 'paper' variable from Raphael JS and build up the JSON object describing all images and paths within it.
buildJSON = function(paper) {
var svgdata = [];
svgdata.push({
width: 390,
height: 400
});
$.each(paper,
function(i, o) {
var node = o;
if (node.type == "image") {
var object = {
type: node.type,
width: node.attrs['width'],
height: node.attrs['height'],
x: node.attrs['x'],
y: node.attrs['y'],
src: node.attrs['src'],
transform: node.transformations ? node.transformations.join(' ') : ''
}
}
if (node.type == "path") {
var path = "";
$.each(node.attrs['path'],
function(i, group) {
$.each(group,
function(index, value) {
if (index < 1) {
path += value;
}
else {
if (index == (group.length - 1)) {
path += value;
}
else {
path += value + ',';
}
}
});
});
var object = {
type: node.type,
fill: node.attrs['fill'],
opacity: node.attrs['opacity'],
translation: node.attrs['translation'],
scale: node.attrs['scale'],
path: path,
stroke: node.attrs['stroke'] === 0 ? 'none': node.attrs['stroke'],
transform: node.transformations ? node.transformations.join(' ') : ''
}
}
svgdata.push(object);
});
$('#output').val(JSON.stringify(svgdata));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment