Circle of Particles #2

Actionscript:
  1. [SWF(width = 400, height = 400)];
  2. var canvas:BitmapData = new BitmapData(400,400, false, 0x000000);
  3. var eraser:BitmapData = new BitmapData(400,400, true, 0x11000000);
  4. addChild(new Bitmap(canvas));
  5.  
  6. var particles:Array = new Array();
  7. for (var i:int = 0; i <500; i++){
  8.     particles.push(makeParticle());
  9. }
  10.  
  11. addEventListener(Event.ENTER_FRAME, onLoop);
  12. function onLoop(evt:Event):void {
  13.     canvas.copyPixels(eraser, eraser.rect, new Point(0,0), null, null, true);
  14.     for (var i:int = 0; i <particles.length; i++){
  15.         particles[i]();
  16.     }
  17. }
  18.  
  19. function makeParticle():Function {
  20.     var dx:Number, dy:Number;
  21.     var x:Number = 200;
  22.     var y:Number = 200;
  23.     var vx:Number = Math.random() * 4 - 2;
  24.     var vy:Number = Math.random() * 4 - 2;
  25.     var ang:Number;
  26.     return function():void {
  27.               x += vx;
  28.               y += vy;
  29.               dx = 200 - x;
  30.               dy=  200 - y;
  31.                if (Math.sqrt((dx * dx) + (dy * dy))> 130){
  32.                 ang = Math.atan2(dy, dx) / Math.PI * 180;
  33.                  vx = Math.cos(ang);
  34.                  vy = Math.sin(ang);
  35.                }
  36.                canvas.setPixel(x, y, 0xFFFFFF);
  37.     }
  38. }

This is an interesting variation on yesterdays post. The result will looks like this...


Click to view swf...

I also decided to do a processing port of this with 3,000 particles:

Processing Version

The processing version eventually evolved into this:


Click for enlarged version...

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

2 Comments

  1. Posted April 8, 2009 at 4:36 am | Permalink

    thanks, i have been wondering for ages how to quickly shift all pixels towards the background colour… copy all the pixels of a transparent bitmap data. love your site.

  2. Posted April 8, 2009 at 5:00 am | Permalink

    no problem… glad you like the site :)

Post a Comment

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

*
*