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

ConvolutionFilter Wipe

ConvolutionFilterを利用したワイプ処理

シュー ドォン!
Get Adobe Flash player
by okoi 08 Feb 2012
/**
 * Copyright okoi ( http://wonderfl.net/user/okoi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zQt6
 */

/**
 * ConvolutionFilterを利用したワイプ処理
 */
package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.ConvolutionFilter;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.system.Security;
    
    [SWF(width = "465", height = "465", frameRate = "60")]
    
    /**
     * ...
     * @author okoi
     */
    public class Main extends Sprite 
    {
        public static const WIDTH:int = 465;
        public static const HEIGHT:int = 465;
        
        private var _effect1:BitmapData;
        private var _effect2:BitmapData;
        private var _mask:BitmapData;
                
        private var _image:Image;
        
        private var _layer:Sprite;
        
        
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
            Security.allowDomain("assets.wonderfl.net");
            Security.loadPolicyFile("http://assets.wonderfl.net/crossdomain.xml");


            this.graphics.beginFill(0);
            this.graphics.drawRect(0, 0, WIDTH, HEIGHT);
            this.graphics.endFill();
            
            
            _layer = new Sprite();
            _layer.blendMode = "layer";
            addChild( _layer );
            
            _effect1 = new BitmapData(WIDTH, HEIGHT, true, 0 );
            _effect2 = new BitmapData(WIDTH, HEIGHT, true, 0 );
            _mask = new BitmapData(WIDTH, HEIGHT, true, 0 );
                    
            _image = new Image();
            _image.Load( "http://assets.wonderfl.net/images/related_images/f/f5/f524/f524e1b13279adeee53aa894fa13f020eeaa7940m", WIDTH, HEIGHT, function( image:Image ) : void {
                
                _layer.addChild( new Bitmap(_image.image) );
                
                var canvasbmp:Bitmap = new Bitmap(_mask);
                canvasbmp.blendMode = "alpha";
                _layer.addChild( canvasbmp );
                
                addEventListener( Event.ENTER_FRAME, _enterFrameHandler );
            
            });
            
        }
        
        private function _enterFrameHandler( e:Event ) : void {
            
            var i:int;
            var filter:ConvolutionFilter;
            var filterMat:Array;
            
            //----------
            //    Effect1
            _effect1.lock();
            
            filterMat = [
                0, 1, 0,
                0, 0.3, 0,
                0, 0, 0,
            ];
            filter = new ConvolutionFilter( 3, 3, filterMat, 1, 0, false, false );
            _effect1.applyFilter( _effect1, _effect1.rect, new Point(), filter );
            
            for ( i = 0; i < 10; i++ ) {
                _effect1.setPixel32( int(Math.random() * WIDTH), + Math.random()*20, 0xFFFFFFFF );
            }
        
            _effect1.unlock();
            
            //-----------
            //    Effect2    
            _effect2.lock();
            filterMat = [
                0, 0, 0,
                0, 0.3, 0,
                0, 1, 0,            
            ];
            
            filter = new ConvolutionFilter( 3, 3, filterMat, 1, 0, false, false );
            _effect2.applyFilter( _effect2, _effect2.rect, new Point(), filter );
            
            for ( i = 0; i < 10; i++ ) {
                _effect2.setPixel32( int(Math.random() * WIDTH), HEIGHT-1 - Math.random()*20, 0xFFFFFFFF );
            }
            _effect2.unlock();
            
            //-----------
            //    Canvas
            _mask.fillRect(_mask.rect, 0 );            
            _mask.copyPixels( _effect1, _effect1.rect, new Point(), _effect1, new Point(), true );
            _mask.draw( _effect2, new Matrix(), null );
        }
    }
    
}


    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    
    import flash.net.URLRequest;

    import flash.events.Event;

    import flash.geom.Matrix;
    
    /**
     * ...
     * @author 
     */
    class Image
    {
        private var _loader:Loader = null;
        private var _loaderInfo:LoaderInfo = null;
        
        protected var _baseImage:BitmapData;
        
        protected var _width:int;
        protected var _height:int;
        protected var _lockAspectRatio:Boolean;
        protected var _image:BitmapData;
        
        private var _loadCompleteCallback:Function = null;
        
        public function Image() 
        {

        }
        
        /**
         * 画像を読み込む
         * @param    sourceImagePath {String} 読み込む画像へのパス
         * @param    w {int} 画像の表示サイズ        
         * @param    h {int} 画像の表示サイズ
         * @param    callback {Function}
         * @param    lockAspectRatio {Boolean} trueの場合w,hのサイズに収まるように画像のアスペクト比を固定して読み込む
         */
        public function Load( sourceImagePath:String, w:int, h:int, callback:Function, lockAspectRatio:Boolean = false ) : void {
            
            _width = w;
            _height = h;
            _lockAspectRatio = lockAspectRatio;
            
            _loadCompleteCallback = callback;
            
            _loader = new Loader();
            _loaderInfo = _loader.contentLoaderInfo;
            _loaderInfo.addEventListener( Event.COMPLETE, _LoadCompleteHandler );
            _loader.load( new URLRequest( sourceImagePath ) );
        }
        
        
        private function _LoadCompleteHandler( e:Event ) : void
        {
            _loaderInfo.removeEventListener( Event.COMPLETE, _LoadCompleteHandler );
            
            _baseImage = new BitmapData( _loader.width, _loader.height, true, 0 );
            _baseImage.draw( _loader );
            
            var scaleX:Number;
            var scaleY:Number;
            
            if( _lockAspectRatio ){
                scaleX = Math.min( _width / _loader.width, 1 );
                scaleY = Math.min( _height / _loader.height, 1 );
                scaleX = scaleY = ( scaleX < scaleY ) ? scaleX : scaleY;
                _width = _loader.width * scaleX;
                _height = _loader.height * scaleY;
            }else {
                scaleX = _width / _loader.width
                scaleY = _height / _loader.height;
            }
            
            _image = new BitmapData( _width, _height, true, 0 );
            _image.draw( _loader, new Matrix(scaleX, 0, 0, scaleY, 0, 0), null, null, null, true );
            
            _loader = null;
            _loaderInfo = null;
            
            if ( _loadCompleteCallback != null ) _loadCompleteCallback( this );
        }
        
        public function get image():BitmapData { return    _image;    }
        
    }