Skip to content

Instantly share code, notes, and snippets.

@eeropic
Created April 14, 2018 12:53
Show Gist options
  • Save eeropic/a3c2d0bca012bfead800d5d6636c2a9f to your computer and use it in GitHub Desktop.
Save eeropic/a3c2d0bca012bfead800d5d6636c2a9f to your computer and use it in GitHub Desktop.
Paper.JS tool module test
(function(global) {
with(paper){
var toolPen=new Tool();
toolPen.snap=true;
toolPen.gridSize=20;
toolPen.buttonClass="icon-fountain-pen";
toolPen.on({
mousedown:function(event){
this.draw=true;
var hitTest=project.hitTest(event.point, {tolerance:2,fill:true,stroke:true,handles:true,segments:true,selected:true});
if(hitTest!=null){
if(hitTest.type=='segment'){
if(hitTest.segment==hitTest.item.lastSegment){
this.path=hitTest.item;
}
if(hitTest.segment==hitTest.item.firstSegment){
hitTest.item.reverse();
this.path=hitTest.item;
}
}
if(hitTest.type=='fill' || hitTest.type=='stroke'){
var insertLocation=hitTest.item.getNearestLocation(event.point);
var insertPoint=hitTest.item.getNearestPoint(event.point);
if(insertLocation.segment!=hitTest.item.firstSegment){
//this.insertedSegment=hitTest.item.insert(insertLocation.index+1,insertPoint);
if(!hitTest.item.closed){
var splitPath=hitTest.item.split(insertLocation)
this.path=hitTest.item.join(splitPath);
}
else{
this.path=hitTest.item.split(insertLocation).join()
}
this.insertedSegment=this.path.segments[hitTest.item.getNearestLocation(event.point).index];
this.draw=false;
project.deselectAll();
this.insertedSegment.selected=true;
}
}
}
if(!event.modifiers.option){
if(this.path==null){
if(this.draw){
project.deselectAll();
this.path=new Path();
this.path.add((this.snap)?(event.point.divide(this.gridSize)).round().multiply(this.gridSize):event.point)
this.path.selected=true;
}
}
else{
//check if we can close the path
var closePt=this.snap?(event.point.divide(this.gridSize)).round().multiply(this.gridSize):event.point;
var closeTest=this.path.hitTest(closePt, {ends:true});
if(closeTest!=null){
if(closeTest.segment==this.path.firstSegment){
this.path.closed=true;
this.path.lastSegment.selected=false;
this.path.firstSegment.selected=true;
this.path=null;
return;
}
}
else{
if(this.draw){
this.path.add((this.snap)?(event.point.divide(this.gridSize)).round().multiply(this.gridSize):event.point)
}
}
}
if(this.draw){
var seg=this.path.lastSegment;
this.path.segments[Math.max(0,seg.index-1)].selected=false;
seg.selected=true;
}
}
},
mousemove:function(event){
var hitTest=project.hitTest(event.point, {fill:true,stroke:true,segments:true,selected:true,ends:false});
if(hitTest!=null){
if(hitTest.type=="stroke"){}
if(hitTest.type=="segment"){}
}
},
mousedrag:function(event){
if(this.path!=null){
if(this.draw)var seg=this.path.lastSegment;
else var seg=this.insertedSegment;
var dragPoint=(this.snap)?((event.point.subtract(seg.point)).divide(this.gridSize)).round().multiply(this.gridSize):event.point.subtract(seg.point);
if(event.modifiers.shift)dragPoint.angle=Math.round(dragPoint.angle/45)*45;
if(seg!=this.path.firstSegment)seg.handleIn=dragPoint.rotate(180)
seg.handleOut=dragPoint;
}
},
});
}
global.toolPen=toolPen
}(self));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment