Tag Archives: copyPixels

10,000 Transparent Sprites

Actionscript:
  1. stage.frameRate = 30;
  2.  
  3. var imageNum:int = 10000;
  4. var point:Point = new Point(0,0);
  5. var s:Sprite = new Sprite();
  6. s.graphics.beginFill(0xCCCCCC);
  7. s.graphics.lineStyle(0,0x000000);
  8. s.graphics.drawCircle(3,3,3);
  9. s.alpha = .1;
  10.  
  11. var nested:Sprite = new Sprite();
  12. nested.addChild(s);
  13. var image:BitmapData = new BitmapData(s.width, s.height, true, 0x00000000);
  14. image.draw(nested);
  15.  
  16. var canvas:BitmapData = new BitmapData(400,400, true, 0xFFFFFFFF);
  17. addChild(new Bitmap(canvas));
  18.  
  19. var xPos:Array = new Array();
  20. var yPos:Array = new Array();
  21. for (var i:int = 0; i<imageNum; i++) {
  22.     xPos.push(Math.random()*400);
  23.     yPos.push(Math.random()*400);
  24. }
  25.  
  26. addEventListener(Event.ENTER_FRAME, onLoop);
  27. function onLoop(evt:Event):void {
  28.     canvas.fillRect(new Rectangle(0,0,400,400), 0xFFFFFFFF);
  29.     var div:Number;
  30.     for (var i:int = 0; i<imageNum; i++) {
  31.         div  = (i / 100)+2;
  32.         xPos[i] += (mouseX - xPos[i])/div;
  33.         yPos[i] += (mouseY - yPos[i])/div;
  34.         point.x = xPos[i];
  35.         point.y = yPos[i];
  36.         canvas.copyPixels(image, image.rect, point, null, null, true);
  37.     }
  38. }

This is from my other blog but I figured it was worth posting here. It draws 10'000 transparent circles that each follow the mouse at different speeds. This is achieved using copyPixels().

Posted in BitmapData, motion | Also tagged , | 2 Comments