My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
pageTitle="SVG2RaphaelJS - TOKI WOKI.">
<fx:Style source="style/main.css"/>
<s:creationComplete>
<![CDATA[
fr.addEventListener(Event.SELECT, onSVGSelect);
]]>
</s:creationComplete>
<fx:Script>
<![CDATA[
/**
*
* TODO:
* - Gradients (Radial focus points)
*
*/
import com.adobe.serialization.json.JSON;
//
protected var fr:FileReference=new FileReference();
protected var jsOs:Array=[];
protected var xml:XML=new XML();
protected var node:XML;
protected var idIndices:Object;
//
protected function onSVGChange():void {
try {
// Yes, that "replace" thing is ugly.
xml=new XML(taSVG.text.replace('xmlns="http://www.w3.org/2000/svg"', ''));
} catch (er:Error) {
taRaphaelJS.text='Error';
xml=new XML();
return;
}
while (xml..*.(attribute('display')=='none').length()) {
delete xml..*.(attribute('display')=='none')[0];
}
jsOs=[];
var paths:XMLList=xml..node;
var o:Object;
var nodeName:String;
var nodeIndex:uint=0;
for each(node in xml..*) {
o={};
//
if (node.@stroke.length()) o.stroke=String(node.@stroke);
else o.stroke='none';
//
nodeName=QName(node.name()).localName;
//
if (node.@fill.length()) {
o.fill=String(node.@fill);
if (o.fill.substr(0, 3)=='url') {
var fillID:String=o.fill.substring(5, o.fill.length-1);
var fill:XML=xml..*.(attribute('id')==fillID)[0];
var fillType:String=QName(fill.name()).localName;
var stops:Array=[];
if (fillType=='linearGradient') {
var p1:Point=new Point(tn(fill.@x1), tn(fill.@y1));
var p2:Point=new Point(tn(fill.@x2), tn(fill.@y2));
var angle:Number = -Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;
if (angle<0) angle+=360;
stops.push(angle);
}
for each(var stop:XML in fill.stop) {
stops.push(String(stop.@style).substr(-7)+':'+100*tn(stop.@offset));
}
if (fillType=='radialGradient' && nodeName!='circle' && nodeName!='ellipse') {
o.fill='#000';
} else {
o.fill=stops.join('-');
if (fillType=='radialGradient') o.fill='r'+o.fill;
}
}
} else {
o.fill='#000';
}
if (node.@style.length()) {
var styles:Array=String(node.@style).split(';');
var style:Array;
for (var i:int=0; i<styles.length; i++) {
style=styles[i].split(':');
o[style[0]]=style[1];
}
}
//
if (node.@['stroke-width'].length()) o['stroke-width']=tn(node.@['stroke-width']);
if (node.@opacity.length()) o.opacity=tn(node.@opacity);
//
switch(nodeName) {
case 'path':
o.type=nodeName;
var d:String=node.@d;
d=d.replace(/\s+/g, ' ');
o.path=d;
addNode(o);
break;
case 'polygon':
o.type='path';
o.path='M'+String(node.@points)+'z';
addNode(o);
break;
case 'line':
o.type='path';
o.path='M'+tn(node.@x1)+' '+tn(node.@y1)+'L'+tn(node.@x2)+' '+tn(node.@y2);
addNode(o);
break;
case 'rect':
o.type=nodeName;
if (node.@x.length()) o.x=tn(node.@x);
if (node.@y.length()) o.y=tn(node.@y);
o.width=tn(node.@width);
o.height=tn(node.@height);
addNode(o);
break;
case 'circle':
o.type=nodeName;
if (node.@cx.length()) o.cx=tn(node.@cx);
if (node.@cy.length()) o.cy=tn(node.@cy);
o.r=tn(node.@r);
addNode(o);
break;
case 'ellipse':
o.type=nodeName;
if (node.@cx.length()) o.cx=tn(node.@cx);
if (node.@cy.length()) o.cy=tn(node.@cy);
o.rx=tn(node.@rx);
o.ry=tn(node.@ry);
addNode(o);
break;
case 'text':
if (node.children().length()==0) break;
o.type=nodeName;
var tm:Array=String(node.@transform).replace(/[^\s\d\.]/g, '').split(' ');
o.x=Math.round(Number(tm[4]));
o.y=Math.round(Number(tm[5]));
o['font-size']=tn(node.@['font-size']);
o['text-anchor']='start';
o.text=node.children()[0].toString();
addNode(o);
break;
}
}
updateOutput();
}
protected function addNode(o:Object):void {
//if (node.@id.length()) idIndices[String(node.@id)]=jsOs.length;
jsOs.push(o);
}
protected function tn(s:*):Number {
return Number(String(s).replace(/[a-z-]/gm, ''));
}
protected function updateOutput():void {
var ar:Array=[];
if (rbAbsolute.selected) {
ar.push(nsX.value, nsY.value, tn(xml.@width), tn(xml.@height));
} else {
ar.push(tiElementID.text, tn(xml.@width), tn(xml.@height));
}
ar=ar.concat(jsOs);
taRaphaelJS.text='var '+tiVarName.text+'='+JSON.encode(ar)+';';
/*var lines:Array=['var paper=Raphael('+nsX.value+', '+nsY.value+', '+tn(xml.@width)+', '+tn(xml.@height)+');'];
var o:Object;
for (var i:int=0; i<jsOs.length; i++) {
o=jsOs[i];
lines.push('paper.'+o.type+'('+objectToParams(o)+');');
}
taRaphaelJS.text=lines.join('\n');*/
}
protected function objectToParams(o:Object):String {
var params:Array=[];
var val:*;
for (var a:String in o) {
if (a=='type') continue;
val=o[a];
if (val is String) params.push("'"+val+"'");
else params.push(val);
}
return params.join(', ');
}
protected function onSVGSelect(e:Event):void {
fr.addEventListener(Event.COMPLETE, onLoadComplete);
fr.load();
}
protected function onLoadComplete(e:Event):void {
taSVG.text=fr.data.readUTFBytes(fr.data.bytesAvailable);
onSVGChange();
}
]]>
</fx:Script>
<mx:HDividedBox width="100%" height="100%" paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10">
<s:VGroup width="100%" height="100%">
<s:HGroup width="100%" verticalAlign="middle">
<s:Label text="Input: SVG file content" width="100%"/>
<s:Button label="Open...">
<s:click>
<![CDATA[
fr.browse([new FileFilter('SVG File', '*.svg')]);
]]>
</s:click>
</s:Button>
</s:HGroup>
<s:TextArea width="100%" height="100%" id="taSVG" change="onSVGChange()"/>
</s:VGroup>
<s:VGroup width="100%" height="100%">
<s:Label text="Output: Raphaƫl object" height="21" verticalAlign="middle"/>
<s:TextArea width="100%" height="100%" id="taRaphaelJS"/>
<s:HGroup width="100%" verticalAlign="middle">
<s:Label text="Name of the JS variable" paddingTop="4" width="100%"/>
<s:TextInput id="tiVarName" text="mySVG" change="updateOutput()" restrict="a-zA-Z0-9_"/>
</s:HGroup>
<s:HGroup width="100%" verticalAlign="middle">
<s:Label text="Inject" paddingTop="4" width="100%"/>
<s:RadioButton label="in Element" id="rbElement" click="updateOutput()"/>
<s:RadioButton label="Absolutely" id="rbAbsolute" click="updateOutput()" selected="true"/>
</s:HGroup>
<s:HGroup width="100%" verticalAlign="middle" visible="{rbElement.selected}" includeInLayout="{rbElement.selected}" height="23">
<s:Label text="Element's DOM ID" paddingTop="4" width="100%"/>
<s:TextInput id="tiElementID" text="myElementID" restrict="a-zA-Z0-9_" change="updateOutput()"/>
</s:HGroup>
<s:HGroup width="100%" verticalAlign="middle" visible="{rbAbsolute.selected}" includeInLayout="{rbAbsolute.selected}">
<s:Label text="Position (x, y)" paddingTop="2" width="100%"/>
<s:NumericStepper id="nsX" change="updateOutput()" maximum="1000"/>
<s:NumericStepper id="nsY" change="updateOutput()" maximum="1000"/>
</s:HGroup>
<s:HGroup width="100%" horizontalAlign="right">
<s:Button label="Copy to Clipboard">
<s:click>
<![CDATA[
System.setClipboard(taRaphaelJS.text);
]]>
</s:click>
</s:Button>
<s:Button label="Save As...">
<s:click>
<![CDATA[
var tfr:FileReference=new FileReference();
tfr.save(taRaphaelJS.text, tiVarName.text+'.js');
]]>
</s:click>
</s:Button>
</s:HGroup>
</s:VGroup>
</mx:HDividedBox>
</s:Application>

Change log

r113 by quentin.t on Jun 29, 2011   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 8543 bytes, 247 lines
Powered by Google Project Hosting