Browse the notebook

Month: July 2009

Gesture Capture

var canvas:Shape = Shape(addChild(new Shape()));
var gestures:Array=[];
var gestureNum:int = 0;
var capGesture:Array;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
function onDown(evt:MouseEvent):void{
	 capGesture=[];
	 addEventListener(Event.ENTER_FRAME, onCapture);
	
	 canvas.graphics.lineStyle(3, 0xFF0000);
	 canvas.x = mouseX;
	 canvas.y = mouseY;
	 canvas.graphics.moveTo(0, 0);
}
function onUp(evt:MouseEvent):void{
	gestures.push(capGesture.concat());
	gestureNum++;
	 canvas.graphics.clear();
	removeEventListener(Event.ENTER_FRAME, onCapture);
}
function onCapture(evt:Event):void{
	capGesture.push(new Point(canvas.mouseX, canvas.mouseY));
	canvas.graphics.lineTo(canvas.mouseX, canvas.mouseY);
}

var currGesture:Array;
var drawing:Boolean = false;
var lineThickness:Number = 0;
var lineColor:Number = 0x000000;
var index:int = 0;
var pnt:Point;
var trans:Matrix = new Matrix();
var i:int
addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void{
	if (gestureNum > 0){
		if (!drawing){
		  currGesture = gestures[int(Math.random() * gestureNum)].concat();
		  trans.identity();
		  trans.rotate(Math.random()*6.28);
		  var scale:Number = Math.random() * 2 + .1;
		  trans.scale(scale, scale);
		  trans.tx = Math.random() * stage.stageWidth 
		  trans.ty = Math.random() * stage.stageHeight
		  for (i = 0; i<currGesture.length; i++){
			 currGesture[i] = trans.transformPoint(currGesture[i]);  
		  }
		  lineThickness = Math.random() * Math.random() * 50;
		  if (int(Math.random()*10) ==1){
			 var col:uint = uint(Math.random()*255);
		     lineColor = col << 16 | col << 8 | col;
		  }
		  index = 0;
		  drawing = true;
		  graphics.lineStyle(lineThickness, lineColor);
		}else{
		   for (i = 0; i<10; i++){
			    if (drawing == true){
				   pnt = currGesture[index];
				   if (index == 0){
					  graphics.moveTo(pnt.x, pnt.y);  
				   }else{
					  graphics.lineTo(pnt.x, pnt.y);
				   }
				   index++;
				   if (index == currGesture.length){
					   drawing = false;
				   }
		  	   }
		   }
		}
	}
}

This snippet is an idea I have been meaning to try for sometime. It’s a mini-drawing program. You can draw single gestures (shapes, letters etc…) and the program then randomly scales, rotates, tints and translates these gestures repeatedly on the canvas. You can continue to draw as it does this, the more gestures you draw, the more the program will have to randomly choose from.


Have a look at the swf here…

Read snippet: Gesture Capture

setVector() CA Texture

[SWF(width=500, height=500)]
var canvasSize:int=stage.stageWidth;
var canvas:BitmapData=new BitmapData(canvasSize,canvasSize,false,0x000001);
addChild(new Bitmap(canvas, "auto", true));
var size:int=canvas.width*canvas.height - canvasSize;
var pixels:Vector.<uint>=canvas.getVector(canvas.rect);
for (var i:int = 0; i<canvasSize; i++) {
	var xp:int=int(Math.random()*canvasSize);
	var yp:int=int(Math.random()*canvasSize);
	pixels[xp+yp*canvasSize]=0xFF000000;
}
var targetCol:uint=0xFF000000;
var buffer:Vector.<uint>=pixels.concat();
var fade:uint=1;

addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void {
	var curr:uint=targetCol;
	var r:int = (curr >> 16) & 0xFF;
	var g:int = (curr >> 8) & 0xFF;
	var b:int=curr&0xFF;
	r+=fade;
	g+=fade;
	b+=fade;
	if (r>255) r=255;
	if (g>255) g=255;
	if (b>255) b=255;
	var darker:uint=0xFF000000|r<<16|g<<8|b;
	if (darker==0xFFFFFFFF) {
		removeEventListener(Event.ENTER_FRAME, onLoop);
	}
	for (var i:int = canvasSize; i<size; i++) {
		curr=pixels[i];
		if (curr==targetCol) {
			var index:int=i-canvasSize+int(Math.random()*3) - 1;
			if (index>0) buffer[index]=darker;
			if (int(Math.random()*50)==1) {
				index=i-canvasSize+int(Math.random()*3)-1;
				if (index>0) buffer[index]=darker;
			}
		}
	}
	targetCol=darker;
	canvas.lock();
	canvas.setVector(canvas.rect, buffer);
	pixels=buffer.concat();
	canvas.unlock();
}

This snippet uses setVector() to draw something that looks like this:

This is a cellular automaton. It has kind of a strange rule set – but you could easily use this snippet to do more traditional cellular automata.

Read snippet: setVector() CA Texture