Spread - Grid
Test about pixel spread
/**
* Copyright ne_ ( http://wonderfl.net/user/ne_ )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ylIa
*/
package {
import flash.text.TextField;
import flash.events.TimerEvent;
import flash.geom.Rectangle;
import flash.utils.Timer;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private var w:int = 30;
private var h:int = 30;
private var data:BitmapData = new BitmapData(w,h,true,0xFF000000);
private var mk:BitmapData = data.clone();
private var bmp:Bitmap = new Bitmap(data);
private var timer:Timer = new Timer(30);
private var nList:Array = new Array();
private var acolor:uint = 0;
private var text:TextField = new TextField();
private var mcolor:uint = 0xFF000000;
public function FlashTest() {
var b:Bitmap = new Bitmap(mk);
addChild(b);
addChild(bmp);
//addChild(text);
b.scaleX = b.scaleY = bmp.scaleX = bmp.scaleY = 7;
b.y = bmp.height;
// text.x = bmp.width, text.y = bmp.height;
// text.width = 200;
//text.border = true;
bmp.smoothing = false;
timer.addEventListener(TimerEvent.TIMER,next);
init();
}
private function init ():void{
acolor = (0xff<<24) + uint(Math.random()*0xFFFFFF);
var c:int = int(Math.random()*w);
var l:int = int(Math.random()*h);
mk.fillRect(mk.rect,0xFF00FF00);
mk.setPixel32(c,l,mcolor);
//c = 5
//l = 5
// text.appendText(String([c,l]+" >> inicio \n"));
getNext([c,l]);
timer.start();
}
private function next(t:TimerEvent = null):void{
var a:Array = nList.splice(int(Math.random()*nList.length),1);
getNext(a[0]);
}
private function getNext (a:Array):void{
var c:int = a[0];
var l:int = a[1];
//data.setPixel32(c,l,acolor);
data.setPixel32(c,l,vary(acolor));
if(c > 0 && mk.getPixel32(c - 1, l) !== mcolor){
nList.push([c-1,l]);
mk.setPixel32(c-1,l,mcolor);
}
if(c < w-1 && mk.getPixel32(c + 1, l) !== mcolor){
nList.push([c+1,l])
mk.setPixel32(c+1,l,mcolor);
}
if(l > 0 && mk.getPixel32(c,l-1) !== mcolor){
nList.push([c,l-1])
mk.setPixel32(c,l-1,mcolor);
}
if(l < h-1 && mk.getPixel32(c,l+1) !== mcolor){
nList.push([c,l+1])
mk.setPixel32(c,l+1,mcolor);
}
if(!nList.length){
timer.stop();
init();
}
}
private function vary(c:uint):uint{
var i:int = Math.random()*85;
var a:uint = c >> 24 & 0xFF;
var r:uint = c >> 16 & 0xFF;
var g:uint = c >> 8 & 0xFF;
var b:uint = c & 0xFF;
if(int(Math.random()*2)){
return uint((a << 24) | (j(r-i) << 16) | (j(g-i) << 8) | j(b-i) )
}
return uint((a << 24) | (j(r+i) << 16) | (j(g+i) << 8) | j(b+i) )
}
private function j(i:int):int{
if(i<0){
return 0;
}if(i>255){
return 255;
}
return i;
}
}
}