Tag Archives: actionscript

Frame Differencing

Actionscript:
  1. [SWF(width = 800, height= 600)]
  2. var sw:Number = 800;
  3. var sh:Number = 600;
  4. var pixelNum:int = sw * sh;
  5. var blurAmount:Number = 10;
  6. var pnt:Point = new Point(0,0);
  7. var rect:Rectangle = new Rectangle(0,0,sw,sh);
  8.  
  9. var canvas:BitmapData = new BitmapData(sw, sh, false, 0x000000);
  10. var buffer:BitmapData = new BitmapData(sw, sh, false, 0x000000);
  11. var feed  :BitmapData = new BitmapData(sw, sh, false, 0x000000);
  12. var prev  :BitmapData = new BitmapData(sw, sh, false, 0x000000);
  13.  
  14. var frame:Bitmap = new Bitmap(canvas, "auto", true);
  15. addChild(frame);
  16.  
  17. var cam:Camera =  Camera.getCamera();
  18. cam.setMode(sw,sh,12);
  19. var video:Video = new Video(sw, sh);
  20. video.attachCamera(cam);
  21.  
  22. cam.addEventListener(ActivityEvent.ACTIVITY, onActivityStart);
  23.  
  24. function onActivityStart(evt:ActivityEvent):void {
  25.     addEventListener(Event.ENTER_FRAME, onRun);
  26.     cam.removeEventListener(ActivityEvent.ACTIVITY, onActivityStart);
  27. }
  28.  
  29. function onRun(evt:Event):void{
  30.     buffer.draw(video);
  31.     feed.copyPixels(buffer, rect, pnt);
  32.     buffer.draw(prev, null, null, BlendMode.DIFFERENCE);
  33.     prev.draw(video);
  34.     canvas.copyPixels(buffer, rect, pnt);
  35. }

This snippet shows a simple method to do frame differencing with a web cam. This is useful for detecting which areas of the screen have change from frame to frame. This post assumes you pretty much know what frame differencing is.


You can view an swf file here:

This is one of those things I do a good deal but never wrapped up into a library... it's easy (for me at least) to forget exactly how to set it up from scratch.

I added an extra buffer because it's pretty common that you'll want to other things aside from just frame differencing alone, so it's nice to have it all wrapped up in a buffer BitmapData that you can use elsewhere. If speed is a real concern, you can do away with the buffer and just use the canvas instead - depending on what kind of analysis your doing on the frame differenced image it may be trickier without the buffer.

Posted in BitmapData, Video, graphics algorithms, pixel manipulation | Also tagged , | 5 Comments

Functions as Object Review

Something I do a fair bit on this website is use functions as objects. Here is a simple example post on wonderfl. Timeline code and a brief description are available below.

Here is a still of what it generates and below you'll find the timeline as code:

Actionscript:
  1. [SWF(width = 500, height=500)]
  2.  
  3. var canvas:BitmapData = new BitmapData(800,800,false, 0x000000);
  4. addChild(new Bitmap(canvas,"auto",true));
  5. scaleX = scaleY = 500 / 800
  6.  
  7.  
  8. var walkerNum:int = 50;
  9. var walkers:Vector.<Function> = new Vector.<Function>(walkerNum, true);
  10.  
  11. makeWalkers();
  12. runWalkers();
  13.  
  14. function makeWalkers():void{
  15.     for (var i:int = 0; i<walkerNum; i++){
  16.         walkers[i] = makeWalker();
  17.     }
  18. }
  19. function runWalkers():void{
  20.     addEventListener(Event.ENTER_FRAME, onRun);
  21. }
  22. function onRun(evt:Event):void{
  23.     for (var i:int = 0; i<walkerNum; i++){
  24.         walkers[i]();
  25.     }
  26. }
  27.  
  28. function makeWalker(xp:Number=400, yp:Number=400):Function{
  29.     var x:Number = xp, y:Number = yp;
  30.     var rad:Number = Math.random() * 4;
  31.     var theta:Number = Math.random() * Math.PI * 2;
  32.     var speed:Number = 0.01 * Math.random() * 2
  33.     if (int(Math.random() * 2) == 1){
  34.         speed *= -1;
  35.     }
  36.     return function():void{
  37.         x += rad * Math.cos(theta);
  38.         y += rad * Math.sin(theta);
  39.         theta += speed
  40.         if (int(Math.random() * 100) == 1){
  41.             theta = Math.random() * Math.PI * 2;
  42.         }
  43.         if (x> 800 || x <0 || y> 800 || y <0){
  44.             x = xp, y = yp;
  45.         }
  46.         canvas.setPixel(x, y, 0xFFFFFF);
  47.     }
  48. }

The basic trick is to have a function return a function. The original function initializes typed local variables and the returned function has access to these values. This is one of many ways to do OOP style coding using functions alone. This is really only for fun and for speed coding reasons - it's worth noting that this method is is significantly slower than using actual classes. I still have lots of fun with it despite the downsides.

Posted in BitmapData, functions | Also tagged , | 7 Comments

Bracket Syntax Reminder

If you haven't looked at every post on this site it's possible you've missed one of my favorite actionscript features.... Bracket Syntax:

Actionscript:
  1. var s:Sprite = Sprite(addChild(new Sprite()));
  2. s["x"] = 100;
  3. s["y"] = 100;
  4.  
  5. s["graphics"]["beginFill"](0xFF0000);
  6. s["graphics"]["drawCircle"](0,0,10);
  7.  
  8. this["addChild"](s);

If you don't realize how powerful this is then there is something wrong with you (joking). If you don't see how powerful this is, take some time and think about it. You can use it to avoid lots of annoying repetitive code in state machines for instance. It's always important to keep things readable if you decide to go this route on a real project.

[EDIT ....and as Quasimondo mentioned there is a notable performance hit when using this syntax. So don't forget to keep that in mind.]


Here is a very old post showing some of the power of this trick.

Posted in dynamic, functions | Also tagged , | 4 Comments

URLVariables Replacement

Actionscript:
  1. function decode(str:String):Object {
  2.     var vars:Object={};
  3.     var parse:Array =str.split("&");
  4.     var i:int=0;
  5.     for (i = 0; i<parse.length; i++) {
  6.         var pair:Array=parse[i].split("=");
  7.         if (pair.length==2) {
  8.             vars[pair[0]]=pair[1];
  9.         }
  10.     }
  11.     return vars;
  12. }
  13.  
  14. var nameValuePairs="one=1&&two=éllo&three=1000&four=0xFF0000";
  15. var parsed:Object=decode(nameValuePairs);
  16.  
  17. trace(parsed.one);
  18. trace(parsed.two);
  19. trace(parsed.three);
  20. trace(parsed.four);
  21.  
  22. /*outputs:
  23. 1
  24. éllo
  25. 1000
  26. 0xFF0000
  27. */

Well not exactly a URLVariables replacement, but I often use the URLVariables.decode() function and find that its very picky... like if there is an extra & or something it freaks out and breaks, or if there are line returns. As a quick and simple solution the other day I wrote this function into part of a class (Model class) that I was working on. Fixed the problem and even handles Spanish characters nicely.

There is plenty of room for improvement with this simple function... feel free to post improvements in the comments.

Posted in external data, string manipulation, strings | Also tagged , | 3 Comments