BitmapData Frame Texture

Actionscript:
  1. [SWF(width = 800, height = 600)]
  2. var circle:Shape = new Shape();
  3. var radius:Number = 4;
  4. var diameter:Number = radius * 2;
  5. var diam4:Number = diameter * 4;
  6. with(circle.graphics) beginFill(0x000000), drawCircle(diameter,diameter,radius);
  7. circle.filters = [new BlurFilter(5, 5, 2)];
  8.  
  9. var currFrame:Frame;
  10.  
  11. // populate the linked list
  12. generateAnimation();
  13.  
  14. var animationNum:int = 8000;
  15. var animation:Vector.<Frame> = new Vector.<Frame>();
  16. var locs:Vector.<Point> = new Vector.<Point>();
  17. // populate locs and animation
  18. while(animation.length <animationNum){
  19.        currFrame = currFrame.next;
  20.        animation.push(currFrame);
  21.        locs.push(new Point(Math.random() * stage.stageWidth - radius,
  22.                            Math.random() * (stage.stageHeight+diam4) - diam4));
  23. }
  24.  
  25. var rect:Rectangle = animation[0].bitmap.rect;
  26. var bottom:Number = stage.stageHeight + rect.height;
  27. var top:Number = -rect.height;
  28.  
  29. var canvas:BitmapData = new
  30. BitmapData(stage.stageWidth,stage.stageHeight,false, 0x000000);
  31. addChild(new Bitmap(canvas));
  32.  
  33. addEventListener(Event.ENTER_FRAME, onLoop);
  34. function onLoop(evt:Event):void {
  35.        // clear the canvas
  36.        canvas.fillRect(canvas.rect, 0x222222);
  37.        // draw the current frame
  38.        for (var i:int = 0; i<animationNum; i++){
  39.                var ani:Frame = animation[i];
  40.                var pnt:Point = locs[i];
  41.                canvas.copyPixels(ani.bitmap, rect, pnt, null, null, true);
  42.                // get the next frame of the animation
  43.                pnt.y += 1;
  44.                if (pnt.y> bottom){
  45.                    pnt.y = top;
  46.                }
  47.                animation[i] = ani.next;
  48.        }
  49.  
  50. }
  51.  
  52. // generate and capture 40 bitmaps by altering the colorTransform of
  53.  
  54. function generateAnimation():void{
  55.        var channel:uint = 0;
  56.        var ct:ColorTransform = new ColorTransform();
  57.        var increase:Boolean = true;
  58.        var firstFrame:Frame;
  59.        var pFrame:Frame;
  60.        for (var i:int = 0; i<40; i++){
  61.                if (increase){
  62.                   channel += 10.25;
  63.                   if (channel> 200){
  64.                          increase = false;
  65.                   }
  66.                }else{
  67.                   channel -= 10;
  68.                }
  69.                ct.color = channel <<16 | channel <<8 | channel;
  70.                circle.transform.colorTransform = ct;
  71.                
  72.                // populate linked list
  73.                currFrame = capture(circle);
  74.                if (pFrame){
  75.                   pFrame.next = currFrame;
  76.                }
  77.                if (i == 0){
  78.                   firstFrame = currFrame;
  79.                }
  80.                pFrame = currFrame;
  81.        }
  82.        // close the list
  83.        currFrame.next = firstFrame;
  84.        currFrame = firstFrame;
  85. }
  86.  
  87. // create the Frame instance and draw the circle to it
  88. // preserving the colorTransform information
  89. function capture(target:Shape):Frame{
  90.        var frame:Frame = new Frame();
  91.        frame.bitmap = new BitmapData(target.width*2, target.height*2, true, 0x000000000);
  92.        frame.bitmap.draw(target, target.transform.matrix, target.transform.colorTransform);
  93.        return frame;
  94. }

This is a variation on the last post. It captures 40 small bitmaps of a blurred circle fading in and out and then draws 8000 of them to the stage.


Have a look at the swf...

This entry was posted in BitmapData, Data Structures, Vector. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted October 31, 2009 at 4:49 pm | Permalink

    Runs great on my 2.2 Ghz Intel Core 2 Duo with Mac OS X 10.6. I’ll bet you could make a lovely snowfall scene out of this…

Post a Comment

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

*
*