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

質感抽出

Get Adobe Flash player
by shohei909 15 Nov 2010
/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/oaM2
 */

// forked from shohei909's 輪郭抽出
// forked from shohei909's decolorエフェクト
// forked from shohei909's NowLoading ライブラリ
// forked from shohei909's NowLoading

/*
blurをかけて差分を取ってます。抽出した質感を元の画像に重ねることで透明感を強くしてます。
クリックで元の画像を表示します。

http://www.flickr.com/photos/pinksherbet/3111657504/#/photos/pinksherbet/3111657504/lightbox/
*/
package {
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.system.LoaderContext;
    import flash.net.URLRequest;
    import flash.display.Sprite;
    import net.kawa.tween.KTween;
    
    
    [SWF(backgroundColor="0",frameRate = "60")]
    public class FlashTest extends Sprite {
        private const W:int = 465;        
        private const H:int = 465;        
        private const SIZE:int = 465;
        
        private var count:int = 0; 
        
        private var loader:Loader = new Loader();
        private var nowLoading:NowLoading = new NowLoading(onLoaded);
        
        private var source:BitmapData = new BitmapData(W,H,true,0xFF000000);
        private var decolored:BitmapData;
        private var bitmapData:BitmapData = new BitmapData(W,H,true,0xFF000000);
        private var diff:BitmapData = new BitmapData(W,H,true,0xFF000000);
        
        private var bitmap:Bitmap;
         
        public var rate:Number = 0;
         
        public function FlashTest(){
            
            with(graphics){
                beginFill( 0x222222, 1 );
                drawRect(0,0,W,H)
            }
            
            //ロードを開始する
            loader.load( 
                new URLRequest("http://farm4.static.flickr.com/3250/3111657504_1138bf80d3.jpg"),
                new LoaderContext(true)
            );
            
            //ローダーを登録する
            nowLoading.addLoader(loader);
            addChild( nowLoading );
            
        }
        
        
        
        //ロードが完了したときに呼び出す
        private function onLoaded():void{
            //ロード画面を消す
            KTween.to( nowLoading, 1, {alpha:0} ,null, function _():void{ removeChild( nowLoading );} );
            init();
        } 
        
        //初期化
        private function init():void{
            var rect:Rectangle = RabBit.autoSizeDraw( source, loader );
            var b:BitmapData = bitmapData = source.clone();
            bitmap = new Bitmap( bitmapData );
                RabBit.material( diff, source, rect, new Point(rect.x , rect.y), 32 );
                b.draw( diff, null, null, "add" );
                RabBit.material( diff, source, rect, new Point(rect.x , rect.y), 4);
                b.draw( diff, null, null, "subtract" );
                RabBit.material( diff, source, rect, new Point(rect.x , rect.y), 16 );
                b.draw( diff, null, null, "add" );
                RabBit.material( diff, source, rect, new Point(rect.x , rect.y), 2 );
                b.draw( diff, null, null, "subtract" );
                RabBit.material( diff, source, rect, new Point(rect.x , rect.y), 8 );
                b.draw( diff, null, null, "add" );
                RabBit.material( diff, source, rect, new Point(rect.x , rect.y), 1 );
                b.draw( diff, null, null, "subtract" );
            
            bitmap.x = ( W - SIZE ) / 2; 
            bitmap.y = ( H - SIZE ) / 2;
            
            addChild( bitmap );
            
            stage.addEventListener( "mouseDown", onOver);
            stage.addEventListener( "mouseUp", onOut);
        }
        
        private function onOver(e:MouseEvent):void{
            bitmap.bitmapData = source;
        }
        
        private function onOut(e:MouseEvent):void{
            bitmap.bitmapData = bitmapData;
        }
     
        
    }
}
import flash.filters.BlurFilter;
import flash.geom.Point;

import flash.geom.Matrix;
import flash.geom.Rectangle;
class RabBit{
    static public const ALING_CENTER:int = 0;
    static public const ALING_LEFT:int = 1;
    static public const ALING_RIGHT:int = 2;
    static public const ALING_TOP:int = 3;
    static public const ALING_BOTTOM:int = 6;
    
    static public function autoSizeDraw( bitmapData:BitmapData, sourceImage:*, rect:Rectangle=null, aling:int = 0 ):Rectangle {
        if(! rect ){ rect = bitmapData.rect; }
        var rw:int = rect.width;
        var rh:int = rect.height;
        var sw:int = sourceImage.width;
        var sh:int = sourceImage.height;
        
        var rate:Number = rw / sw; 
        var rateH:Number = rh / sh; 
        rate = rate < rateH ? rate : rateH ;
        
        var nw:int = sw * rate;
        var nh:int = sh * rate;
        
        var x:int = rect.x,y:int = rect.y;
        switch( aling % 3 ){
            case ALING_LEFT: 
                x += 0;
                break;
            case ALING_RIGHT: 
                x += ( rw - nw ) >>0;
                break;
            default:
                x += ( rw - nw ) >> 1;
        }
        switch( ((aling/3)>>0 % 3) * 3 ){
            case ALING_TOP: 
                y += 0;
                break;
            case ALING_BOTTOM: 
                y += ( rh - nh ) >> 0;
                break;
            default:
                y += ( rh - nh ) >> 1;
        }
            
        var mtr:Matrix = new Matrix( rate, 0, 0, rate, x, y );
        bitmapData.draw( sourceImage, mtr );
        
        return new Rectangle(x,y,nw,nh);
    }
    
