Skip to content

Instantly share code, notes, and snippets.

@companje
Last active October 27, 2023 20:32
Show Gist options
  • Save companje/727074098eb256b0765f20c288f55477 to your computer and use it in GitHub Desktop.
Save companje/727074098eb256b0765f20c288f55477 to your computer and use it in GitHub Desktop.
Multiple Zoomable Windows in Processing
ZoomWindow win1, win2;
void setup() {
size(100, 100);
win1 = new ZoomWindow(dataPath("saenredam.jpg"), 0, 0, displayWidth/2, displayHeight);
win2 = new ZoomWindow(dataPath("zadelstraat.jpg"), displayWidth/2, 0, displayWidth/2, displayHeight);
}
void draw() {
}
public class ZoomWindow extends PApplet {
String filename;
int winX, winY, winW, winH;
PImage img;
float x, y, scale = 1;
float minScale = 0.1f;
float maxScale = 40.0f;
ZoomWindow(String filename, int winX, int winY, int winW, int winH) {
super();
runSketch(new String[]{this.getClass().getName()}, this);
this.filename = filename;
this.winX = winX;
this.winY = winY;
this.winW = winW;
this.winH = winH;
}
void setup() {
windowTitle(filename);
surface.setLocation(winX, winY);
surface.setSize(winW, winH);
img = loadImage(filename);
//center
x = img.width/2 - winW/2;
y = img.height/2 - winH/2;
}
void draw() {
background(0);
scale(scale);
translate(-x, -y);
image(img, 0, 0);
}
void mouseDragged() {
x -= (mouseX-pmouseX)/scale;
y -= (mouseY-pmouseY)/scale;
}
void mouseWheel(MouseEvent event) {
float zoomFactor = -event.getCount()*.01f + 1.0f;
float newScale = constrain(scale * zoomFactor, minScale, maxScale);
x -= (mouseX/newScale - mouseX/scale);
y -= (mouseY/newScale - mouseY/scale);
scale = newScale;
}
}
@companje
Copy link
Author

Screenshot 2023-10-27 at 22 18 51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment