BlendMode lab (lighten)

by wh0
Copy an image URL from somewhere and paste it into the movie.
Use ARROW keys to nudge it into position (CTRL+ARROW for 10px nudging).
Press HOME to reset to center.
Press DELETE to remove the current image (you can never remove more than one, so be careful).
Press SPACE or ENTER to commit the composition.
♥0 | Line 90 | Modified 2010-12-18 14:04:46 | MIT License
play

ActionScript3 source code

/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/w2UO
 */

package {
    import flash.geom.Matrix;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import flash.ui.*;
    
    import com.actionscriptbible.Example;
    [SWF(backgroundColor=0x000000)]
    public class Compositor extends Example {
        
        private var composite:BitmapData;
        private var latest:Bitmap;
        private var mode:String;
        
        public function Compositor(m:String=BlendMode.LIGHTEN) {
            loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, function(e:UncaughtErrorEvent):void { trace(e.error); });
            Wonderfl.disable_capture();
            stage.scaleMode = StageScaleMode.NO_SCALE
            composite = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
            addChild(new Bitmap(composite));
            mode = m;
            
            var tf:TextField = new TextField();
            tf.type = TextFieldType.INPUT;
            stage.focus = tf;
            tf.addEventListener(TextEvent.TEXT_INPUT, onTextInput);
            tf.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
            tf.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        }
        
        private function onFocusOut(e:FocusEvent):void {
            stage.focus = e.target as TextField;
        }
        
        private function onTextInput(e:TextEvent):void {
            (e.target as TextField).text = '';
            if (e.text.length > 1) loadFirst(e.text);
        }
        
        private function loadFirst(url:String):void {
            var l:Loader = new Loader();
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, loadSecond);
            l.load(new URLRequest(url));
        }
        
        private function loadSecond(e:Event):void {
            var li:LoaderInfo = e.target as LoaderInfo;
            var l:Loader = new Loader();
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, nextImage);
            l.loadBytes(li.bytes);
        }
        
        private function nextImage(e:Event):void {
            var li:LoaderInfo = e.target as LoaderInfo;
            compose();
            // loadBytes always creates a MovieClip ):
            var mc:MovieClip = li.content as MovieClip;
            latest = mc.getChildAt(0) as Bitmap;
            latest.x = Math.floor((composite.width - latest.width) / 2);
            latest.y = Math.floor((composite.height - latest.height) / 2);
            latest.blendMode = mode;
            addChild(latest);
        }
        
        private function onKeyDown(e:KeyboardEvent):void {
            if (!latest) return;
            var delta:int = e.ctrlKey ? 10 : 1;
            switch (e.keyCode) {
                case Keyboard.UP:
                    latest.y -= delta; break;
                case Keyboard.DOWN:
                    latest.y += delta; break;
                case Keyboard.LEFT:
                    latest.x -= delta; break;
                case Keyboard.RIGHT:
                    latest.x += delta; break;
                case Keyboard.HOME:
                    latest.x = Math.floor((composite.width - latest.width) / 2);
                    latest.y = Math.floor((composite.height - latest.height) / 2);
                    break;
                case Keyboard.SPACE:
                case Keyboard.ENTER:
                    compose(); break;
                case Keyboard.DELETE:
                    pop(); break;
            }
        }
        
        private function compose():void {
            if (!latest) return;
            composite.draw(latest, new Matrix(1, 0, 0, 1, latest.x, latest.y), null, mode);
            pop();
        }
        
        private function pop():void {
            if (!latest) return;
            removeChild(latest);
            latest = null;
        }
        
    }
}