    static public function material( output:BitmapData, source:BitmapData, rect:Rectangle = null, point:Point = null, scale:int = 1 ):void{
        if(! rect ){ rect = source.rect; }
        if(! point ){ point = new Point; }
        var b1:BitmapData = new BitmapData( rect.width, rect.height, output.transparent );
        var b2:BitmapData = new BitmapData( rect.width, rect.height, output.transparent );
        
        
        var filter:BlurFilter = new BlurFilter( scale , scale );
        b2.applyFilter( source, rect, new Point, filter );
        
        filter = new BlurFilter( scale*2 , scale*2 );
        b1.applyFilter( source, rect, new Point, filter );
        
        difference( output, b1, b2, rect );
        output.applyFilter( output, rect, point, filter );
    }
    
    static public function difference( output:BitmapData, source1:BitmapData, source2:BitmapData, rect:Rectangle = null ):void{
        if(! rect ){ rect = source1.rect; }
        var vec1:Vector.<uint> = source1.getVector( source1.rect );
        var vec2:Vector.<uint> = source2.getVector( source1.rect );
        var l:int = vec1.length; 
        for ( var i:int = 0; i<l; i++ ){
            var v1:int = vec1[i];
            var v2:int = vec2[i];
            
            var alpha:int = 0xFF;
            var red:int = r( v1 ) - r( v2 );
            var green:int = g( v1 ) - g( v2 );
            var blue:int = b( v1 ) - b( v2 );
            
            vec1[i] = argb( 
                            alpha,
                            red < 0 ? -red : red, 
                            green < 0 ? -green: green, 
                            blue < 0 ? -blue : blue 
                         )
        }
        output.setVector( rect, vec1 );
    }
    
    static public function a( color:uint ):uint{ return color >>> 24 }
    static public function r( color:uint ):uint{ return ( color >>> 16 ) % 0x100 }
    static public function g( color:uint ):uint{ return ( color >>> 8 ) % 0x100 }
    static public function b( color:uint ):uint{ return color % 0x100 }
    static public function argb( a:uint, r:uint, g:uint, b:uint ):uint{ return (a<<24)+(r<<16)+(g<<8)+b }
    
}
import flash.filters.ConvolutionFilter;
class OutlineFilter extends ConvolutionFilter{
    private var _rate:Number, _dx:Number, _dy:Number;
    public function set rate(n:Number):void{ _rate=n, update()}
    public function get rate():Number{ return _rate }
    
    public function set dx(n:Number):void{ _dx=n, update()}
    public function get dx():Number{ return _dx }
    public function set dy(n:Number):void{ _dy=n, update()}
    public function get dy():Number{ return _dy }
    
    function OutlineFilter ( rate:Number = 1, dx:int = 1, dy:int = 1 ){
        super( 3, 3, null, 1, 0, true, true, 0, 1 );
        _rate = rate;
        _dx = dx;
        _dy = dy;
        update();
    }
    
    private function update():void{
        var mtr:Array = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
        var xMtr:Array = [ -1, -1, -1, 0, 0, 0, 1, 1, 1 ];
        var yMtr:Array = [ -1, 0, 1, -1, 0, 1, -1, 0, 1 ];
        
        for( var i:uint = 0;  i<9; i++ ){
            mtr[i] += rate * xMtr[i] * dx + rate * yMtr[i] * dy 
        }

        matrix = mtr;
    }
}


//NowLoadingライブラリ===============================================================
import flash.display.Loader;
import flash.media.Sound;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.net.URLLoader;
class NowLoading extends Sprite {
    static public const COMPLETE:String = "complete";
    private var loaders:Vector.<Object> = new Vector.<Object>;
    private var bytesTotal:uint=0,bytesLoaded:uint=0;
    private var sprite:ProgressSprite;
    
    private var _loaderNum:uint=0, _completedNum:uint=0, _openNum:uint=0;
    private var text:Bitmap;
    private var onLoaded:Function;
    
