Monthly Archives: April 2009

BitmapData Snapshot Grid

Actionscript:
  1. [SWF(width=600,height=650)]
  2. var canvas:BitmapData=Bitmap(addChild(new Bitmap(new BitmapData(600,300,false,0xCCCCCC),"auto",true))).bitmapData;
  3. var stills:BitmapData=Bitmap(addChild(new Bitmap(new BitmapData(600,380,false,0xAAAAAA),"auto",true))).bitmapData;
  4. getChildAt(1).y=300;
  5.  
  6. var c:Shape = new Shape();
  7. var m:Matrix = new Matrix();
  8. m.createGradientBox(40, 40, 0, 0, 0);
  9. c.graphics.beginGradientFill(GradientType.RADIAL, [0xCC0000, 0xCC0000],  [1, 0], [0, 255], m);
  10. c.graphics.drawCircle(20,20,20);
  11.  
  12. addEventListener(Event.ENTER_FRAME, onLoop);
  13. function onLoop(evt:Event):void {
  14.     c.x=mouseX-c.width/2;
  15.     c.y=mouseY-c.height/2;
  16.     canvas.draw(c, c.transform.matrix);
  17. }
  18.  
  19. var spacing:Number = 10;
  20. var cols:Number = 4;
  21. var max:Number = cols * cols;
  22. var size:Number = 1/(canvas.width/((canvas.width / cols) - spacing));
  23. var st:Matrix = new Matrix();
  24. st.scale(size, size);
  25. var w:Number = canvas.width * st.d  + spacing;
  26. var h:Number = canvas.height * st.d + spacing;
  27. var timer:Timer=new Timer(500);
  28. timer.start();
  29. timer.addEventListener(TimerEvent.TIMER, onCapture);
  30. function onCapture(evt:TimerEvent):void {
  31.     var inc:int = timer.currentCount - 1;
  32.     st.tx = (inc% cols) * w+ spacing / 2;
  33.     st.ty = int(inc / cols) * h + spacing;
  34.     stills.draw(canvas, st);
  35.     if (timer.currentCount==max) {
  36.         timer.reset();
  37.         timer.start();
  38.     }
  39. }

Take snapshots of a given BitmapData and arrange them in a grid. I wrote this snippet quickly in response to a question so it could probably use a little clean up...

You'll need to move your mouse over the large canvas bitmap (drawing to it) to see anything...

Posted in BitmapData | Tagged , | Leave a comment

Slow Circle Drawing

Actionscript:
  1. var canvas:BitmapData = new BitmapData(400,400,false, 0xCCCCCC);
  2. addChild(new Bitmap(canvas));
  3.  
  4. addEventListener(Event.ENTER_FRAME, onLoop);
  5. function onLoop(evt:Event):void {
  6.     canvas.fillRect(canvas.rect, 0xCCCCCC);
  7.    
  8.     var r:Number = Math.abs(200 - mouseX);
  9.     var r2:Number = r * r;
  10.     var inc:Number = 1 / r;
  11.     var xp:Number = .00000001;
  12.     var yp:Number = -r;
  13.     while(yp<r){
  14.           var y:Number = 200 + yp;
  15.           yp += xp * inc;
  16.           xp = Math.sqrt(r2 - yp * yp);
  17.           canvas.setPixel(200 + xp, y, 0x000000);
  18.           canvas.setPixel(200 - xp, y, 0x000000);
  19.      }
  20. }

A slow way to draw circles using setPixel().

