topical media & game development

talk show tell print

professional-flex-code-23-Reflection.ax

professional-flex-code-23-Reflection.ax [swf] flex


  package
  {
          import mx.core.UIComponent;
          import flash.display.DisplayObject;
          import flash.display.Bitmap;
          import flash.display.BitmapData;
          import flash.events.Event;
          import mx.events.FlexEvent;
          import flash.geom.Matrix;
          import flash.geom.Rectangle;
          import flash.display.Graphics;
          import flash.display.GradientType;
          import flash.display.Shape;
          import flash.geom.Point;
          import flash.display.BlendMode;
          import flash.utils.Timer;
          import flash.events.TimerEvent;
          import flash.filters.BlurFilter;
  
          public class @ax-professional-flex-code-23-Reflection extends UIComponent
          {
                  public var target:UIComponent;
                  public var bitmap:Bitmap = new Bitmap(new BitmapData( 1, 1, true, 0 ));
                  public var gr:Graphics;
                  public var fadeFrom:Number = 0.5;
                  public var fadeTo:Number = 0;
                  public var fadeCenter:Number = 0.4;
                  public var skewX:Number = 0;
                  public var scale:Number = 1;
                  public var delay:Number = 5;
                  
                  private var timer:Timer;
                  
                  public function @ax-professional-flex-code-23-Reflection():void
                  {        
                          addChild(bitmap);
                          addEventListener( FlexEvent.CREATION_COMPLETE, draw@ax-professional-flex-code-23-Reflection );
                          
                          timer = new Timer(delay);
                          timer.addEventListener(TimerEvent.TIMER, onTimerComplete);
                          timer.start();
                  }
                  
                  private function onTimerComplete(event:TimerEvent):void
                  {
                          draw@ax-professional-flex-code-23-Reflection();
                  }
                  
                  public function draw@ax-professional-flex-code-23-Reflection( e:Event = null ):void
                  {
                          if( this.width>0 && this.height>0 )
                          {
                                  // add blur
                                  var blur:BlurFilter = new BlurFilter();
                                  blur.blurX = 5;
                                  blur.blurY = 5;
                                  
                                  //draw reflection
                                  var bitmapData:BitmapData = new BitmapData(this.width, this.height, true, 0);
                                  var matrix:Matrix = new Matrix( 1, 0, skewX, -1*scale, 0, target.height );
                                  var rectangle:Rectangle = new Rectangle(0,0,this.width,this.height*(2-scale));
                                  var delta:Point = matrix.transformPoint(new Point(0,target.height));
                                  matrix.tx = delta.x*-1;
                                  matrix.ty = (delta.y-target.height)*-1;
                                  bitmapData.draw(target, matrix, null, null, rectangle, true);
                                  
                                  //add fade
                                  var shape:Shape = new Shape();
                                  var gradientMatrix:Matrix = new Matrix();
                                  gradientMatrix.createGradientBox(this.width,this.height, 0.5*Math.PI);
                                  shape.graphics.beginGradientFill(GradientType.LINEAR, new Array(0,0,0), new Array(fadeFrom,(fadeFrom-fadeTo)/2,fadeTo), new Array(0,0xFF*fadeCenter,0xFF), gradientMatrix)
                                  shape.graphics.drawRect(0, 0, this.width, this.height);
                                  shape.graphics.endFill();
                                  bitmapData.draw(shape, null, null, BlendMode.ALPHA);
                                  
                                  //apply result
                                  bitmap.bitmapData.dispose();
                                  bitmap.bitmapData = bitmapData;
                                  
                          }
                  }
          }
  }


(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.