Copy link to clipboard
Copied
I have written an image generator that draws using flash.display.Graphics like circle, rect and line. I need to save the .svg text for each command and write the result to a vector. svg file so the resulting drawing can be subsequently resized without loss of detail.
Copy link to clipboard
Copied
figured it out myself. This is the class I created - it's all I need:
package classes
{
import flash.geom.Point;
public class drawSVG
{
public var svgData:String = "";
private var svgID:Number = 1;
public function drawSVG()
{
}
public function start(svgTitle:String,inw:Number,inh:Number,inAngle:Number) : void
{
svgData = '<svg width="' + inw.toString() + '"' + ' height="' + inh.toString() + '"' +
' x="' + -inw/2 + '" y="' + -inh/2 + '" xmlns="http://www.w3.org/2000/svg">\r\n';
svgData += '<g transform="rotate(' + inAngle + ' ' + '0' + ' ' + '0' + ')">\r\n';
svgData += '<title>' + svgTitle + '</title>\r\n';
}
public function drawLine(inStrokeClr:String,inStrokeW:uint,
inAngle:Number=0) : void // angle in degrees
{
if (inStrokeW != 0)
{
var x1:Number = inx + inw/2;
var y1:Number = iny - inh/2;
var x2:Number = inx + inw/2;
var y2:Number = iny + inh/2;
var svgStrokeClr:String = inStrokeClr.split("0x").join("#");
svgData += '<line x1="' + x1 + '" y1="' + y1 + '" x2="' + x2 + '" y2="' + y2 + '"';
svgData += ' style="';
svgData += ' stroke:' + svgStrokeClr + ';stroke-width:' + inStrokeW;
svgData += '"';
svgData += ' id="line_' + svgID.toString() + '"';
svgData += '/>\r\n';
}
public function drawEllipse(inStrokeClr:String,inStrokeW:uint,
inAngle:Number=0) : void // angle in degrees
{
if (isFill || inStrokeW!= 0)
{
var svgStrokeClr:String = inStrokeClr.split("0x").join("#");
var svgFillClr:String = infillClr.split("0x").join("#");
svgData += '<ellipse rx="' + inw/2 + '"' + ' ry="' + inh/2 + '"' +
' cx="' + inCenterX +'"' + ' cy="' + inCenterY + '"';
svgData += ' style="';
if (isFill)
svgData += ' fill:' + svgFillClr;
if (inStrokeW!= 0)
svgData += ' stroke:' + svgStrokeClr + ' stroke-width:' + inStrokeW;
svgData += '"';
svgData += ' id="ellipse_' + svgID.toString() + '"';
svgData += '/>\r\n';
}
}
public function drawRectangle(inStrokeClr:String,inStrokeW:uint,
isCenter:Boolean,inAngle:Number=0) : void // angle in degrees
{
if (isFill || inStrokeW!= 0)
{
var x:Number = (isCenter ? inx-(inw/2) : inx);
var y:Number = (isCenter ? iny-(inh/2) : iny);
var svgStrokeClr:String = inStrokeClr.split("0x").join("#");
var svgFillClr:String = infillClr.split("0x").join("#");
svgData += '<rect x="' + x + '" y="' + y + '"' +
' width="' + inw + '" height="' + inh + '"';
svgData += ' style="';
if (isFill)
svgData += ' fill:' + svgFillClr;
if (inStrokeW!= 0)
svgData += ' stroke:' + svgFillClr + ' stroke-width:' + inStrokeW;
svgData += '"';
svgData += ' id="rect_' + svgID.toString() + '"';
svgData += '/>\r\n';
}
}
public function terminate() : void
{
svgData += "</g>\r\n";
svgData += "</svg>";
}
}
}