package { import flash.display.*; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.ui.Keyboard; [SWF(width=550, height=400, backgroundColor=0xFFFFFF)] /** * Demonstrates how path data can be saved as commands and easily redrawn using drawGraphicsData(). */ public class graphic_flex_image_effects_01_Flex_PreservingPathData extends Sprite { private static const INIT_THICKNESS:uint = 5; private static const MAX_THICKNESS:uint = 50; private static const MIN_THICKNESS:uint = 1; private var _thickness:uint; private var _drawing:Boolean; private var _commands:Vector.; private var _lineStyle:GraphicsStroke; private var _currentPath:GraphicsPath; /** * Constructor. Calls init(). */ public function graphic_flex_image_effects_01_Flex_PreservingPathData() { init(); } /** * Initializes properties and sets up listeners. */ private function init():void { var color:uint = Math.random()*0xFFFFFF; _thickness = INIT_THICKNESS; graphics.lineStyle(_thickness, color); // creates GraphicsStoke to save into vector _commands _lineStyle = new GraphicsStroke(_thickness); _lineStyle.fill = new GraphicsSolidFill(color); _commands = new Vector.(); _commands.push(_lineStyle); stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp); stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown); } /** * Redraws the path using the saved commands with drawGraphicsData(). */ private function redrawPath():void { graphics.clear(); graphics.drawGraphicsData(_commands); } /** * Handler for when a key is pressed. This alters the color or thickness and forces the redrawing of the path. * * @param event Event fired by stage. */ private function onStageKeyDown(event:KeyboardEvent):void { if (!_drawing) { switch (event.keyCode) { // increase thickness with UP arrow case Keyboard.UP: if (_thickness < MAX_THICKNESS) { _lineStyle.thickness = ++_thickness; redrawPath(); } break; // decrease thickness with DOWN arrow case Keyboard.DOWN: if (_thickness > MIN_THICKNESS) { _lineStyle.thickness = --_thickness; redrawPath(); } break; // recolor with SPACE case Keyboard.SPACE: _lineStyle.fill = new GraphicsSolidFill(Math.random()*0xFFFFFF); redrawPath(); break; } } } /** * Handler for when the mouse is clicked on the stage. This sets up listeners for drawing while the mouse is depressed. * * @param event Event fired by stage. */ private function onStageMouseDown(event:MouseEvent):void { _drawing = true; var x:Number = stage.mouseX; var y:Number = stage.mouseY; _currentPath = new GraphicsPath(); _currentPath.moveTo(x, y); _commands.push(_currentPath); graphics.moveTo(x, y); stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } /** * Handler for when the mouse is moved on the stage. This adds to the drawn path. * * @param event Event fired by stage. */ private function onStageMouseMove(event:MouseEvent):void { var x:Number = stage.mouseX; var y:Number = stage.mouseY; _currentPath.lineTo(x, y); graphics.lineTo(x, y); event.updateAfterEvent(); } /** * Handler for when the mouse is released on the stage. This stops the drawing. * * @param event Event fired by stage. */ private function onStageMouseUp(event:MouseEvent):void { _drawing = false; stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } } }