In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: 動体検知5

// forked from ll_koba_ll's 動体検知5
// forked from ll_koba_ll's 動体検知4
// forked from ll_koba_ll's 動体検知3
// forked from ll_koba_ll's forked from: 動体検知
// forked from ll_koba_ll's 動体検知
// write as3 code here..
package {

    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.filters.*;
    import flash.media.Camera;
    import flash.media.Video;

    [SWF(frameRate="10", backgroundColor="#000000")]
    public class MotionCamera extends Sprite
    {
        
        private var camera:Camera;
        private var video:Video;
        private var button:Sprite;
        private var bmd:BitmapData;

        private var now:BitmapData;
        private var before:BitmapData;
        private var rect:Rectangle;
        private var pt:Point;
        private var blur:BlurFilter
        
        public function MotionCamera()
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void
        {
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            blur = new BlurFilter(3,3); 
            camera = Camera.getCamera();

            if (camera != null)
            {
                setupCamera();
            }
                   
            bmd = new BitmapData(camera.width*2, camera.height*2 ,false,0xffffff);
            var bmp:DisplayObject =  addChild(new Bitmap(bmd));
            bmp.scaleX = 1.5;
            bmp.scaleY = 1.5;

            addEventListener(Event.ENTER_FRAME, loop);

            now = new BitmapData(camera.width*2, camera.height*2);
            before = new BitmapData(camera.width*2, camera.height*2);
            rect = new Rectangle(0, 0, camera.width*2, camera.height*2);
            pt = new Point(0, 0);
            
        }


        private function setupCamera():void
        {
            video = new Video(camera.width*2, camera.height*2);
            video.attachCamera(camera);
            //addChild(video);
        }


        private function loop(e:Event):void
        {
            now.draw(video);
            now.draw(before, new Matrix(), new ColorTransform(), BlendMode.DIFFERENCE);
            var ret:uint = now.threshold(now, rect, pt, ">", 0xff111111, 0xffffffff);
            before.draw(video);
            bmd.draw(now, null, null, BlendMode.DIFFERENCE);
            bmd.draw(video, null, null, BlendMode.ADD);
            bmd.applyFilter(bmd, bmd.rect, new Point(), blur);              
        }
    }
}