/* * Exile.as - Live Chroma Key ActionScript Library * Copyright (c) 2009 Yusuke Kawasaki http://www.kawa.net/ * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */ package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.PixelSnapping; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.geom.Matrix; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.ui.Keyboard; import org.libspark.LiveChromaKey.LCK_Core; public class Exile extends Sprite { private var chromakey:LCK_Core; private var count:int = 0; private var map:Array; private var frames:int = 20; public function Exile ():void { chromakey = new LCK_Core(); chromakey.workX = 160; chromakey.workY = 120; chromakey.init(); this.addChild( chromakey.getBackground() ); map = new Array(frames); this.addEventListener( MouseEvent.CLICK, this.onClick ); this.addEventListener( Event.ENTER_FRAME, this.onEnterFrame ); stage.addEventListener( KeyboardEvent.KEY_DOWN, this.onKeyDown ); } private function onEnterFrame (e:Event):void { var fore:Sprite = chromakey.getForeground(); var bmd:BitmapData = new BitmapData( 640, 480, true, 0 ); bmd.draw( fore, null, null, null, null, true ); var bm:Bitmap = new Bitmap( bmd, PixelSnapping.AUTO, true ); var sp:Sprite = new Sprite(); sp.addChild( bm ); var idx:int = count % frames; if ( map[idx] ) { this.removeChild( map[idx] ); } map[idx] = sp; this.addChild( sp ); for ( var i:int = 1; i < frames; i ++ ) { var sp0:Sprite = map[(idx + i) % frames]; if ( ! sp0 ) continue; var alpha:Number = 1.0 * i / frames; sp0.alpha = alpha; } count ++; } private function onKeyDown (e:KeyboardEvent):void { if ( e.keyCode == Keyboard.ESCAPE ) { this.refresh(); } } private function onClick (e:MouseEvent):void { this.refresh(); } private function refresh ():void { for ( var i:int = 0; i < frames; i ++ ) { if ( ! map[i] ) continue; this.removeChild( map[i] ); map[i] = null; } chromakey.runDetector(); } } }