package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BitmapDataChannel; import flash.display.Shape; import flash.filters.BlurFilter; import flash.filters.DisplacementMapFilter; import flash.filters.DisplacementMapFilterMode; import flash.geom.Matrix; import flash.geom.Point; [SWF(width=600, height=400, backgroundColor=0xEEEEEE)] /** * Tests application of the DisplacementMapFilter to a loaded image. */ public class graphic_flex_image_effects_02_Flex_DisplacementMapFilterTest extends graphic_flex_image_effects_02_Flex_ImageFilterTest { /** * Constructor. Sends image path to super method. */ public function graphic_flex_image_effects_02_Flex_DisplacementMapFilterTest() { super("graphic-flex-image-effects-02-assets-checkers.jpg"); } /** * Draws a shape, then uses it to displace the loaded image. */ override protected function applyFilter():void { // darws a white circle var shape:Shape = new Shape(); shape.graphics.beginFill(0xFFFFFF); var radius:Number = (Math.min(_bitmap.width, _bitmap.height) - 150)/2; shape.graphics.drawCircle(radius, radius, radius); shape.graphics.endFill(); // creates a BitmapData to draw the shape into var map:BitmapData = new BitmapData(_bitmap.width, _bitmap.height, false, 0xFF000000); var matrix:Matrix = new Matrix(); // matrix is used to draw shape into middle of bitmap matrix.translate((_bitmap.width - radius*2)/2, (_bitmap.height - radius*2)/2); map.draw(shape, matrix); // blur is applied to create more levels of gray var blurFilter:BlurFilter = new BlurFilter(80, 80); map.applyFilter(map, map.rect, new Point(), blurFilter); // map is positioned and added to stage var mapBitmap:Bitmap = new Bitmap(map); mapBitmap.y = _bitmap.y; _bitmap.x = mapBitmap.width; addChild(mapBitmap); // displacement of image using the drawn map var filter:DisplacementMapFilter = new DisplacementMapFilter(); filter.mapBitmap = map; filter.componentX = BitmapDataChannel.RED; filter.componentY = BitmapDataChannel.RED; filter.scaleX = 30; filter.scaleY = 30; // pixels outside range won't wrap; instead, pixels will be repeated filter.mode = DisplacementMapFilterMode.CLAMP; _bitmap.filters = [filter]; } } }