Archive for the 'actionscript' Category

Microscopy Sketch (emboss)

Monday, September 29th, 2008

I've been tweaking a programmatic sketch for the past couple days... It contains three cell-like elements that randomly move around the screen. I've been trying to decide how to take it further...



Click above image to view flash version

To take it farther I just want to add some additional elements and maybe change the cells behavior a bit - something more than just floating around randomly - I'd like to have them eat other little cells - or change/die over time...

A few years back I wrote something like this in director. It runs super fast now because I put the framerate at 999... too lazy to go through my archives and dig up the .dir... so here's a still:

Using a convolution filter to create an emboss works nicely, but is very very slow. You can create an emboss effect by using copyPixels(), draw(), blend modes and a BlurFilter:

Actionscript:
  1. var canvas:BitmapData = new BitmapData(500, 500, true, 0xFFFFFFFF);
  2. var color:BitmapData= new BitmapData(500, 500, true, 0x81666666);
  3. var over:BitmapData = new BitmapData(500, 500, true, 0xFF000000);
  4. addChild(new Bitmap(canvas, "auto", true));
  5.  
  6. var circle:Shape = new Shape();
  7. circle.graphics.beginFill(0xFFFFFF,1);
  8. circle.graphics.drawCircle(0,0,50);
  9.  
  10.  
  11. var m:Matrix = new Matrix();
  12.  
  13. m.tx = 0;
  14. m.ty = 1;
  15.  
  16. addEventListener(Event.ENTER_FRAME, onLoop);
  17. function onLoop(evt:Event):void {
  18.  
  19.     circle.x = mouseX;
  20.     circle.y = mouseY;
  21.     canvas.draw(circle, circle.transform.matrix);
  22.  
  23.     canvas.copyPixels(color, color.rect, new Point(0,0), null, null, true);
  24.    
  25.     over.copyPixels(canvas, canvas.rect, new Point(0,0), null, null, true);
  26.  
  27.     canvas.draw(over, m, null, BlendMode.SCREEN);
  28.     over.applyFilter(over, over.rect, new Point(-2,-2), new BlurFilter(10,10,1));
  29.  
  30.     canvas.draw(over, m, null, BlendMode.SUBTRACT);
  31. }

I also did this kind of emboss in processing a few years back. The meat of that looked like this:

JAVA:
  1. ellipse(mouseX,mouseY,60,60);
  2.  
  3.   for(int i=0; i<(width*height)-offset; i++){
  4.     pixels[i] = ((int)((~pixels[i] & 0xff)*0.25f + (pixels[i+offset] & 0xff) * 0.75f)*65793);
  5.   }

City From Fire (Cellular Automata)

Saturday, September 27th, 2008

This is another sketch dug up from my Cellular Automata folder. As I mentioned in my previous post about Cellular Automata - I like to base the rules on the color of the current pixel and its neighbors. This example looks like a city being built from fire:





Click either of the above images to view flash version

If you watch this for about 20 or 30 seconds nearly all the fire should disappear - leaving red pixels wandering through gray corridors.

Once you've got a basic template for this type of CA... you can just play with the code. After awhile you can even begin to predict some of the results and really control what your CA does. The following code snippet should help you get started... it creates 300 vertically wandering red pixels:

Actionscript:
  1. // current pixel
  2. var pix:uint;
  3.  
  4. var size:Number = 200;
  5. // read from the pixels BitmapData and write to the buffer BitmapData
  6. var pixels:BitmapData = new BitmapData(size,size, false, 0x000001);
  7. var buffer:BitmapData = new BitmapData(size,size, false, 0x000001);
  8.  
  9. var frame:Bitmap = new Bitmap(pixels);
  10. addChild(frame);
  11.  
  12. frame.scaleX= 2;
  13. frame.scaleY = 2;
  14.  
  15. // place 300 red pixels
  16. for (var i = 0; i<300; i++) {
  17.     buffer.setPixel(100 + Math.random()*40-20, 100 + Math.random()*40-20, 0xFF0000);
  18. }
  19.  
  20. addEventListener(Event.ENTER_FRAME, onLoop);
  21. function onLoop(evt:Event):void {
  22.     for (var i:int = 0; i<size; i++) {
  23.         for (var j:int = 0; j<size; j++) {
  24.             pix = pixels.getPixel(j, i);
  25.             // add additional logic here:
  26.             if (pix == 0xFF0000) {
  27.                   buffer.setPixel(j, i, 0x000001);
  28.                   buffer.setPixel(j, i+Math.random()*4-2, 0xFF0000);
  29.                   // and here
  30.             }
  31.         }
  32.     }
  33.     pixels.draw(buffer);
  34. }

Ceiling Scape

Tuesday, September 23rd, 2008

I've been enjoying Keith Peters recent linescapes...

I started messing with something inspired by those and this is what I ended up with this:





Click either of the above images to see the flash version

In the end, these look less like a terrain and more like the ceiling of an alien cathedral...

The basic code behind the texture is pretty simple. Here's a demo you can run in your flash timeline:

Actionscript:
  1. var canvas:BitmapData = new BitmapData(550,400,true, 0xFFFFFFFF);
  2. addChild(new Bitmap(canvas, "auto", true));
  3.  
  4. var wave:Shape = new Shape();
  5.  
  6. var g:Graphics = wave.graphics;
  7.  
  8. for (var j:int = 0; j<160; j++) {
  9.     g.clear();
  10.     g.beginFill(0xffffff,.5);
  11.     g.lineStyle(1,0x000000,.5);
  12.     for (var i:int = 0; i<400; i++) {
  13.         var xp:Number = i*2;
  14.         var yp:Number = 40 - 30 * Math.sin((i*j) * Math.PI/180 );
  15.         if (i == 0) {
  16.             g.moveTo(xp, yp);
  17.         } else {
  18.             g.lineTo(xp, yp);
  19.         }
  20.     }
  21.     g.lineTo(xp, 100);
  22.     g.lineTo(0,100);
  23.     wave.y += 2;
  24.     canvas.draw(wave, wave.transform.matrix);
  25. }

If you run the above code you'll end up with this: