forked from: flash on 2009-5-26

by set0 forked from flash on 2009-5-26 (diff: 29)
♥0 | Line 85 | Modified 2009-05-29 22:09:12 | MIT License
play

ActionScript3 source code

/**
 * Copyright set0 ( http://wonderfl.net/user/set0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/63WI
 */

// forked from set0's flash on 2009-5-26
package 
{
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;
  import flash.system.*;
  import flash.geom.*;
  import flash.text.*;
  import flash.filters.*;
  
  [SWF(width=465, height=465, frameRate=60, backgroundColor=0xffffff)]
  public class FlashTest extends Sprite {
      
      private var image_width:Number = 0;
      private var image_height:Number = 0;
      private var image_x:Number = 0;
      private var image_y:Number = 0;
      private var pixel_n:int = 0;
      private var pixel_max:int = 0;
      private var image_array:Array = [];

      
      public function FlashTest()
      {
          var image_url:String = "http://farm2.static.flickr.com/1271/1168949483_4b0523cc19_m.jpg";
          
          var loader:Loader = new Loader();
          loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoadComplete);
          loader.load(new URLRequest(image_url), new LoaderContext(true));
          
      }
      
      private function onImageLoadComplete(e:Event):void
      {
         var loader:Loader = LoaderInfo(e.target).loader;
         var base:BitmapData = Bitmap(loader.content).bitmapData;
         var bmp:Bitmap = loader.content as Bitmap
         
         image_width = loader.width;
         image_height = loader.height;
         image_x = stage.stageWidth / 2 - image_width / 2;
         image_y = stage.stageHeight / 2 - image_height / 2;
         
         var count:int = 0;
         
         for(var img_y:int=1;img_y<=image_height;img_y++) {
             for(var img_x:int=1;img_x<=image_width;img_x++) {
                 image_array[count] = [];
                 image_array[count]["color"] = getColorArray(base.getPixel(img_x, img_y));
                 image_array[count]["x"] = String(img_x + image_x);
                 image_array[count]["y"] = String(img_y + image_y);
                 
                 count++;
             }
         }
         
         pixel_max = image_array.length;
         image_array.sortOn("color");
 
         addEventListener(Event.ENTER_FRAME, onEnterFrame);
       }

       private function onEnterFrame(e:Event):void
       {
           var from_x:Number = 0;
           var from_y:Number = 0;
           var to_x:Number = 0;
           var to_y:Number = 0;
           
           
           for(var i:int=0;i<100;i++) {
               if(pixel_n > pixel_max) {
                   removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                   return;
               }

			   drawPixel();
			   
               pixel_n++;
           }
       }
	   
	   private function drawPixel():void
	   {	
			var sp:Sprite = new Sprite();
          	addChild(sp);
			sp.graphics.lineStyle(1, image_array[pixel_n]["color"], 1.0, false, "none");
			sp.graphics.moveTo (0, 0);
	        sp.graphics.lineTo(1, 1);
			sp.x = int(image_array[pixel_n]["x"]);
            sp.y = int(image_array[pixel_n]["y"]);
			
			
	   }
       
       private function getColorArray(c:Number):String
       {
         var c_string:String = "0x" + getRgbString(c);
        
         return c_string;
       }
       
       private function getRgbString(c:Number):String
       {
           var rgb:String = ("00000" + c.toString(16)).substr(-6).toUpperCase();
           return rgb;
       }
  }
}

Forked