var rect:Rectangle = new Rectangle(10, 10, 100, 100);
var offset:Number = 0;
var lines:Array = [10, 5, 20, 5];
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
protected function enterFrameHandler(event:Event):void {
DrawUtil.drawDashedRect(this.graphics, rect, offset, lines);
offset++; // offset-- to animate backwards
}
**/
public static function dashTo(target:*, from:Point, to:Point, offset:Number=0, lines:Array=null, thickness:Number=1, color:uint=0x000000, alpha:Number=1):Number {
var graphics:Graphics = (target.hasProperty("graphics")) ? target["graphics"] as Graphics : null;
var bitmapData:BitmapData = (target is BitmapData) ? target as BitmapData : (target is Bitmap) ? (target as Bitmap).bitmapData : null;
DebugUtil.assert((graphics != null || bitmapData != null),
"target must have a graphics property or needs to be a Bitmap or BitmapData instance");
if (lines == null) lines = [3, 3];
DebugUtil.assert(lines.length > 0 && lines.length % 2 == 0,
"lines must have two or more elements, and be an even set");
// segment is the total length of the lines array
var segment:Number = 0;
// totals is a lookup table for total values up until the value in the lines array
var totals:Array = [];
// create totals and calculate segment length
for (var i:uint=0; iexample:
*var rect:Rectangle = new Rectangle(50, 50, 200, 200);
* var offset:Number = 13;
* var lines:Array = [10, 3, 2, 3, 20, 5];
* DrawUtil.drawDashedRect(drawGroup.graphics, rect, offset, lines);
*/
public static function drawDashedRect(g:Graphics, rect:Rectangle, offset:Number=0, lines:Array=null, thickness:Number=1, color:uint=0x000000, alpha:Number=1):Number {
var p1:Point = new Point(rect.left, rect.top);
var p2:Point = new Point(rect.right, rect.top);
var p3:Point = new Point(rect.right, rect.bottom);
var p4:Point = new Point(rect.left, rect.bottom);
var rest:Number;
rest = DrawUtil.dashTo(g, p1, p2, offset, lines, thickness, color, alpha);
rest = DrawUtil.dashTo(g, p2, p3, rest, lines, thickness, color, alpha);
rest = DrawUtil.dashTo(g, p3, p4, rest, lines, thickness, color, alpha);
rest = DrawUtil.dashTo(g, p4, p1, rest, lines, thickness, color, alpha);
return rest;
}
/**
* drawDashedRect2 that draws dashed lines in a rectangle uninterrupted and in union from the top-left corner to the bottom-right corner
*/
public static function drawDashedRect2(g:Graphics, rect:Rectangle, offset:Number=0, lines:Array=null, thickness:Number=1, color:uint=0x000000, alpha:Number=1):Number {
var p1:Point = new Point(rect.left, rect.top);
var p2:Point = new Point(rect.right, rect.top);
var p3:Point = new Point(rect.right, rect.bottom);
var p4:Point = new Point(rect.left, rect.bottom);
var rest:Number;
rest = DrawUtil.dashTo(g, p1, p2, offset, lines, thickness, color, alpha);
rest = DrawUtil.dashTo(g, p2, p3, rest, lines, thickness, color, alpha);
rest = DrawUtil.dashTo(g, p1, p4, offset, lines, thickness, color, alpha);
rest = DrawUtil.dashTo(g, p4, p3, rest, lines, thickness, color, alpha);
return rest;
}
}
}