package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BlendMode; import flash.display.GradientType; import flash.display.Shape; import flash.geom.Matrix; [SWF(width=600, height=300, backgroundColor=0x333333)] /** * Tests application of the ERASE blend mode. */ public class graphic_flex_image_effects_02_Flex_EraseModeTest extends graphic_flex_image_effects_02_Flex_AbstractImageLoader { /** * Constructor. Sends image path to super method. */ public function graphic_flex_image_effects_02_Flex_EraseModeTest() { super("graphic-flex-image-effects-02-assets-harbor.jpg"); } /** * Called from super class when image completes load and is ready * to be processed. This draws a mask shape and uses this with * the ERASE blend mode to apply it to the image in order to mask it. */ override protected function runPostImageLoad():void { var width:Number = stage.stageWidth/2; var height:Number = stage.stageHeight; // creates a gradient donut, more or less var colors:Array = [0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF]; var alphas:Array = [0, 1, 1, 0]; var ratios:Array = [80, 150, 200, 235]; var matrix:Matrix = new Matrix(); matrix.createGradientBox(width, height); var shape:Shape = new Shape(); shape.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix); shape.graphics.drawCircle(width/2, height/2, width/2); shape.graphics.endFill(); addChild(shape); // places the loaded image to the right of the drawn shape _loadedBitmap.x = width; addChild(_loadedBitmap); // draws shape into BitmapData to be used as mask over the bitmap var mask:BitmapData = new BitmapData(width, height, true, 0x00000000); mask.draw(shape); var bitmap:Bitmap = new Bitmap(mask); bitmap.x = _loadedBitmap.x; // sets the mask to ERASE so that only its transparent pixels will reveal colors below it bitmap.blendMode = BlendMode.ERASE; addChild(bitmap); // you can swap these lines and have the same effect // cacheAsBitmap = true; blendMode = BlendMode.LAYER; } } }