    private var LETTER:Object = {
        "1":[[0,1,1],[0,0,1],[0,0,1],[0,0,1],[0,0,1]],"2":[[1,1,1],[0,0,1],[0,1,1],[1,0,0],[1,1,1]],"3":[[1,1,1],[0,0,1],[1,1,1],[0,0,1],[1,1,1]],"4":[[1,0,1],[1,0,1],[1,0,1],[1,1,1],[0,0,1]],"5":[[1,1,1],[1,0,0],[1,1,1],[0,0,1],[1,1,1]],
        "6":[[1,1,1],[1,0,0],[1,1,1],[1,0,1],[1,1,1]],"7":[[1,1,1],[0,0,1],[0,0,1],[0,0,1],[0,0,1]],"8":[[1,1,1],[1,0,1],[1,1,1],[1,0,1],[1,1,1]],"9":[[1,1,1],[1,0,1],[1,1,1],[0,0,1],[0,0,1]],"0":[[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],
        ".":[[0],[0],[0],[0],[1]]," ":[[0],[0],[0],[0],[0]],"n":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,0,1]],"w":[[0,0,0,0,0],[0,0,0,0,0],[1,0,1,0,1],[1,0,1,0,1],[1,1,1,1,1]],"o":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,1,1]],
        "a":[[0,0,0],[0,0,1],[1,1,1],[1,0,1],[1,1,1]],"l":[[1],[1],[1],[1],[1]],"i":[[1],[0],[1],[1],[1]],"d":[[0,0,1],[0,0,1],[1,1,1],[1,0,1],[1,1,1]],"g":[[0,0,0],[0,0,0],[1,1,1],[1,0,1],[1,1,1],[0,0,1],[1,1,1]],
        "C":[[1,1,1],[1,0,0],[1,0,0],[1,0,0],[1,1,1]],"O":[[1,1,1],[1,0,1],[1,0,1],[1,0,1],[1,1,1]],"M":[[1,1,1,1,1],[1,0,1,0,1],[1,0,1,0,1],[1,0,1,0,1],[1,0,1,0,1]],"P":[[1,1,1],[1,0,1],[1,1,1],[1,0,0],[1,0,0]],
        "T":[[1,1,1],[0,1,0],[0,1,0],[0,1,0],[0,1,0]],"L":[[1,0,0],[1,0,0],[1,0,0],[1,0,0],[1,1,1]],"E":[[1,1,1],[1,0,0],[1,1,1],[1,0,0],[1,1,1]]
    }
    
    public function NowLoading( onLoaded:Function = null, progressSprite:ProgressSprite = null ){
        this.onLoaded = onLoaded;
        if ( progressSprite == null ) {
            sprite = new ProgressSprite();
        }else{
            sprite = progressSprite;
        }
        
        text = new Bitmap( new BitmapData(30 * 4, 8, true, 0x00000000 ) ); 
        
        addChild(sprite).blendMode = "invert"; 
        addChild(text).blendMode = "invert"; 
        
        addEventListener( Event.ADDED_TO_STAGE, init );
    }
    
    private function init(e:Event):void{
        removeEventListener( Event.ADDED_TO_STAGE, init );
        with (text) { 
            scaleX = scaleY = 1; 
            x = stage.stageWidth - text.width;
            y = stage.stageHeight - text.height;
        }
    }
    
    public function addLoader(loader:Loader):Loader{ setListener(loader.contentLoaderInfo);_loaderNum++;return loader;}
    public function addURLLoader(loader:URLLoader):URLLoader{setListener(loader); _loaderNum++; return loader;}
    public function addSound(sound:Sound):Sound{setListener(sound); _loaderNum++; return sound;}
    
    private function setListener(loader:*):void{
        loader.addEventListener("open", onOpen);
        loader.addEventListener("complete", onComplete);
        loader.addEventListener("progress", update);
    }
    
    private function update(e:Event=null):void{
        bytesLoaded=0; bytesTotal=0;
        for each(var loadObj:Object in loaders){
            bytesLoaded += loadObj.bytesLoaded;
            bytesTotal += loadObj.bytesTotal;
        };
        sprite.progress( bytesLoaded/bytesTotal * _openNum / _loaderNum );
        if (bytesTotal != 0) {
            setText( "now loading... " + (bytesLoaded / bytesTotal * _openNum / _loaderNum * 100).toFixed(1) );
        }
    }
    
    private function onOpen(e:Event):void { 
        e.currentTarget.removeEventListener("open", onOpen); 
        _openNum++;
        loaders.push(e.currentTarget);
        bytesTotal += e.currentTarget.bytesTotal;
    }
    
    private function onComplete(e:Event):void {  
        e.currentTarget.removeEventListener("complete", onComplete);
        e.currentTarget.removeEventListener("progress", update);
        _completedNum++; 
        if (_loaderNum == _completedNum) { 
            setText( "COMPLETE" );
            if ( onLoaded != null ) { onLoaded() } 
        } 
    }
    
    
    private function setText(str:String):void{
        var b:BitmapData = text.bitmapData; var l:int = str.length; var position:int = b.width;
        b.lock();b.fillRect(b.rect,0x000000);
        for(var i:int=0;i<l;i++){
            var letterData:Array = LETTER[str.substr(l-i-1,1)];position-=letterData[0].length+1;
            for(var n:int=0;n<letterData.length;n++){ for(var m:int=0;m<letterData[n].length;m++){ 
                if(letterData[n][m]==1){b.setPixel32(m+position,n+1,0xFF000000);} 
            } }
        }
        b.unlock();
    }
}
class ProgressSprite extends Sprite{
    public function progress(rate:Number):void {
        if( stage != null ){
            graphics.clear();
            graphics.beginFill( 0x000000, 1 )
            graphics.drawRect( 0, stage.stageHeight - 11, rate * stage.stageWidth, 11);
        }
    }
}
//NowLoadingライブラリここまで==============================================================