package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.events.KeyboardEvent; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.ui.Keyboard; [SWF(width=600, height=400, backgroundColor=0x000000)] /** * Demonstrates BitmapData's draw() command by duplicating the entirety of the stage using draw() * whenever the space bar is pressed. A smaller copy is also drawn onto the stage to show how draw() * can be used to transform the pixels in the image being drawn. */ public class graphic_flex_image_effects_03_Flex_DrawTest extends Sprite { private var _circle:Sprite; private var _bitmapData:BitmapData; /** * Constructor. Creates bitmap that will be drawn into and circle that will follow the mouse. * A listener is also set up to handle then a key is pressed. */ public function graphic_flex_image_effects_03_Flex_DrawTest() { createBitmap(); createCircle(); stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown); } /** * Creates bitmap data that the stage contents will be drawn into and adds this to the stage. */ private function createBitmap():void { _bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000); var bitmap:Bitmap = new Bitmap(_bitmapData); addChild(bitmap); } /** * Creates a circle shape that is set to follow the mouse as it moves. */ private function createCircle():void { _circle = new Sprite(); _circle.graphics.beginFill(0xFF0000); _circle.graphics.drawCircle(0, 0, 25); _circle.graphics.endFill(); addChild(_circle); _circle.x = stage.mouseX; _circle.y = stage.mouseY; _circle.startDrag(); } /** * Handler for when a key is pressed. If it is the space bar, the current stage view is drawn into bitmap. * * @param event Event dispatched by stage. */ private function onStageKeyDown(event:KeyboardEvent):void { if (event.keyCode == Keyboard.SPACE) { _bitmapData.draw(stage); // added in step 2, this enables the scaling down of a smaller copy of the stage image data var matrix:Matrix = new Matrix(); matrix.scale(.5, .5); // added in step 3, this moves the smaller copy to the lower right corner of the bitmap matrix.translate(300, 200); // added in step 4, this inverts the colors of the smaller copy var transform:ColorTransform = new ColorTransform(-1, -1, -1, 1, 255, 255, 255); _bitmapData.draw(stage, matrix, transform); // transform the current color of the circle transform = new ColorTransform(); transform.color = Math.random()*0xFFFFFF; _circle.transform.colorTransform = transform; } } } }