Guest User

Untitled

a guest
Apr 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. (function(global) {
  2. with(paper){
  3.  
  4. var toolPen=new Tool();
  5. toolPen.snap=true;
  6. toolPen.gridSize=20;
  7. toolPen.buttonClass="icon-fountain-pen";
  8. toolPen.on({
  9. mousedown:function(event){
  10. this.draw=true;
  11. var hitTest=project.hitTest(event.point, {tolerance:2,fill:true,stroke:true,handles:true,segments:true,selected:true});
  12.  
  13. if(hitTest!=null){
  14. if(hitTest.type=='segment'){
  15. if(hitTest.segment==hitTest.item.lastSegment){
  16. this.path=hitTest.item;
  17. }
  18. if(hitTest.segment==hitTest.item.firstSegment){
  19. hitTest.item.reverse();
  20. this.path=hitTest.item;
  21. }
  22. }
  23. if(hitTest.type=='fill' || hitTest.type=='stroke'){
  24. var insertLocation=hitTest.item.getNearestLocation(event.point);
  25. var insertPoint=hitTest.item.getNearestPoint(event.point);
  26. if(insertLocation.segment!=hitTest.item.firstSegment){
  27. //this.insertedSegment=hitTest.item.insert(insertLocation.index+1,insertPoint);
  28. if(!hitTest.item.closed){
  29. var splitPath=hitTest.item.split(insertLocation)
  30. this.path=hitTest.item.join(splitPath);
  31. }
  32. else{
  33. this.path=hitTest.item.split(insertLocation).join()
  34. }
  35. this.insertedSegment=this.path.segments[hitTest.item.getNearestLocation(event.point).index];
  36. this.draw=false;
  37. project.deselectAll();
  38. this.insertedSegment.selected=true;
  39. }
  40. }
  41. }
  42.  
  43. if(!event.modifiers.option){
  44. if(this.path==null){
  45. if(this.draw){
  46. project.deselectAll();
  47. this.path=new Path();
  48. this.path.add((this.snap)?(event.point.divide(this.gridSize)).round().multiply(this.gridSize):event.point)
  49. this.path.selected=true;
  50. }
  51. }
  52. else{
  53. //check if we can close the path
  54. var closePt=this.snap?(event.point.divide(this.gridSize)).round().multiply(this.gridSize):event.point;
  55. var closeTest=this.path.hitTest(closePt, {ends:true});
  56. if(closeTest!=null){
  57. if(closeTest.segment==this.path.firstSegment){
  58. this.path.closed=true;
  59. this.path.lastSegment.selected=false;
  60. this.path.firstSegment.selected=true;
  61. this.path=null;
  62. return;
  63. }
  64. }
  65. else{
  66. if(this.draw){
  67. this.path.add((this.snap)?(event.point.divide(this.gridSize)).round().multiply(this.gridSize):event.point)
  68.  
  69. }
  70. }
  71. }
  72.  
  73. if(this.draw){
  74. var seg=this.path.lastSegment;
  75. this.path.segments[Math.max(0,seg.index-1)].selected=false;
  76. seg.selected=true;
  77. }
  78. }
  79.  
  80. },
  81. mousemove:function(event){
  82. var hitTest=project.hitTest(event.point, {fill:true,stroke:true,segments:true,selected:true,ends:false});
  83. if(hitTest!=null){
  84. if(hitTest.type=="stroke"){}
  85. if(hitTest.type=="segment"){}
  86. }
  87. },
  88. mousedrag:function(event){
  89. if(this.path!=null){
  90. if(this.draw)var seg=this.path.lastSegment;
  91. else var seg=this.insertedSegment;
  92.  
  93. var dragPoint=(this.snap)?((event.point.subtract(seg.point)).divide(this.gridSize)).round().multiply(this.gridSize):event.point.subtract(seg.point);
  94. if(event.modifiers.shift)dragPoint.angle=Math.round(dragPoint.angle/45)*45;
  95. if(seg!=this.path.firstSegment)seg.handleIn=dragPoint.rotate(180)
  96. seg.handleOut=dragPoint;
  97. }
  98. },
  99. });
  100.  
  101.  
  102.  
  103. }
  104. global.toolPen=toolPen
  105. }(self));
Add Comment
Please, Sign In to add comment