绘制渐变线条

package
{
	import flash.display.CapsStyle;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;

	[SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
	
	public class as6 extends Sprite
	{
		private var _currentShape:Shape;
		private var _color:uint;
		private var _startPosition:Point;
		
		public function as6()
		{
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}
		
		private function drawLine():void
		{
			var thickness:Number = 40;
			var colors:Array = [_color, _color];
			var alphas:Array = [1, 0];
			var rations:Array = [127, 127];
			
			var currentPosition:Point = new Point(stage.mouseX,stage.mouseY);
			var xDist:Number = (currentPosition.x - _startPosition.x);
			var yDist:Number = (currentPosition.y - _startPosition.y);
			var angle:Number = Math.atan2(yDist,xDist);
			var matrix:Matrix = new Matrix();
			
			matrix.createGradientBox(thickness, thickness, angle);
			_currentShape.graphics.clear();
			_currentShape.graphics.lineStyle(thickness, 0, 1,false,null,CapsStyle.SQUARE);
			_currentShape.graphics.lineGradientStyle(GradientType.LINEAR, colors, alphas, rations, matrix, SpreadMethod.REPEAT);
			_currentShape.graphics.moveTo(_startPosition.x, _startPosition.y);
			_currentShape.graphics.lineTo(stage.mouseX, stage.mouseY);
		}
		
		private function onStageMouseDown(event:MouseEvent):void
		{
			_color = Math.random()*0xFFFFFF;
			_currentShape = new Shape();
			addChild(_currentShape);
			_startPosition = new Point(stage.mouseX,stage.mouseY);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
		}
		
		private function onStageMouseMove(event:MouseEvent):void
		{
			drawLine();
			event.updateAfterEvent();
		}
		
		private function onStageMouseUp():void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
		}
	}
}