Sunday, December 12, 2010

Re-Painting Pixels with Actionscript 3

In this little experiment I am reading in all of the pixel values from the image of the strawberries below using the BitmapData class. I then attach a MovieClip and color it to match the pixel value. This is animated over time to give off a nice effect. Each time it runs through I am choosing a random scale value for the MovieClips. It’s fun to watch this thing run for a while to see the different patterns that it makes. Click on the image below to check it out.


[as]var bmd:BitmapData = new BitmapData(tucc.width, tucc.height);
bmd.draw(tucc);

var xc:int = 1;
var yc:int = 0;

var scale:Number = Math.random()*10;
var t:Timer = new Timer(1);
t.addEventListener(TimerEvent.TIMER, drawIt);
t.start();

var rots:Array = [90,270,180];
var rotCount = 0;

var cont:MovieClip = new MovieClip();
this.addChild(cont);

function drawIt(e:TimerEvent):void
{
var d:dot = new dot();
var colorTransform:ColorTransform = d.transform.colorTransform;
colorTransform.color = bmd.getPixel(xc, yc);
d.transform.colorTransform = colorTransform;
cont.addChild(d);
/*var am:Number = Math.random();
if(am<0.25) d.rotation = rots[0];
if(am<0.50 && am>0.25) d.rotation = rots[1];
if(am<0.75 && am>0.50) d.rotation = rots[2];*/
d.rotation = rots[rotCount];
if(rotCount == 2) rotCount = 0;
else rotCount++;
d.scaleX = d.scaleY = scale;
d.x = xc*2-10;
d.y = yc*2+20;
if(xc >= 500 && yc >= 333)
{
trace(“in”);
t.stop();
clearIt();
}
else if(xc >= 500)
{
xc = 0;
yc += 15;
}

xc += 15;
}

function clearIt():void
{
this.removeChild(cont);
cont = new MovieClip();
this.addChild(cont);
xc = 0;
yc = 0;
scale = Math.random()*8;
t.start();
}[/as]

No comments:

Post a Comment