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 munegon 18 Dec 2008
// write as3 code here..
package {
    import flash.display.*;
    import flash.events.*;
    import flash.ui.Keyboard;
    
    public class DrawApp extends Sprite {
        private const COLORS:Array = [ 0x000000, 0xcc0000, 0xcccc00, 0x00cc00, 0x00cccc, 0x0000cc, 0xcc00cc ];
        private const THICKNESS_MIN:int = 1;
        private const THICKNESS_MAX:int = 10;
        
        private var canvas_bd:BitmapData;
        private var canvas_sh:Shape;
        private var pen_sh:Shape;
        
        private var index:int;
        private var color:int;
        private var thickness:int;
        
        public function DrawApp() {
            addEventListener( Event.ADDED_TO_STAGE, added );
        }
        
        private function added( e:Event ):void {
            if ( e.target == this ) {
                removeEventListener( e.type, arguments.callee );
                
                setup();   
            }    
        }
        
        private function setup():void {
            canvas_bd = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0xffffff );
            addChild( new Bitmap( canvas_bd ) );
            
            canvas_sh = new Shape();
            addChild( canvas_sh );
            
            pen_sh = new Shape();
            pen_sh.x = 5;
            pen_sh.y = 5;
            addChild( pen_sh );
            
            index = 0;
            color = COLORS[ index ];
            thickness = 5;
            
            refreshPen();
            
            stage.addEventListener( MouseEvent.MOUSE_DOWN, startDraw );
            stage.addEventListener( KeyboardEvent.KEY_UP, keyUpHandler );
        }
        
        private function refreshPen():void {
            var g:Graphics = pen_sh.graphics;
            g.clear();
            g.lineStyle( 0, 0x000000 );
            g.beginFill( 0xffffff, 0.9 );
            g.drawRect( 0, 0, 16, 16 );
            g.endFill();
            
            g.lineStyle( thickness, color );
            g.moveTo( 8.0, 8 );
            g.lineTo( 8.2, 8 );  
        }
        
        private function keyUpHandler( e:KeyboardEvent ):void {
            switch ( e.keyCode ) {
                case Keyboard.LEFT:
                    index = ( COLORS.length + index - 1 ) % COLORS.length;
                    color = COLORS[ index ];
                    break;
                case Keyboard.RIGHT:
                    index = ( index + 1 ) % COLORS.length;
                    color = COLORS[ index ];
                    break;
                case Keyboard.UP:
                    thickness = Math.min( thickness + 1, THICKNESS_MAX );
                    break;
                case Keyboard.DOWN:
                    thickness = Math.max( thickness - 1, THICKNESS_MIN );
                    break;
            }
            
            refreshPen();
        }
        
        private function startDraw( e:MouseEvent ):void {
            var g:Graphics = canvas_sh.graphics;
            
            g.clear();
            g.lineStyle( thickness, color, 0.8 );
            g.moveTo( e.stageX, e.stageY );
            
            var up:Function = function( e:MouseEvent ):void {
                stage.removeEventListener( MouseEvent.MOUSE_MOVE, move );
                stage.removeEventListener( MouseEvent.MOUSE_UP, up );
                
                canvas_bd.draw( canvas_sh );
                g.clear();
            }
            var move:Function = function( e:MouseEvent ):void {
                g.lineTo( e.stageX, e.stageY );
            }
            
            stage.addEventListener( MouseEvent.MOUSE_MOVE, move );
            stage.addEventListener( MouseEvent.MOUSE_UP, up );
        }
    }
}