Actionscript:
  1. var canvas:BitmapData = new BitmapData(400,400,false, 0xCCCCCC);
  2. addChild(new Bitmap(canvas));
  3.  
  4. addEventListener(Event.ENTER_FRAME, onLoop);
  5. function onLoop(evt:Event):void {
  6.     canvas.fillRect(canvas.rect, 0xCCCCCC);
  7.     var r:Number = Math.abs(200 - mouseX);
  8.     var r2:Number = r * r;
  9.     var inc:Number = 1 / r;
  10.     var xp:Number = .000001;
  11.     var yp:Number = -r;
  12.     while(yp <= 0){;
  13.          var x1:Number = 200 + xp;
  14.          var y1:Number = 200 + yp
  15.          var x2:Number = 200 - xp;
  16.          var y2:Number = 200 - yp;
  17.          canvas.setPixel(x1, y1, 0x000000);
  18.          canvas.setPixel(x1, y2, 0x000000);
  19.          canvas.setPixel(x2, y1, 0x000000);
  20.          canvas.setPixel(x2, y2, 0x000000);
  21.           yp += xp * inc;
  22.           xp = Math.sqrt(r2 - yp * yp);
  23.      }
  24. }

Little better, still slow.

I was brainstorming about a few things today and somehow these two slow circle drawing algorithms popped out. These are pretty useless compared to some of the famous algorithms I've posted in the past. Kind of interesting nevertheless.

Posted in misc, setPixel | Tagged , | Leave a comment

Function Chaining & Classes

Actionscript:
  1. package {
  2.     import flash.display.Sprite;
  3.     public class Chaining extends Sprite{
  4.         public function Chaining(){
  5.               print(n(100).divide(2).plus(2));
  6.               // outputs 52              
  7.               print(n(100).plus(n(10).multiply(2)));
  8.               // outputs 120
  9.         }
  10.     }
  11. }
  12.  
  13. function print(n:Num):void{
  14.    trace(n.getValue());
  15. }
  16. function n(n:Number):Num{
  17.    return new Num(n);
  18. }
  19.  
  20. class Num{
  21.     private var value:Number;
  22.    
  23.     public function Num(n:Number):void{
  24.        value = n;
  25.     }
  26.     private function convert(n:*):Number{
  27.         if (n is Num) n = n.getValue();
  28.         return n;
  29.     }
  30.     public function getValue():Number{
  31.         return value;
  32.     }
  33.     public function plus(n:*):Num{
  34.         n = convert(n);
  35.         value += n;
  36.         return this;
  37.     }
  38.     public function minus(n:*):Num{
  39.         n = convert(n);
  40.         value -= n;
  41.         return this;
  42.     }
  43.     public function multiply(n:*):Num{
  44.         n = convert(n);
  45.         value *= n;
  46.         return this;
  47.     }
  48.     public function divide(n:*):Num{
  49.         n = convert(n);
  50.         value /= n;
  51.         return this;
  52.     }
  53. }

This snippet is meant to be run as a document class. It shows how one might go about designing a class to make extensive use of function chaining.

Posted in OOP, functions | Tagged , | 2 Comments

Save BitmapData to your HD

Actionscript:
  1. import com.adobe.images.JPGEncoder;
  2.  
  3. var jpgEncoder:JPGEncoder = new JPGEncoder(80);
  4. var file:FileReference = new FileReference();
  5. var id:int = 0;
  6.  
  7. var bit:BitmapData = new BitmapData(400, 400, false, 0x000000);
  8. addChild(new Bitmap(bit));
  9.  
  10. stage.addEventListener(MouseEvent.CLICK, onClick);
  11. function onClick(evt:MouseEvent):void{
  12.     // draw some perlin noise
  13.     bit.perlinNoise(200,200, 2, Math.random()*100, true, false,0, true);
  14.     //bit.draw(someClip);
  15.     //bit.draw(someVideo);
  16.     id++;
  17.     file.save(jpgEncoder.encode(bit), "image_" +id+".jpg");
  18. }

This snippet uses the as3corelib which you can download on this page. It uses the JPGEncoder class to save a jpeg image to a users HD. It also makes use of the flash player 10 FileReference.save() method.

I used BitmapData.perlinNoise() to quickly create an arbitrary image.... as the code comments suggest you could easily snapshot a video or other DisplayObject.

Posted in BitmapData, misc | Tagged , | 3 Comments