-
Notifications
You must be signed in to change notification settings - Fork 27
AS3 only example, manual scaling and centering
LucasLorentz edited this page Feb 24, 2013
·
1 revision
package {
import flash.display.MovieClip;
import flash.geom.Rectangle;
import flash.utils.ByteArray;
import lorentz.SVG.display.SVGDocument;
import lorentz.SVG.events.SVGEvent;
import lorentz.processing.ProcessExecutor;
[SWF(width="800", height="600")]
public class SVGTest extends MovieClip {
public function SVGTest() {
//Starts the asynchronous processor
ProcessExecutor.instance.initialize(stage);
//Creates a document
var svgDocument:SVGDocument=new SVGDocument();
//Tells the document to remove extra space on top and left
svgDocument.autoAlign = true;
//Tells the document to don't draw on the screen until it is completely parsed
svgDocument.validateWhileParsing = false;
//Add the document to the screen
addChild(svgDocument);
//Parses the svg string
svgDocument.parse(getSVGString());
svgDocument.addEventListener(SVGEvent.RENDERED, function(e:SVGEvent):void {
//Get the bounds of the drawed svg
var bounds:Rectangle = svgDocument.getBounds(svgDocument.parent);
var widthIWant:Number = 400;
var heightIWant:Number = 400;
//Apply the same scale on x and y
var scale:Number = Math.min(widthIWant / bounds.width, heightIWant / bounds.height);
svgDocument.scaleX = scale;
svgDocument.scaleY = scale;
//Align it on the center of the screen
svgDocument.x = (widthIWant - (bounds.width * scale))/2;
svgDocument.y = (heightIWant - (bounds.height * scale))/2;
//Draw a rect to show the swf area
graphics.beginFill(0xEEEEEE);
graphics.drawRect(0, 0, widthIWant, heightIWant);
});
}
[Embed(source="tiger.svg", mimeType="application/octet-stream")]
private var TextClass:Class;
private function getSVGString():String {
var byteArray:ByteArray=new TextClass() as ByteArray;
return byteArray.readUTFBytes(byteArray.length);
}
}
}