topical media & game development

talk show tell print

#graphic-flex-image-effects-02-Flex-EraseModeTest.ax

#graphic-flex-image-effects-02-Flex-EraseModeTest.ax [swf] [flash] flex


  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 @ax-graphic-flex-image-effects-02-Flex-EraseModeTest extends graphic_flex_image_effects_02_Flex_AbstractImageLoader {
  
                  
Constructor. Sends image path to super method.

  
                  public function @ax-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;
                  }
  
          }
  
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.