forked from: forked from: flash on 2012-11-15

by ysissy forked from forked from: flash on 2012-11-15 (diff: 31)
♥0 | Line 45 | Modified 2012-11-16 02:26:16 | MIT License
play

ActionScript3 source code

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

package {
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.Sprite;
    public class syBrush extends Sprite {
        
        public var drawpad : Sprite = new Sprite();
    
        public function syBrush() {
            
            addChild(drawpad);

            // Capturing Mouse Cursor Coordinate
            stage.addEventListener(MouseEvent.MOUSE_DOWN,doMouseDown);
            stage.addEventListener(MouseEvent.MOUSE_UP,doMouseUp);
            stage.addEventListener(MouseEvent.MOUSE_MOVE,doMouseMove);
            // Draw line during every frames
            drawpad.addEventListener(Event.ENTER_FRAME,doEnter);


            var drawflag : Boolean = false;
            var linedata : Array = new Array();


            // Start to add coordinate into array when mouse is down
            function doMouseDown(e:MouseEvent):void{
                drawflag = true;
                linedata = new Array(); // To Create multidimensional arrays Perhaps??? Or Initialize array??
                linedata.push ([mouseX,mouseY]);
            }


            // Add coordinate to array when mouse moves
            function doMouseMove(e:MouseEvent):void{
                if(drawflag) {
                       linedata.push([mouseX,mouseY]);
                }
            }


            // Stop to add coordinate when mouse is up
            function doMouseUp(e:MouseEvent):void{
                drawflag = false;
                
                // drawpad.removeEventListener(Event.ENTER_FRAME,doEnter);
                
            }


            
            
            function doEnter(e:Event) : void {
                var wx1:Number;
                var wy1:Number;
                
                //var wx2:Number;
                //var wy2:Number;
                
                
                // when array starts to fill in (equal to when mouse clicked)
                if (0 < linedata.length) { 
                    
                    // Clean at once : only draw one stroke in one time
                    //drawpad.graphics.clear();
            
                    //Draw with Red Line
                    drawpad.graphics.lineStyle(  1, 0xFF0000 );
                    
                    // Position when mouse click
                    wx1 = linedata[0][0];
                    wy1 = linedata[0][1];
                    
                    //wx2 = wx1 + 10;
                    //wy2 = wy1 + 10;
                    
                    
                    // And Ready for Drawing ; Move Pen to clicked coordinate
                    drawpad.graphics.moveTo(wx1,wy1);
                    
                                     
                    // Draw
                    for(var i:int = 1; i < linedata.length; i++) {
                            wx1 = linedata[i][0]; // First value i = 1 means "After Click"
                            wy1 = linedata[i][1];
                            drawpad.graphics.lineTo(wx1,wy1);
                            //drawpad.graphics.lineTo(wx2,wy2);
                    }
                    
                }
            }
            
            
            
            
            
            
            
        }
    }
}