I am wondering what options I have for displaying SVG data using
degrafa.
Is there a SVG -> degrafa converter?
Does degrafa have a SVG import/viewing function?
Does anyone have any suggestions on how to go about this?
Kind regards,
Matthijs
To load svg into degrapha.
You load the svg file with URLRequest, and then you need to parse it.
Since it is xml-like , we want to look for the path-tag which has the
important info for us.
When we have it, we put it in to a path class, which loads into a
geometryCollection, which loads into a
Degrapha surface.
To load the file:
var request:URLRequest=new URLRequest("svg_file_name.svg");
var loaderSVG:URLLoader=new URLLoader(request);
loaderSVG.addEventListener(Event.COMPLETE, parseSVG);
loaderSVG.addEventListener(IOErrorEvent.IO_ERROR, errorLoadFile);
To parse recursively:
Private function parseSVG(e:Event):void {
var xmlData: XML = new XML(e.target.data);
for each ( var element:XML in xmlData.elements ( ) ) {
if ((element.localName() == 'path') || (element.localName() ==
'Path')){
var newPath:Path = new Path();
var newGc:GeometryGroup = new GeometryGroup();
// if you want to use the fill color in the file
var color:uint = getColor4Style(element.@style);
if (color != 0x000000) {
newPath.fill = new SolidFill(color);
}
newPath.data = element.@d;
newGc.geometryCollection.addItem(newPath);
// if you want to add interactivity
newGc.addEventListener(MouseEvent.ROLL_OVER,onRollOver);
newGc.addEventListener(MouseEvent.ROLL_OUT,onRollOut);
newGc.addEventListener(MouseEvent.CLICK,onClick);
newGc.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
GeometryGroup.geometryCollection.addItem(newGc);
newGc.target = GeometryGroup;
}
getDataFromXML( element);
}
private function getColor4Style(s:String):uint {
var start:int;
if((s=="none") || (s=="") || (s=='fill:none;')) {
return 0x000000;
} else
if (s.charAt(5)=="#") {
s = s.substring(6,12);
if(s.length<6) {
s = s.charAt(0)+s.charAt(0)+s.charAt(1)+s.charAt(1)+s.charAt
(2)+s.charAt(2);
}
} else
// if the # is somewhere else
if ((start=s.indexOf('#')) != -1) {
s = s.substring(start+1,start+6);
if(s.length<6) {
s = s.charAt(0)+s.charAt(0)+s.charAt(1)+s.charAt(1)+s.charAt
(2)+s.charAt(2);
}
}
return new Number("0x" + s);
}
This puts the svg file in your degrapha surface. Not sure why you want
to use degrapha as a viewer because there are other svg viewers (just
google them).
Hope this helps
Elisheva