topical media & game development

talk show tell print

#graphic-flex-image-effects-03-Flex-GetPixelTest.ax

#graphic-flex-image-effects-03-Flex-GetPixelTest.ax [swf] [flash] flex


  package {
  
          import flash.display.Bitmap;
          import flash.display.BitmapData;
          import flash.events.MouseEvent;
  
          [SWF(width=1200, height=600, backgroundColor=0x000000)]
  
          
Colors background based on pixel color below mouse.

  
          public class @ax-graphic-flex-image-effects-03-Flex-GetPixelTest extends graphic_flex_image_effects_03_Flex_DualImageTest {
  
                  private var _screenshot:BitmapData;
  
                  
Takes image grab of stage so that colors can be retrieved from it, then sets up a listener for when the mouse moves.

  
                  override protected function operateOnImages():void {
                          _screenshot = new BitmapData(stage.stageWidth, stage.stageHeight);
                          _screenshot.draw(stage);
                          stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
                  }
  
                  
Handler for when the mouse moves. This recolors the background based on the pixel color below the mouse.
parameter: event Event dispatched by Stage.

  
                  private function onStageMouseMove(event:MouseEvent):void {
                          var x:Number = event.localX;
                          var y:Number = event.localY;
                          // grabs color from screenshot
                          var color:uint = _screenshot.getPixel(x, y);
                          // colors whole background
                          graphics.clear();
                          graphics.beginFill(color);
                          graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
                          graphics.endFill();
                  }
  
          }
  
  }
  


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