package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.display.LoaderInfo; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; import flash.net.URLRequest; [SWF(width=750, height=500, backgroundColor=0x000000)] /** * Demonstrates BitmapData's copyPixels() by loading an image and displaying a scaled down version. * As the used hovers over the image, a small portion of the full size image is drawn to its right. */ public class graphic_flex_image_effects_03_Flex_CopyPixelsTest extends Sprite { private var _largeImage:BitmapData; private var _smallImage:BitmapData; private var _zoomedImage:BitmapData; private var _scale:Number; /** * Constructor. Passes path of image to load to super class. */ public function graphic_flex_image_effects_03_Flex_CopyPixelsTest() { loadImage("graphic-flex-image-effects-03-assets-canyon.jpg"); } /** * Loads the specified image. * * @param imagePath The path to the image to load. */ private function loadImage(imagePath:String):void { var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded); loader.load(new URLRequest(imagePath)); } /** * Creates scaled down version of loaded image as well as data in which the full size * region will be rendered. */ private function createBitmaps():void { // matrix to scale down loaded image so that it fits on the stage var matrix:Matrix = new Matrix(); _scale = stage.stageHeight/_largeImage.height; matrix.scale(_scale, _scale); // scaled down version that will show whole image _smallImage = new BitmapData(_largeImage.width*_scale, _largeImage.height*_scale); // the zoomed region will be the same size as the scaled image _zoomedImage = _smallImage.clone(); // draw in the scaled down image _smallImage.draw(_largeImage, matrix); var bitmap:Bitmap = new Bitmap(_smallImage); addChild(bitmap); // add the zoomed region to the scaled down image's right bitmap = new Bitmap(_zoomedImage); bitmap.x = bitmap.width; addChild(bitmap); } /** * Draws the zoomed region of the iamge based on the x/y position. * * @param x The left coordinate of the zoomed region. * @param x The top coordinate of the zoomed region. */ private function drawZoomedImage(x:Number, y:Number):void { // make sure that x/y is within limits based on image size x = Math.min(x, _largeImage.width - _zoomedImage.width); y = Math.min(y, _largeImage.height - _zoomedImage.height); // the region to draw var rectangle:Rectangle = new Rectangle(x, y, _zoomedImage.width, _zoomedImage.height); _zoomedImage.copyPixels(_largeImage, rectangle, new Point()); } /** * Handler for when the mouse moves. This updates the zoomed image if the mouse * is over the scaled down image. * * @param event Event dispatched by stage. */ private function onStageMouseMove(event:MouseEvent):void { var x:Number = event.localX; var y:Number = event.localY; // only update zoomed image if mouse is over scaled down image if (getChildAt(0).hitTestPoint(x, y)) { drawZoomedImage(x/_scale, y/_scale); } } /** * Handler for when the image loads. This calls the methods to create and draw the bitmaps * and sets up a listener for when the mouse moves. * * @param event Event dispatched by stage. */ private function onImageLoaded(event:Event):void { var loaderInfo:LoaderInfo = event.target as LoaderInfo; var bitmap:Bitmap = loaderInfo.content as Bitmap; // save data for loaded, large image, which will be used to draw the zoomed region _largeImage = bitmap.bitmapData; createBitmaps(); // initial zoomed region will be top left of loaded bitmap drawZoomedImage(0, 0); stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } } }