Advertisement
cornedor

cubicCurveTo haXe

May 29th, 2011
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ;
  2.  
  3. import flash.display.MovieClip;
  4. import flash.events.MouseEvent;
  5. import flash.geom.Point;
  6. import flash.Lib;
  7.  
  8. /**
  9.  * ...
  10.  * @author Corné Dorrestijn
  11.  */
  12.  
  13. class Main extends MovieClip
  14. {
  15.     inline public static var snapSize:Int = 10;
  16.     public var points:Array<Point>;
  17.     public var ssh:Float;
  18.     public var ssw:Float;
  19.     public function new()
  20.     {
  21.         super();
  22.         ssw = Lib.current.stage.stageWidth / 2;
  23.         ssh = Lib.current.stage.stageHeight / 2;
  24.         points = new Array();
  25.         Lib.current.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  26.         Lib.current.stage.addEventListener(MouseEvent.CLICK, onClick);
  27.         graphics.beginFill(0xFF0000);
  28.         graphics.drawCircle(ssw, ssh, 2);
  29.     }
  30.    
  31.     public function onClick(e:MouseEvent):Void
  32.     {
  33.         if (points.length > 1)
  34.         {
  35.             var ax = mouseX;
  36.             var ay = mouseY;
  37.             var bx = points[0].x;
  38.             var by = points[0].y;
  39.             if((bx - snapSize < ax && bx + snapSize > ax) && (by - snapSize < ay && by + snapSize > ay))
  40.             {
  41.                 ax = bx;
  42.                 ay = by;
  43.                 Lib.current.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  44.                 Lib.current.stage.removeEventListener(MouseEvent.CLICK, onClick);
  45.             }
  46.             points.push(new Point(ax, ay));
  47.         }else
  48.             points.push(new Point(mouseX, mouseY));
  49.     }
  50.     public function onMouseMove(e:MouseEvent):Void
  51.     {      
  52.         if (points.length > 0)
  53.         {          
  54.             graphics.clear();
  55.             graphics.beginFill(0xFF0000);
  56.             graphics.drawCircle(ssw, ssh, 2);
  57.             graphics.lineStyle(4, 0xFFFFFF);
  58.             graphics.beginFill(0x454545);
  59.             graphics.beginFill(0xFFFFFF);
  60.             graphics.moveTo(points[0].x, points[0].y);
  61.             if (points.length > 1)
  62.             {
  63.                 for (i in 1...points.length)
  64.                     graphics.cubicCurveTo(ssw, ssh, ssw, ssh, points[i].x, points[i].y);
  65.                 var ax = mouseX;
  66.                 var ay = mouseY;
  67.                 var bx = points[0].x;
  68.                 var by = points[0].y;
  69.                 if((bx - snapSize < ax && bx + snapSize > ax) && (by - snapSize < ay && by + snapSize > ay))
  70.                 {
  71.                     ax = bx;
  72.                     ay = by;
  73.                 }
  74.                 graphics.cubicCurveTo(ssw, ssh, ssw, ssh, ax, ay);
  75.             }else
  76.                 graphics.cubicCurveTo(ssw, ssh, ssw, ssh, mouseX, mouseY);
  77.             }
  78.     }
  79.     static function main()
  80.     {
  81.         Lib.current.addChild(new Main());
  82.     }
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement