grid - auto
Some random boxes timing test. Drawing is expensive!
- Change value to generate new pseudo random boxes.
- Click box to split.
Enjoy ;)
/**
* Copyright chiqui ( http://wonderfl.net/user/chiqui )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/njqP
*/
package {
import com.greensock.*;
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
/**
* generative grid
*/
public class Main extends Sprite {
private var CANT_BOXES:int = 50;
private var boxes:Array;
private var tt:TextField;
private var input:TextField;
private var timeoutChange:int;
private var boxHolder:Sprite;
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
boxHolder = new Sprite();
this.addChild(boxHolder);
input = new TextField();
input.defaultTextFormat = new TextFormat("_sans", 10);
input.type = TextFieldType.INPUT;
input.width = 45;
input.background = true;
input.text = "50";
input.restrict="0-9";
input.addEventListener(Event.CHANGE, onInputChange);
this.addChild(input);
tt = new TextField();
tt.defaultTextFormat = new TextFormat("_sans", 10);
tt.autoSize = TextFieldAutoSize.LEFT;
tt.x = 50;
tt.alpha = .8;
tt.background = true;
tt.text = "HOLA";
input.height = tt.height;
this.addChild(tt);
redraw();
}
private function onInputChange(e:Event):void {
clearTimeout(timeoutChange);
if(input.text == "") return
CANT_BOXES = int(input.text);
if (CANT_BOXES < 2) return;
if (CANT_BOXES < 2500) {
}else if (CANT_BOXES < 8000) {
tt.text = " This may take a while";
}else if (CANT_BOXES < 13000) {
tt.text = " This may crash your browser...";
}else {
tt.text = " Not doing this, too large.";
return;
}
timeoutChange = setTimeout(redraw, 1000);
}
private function redraw():void {
if (boxHolder.numChildren > 1) {
var len:int = boxHolder.numChildren;
for (var i:int = 0; i < len; i++) {
boxHolder.removeChildAt(0);
}
}
boxes = new Array();
var b:Box = new Box(this.stage.stageWidth, this.stage.stageHeight, 0, 0, boxHolder, Math.random()*0xAAAAAA, (Math.random()*0x555555)+0xAAAAAA, true, 0);
boxes.push(b);
this.stage.addChild(b);
this.stage.addEventListener(MouseEvent.CLICK, onClick);
this.stage.addEventListener(MouseEvent.MOUSE_OVER, onmover);
this.stage.addEventListener(MouseEvent.MOUSE_OUT, onmout);
var ms:int = getTimer();
divideRecusrsive();
tt.text = " Generated in "+String(getTimer() - ms) + " ms with odd squared lines. Click to split. Change first value to redraw.";
}
public function divideRecusrsive():void {
var newBoxes:Array = new Array();
var currBox:int = 0;
while (boxes.length < CANT_BOXES) {
newBoxes = boxes[currBox].divide();
boxes.splice(currBox, 1);
boxes = boxes.concat(newBoxes);
if (boxes.length >= CANT_BOXES) return
}
}
private function onmout(e:MouseEvent):void {
try {
var b:Box = Box(e.target);
TweenMax.to(b, .1, { alpha:1 } );
}catch (e:*) { return; }
}
private function onmover(e:MouseEvent):void {
try {
var b:Box = Box(e.target);
TweenMax.to(b, .1, { alpha:.5 } );
}catch (e:*) { return; }
}
private function onClick(e:MouseEvent):void {
try {
var b:Box = Box(e.target);
b.divide();
}catch (e:*) { return; }
}
}
}
import com.greensock.*;
import flash.display.*;
class Box extends flash.display.Sprite {
private var _ww:Number;
private var _hh:Number;
private var _ref:DisplayObjectContainer;
private var _ca:uint;
private var _cb:uint;
private var _lastDiv:Boolean;
public var _diveIndex:int;
public function Box(ww:Number, hh:Number, tx:Number, ty:Number, ref:DisplayObjectContainer, ca:uint, cb:uint, last:Boolean, divideIndex:int) {
_ww = ww;
_hh = hh;
_ref = ref;
_ca = ca;
_cb = cb;
_lastDiv = !last;
_diveIndex = divideIndex;
this.x = tx;
this.y = ty;
this.graphics.beginFill(blend(_ca, _cb, Math.random()), .8);
this.graphics.drawRect(0, 0, ww, hh);
this.graphics.endFill();
this.graphics.beginFill(_ca, 1);
var px:Number = ww * 30 / ref.stage.stageWidth;
var py:Number = hh * 30 / ref.stage.stageHeight;
if (py < px) px = py;
if (px < 1) px = 1;
this.graphics.drawCircle(ww / 2, hh / 2, px);
this.graphics.endFill();
TweenMax.from(this, .2, { alpha:0 } );
}
public function divide():Array {
var res:Array = new Array();
this.graphics.clear();
var off:int = 0;
var tv:int = 0;
var b:Box;
if (_lastDiv) {
tv = (_hh - off) * ((Math.random()/4)+.357);
b = new Box(_ww, tv, this.x, off+this.y, _ref,_ca,_cb, _lastDiv, _diveIndex+1);
}else {
tv = (_ww - off) * ((Math.random()/4)+.357);
b = new Box(tv, _hh, off+this.x, this.y, _ref,_ca,_cb, _lastDiv, _diveIndex+1);
}
off += tv;
_ref.addChild(b);
res.push(b);
if (_lastDiv) {
tv = _hh - off;
b = new Box(_ww, tv, this.x, off+this.y, _ref,_ca,_cb, _lastDiv, _diveIndex+1);
}else {
tv = _ww - off;
b = new Box(tv, _hh, off+this.x, this.y, _ref,_ca,_cb, _lastDiv, _diveIndex+1);
}
res.push(b);
_ref.addChild(b);
this.parent.removeChild(this);
return res;
}
public static function blend(first:uint, second:uint, percent:Number):uint {
var r:int = ((first & 0xff0000) >> 16) * (1 - percent) + ((second & 0xff0000) >> 16) * percent;
var g:int = ((first & 0x00ff00) >> 8) * (1 - percent) + ((second & 0x00ff00) >> 8) * percent;
var b:int = ((first & 0x0000ff) ) * (1 - percent) + ((second & 0x0000ff) ) * percent;
return r << 16 | g << 8 | b;
}
}