Pixel Fun
Pixel Fun
* @author civet
*
* BUG: when CLearType enable (2009-11-10)
/**
* Pixel Fun
* @author civet
*
* BUG: when ClearType enable (2009-11-10)
*/
package {
import com.greensock.*;
import com.greensock.easing.*;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;
[SWF(frameRate='24', backgroundColor='0xffffff')]
public class PixelFun extends Sprite {
private static const DOT_SIZE:uint = 4;
private static const DOT_SPACE:uint = 1;
private var textfield:TextField;
private var source:BitmapData;
private var canvas:Sprite;
private var list:Array;
private var index:int = 0;
public function PixelFun() {
init();
}
private function init():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var str:String = "Hello World"
+ "|Ahhhhhhhhhhhhhhhh!"
+ "|The quick brown fox\njumps over the lazy dog.";
list = str.split("|");
textfield = new TextField();
textfield.defaultTextFormat = new TextFormat("_san", 12, 0);
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.cacheAsBitmap = true;
source = new BitmapData(128, 32, false, 0xffffff);//range: 128 x 32
canvas = new Sprite();
addChild(canvas);
onTick();
var t:Timer = new Timer(3000);
t.addEventListener(TimerEvent.TIMER, onTick);
t.start()
}
private function onTick(e:Event=null):void
{
clear();
textfield.text = list[ index++ ];
if(index > list.length-1) index = 0;
source.draw(textfield, null, null, null, null, true);
render();
}
private function clear():void
{
var i:int = canvas.numChildren;
while(i--) canvas.removeChildAt(i);
source.fillRect(new Rectangle(0, 0, source.width, source.height), 0xffffff);
}
private function render():void
{
var w:uint = source.width;
var h:uint = source.height;
var i:uint;
var j:uint;
var color:uint;
var dot:Shape;
var SCALE:uint = DOT_SIZE + DOT_SPACE;
for(i = 0; i < h; ++i) {
for(j = 0; j < w; ++j) {
color = source.getPixel(j, i);
if(color < 0xffffff) {
dot = createDot(color, DOT_SIZE);//0xfff << (j >> 2)
dot.x = int(Math.random()* textfield.width* SCALE);
dot.y = int(Math.random()* textfield.height* SCALE);
canvas.addChild(dot);
//new GTween(dot, .5, {x: j* SCALE, y: i* SCALE}, {ease: Back.easeOut});
TweenLite.to(dot, .5, {x: j* SCALE, y: i* SCALE, ease:Back.easeOut}); }
}
}
}
private function createDot(color:uint, size:uint):Shape
{
var s:Shape = new Shape();
s.graphics.beginFill(color, 1);
s.graphics.drawRect(0, 0, size, size);
s.graphics.endFill();
return s;
}
}
}