package { import flash.display.Sprite; import flash.events.MouseEvent; [SWF(width=550, height=400, backgroundColor=0xFFFFFF)] /** * Demonstrates the quadtratic curve of the drawing API, * allowing user to drag control point and anchors. */ public class graphic_flex_image_effects_01_Flex_DrawingCurves extends Sprite { private var _controlPoint:Sprite; private var _anchor0:Sprite; private var _anchor1:Sprite; /** * Constructor. Creates anchors and control point and draws initial curve. */ public function graphic_flex_image_effects_01_Flex_DrawingCurves() { _anchor0 = addControlPoint(50, 300); _anchor1 = addControlPoint(500, 300); _controlPoint = addControlPoint(275, 100); drawCurve(); } /** * Adds a draggable control point to the stage. * * @param x The x position of the control point. * @param y The y position of the control point. * * @return The Sprite instance representing the control point. */ private function addControlPoint(x:Number, y:Number):Sprite { var controlPoint:Sprite = new Sprite(); // draw a big stroke thickness with little length to create a circle controlPoint.graphics.lineStyle(20); controlPoint.graphics.lineTo(1, 0); controlPoint.addEventListener(MouseEvent.MOUSE_DOWN, onControlDown); controlPoint.addEventListener(MouseEvent.MOUSE_UP, onControlUp); controlPoint.x = x; controlPoint.y = y; addChild(controlPoint); return controlPoint; } /** * Redraws the curve based on the current position of the points. */ private function drawCurve():void { graphics.clear(); graphics.lineStyle(3, 0xFF); graphics.moveTo(_anchor0.x, _anchor0.y); graphics.curveTo(_controlPoint.x, _controlPoint.y, _anchor1.x, _anchor1.y); graphics.lineStyle(1, 0, .5); graphics.lineTo(_controlPoint.x, _controlPoint.y); graphics.lineTo(_anchor0.x, _anchor0.y); } /** * Handler for when a control point or anchor is clicked. * This begins the drag of the point and forces a draw update on move. * * @param event Event dispatched by control point sprite. */ private function onControlDown(event:MouseEvent):void { (event.target as Sprite).startDrag(); stage.addEventListener(MouseEvent.MOUSE_MOVE, onControlMove); } /** * Handler for when a control point or anchor is released. * This stops the drag and removes the MOUSE_MOVE listener. * * @param event Event dispatched by control point sprite. */ private function onControlUp(event:MouseEvent):void { (event.target as Sprite).stopDrag(); stage.removeEventListener(MouseEvent.MOUSE_MOVE, onControlMove); } /** * Handler for when a control point or anchor is dragged. * This forces a redraw of the curve. * * @param event Event dispatched by stage. */ private function onControlMove(event:MouseEvent):void { drawCurve(); event.updateAfterEvent(); } } }