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: 空間に絵を描く

Get Adobe Flash player
by tepe 08 Mar 2011
// forked from miniapp's 空間に絵を描く
package { 
    
    import flash.display.GraphicsPathCommand;
    import flash.display.SpreadMethod;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.GlowFilter;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.geom.Matrix3D;
    import flash.geom.PerspectiveProjection;
    import flash.geom.Point;
    import flash.geom.Utils3D;
    import flash.geom.Vector3D;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.ui.Keyboard;
    
    [SWF(backgroundColor="0x000000", width="465", height="465", frameRate="60")]
    public class Projection1 extends Sprite {
        
        private static var identityMatrix:Matrix3D = new Matrix3D();
        private var sp:Sprite = new Sprite();
        
        public function Projection1() {
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            sp.filters = [new GlowFilter(0x3300CC, 0.7, 32, 32, 2, 2)];
            addChild(sp);
            var tf:TextField = new TextField();
            
            tf.autoSize = TextFieldAutoSize.LEFT;
            tf.textColor = 0xffffff;
            tf.text = 'マウスドラッグで空間に絵を描きます。\nshiftキーを押してると回転します。';
            addChild(tf);

                        Wonderfl.capture_delay( 60 );
            draw3();
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
        
        public var uv:Vector.<Number> = new Vector.<Number>();
        public var verts:Vector.<Number>  = new Vector.<Number>();
        public var projectedVerts:Vector.<Number> = new Vector.<Number>();
        public var commands:Vector.<int> = new Vector.<int>();
        public var matrix:Matrix3D = new Matrix3D();
        public var isMouseDowm:Boolean = false;
        public var shiftkeyIsDown:Boolean = false;
        public var centerX:Number = stage.stageWidth / 2;
        public var centerY:Number = stage.stageHeight / 2;
        public var pmouseX:Number;
        public var pmouseY:Number;
        
      
        
        private function keyUpHandler(e:KeyboardEvent):void {
            if (e.keyCode == Keyboard.SHIFT) shiftkeyIsDown = false;
        }
        
        private function keyDownHandler(e:KeyboardEvent):void {
            if (e.keyCode == Keyboard.SHIFT) shiftkeyIsDown = true;
        }
        
        //毎フレーム処理
        private function enterFrameHandler(e:Event):void {
            if (isMouseDowm) {                
                commands.push(GraphicsPathCommand.LINE_TO);
                verts.push(mouseX, mouseY, 0);
            }
            
            //rotate1(0,10);
            
           
            
            if (shiftkeyIsDown) { 
                 //1. 行列をリセット
                matrix.identity();
                //2. 回転の中心になる座標を原点に移動
                matrix.appendTranslation( -centerX, -centerY, 0);
            
                //一フレーム前のマウス座標との差分を回転角度にする。
                var dx:Number = mouseY - pmouseY;
                var dy:Number = mouseX - pmouseX;
                matrix.appendRotation(-dx, Vector3D.X_AXIS);
                matrix.appendRotation(dy, Vector3D.Y_AXIS);
                
                //3. 2で移動した分だけ元に戻す
                matrix.appendTranslation(centerX, centerY, 0);
            
                //4. 頂点データを回転させる。
                matrix.transformVectors(verts, verts);    
                
            }
            

            
            //5. 2次元に投影
            Utils3D.projectVectors(identityMatrix, verts, projectedVerts, uv);
            
            sp.graphics.clear(); 
            sp.graphics.lineStyle(1, 0xffffff);
            
            sp.graphics.drawPath(commands, projectedVerts);
            
            pmouseX = mouseX;
            pmouseY = mouseY;
        }
        
        private function rotate1(x:Number=0,y:Number=0):void{
             //1. 行列をリセット
            matrix.identity();
            //2. 回転の中心になる座標を原点に移動
            matrix.appendTranslation( -centerX, -centerY, 0);
            

            matrix.appendRotation(-x, Vector3D.X_AXIS);
            matrix.appendRotation(y, Vector3D.Y_AXIS);

            
            //3. 2で移動した分だけ元に戻す
            matrix.appendTranslation(centerX, centerY, 0);
            
            //4. 頂点データを回転させる。
            matrix.transformVectors(verts, verts);
        }

        private function draw1(x:Number,y:Number,z:Number):void{
            commands.push(GraphicsPathCommand.MOVE_TO);
            verts.push(x,y,z); 
        }
        
        private function draw2(x:Number,y:Number,z:Number):void{            
            commands.push(GraphicsPathCommand.LINE_TO);
            verts.push(x,y,z); 
        }
        
        private function draw3():void{
            draw1(100,100,0);
            draw2(200,100,0);
            draw2(200,200,0);
            draw2(200,200,100);

        }


        
        //描画
        private function mouseDownHandler(e:MouseEvent):void {
            //commands.push(GraphicsPathCommand.MOVE_TO);
            //verts.push(mouseX, mouseY, 0);
            //verts.push(mouseX, mouseY, 0);
            draw1(mouseX, mouseY, 0);
            
            isMouseDowm = true;
        }
        
        private function mouseUpHandler(e:MouseEvent):void {
            isMouseDowm = false;
        }
    }
}