Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pan using paperjs

Tags:

paperjs

I have been trying to figure out how to pan/zoom using onMouseDrag, and onMouseDown in paperjs.
The only reference I have seen has been in coffescript, and does not use the paperjs tools.

like image 717
Sam P Avatar asked Sep 12 '15 14:09

Sam P


3 Answers

This took me longer than it should have to figure out.

var toolZoomIn = new paper.Tool();
toolZoomIn.onMouseDrag = function (event) {
    var a = event.downPoint.subtract(event.point);
    a = a.add(paper.view.center);
    paper.view.center = a;
}
like image 178
Sam P Avatar answered Oct 18 '22 07:10

Sam P


you can simplify Sam P's method some more:

var toolPan = new paper.Tool();
toolPan.onMouseDrag = function (event) {
    var offset = event.downPoint - event.point;
    paper.view.center = paper.view.center + offset;
};

the event object already has a variable with the start point called downPoint.

i have put together a quick sketch to test this.

like image 2
Stefan Krüger s-light Avatar answered Oct 18 '22 06:10

Stefan Krüger s-light


Unfortunately you can't rely on event.downPoint to get the previous point while you're changing the view transform. You have to save it yourself in view coordinates (as pointed out here by Jürg Lehni, developer of Paper.js).

Here's a version that works (also in this sketch):

let oldPointViewCoords;

function onMouseDown(e) {
    oldPointViewCoords = view.projectToView(e.point);
}

function onMouseDrag(e) {
    const delta = e.point.subtract(view.viewToProject(oldPointViewCoords));
    oldPointViewCoords = view.projectToView(e.point);
    view.translate(delta);
}

view.translate(view.center);
new Path.Circle({radius: 100, fillColor: 'red'});
like image 1
Rick Mohr Avatar answered Oct 18 '22 05:10

Rick Mohr