forked from jspies/raphael.serialize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraphael.serialize.js
129 lines (118 loc) · 3.89 KB
/
raphael.serialize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*!
* raphaeljs.serialize
*
* Copyright (c) 2010 Jonathan Spies
* Licensed under the MIT license:
* (http://www.opensource.org/licenses/mit-license.php)
*
*/
Raphael.fn.serialize = {
paper: this,
json: function() {
var svgdata = [];
for(var node = paper.bottom; node != null; node = node.next) {
if (node && node.type) {
switch(node.type) {
case "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(' ') : ''
}
break;
case "ellipse":
var object = {
type: node.type,
rx: node.attrs['rx'],
ry: node.attrs['ry'],
cx: node.attrs['cx'],
cy: node.attrs['cy'],
stroke: node.attrs['stroke'] === 0 ? 'none': node.attrs['stroke'],
'stroke-width': node.attrs['stroke-width'],
fill: node.attrs['fill']
}
break;
case "rect":
var object = {
type: node.type,
x: node.attrs['x'],
y: node.attrs['y'],
width: node.attrs['width'],
height: node.attrs['height'],
stroke: node.attrs['stroke'] === 0 ? 'none': node.attrs['stroke'],
'stroke-width': node.attrs['stroke-width'],
fill: node.attrs['fill']
}
break;
case "text":
var object = {
type: node.type,
font: node.attrs['font'],
'font-family': node.attrs['font-family'],
'font-size': node.attrs['font-size'],
stroke: node.attrs['stroke'] === 0 ? 'none': node.attrs['stroke'],
fill: node.attrs['fill'] === 0 ? 'none' : node.attrs['fill'],
'stroke-width': node.attrs['stroke-width'],
x: node.attrs['x'],
y: node.attrs['y'],
text: node.attrs['text'],
'text-anchor': node.attrs['text-anchor']
}
break;
case "path":
var path = "";
if(node.attrs['path'].constructor != Array){
path += node.attrs['path'];
}
else{
$.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'],
'stroke-width': node.attrs['stroke-width'],
transform: node.transformations ? node.transformations.join(' ') : ''
}
}
if (object) {
svgdata.push(object);
}
}
}
return(JSON.stringify(svgdata));
},
load_json : function(json) {
if (typeof(json) == "string") { json = JSON.parse(json); } // allow stringified or object input
var paper = this;
var set = paper.set();
$.each(json, function(index, node) {
try {
var el = paper[node.type]().attr(node);
set.push(el);
} catch(e) {}
});
return set;
}
}