forked from: flash on 2009-5-12
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_x:int = 0;
private var pixel_y:int = 0;
private var pixel_n:int = 0;
private var d_x:int = 50;
private var d_y:int = 10;
private var rgb_array:Array = new Array();
private var rgb_count:int = 0;
private var rgb_list:Array = [0xFF0000, 0x00FF00, 0x0000FF];
private var ball:Sprite = new Sprite();
private var line:Sprite = new Sprite();
private var line_array:Array = new 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;
for(var i:int=0;i<3;i++) {
for(var img_y:int=1;img_y<=image_height;img_y++) {
for(var img_x:int=1;img_x<=image_width;img_x++) {
rgb_array.push(getColorArray(base.getPixel(img_x, img_y), i));
}
}
}
for(var z:int=0;z<rgb_list.length;z++) {
line_array[z] = new Sprite();
addChild(line_array[z]);
}
addChild(ball);
addChild(line);
line.filters = [new GlowFilter()];
drawBall(ball);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function drawBall(sp:Sprite):void
{
sp.graphics.lineStyle(1, 0xffffff, 1.0, false, "none");
sp.graphics.moveTo (-6, 0);
sp.graphics.lineTo(6, 0);
sp.graphics.moveTo (0, -6);
sp.graphics.lineTo(0, 6);
sp.graphics.moveTo (-6, -6);
sp.graphics.lineTo(6, 6);
sp.graphics.moveTo (6, -6);
sp.graphics.lineTo(-6, 6);
sp.filters = [new GlowFilter()];
}
private function moveBall(x:Number, y:Number):void
{
ball.x = x;
ball.y = y;
ball.alpha = Math.random() * 100;
ball.rotation += (Math.round(Math.random()) == 1 ? 1 : -1) * Math.random() * 10;
ball.scaleX = Math.random() * 1.5;
ball.scaleY = ball.scaleX;
}
private function drawLine(x:Number, y:Number):void
{
line.graphics.clear();
line.graphics.lineStyle(1.0, 0xffffff, 0.3, false, "none");
line.graphics.moveTo (stage.stageWidth, stage.stageHeight);
line.graphics.lineTo(x, y);
}
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 = line_array[rgb_count];
for(var i:int=0;i<d_x;i++) {
if(pixel_n > rgb_array.length) {
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeChild(ball);
removeChild(line);
ball = null;
line = null;
break;
}
sp.graphics.lineStyle(1, rgb_array[pixel_n], 1.0, false, "none");
from_x = pixel_x + image_x;
from_y = pixel_y + image_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);
moveBall(to_x, to_y);
drawLine(to_x, to_y);
setChildIndex(ball,this.numChildren-1);
setChildIndex(line,this.numChildren-1);
if(pixel_x >= image_width) {
pixel_x = 1;
pixel_y++;
} else {
pixel_x++;
}
if(pixel_y >= image_height) {
pixel_y = 0;
rgb_count++;
ball.filters = [new GlowFilter(rgb_list[rgb_count], 1.0)];
line.filters = [new GlowFilter(rgb_list[rgb_count], 1.0)];
}
pixel_n++;
}
}
private function getColorArray(c:Number, i:int):String
{
var r:Number = (c & 0xFF0000) >> 16;
var g:Number = (c & 0xFFFF00) >> 8;
//var b:Number = c & 0x0000FF;
var c_string:String = "0x" + getRgbString(c);
var r_string:String = "0x" + getRgbString(r << 16);
var g_string:String = "0x" + getRgbString(g << 8);
//var b_string:String = "0x" + getRgbString(b);
//var tmp:Object = {r:r_string, g:g_string, b:b_string};
if(i == 0) {
return r_string;
} else if(i == 1) {
return g_string;
}
return c_string;
//return tmp;
}
private function getRgbString(c:Number):String
{
var rgb:String = ("00000" + c.toString(16)).substr(-6).toUpperCase();
return rgb;
}
}
}