Skip to content

Instantly share code, notes, and snippets.

@hysysk
Created August 19, 2014 20:10
Show Gist options
  • Save hysysk/10b91b7b5522b03a197d to your computer and use it in GitHub Desktop.
Save hysysk/10b91b7b5522b03a197d to your computer and use it in GitHub Desktop.
Zoom in and out on mouse position. You need to put a image file in the data folder.
PImage img;
float scaleValue;
float diff;
float SCALE_VALUE_MIN = 1.0;
float SCALE_VALUE_MAX = 3.0;
float EASE_VALUE = 0.2;
void setup() {
img = loadImage("image.jpg");
size(img.width, img.height);
scaleValue = SCALE_VALUE_MIN;
}
void draw() {
background(255);
if(mousePressed) {
diff = SCALE_VALUE_MAX - scaleValue;
scaleValue += diff * EASE_VALUE;
} else {
diff = scaleValue - SCALE_VALUE_MIN;
scaleValue -= diff * EASE_VALUE;
}
if(scaleValue <= SCALE_VALUE_MIN) {
scaleValue = SCALE_VALUE_MIN;
} else if(scaleValue >= SCALE_VALUE_MAX) {
scaleValue = SCALE_VALUE_MAX;
}
pushMatrix();
translate(mouseX, mouseY);
scale(scaleValue);
translate(-mouseX, -mouseY);
image(img, 0, 0);
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment