Inconsistent dragging speed when zoomed in

I have created a Processing sketch which supports dragging the whole display as well as zooming it in. Here are the methods I use to drag and zoom:

public void mousePressed(){
    dragBeginX = (mouseX - offsetX);
    dragBeginY = (mouseY - offsetY);

public void mouseDragged(){
    if (mousePressed) {
    offsetX = (mouseX - dragBeginX);
    offsetY = (mouseY - dragBeginY);

public void zoomTo(float sc){
    translate(width/2, height/2);
    translate(-width/2, -height/2);
    //actual zooming handled in separate thread for smoothness

and in before doing the graphic stuff:

translate(offsetX, offsetY);

There is no actual bug in that code, but it doesn't quite work the way I want - when the application is zoomed in, the dragging speed remains the same which means that the graphics move much faster than the cursor. Opposite applies when application is zoomed out. I have tried inserting the zoom value in the dragging math so that the drag speed would depend on the zoom, but I can't find where exactly it needs to be.

To sum it up, I need to make it so that when zoomed in, the object moving speed matches with the cursor dragging speed.

Any help is appreciated.

EDIT: If it was unclear, offsetX/Y is the current dragged position of the screen;


Sign In or Register to comment.