hi all,
I have a simple code that allows drawing in the Processing canvas. Scrolling the mouse allows you to zoom in and out while press and drag the right button moves the drawing accordingly (PAN).
What I’m trying to do now is define the borders of the canvas. the zoom and pan actions move the origin point everywhere without limits and thus in fact I can never return to the starting point.
I want the first frame to be the absolute limits of the drawing space where it can’t be moved at all. And when I’m zooming in, I can’t Pan over the certain limit. A good example of this would be navigating in Google maps that zoom out always brings you back to same origin point on the X axis.
The code is attached and I would like to hear if anyone has any idea how I should do it.
Thanks!
ArrayList<line> All_draw;
float scale=1;
float translateX = 0.0;
float translateY = 0.0;
void setup(){
// fullScreen();
size(1500,800);
background(51);
All_draw = new ArrayList<line>();
}
void draw(){
background(51);
pushMatrix();
translate(translateX,translateY);
stroke(255);
if (mousePressed == true && scale>=0 && mouseButton!=39) {
All_draw.add(new line((-translateX+mouseX)/scale, (-translateY+mouseY)/scale, (-translateX+pmouseX)/scale, (-translateY+pmouseY)/scale));}
scale (scale);
for (line l : All_draw){
l.display();}
popMatrix();
}
void mouseDragged(MouseEvent e) {
if (e.getButton()==39){
translateX += mouseX - pmouseX;
translateY += mouseY - pmouseY;}
}
void mouseWheel(MouseEvent e) {
float p = -e.getCount()/10.0;
float delta = pow(2, p);
translateX -= mouseX;
translateY -= mouseY;
scale *= delta;
translateX *= delta;
translateY *= delta;
translateX += mouseX;
translateY += mouseY;
}
class line {
float x1,y1,x2,y2;
line (float x1_temp,float y1_temp,float x2_temp,float y2_temp){
x1=x1_temp;
y1=y1_temp;
x2=x2_temp;
y2=y2_temp;
}
void display(){
line(x1, y1, x2, y2);}
}