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: Pixel Bender Test

Pixel Bender Test
// forked from northprint's Pixel Bender Test
// write as3 code here..
//Pixel Bender Test
package {
    import flash.display.*;
    import flash.events.*;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLLoaderDataFormat;
    import flash.system.Security;
    import flash.filters.ShaderFilter;
    import flash.system.LoaderContext;

    [SWF(backgroundColor="#ffffff")]

    public class PixelBenderTest extends MovieClip{  

        private var _pbloader:URLLoader;
        private var _imgloader:Loader;
        private var _twirlShader:Shader;
        private var _filter:ShaderFilter;
        private var _bmp:Bitmap;
        private var _radius:uint;
        private var _twist:uint;

        public function PixelBenderTest(){
            Security.loadPolicyFile("http://narayama.heteml.jp/crossdomain.xml");
            _pbloader = new URLLoader();
            _pbloader.dataFormat = URLLoaderDataFormat.BINARY;
            _pbloader.addEventListener(Event.COMPLETE,PbLoadHandler);
            _pbloader.load(new URLRequest("http://narayama.heteml.jp/test/pixelbender/twirlFlash.pbj"));
        }
        private function PbLoadHandler(e:Event):void{
            _imgloader = new Loader();
            _imgloader.contentLoaderInfo.addEventListener(Event.COMPLETE,ImgLoadHandler);
            _imgloader.load(new URLRequest("http://narayama.heteml.jp/test/pixelbender/testimage.jpg"), new LoaderContext(true));
        }
        private function ImgLoadHandler(e:Event):void{
            var loaderInfo:LoaderInfo = LoaderInfo(e.currentTarget);
            var loader:Loader = loaderInfo.loader;
            var bmd:BitmapData = new BitmapData(loader.width, loader.height, true);
            bmd.draw(loader);

            _bmp = new Bitmap(bmd);
            _bmp.x = (stage.stageWidth/2) - (loader.width/2);
            _bmp.y = (stage.stageHeight/2) - (loader.height/2);
			
			_radius = 200;
			_twist = 0;
			_twirlShader = new Shader(_pbloader.data);
            _twirlShader.data["radius"]["value"] = [_radius];
            _twirlShader.data["twist"]["value"] = [_twist];
            _twirlShader.data["center"]["value"] = [loader.width/2,loader.height/2];

            _filter = new ShaderFilter(_twirlShader);

            _bmp.filters = [_filter];
            addChild(_bmp);

            start();
            
            stage.addEventListener(MouseEvent.CLICK, click);
        }
        
        private function click(event:MouseEvent):void {
        		_twirlShader.data["center"]["value"] = [_bmp.mouseX,_bmp.mouseY];
        		start();
        }
        
        private function start():void {
        		_radius = 200;
            	_twist = 0;
        		addEventListener(Event.ENTER_FRAME,EnterframeHandler);
        }
        
        private function EnterframeHandler(e:Event):void{
        		_radius -= 10;
        		_twist += 1;
            //_twirlShader.data["center"]["value"] = [_bmp.mouseX,_bmp.mouseY];
            _twirlShader.data["radius"]["value"] = [_radius];
            _twirlShader.data["twist"]["value"] = [_twist];
            _bmp.filters = [_filter];
            
            if (_radius <= 0) {
            		removeEventListener(Event.ENTER_FRAME,EnterframeHandler);
            		
            }
        }
    }
}