drawPath() Boxes

const TWO_PI:Number = Math.PI * 2;
var boxNum:int = 3000;
var pointNum:int = boxNum * 10;
var rot:Vector.<Number> = new Vector.<Number>();
var posX:Vector.<Number> = new Vector.<Number>();
var posY:Vector.<Number> = new Vector.<Number>();
var velX:Vector.<Number> = new Vector.<Number>();
var velY:Vector.<Number> = new Vector.<Number>();
var scale:Vector.<Number> = new Vector.<Number>();
var rotSpeed:Vector.<Number> = new Vector.<Number>();
var geometry:Vector.<Number> = new Vector.<Number>();
var cmds:Vector.<int> = new Vector.<int>();

var stageWidth:Number = stage.stageWidth;
var stageHeight:Number = stage.stageHeight;

populateGeom();
function populateGeom():void{
	for (var i:int = 0; i<pointNum; i+=10){
		posX.push(Math.random() * stageWidth);
		posY.push(Math.random() * stageHeight);
		velX.push(Math.random()*4 - 2);
		velY.push(Math.random()*4 - 2);
		scale.push(Math.random() * 1 + .2);
		rot.push(Math.random() * TWO_PI);
		rotSpeed.push(Math.random() * 0.2 - 0.1);
		geometry[i] = 0;
		geometry[i + 1] = 0;
		cmds.push(1);
		geometry[i + 2] =  0;
		geometry[i + 3] =  0;
		cmds.push(2);
		geometry[i + 4] =  0;
		geometry[i + 5] =  0;
		cmds.push(2);
		geometry[i + 6] = 0;
		geometry[i + 7] =  0;
		cmds.push(2);
		geometry[i + 8] =  0;
		geometry[i + 9] =  0;
		cmds.push(2);
	}
}

function run():void{
	var inc:int = 0;
	var xt:Number, yt:Number, s:Number, r:Number, rs:Number;
	var cos:Number, sin:Number;
	var cosN:Number, cosP:Number, sinN:Number, sinP:Number;
	for (var i:int = 0; i<pointNum; i+=10){
		xt = posX[inc] += velX[inc];
		yt = posY[inc] += velY[inc];
		
		if (xt < 0 || xt > stageWidth){
			velX[inc] *= -1;
		}
		if (yt < 0 || yt > stageHeight){
			velY[inc] *= -1;
		}
		 
		s = scale[inc];
		r = rot[inc] += rotSpeed[inc];
		inc++;
		 
		cos = Math.cos(r);
		sin = Math.sin(r);
		
        cosN = -10  * cos;
		cosP = 10  * cos
		sinN = -10 * sin;
		sinP = 10 * sin;
		geometry[i] = xt + (cosN - sinN) * s;
		geometry[i + 1] = yt + (cosN + sinN) * s;
		geometry[i + 2] = xt + (cosP - sinN) * s;
		geometry[i + 3] = yt + (cosN + sinP) * s;
		geometry[i + 4] = xt + (cosP - sinP) * s;
		geometry[i + 5] = yt + (cosP + sinP) * s;
		geometry[i + 6] = xt + (cosN - sinP) * s;
		geometry[i + 7] = yt + (cosP + sinN) * s;
		geometry[i + 8]  = geometry[i];
		geometry[i + 9]  = geometry[i + 1];
	}
}

addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void {
     run();
	 graphics.clear();
	 graphics.beginFill(0x000000);
	 graphics.drawPath(cmds, geometry,GraphicsPathWinding.NON_ZERO);
	 graphics.endFill();
}

I was messing with drawPath() today and did this snippet as a sort of performance test… the code isn’t fully optimized but it’s clear that drawPath() is rather fast. I wish that there were beginFill() and lineStyle() drawing commands that worked with it though… oh yeah, that’s what IGraphicsData is for…

Leave a Reply

Your email address will not be published. Required fields are marked *