flash on 2009-5-26
/**
* Copyright set0 ( http://wonderfl.net/user/set0 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/81Dd
*/
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;
var sp:Sprite = new Sprite();
addChild(sp);
for(var i:int=0;i<100;i++) {
if(pixel_n > pixel_max) {
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
return;
}
sp.graphics.lineStyle(1, image_array[pixel_n]["color"], 1.0, false, "none");
from_x = int(image_array[pixel_n]["x"]);
from_y = int(image_array[pixel_n]["y"]);
to_x = from_x + 1;
to_y = from_y + 1;
sp.graphics.moveTo (from_x, from_y);
sp.graphics.lineTo(to_x, to_y);
pixel_n++;
}
}
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;
}
}
}