This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
raphael-svg-import.js
101 lines (88 loc) · 2.66 KB
/
raphael-svg-import.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
/*
* Raphael SVG Import 0.0.4 - Extension to Raphael JS
*
* Copyright (c) 2011 Wout Fierens
* - Load order fix by Georgi Momchilov
* - Prototype dependency removed by Matt Cook
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
Raphael.fn.importSVG = function (rawSVG, set) {
try {
if (typeof rawSVG === 'undefined')
throw 'No data was provided.';
rawSVG = rawSVG.replace(/\n|\r|\t/gi, '');
if (!rawSVG.match(/<svg(.*?)>(.*)<\/svg>/i))
throw "The data you entered doesn't contain valid SVG.";
var findAttr = new RegExp('([a-z\-]+)="(.*?)"','gi'),
findStyle = new RegExp('([a-z\-]+) ?: ?([^ ;]+)[ ;]?','gi'),
findNodes = new RegExp('<(rect|polyline|circle|ellipse|path|polygon|image|text).*?\/>','gi');
while(match = findNodes.exec(rawSVG)){
var shape, style,
attr = { 'fill':'#000' },
node = RegExp.$1;
while(findAttr.exec(match)){
switch(RegExp.$1) {
case 'stroke-dasharray':
attr[RegExp.$1] = '- ';
break;
case 'style':
style = RegExp.$2;
break;
default:
attr[RegExp.$1] = RegExp.$2;
break;
}
};
if (typeof attr['stroke-width'] === 'undefined')
attr['stroke-width'] = (typeof attr['stroke'] === 'undefined' ? 0 : 1);
if (style)
while(findStyle.exec(style))
attr[RegExp.$1] = RegExp.$2;
switch(node) {
case 'rect':
shape = this.rect();
break;
case 'circle':
shape = this.circle();
break;
case 'ellipse':
shape = this.ellipse();
break;
case 'path':
shape = this.path(attr['d']);
break;
case 'polygon':
shape = this.polygon(attr['points']);
break;
case 'image':
shape = this.image();
break;
//-F case 'text':
//-F shape = this.text();
//-F break;
}
shape.attr(attr);
if (typeof set !== 'undefined')
set.push(shape);
};
} catch (error) {
alert('The SVG data you entered was invalid! (' + error + ')');
}
};
// extending raphael with a polygon function
Raphael.fn.polygon = function(pointString) {
var poly = ['M'],
point = pointString.split(' ');
for(var i=0; i < point.length; i++) {
var c = point[i].split(',');
for(var j=0; j < c.length; j++) {
var d = parseFloat(c[j]);
if (d)
poly.push(d);
};
if (i == 0)
poly.push('L');
}
poly.push('Z');
return this.path(poly);
};