Last active
October 27, 2023 20:32
-
-
Save companje/727074098eb256b0765f20c288f55477 to your computer and use it in GitHub Desktop.
Multiple Zoomable Windows in Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Author
companje
commented
Oct 27, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment