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

Ambilight effect

lights the background, sampling the video.
Get Adobe Flash player
by owenray 09 Feb 2012
/**
 * Copyright owenray ( http://wonderfl.net/user/owenray )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/nucu
 */

package {
    import flash.system.LoaderContext;
    import flash.system.Security;
    import flash.net.URLRequest;
    import flash.display.Loader;
    import flash.text.TextField;
    import flash.media.Camera;
    import flash.geom.ColorTransform;
    import flash.filters.BitmapFilterQuality;
    import flash.geom.Point;
    import flash.events.TimerEvent;
    import flash.display.BlendMode;
    import flash.utils.Timer;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Bitmap;
    import flash.filters.BlurFilter;
    import flash.display.BitmapDataChannel;
    import flash.geom.Rectangle;
    import flash.geom.Matrix;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.media.Video;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        private var v:Video;
        private var fg:Bitmap;
        
        private var bmTop:Bitmap = new Bitmap(),
                    bmBottom:Bitmap = new Bitmap(),
                    bmTopBuffer:Bitmap = new Bitmap(),
                    bmBottomBuffer:Bitmap = new Bitmap();
        private var bmd:BitmapData;
        private var black:Sprite = new Sprite();
        private var theMask:Sprite = new Sprite();
        
        private var bmtest:Bitmap = new Bitmap();
        private var bmtest2:Bitmap = new Bitmap();
        
        
        private var renderEvery:int = 10;//render full frame every X frames
        private var renderSequence:int = 0;
        private var tf:TextField = new TextField();
        
        public function FlashTest()
        {
            if(stage) init();
            else stage.addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void
        {
            var c:LoaderContext = new LoaderContext(true);
            var l:Loader = new Loader();
            l.load(new URLRequest("http://leaveatag.com/Seamless_Brick_Wall_Texture_by_cfrevoir.jpeg"), c);
            addChild(l);
            
            stage.align = StageAlign.TOP_LEFT;
            stage.frameRate = 60;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            theMask.addChild(black);
            addChild(theMask);
            
            var t:Timer = new Timer(40);
            t.addEventListener(TimerEvent.TIMER, pool);
            t.start();

            stage.addEventListener(Event.RESIZE, repos);
            v = new Video(480, 270);
            /*
            
            var nc:NetConnection = new NetConnection();
            nc.connect(null);
            var ns:NetStream = new NetStream(nc);
            ns.play("http://www.flashyourweb.com/gallery3/var/albums/FYW-Demo/Video/big_buck_bunny_720p_h264.flv?m=1319069685");
            v.attachNetStream(ns);
            */
            tf.text = Camera.names.length+"";
            tf.textColor = 0xFFFFFF;
            addChild(tf);
            var cam:Camera = Camera.getCamera("0");
            cam.setMode(480, 270, 30);
            if(Camera.names.length>0)
               v.attachCamera(cam);
            addChild(v);
            
            bmBottom.alpha = bmTop.alpha = 1;
            black.cacheAsBitmap = bmBottom.cacheAsBitmap = bmTop.cacheAsBitmap = true;
            
            addChild(v);
            repos();
            
            theMask.blendMode = BlendMode.LAYER;
            theMask.alpha = .9;
            theMask.cacheAsBitmap = true;
            bmBottom.blendMode = bmTop.blendMode = BlendMode.ERASE;
            addChild(bmtest);
            bmtest.blendMode = BlendMode.ADD;
            bmtest.alpha = .3;
            bmtest2.blendMode = BlendMode.ADD;
            bmtest2.alpha = .3;
        }
        
        private function pool(e:Event = null):void
        {
            renderSequence++;
            if(renderSequence>=renderEvery){
                renderSequence = 0;
                var smooth:Boolean = true;
                bmd = bmTopBuffer.bitmapData;
                //bmd.colorTransform(new Rectangle(0, 0, bmd.width, bmd.height), new ColorTransform(1, 1, 1, .5));
                //return
    
                var raty:Number = 1;
                
                var bmd2:BitmapData = new BitmapData(bmd.width, bmd.height, true, 0x00FFFFFF);
                tf.text = ""+(v.scaleX/1);
                var m:Matrix = new Matrix(v.scaleX/1, 0, 0, -1, 0, bmd.height-(bmd.height*.1));
                //bmd2.draw(v, m, null, null, new Rectangle(0, bmd.height*.6, v.width, bmd.height*.3));
                bmd2.draw(v, m, null, null, new Rectangle(50, bmd.height*.6, bmd.width-100, bmd.height*.3));
                bmd.draw(bmd2);
                bmd.copyChannel(bmd2, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), BitmapDataChannel.BLUE, BitmapDataChannel.ALPHA);
                bmd.copyChannel(bmd2, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
                bmd.copyChannel(bmd2, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), BitmapDataChannel.GREEN, BitmapDataChannel.ALPHA);
                //bmd.draw(bmd2);
                //bmd.cl
                //bmd.applyFilter(bmd, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), new BlurFilter(500/scaleDown, 600/scaleDown, BitmapFilterQuality.MEDIUM));
                bmd.applyFilter(bmd, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), new BlurFilter(100, 100, BitmapFilterQuality.MEDIUM));
                //for(var c:int = 0; c<5; c++)
                    //bmd.draw(bmd);1
                //bmd.draw(s);
                
                bmd = bmBottomBuffer.bitmapData;
                
                bmd2 = new BitmapData(bmd.width, bmd.height, true, 0x00FFFFFF);
                //bmd.colorTransform(new Rectangle(0, 0, bmd.width, bmd.height), new ColorTransform(1, 1, 1, .5));
                m = new Matrix(1/v.scaleX, 0, 0, -1/v.scaleX, 0, (v.height/v.scaleX));
                bmd2.draw(v, m, null, null, new Rectangle(50, 0, bmd.width-100, bmd.height*.4));
                bmd.draw(bmd2);
                bmd.copyChannel(bmd2, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), BitmapDataChannel.BLUE, BitmapDataChannel.ALPHA);
                bmd.copyChannel(bmd2, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
                bmd.copyChannel(bmd2, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), BitmapDataChannel.GREEN, BitmapDataChannel.ALPHA);
                
                
                bmd.applyFilter(bmd, new Rectangle(0, 0, bmd.width, bmd.height), new Point(0,0), new BlurFilter(100, 100, BitmapFilterQuality.MEDIUM));
                //bmd.colorTransform(new Rectangle(0, 0, bmd.width, bmd.height), new ColorTransform(1.5, 1.5, 1.5, 1.5));
            }
            bmTop.bitmapData.colorTransform(new Rectangle(0, 0, bmTop.width, bmTop.height), new ColorTransform(1, 1, 1, .7));
            bmTop.bitmapData.draw(bmTopBuffer);
            bmBottom.bitmapData.colorTransform(new Rectangle(0, 0, bmBottom.width, bmBottom.height), new ColorTransform(1, 1, 1, .7));
            bmBottom.bitmapData.draw(bmBottomBuffer);
        }
        
        private function repos(e:Event = null):void
        {
            graphics.clear();
            graphics.beginFill(0xFFFFFF);
            graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            v.width = stage.stageWidth;
            v.scaleY = v.scaleX;
            v.y = stage.stageHeight/2-v.height/2;
            //v.x = stage.stageWidth/2-bg.width/2;
            /*
            var spr:Sprite = new Sprite();
            spr.graphics.beginFill(0, 1);
            spr.graphics.drawRect(0, 0, stage.stageWidth, 490);
            var i:Number = 100;
            spr.graphics.drawRoundRect(i, i, stage.stageWidth-i*2, 390-i*2, 390-i*2, 390-i*2);
            s = new BitmapData(Math.ceil(stage.stageWidth), Math.ceil(275), true, 0);
            s.draw(spr);*/
            //s.applyFilter(s, new Rectangle(0, 0, s.width, s.height), new Point(0,0), new BlurFilter(500/scaleDown, 600/scaleDown, BitmapFilterQuality.MEDIUM));
            var even:Boolean = false;
            
            //v.visible = false;
            //fg = new Bitmap(new BitmapData(stage.stageWidth, 195, true, 0));
            trace('!repos');
            bmTop.bitmapData = new BitmapData(stage.stageWidth, Math.ceil((stage.stageHeight-v.height)/2), true, 0x00FFFFFF);
            bmBottom.bitmapData = new BitmapData(stage.stageWidth, Math.ceil((stage.stageHeight-v.height)/2), true, 0x00FFFFFF);
            theMask.addChild(bmTop);
            theMask.addChild(bmBottom);
            
            bmTop.y = 0;
            bmBottom.y = v.y+v.height;
            renderSequence = renderEvery;
            bmTopBuffer.bitmapData = bmTop.bitmapData.clone();
            bmBottomBuffer.bitmapData = bmBottom.bitmapData.clone();
            pool();
            
            black.graphics.beginFill(0);
            black.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            
            bmtest.bitmapData = bmTop.bitmapData;
            bmtest2.bitmapData = bmBottom.bitmapData;
            bmtest2.y = bmBottom.y;
        }
    }
}