forked from: flash on 2013-9-20

by imo_
♥0 | Line 84 | Modified 2013-09-24 22:05:15 | MIT License
play

ActionScript3 source code

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

// forked from novic.one's flash on 2013-9-20
package {
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Matrix;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        private const WIDTH:Number = 100;
        private const HEIGHT:Number = 100;
        
        private var points:Array = [];
        private var matrix:Matrix;
        
        private var shape:Sprite;
        private var handlesContainer:Sprite;
        
        private var P1:Sprite;
        private var P2:Sprite;
        
        public function FlashTest() {
            x = y = 100;
            addChild(shape = new Sprite());
            
            shape.graphics.lineStyle(2, 0, 1, false, "none");
            shape.graphics.beginFill(0xcdefab);
            shape.graphics.drawRect(0, 0, WIDTH, HEIGHT);
            
            addChild(handlesContainer = new Sprite());
            
            createHandle(new Point(0, HEIGHT));
            createHandle(new Point(WIDTH / 2, 0));
            createHandle(new Point(WIDTH, HEIGHT));
            
            addChild(P1 = new Sprite());
            addChild(P2 = new Sprite());
            
            P1.graphics.beginFill(0xff0000);
            P1.graphics.drawRect(-5, -5, 10, 10);
            
            P2.graphics.beginFill(0xff0000);
            P2.graphics.drawRect(-5, -5, 10, 10);
            
            update();
        }
        
        private function update():void {
            var pox:Number = points[1].x - (points[2].x - points[0].x) * 0.5;
            var poy:Number = points[1].y - (points[2].y - points[0].y) * 0.5;
            var pix:Number = points[1].x + (points[2].x - points[0].x) * 0.5;
            var piy:Number = points[1].y + (points[2].y - points[0].y) * 0.5;
            var pjx:Number = points[0].x;
            var pjy:Number = points[0].y;
            
            var e00:Number = (pix - pox) / WIDTH;
            var e10:Number = (piy - poy) / HEIGHT;
            var e01:Number = (pjx - pox) / WIDTH;
            var e11:Number = (pjy - poy) / HEIGHT;
            
            var matrix:Matrix = new Matrix();
            matrix.tx = pox;
            matrix.ty = poy;
            matrix.a = e00;
            matrix.b = e10;
            matrix.c = e01;
            matrix.d = e11;
            
            P1.x = pox;
            P1.y = poy;
            P2.x = pix;
            P2.y = piy;
            shape.transform.matrix = matrix;
        }

        
        private function createHandle(pt:Point):void {
            var handle:Sprite = new Sprite();
            handle.graphics.lineStyle(1);
            handle.graphics.beginFill(0xabcdef);
            handle.graphics.drawRect(-5, -5, 10, 10);
            
            handlesContainer.addChild(handle);
            handle.x = pt.x;
            handle.y = pt.y;
            
            points.push(pt);
            
            var moveHandler:Function = function(e:Event):void {
                pt.x = handle.x;
                pt.y = handle.y;
                update();
            };
            var upHandler:Function = function(e:Event):void {
                stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
                stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
                handle.stopDrag();
            };
            
            handle.addEventListener(MouseEvent.MOUSE_DOWN, function(e:Event):void {
                handle.startDrag();
                stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
                stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
            });

        }
        
        
    }
}