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

Simple brush

Get Adobe Flash player
by Creta 30 Aug 2016
    Embed
/**
 * Copyright Creta ( http://wonderfl.net/user/Creta )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/eyNl
 */

package {
    import flash.geom.Rectangle;
    import flash.events.KeyboardEvent;
    import flash.text.TextField;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.events.MouseEvent;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        //그림판을 만들자
        
        public var Canvas:BitmapData;
        public var CanvasRenderer:Bitmap;
        public var Brush:Shape;
        public var postMousePosition:Point;
        public var post_postMousePosition:Point;
        public var Transform:Matrix;
        
        public var debug_log:TextField;
        
        public var isMouseDown:Boolean;
        public var accelerateAtMouse:Number;
        
        public var brushColor:uint = 0;
        
        public function FlashTest():void {
            // write as3 code here..
            stage.frameRate = 60;
            if(!stage)
            {
                addEventListener(Event.ADDED_TO_STAGE, $init);
            }
            else
            {
                $init();
            }
        }
        
        public function $init(e:Event = null):void
        {
            Canvas = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
            CanvasRenderer = new Bitmap(Canvas, "auto", true);
            addChild(CanvasRenderer);
            
            Brush = new Shape();
            
            isMouseDown = false;
            accelerateAtMouse = 0;
            
            postMousePosition = new Point();
            post_postMousePosition = new Point();
            Transform = new Matrix();
            
            debug_log = new TextField();
            debug_log.selectable = false;
            addChild(debug_log);
            debug_log.width = stage.stageWidth;
            debug_log.height = stage.stageHeight;
            
            Trace("Start at mouse dragging : Draw with a brush");
            Trace("Spacebar : Clear");
            Trace("'R' key : Setting random color of brush");
            
            stage.addEventListener(Event.ENTER_FRAME, draw);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, reset);

            drawCurrentColor();
        }
        
        public function reset(e:KeyboardEvent):void
        {
            if(e.keyCode == 32)
                Canvas.fillRect(Canvas.rect, 0);
            if(e.keyCode == 82)
                brushColor = uint(Math.random() * 0xFFFFFF);
            drawCurrentColor();
        }
        
        public function drawCurrentColor():void
        {
            Canvas.fillRect(new Rectangle(stage.stageWidth - 10, 0, 10, 10), brushColor + 0xFF000000);
        }

        
        public function draw(e:Event):void
        {
            var postAccelerate:Number;
            var i:int;
            var clearNess:uint = 70;
            if(isMouseDown)
            {
                postAccelerate = accelerateAtMouse;
                var accValue:Number = 0;
                
                if(stage.mouseX != postMousePosition.x || stage.mouseY != postMousePosition.y)
                {
                    accelerateAtMouse = (Math.sqrt((stage.mouseX - postMousePosition.x) * (stage.mouseX - postMousePosition.x) + (stage.mouseY - postMousePosition.y) * (stage.mouseY - postMousePosition.y))) / 20;
                    accelerateAtMouse *= 5
                }
                
                Brush.graphics.clear();
                
                Brush.graphics.moveTo(0, 0);
                for(i = 0;i <= clearNess;i += 1)
                {
                    Brush.graphics.lineStyle(postAccelerate + (((accelerateAtMouse - postAccelerate) / clearNess) * i), brushColor);
                    
                    Brush.graphics.lineTo((stage.mouseX - postMousePosition.x) / clearNess * i, (stage.mouseY - postMousePosition.y) / clearNess * i);
                }
                
                Transform.tx = postMousePosition.x;
                Transform.ty = postMousePosition.y;
                
                Canvas.draw(Brush, Transform);
                
                postMousePosition.x = stage.mouseX;
                postMousePosition.y = stage.mouseY;
            }
            else if(accelerateAtMouse != 0)
            {
                while(accelerateAtMouse != 0)
                {
                    post_postMousePosition.x += (post_postMousePosition.x - postMousePosition.x);
                    post_postMousePosition.y += (post_postMousePosition.y - postMousePosition.y);
                    
                    postAccelerate = accelerateAtMouse;
                    
                    accelerateAtMouse = 0;
                    
                    Brush.graphics.clear();
                    
                    Brush.graphics.moveTo(0, 0);
                    
                    for(i = 0;i <= clearNess;i += 1)
                    {
                        Brush.graphics.lineStyle(postAccelerate + (((accelerateAtMouse - postAccelerate) / clearNess) * i), brushColor);
                        
                        Brush.graphics.lineTo((post_postMousePosition.x - postMousePosition.x) / clearNess * i, (post_postMousePosition.y - postMousePosition.y) / clearNess * i);
                    }
                    
                    Transform.tx = postMousePosition.x;
                    Transform.ty = postMousePosition.y;
                    
                    Canvas.draw(Brush, Transform);
                    
                    postMousePosition.x = post_postMousePosition.x;
                    postMousePosition.y = post_postMousePosition.y;
                    
                    if(accelerateAtMouse < 0.00001) accelerateAtMouse = 0;
                }

            }

        }
        
        public function mouseDown(e:MouseEvent):void
        {
            isMouseDown = true;
            postMousePosition.x = stage.mouseX;
            postMousePosition.y = stage.mouseY;
        }

        public function mouseUp(e:MouseEvent):void
        {
            isMouseDown = false;
            post_postMousePosition.x = stage.mouseX;
            post_postMousePosition.y = stage.mouseY;
        }
        
        public function Trace(...paramiters):void
        {
            var i:int;
            var str:String = "";
            for(i = 0;i < paramiters.length;i += 1)
            {
                str += i == paramiters.length - 1 ? paramiters[i].toString() : paramiters[i].toString() + ", ";
            }
            debug_log.appendText(str + "\n");
            debug_log.scrollV = debug_log.maxScrollV;
        }
    }
}