VectorImage class

by shohei909
♥0 | Line 54 | Modified 2010-07-03 11:21:46 | MIT License
play

ActionScript3 source code

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

package{
    import flash.display.*;
    public class VectorTest extends Sprite{
        private static const IMG_PASS:Array = [[[1,1.5,0x000000,1.0,0x00FF22,1.0],[12,6,1],[38,6,1],[50,35,1],[40,35,1],[40,50,1],[25,40,1],[10,50,1],[10,35,1],[0,35,1]],[[1,1.5,0x000000,1.0,0xFF0000,1.0],[36,14,0],[14,14,0],[14,22,0],[36,22,0]],[[1,1.5,0x000000,1.0,0xFF0000,1.0],[6,10,0],[22,10,0]],[[1,1.5,0x000000,1.0,0xFF0000,1.0],[42,10,0],[28,10,0]]];
        public function VectorTest():void{
            addChild( new VectorImage(IMG_PASS) );
        }
    }
}


import flash.display.Shape;

//ベクタ画像用クラス=================================================================================================================================================================
class VectorImage extends Shape{
    public var data:Array;
    public var styles:Vector.<Array>;        //styles[][],,,style:[open/close,lineWeight,lineColor,lineAlpha,fillColor,fillAlpha]
    public var dots:Vector.<Array>;            //dots[][][],,,dot:[x,y,curve]
    
    //コンストラクタ
    public function VectorImage(pass:Array){ read(pass); }

    //データの読み込み
    public function read(pass:Array):void{
        data=pass;
        styles = new Vector.<Array>();
        dots = new Vector.<Array>();
        var size:int = pass.length
        for(var i:int=0;i<size;i++){
            styles[i] = [];dots[i] = [];
            for(var j:int=0;j<6;j++){ styles[i][j]=pass[i][0][j]; }
            var l:int =pass[i].length-1;
            for(var k:int=0;k<l;k++){
                dots[i][k] = [];
                for(var n:int=0;n<3;n++){ dots[i][k][n]=pass[i][k+1][n]; }
            }
        }
        drow();
    }

    //データの描画
    public function drow():void{
        var size:int = styles.length;
        graphics.clear();
        for(var i:int=0;i<size;i++){
            var l:int=dots[i].length;
            graphics.lineStyle(styles[i][1],styles[i][2],styles[i][3]);
            if(styles[i][0]==1){ graphics.beginFill(styles[i][4],styles[i][5]); var bf:Array=dots[i][l-1];}
            else{ bf=dots[i][0];}
            graphics.moveTo( center(bf,dots[i][0],0),center(bf,dots[i][0],1) ); 
            for(var j:int=0;j<l-1;j++){  line(bf,dots[i][j],dots[i][j+1]);bf=dots[i][j]; }
            if(styles[i][0]==1){ line( bf, dots[i][j], dots[i][0]);  }
            else{  line(bf, dots[i][j], dots[i][j] ); }
        }
    }
    private function line(dot1:Array,dot2:Array,dot3:Array):void{
        graphics.lineTo( center(dot2,dot1,0),center(dot2,dot1,1) );
        if(dot2[2]>0){graphics.curveTo( dot2[0],dot2[1],center(dot2,dot3,0),center(dot2,dot3,1) );}
    }
    private function center(dot1:Array,dot2:Array,xy:int):Number{
        if( dot1[2] == 0){return dot1[xy];}
        return (dot1[xy]*dot2[2]+dot2[xy]*dot1[2]*dot1[2])/(dot1[2]*dot1[2]+dot2[2]);
    }
}
//=========================================================================================================================================================================================

Forked