setVector() CA Texture

Actionscript:
  1. [SWF(width=500, height=500)]
  2. var canvasSize:int=stage.stageWidth;
  3. var canvas:BitmapData=new BitmapData(canvasSize,canvasSize,false,0x000001);
  4. addChild(new Bitmap(canvas, "auto", true));
  5. var size:int=canvas.width*canvas.height - canvasSize;
  6. var pixels:Vector.<uint>=canvas.getVector(canvas.rect);
  7. for (var i:int = 0; i<canvasSize; i++) {
  8.     var xp:int=int(Math.random()*canvasSize);
  9.     var yp:int=int(Math.random()*canvasSize);
  10.     pixels[xp+yp*canvasSize]=0xFF000000;
  11. }
  12. var targetCol:uint=0xFF000000;
  13. var buffer:Vector.<uint>=pixels.concat();
  14. var fade:uint=1;
  15.  
  16. addEventListener(Event.ENTER_FRAME, onLoop);
  17. function onLoop(evt:Event):void {
  18.     var curr:uint=targetCol;
  19.     var r:int = (curr>> 16) & 0xFF;
  20.     var g:int = (curr>> 8) & 0xFF;
  21.     var b:int=curr&0xFF;
  22.     r+=fade;
  23.     g+=fade;
  24.     b+=fade;
  25.     if (r>255) r=255;
  26.     if (g>255) g=255;
  27.     if (b>255) b=255;
  28.     var darker:uint=0xFF000000|r<<16|g<<8|b;
  29.     if (darker==0xFFFFFFFF) {
  30.         removeEventListener(Event.ENTER_FRAME, onLoop);
  31.     }
  32.     for (var i:int = canvasSize; i<size; i++) {
  33.         curr=pixels[i];
  34.         if (curr==targetCol) {
  35.             var index:int=i-canvasSize+int(Math.random()*3) - 1;
  36.             if (index>0) buffer[index]=darker;
  37.             if (int(Math.random()*50)==1) {
  38.                 index=i-canvasSize+int(Math.random()*3)-1;
  39.                 if (index>0) buffer[index]=darker;
  40.             }
  41.         }
  42.     }
  43.     targetCol=darker;
  44.     canvas.lock();
  45.     canvas.setVector(canvas.rect, buffer);
  46.     pixels=buffer.concat();
  47.     canvas.unlock();
  48. }

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.

This entry was posted in BitmapData, Vector, pixel manipulation and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*