We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Here's my load image program so far. It loads images, resizes the window to either the image size or 90% of the display size (whichever is less), allows you to zoom in on the image or zoom out and allows you to drag images that are larger than the window. My problem is I would like the zoom to zoom in on the center of the image in the window. I want the current center pixel in the window to be the center pixel after the zoom. I can't figure out how to do it.
Here's my code so far:
/* L or l loads a new image
Z zooms in on the image
z zoom out
X or X zooms to original size
Drag mouse to drag around zoomed image
*/
PImage img = null;
boolean imageSelected = false;
float zoom = 1.0;
float cX = 0; //corner placement
float cY = 0;
float dX = 0.0; //dragged offset
float dY = 0.0;
int oX = 0; // mouse offset
int oY = 0;
boolean locked = false;
void setup() {
frame.setResizable(true);
size(600, 600);
selectInput("Select a file to process:", "fileSelected");
}
void draw() {
background(100);
if (!imageSelected)
;
else{
cornerOffset(); //process the location of the corner x,y
image(img, cX, cY, img.width * zoom, img.height * zoom);
}
}
void fileSelected(File selection) {
if (selection == null)
println("Window was closed or the user hit cancel.");
else {
img = loadImage( selection.getAbsolutePath());
imageSelected = true;
int h = img.height; //process the window size
if(h > (int)((float) displayHeight * 0.9))
h = (int)((float)displayHeight * 0.9);
int w = img.width;
if(w > (int)((float)displayWidth * 0.9))
w = (int)((float)displayWidth * 0.9);
frame.setSize(w,h);
}
}
void keyPressed() {
if ((key == 'L') || (key == 'l')) {
selectInput("Select a file to process:", "fileSelected");
}
else if (key =='Z'){
if (zoom < 4.0){
zoom = zoom+0.2;
//dX = -(dX/2)-(img.width * zoom - width)/2; // here is my problem
//how do I make sure that durning the zoom, the current cent of the display remains the center?
//dY = dY-((img.height * zoom) - img.height)/2;
}
}
else if (key == 'z'){
if(zoom > 0.2){
zoom = zoom - 0.2;
if(dX < width - img.width *zoom)
dX = width - img.width * zoom ;
else if (dX >0)
dX = 0;
if(dY < height - img.height * zoom)
dY = height - img.height * zoom;
else if (dY >0)
dY = 0;
}
}
else if(key == 'X'||key =='x'){
zoom = 1.0;
dX =0;
dY = 0;
}
}
void cornerOffset(){
if(img.width*zoom < width){ //center smaller image in window
cX = ((width - img.width*zoom) / 2);
dX = 0;
}
else cX = dX;
if(img.height * zoom < height){ // center smaller image in window
cY = ((height - img.height * zoom)/2);
dY= 0;
}
else cY = dY;
}
void mousePressed() {
locked = true;
oX= mouseX;
oY=mouseY;
}
void mouseReleased() {
locked = false;
}
void mouseDragged() {
if(locked) {
dX += mouseX-oX;
dY += mouseY-oY;
if(dX < width - img.width *zoom)
dX = width - img.width * zoom ; // constrain so image isn't pulled past its dimensions
else if (dX >0)
dX = 0;
if(dY < height - img.height * zoom)
dY = height - img.height * zoom;
else if (dY >0)
dY = 0;
oX = mouseX;
oY = mouseY;
}
}
Answers
I figured out the zoom thing ( I think). The program has gotten a lot larger and is more functional. I have a question. I have this written now to use a pen tablet. It wont draw with the mouse. If you want to try it out and you don't have a pen tablet of the tablet library, you're going to have to edit the code in the mouse functions to not use pen pressure. Is there a way I could fix this code to except input from either a mouse or a pen? Here's the program so far. Parts are still hard coded.
Part 1:
Part 2:
Part 3: