zoom centered on mouse coordinates, in conjunction with zoom on the center of the screen
in
Programming Questions
•
1 year ago
Hey all,
I'm trying to get write a sketch which will allow me to combine zooming in and out on the center of the screen with "minus" and "equals" with zooming in on the mouse coordinates of the screen with a double click. I have two problems.
1) Zooming in on the center of the screen works fine, but when it's combined with zooming in on the mouse coordinates it doesn't work.
2) Zooming in on the mouse coordinates only _sort of_ works if not used in conjunction with the center of screen zoom. By sort of I mean if I choose one spot on the screen to zoom towards and keep zooming on that spot, it works. However, if I zoom on one spot and then on another the second zoom will not zoom towards the second spot I've chosen. If I leave the third zoom in the same spot as the second zoom, the third zoom _will_ zoom in on the correct spot. So it's the change from one zoom location to another that's causing problems.
By the way, with mouse zoom, I don't want to move the target of the zoom to center of the screen--I want the target of the zoom to have the same coordinates on the screen as it did before the zoom. When it works it's currently doing this, and this is one thing I don't want to change.
Thanks for the help!
~~~~~
Code:
- //a test sketch to get zoom to work
- //scale values for mouse and keys, translate values
- float scmouse=1;
- float sckeys=1;
- float Xmouse=width/2;
- float Ymouse=height/2;
- float tx = 0, ty = 0;
- void setup() {
- size(500, 500);
- }
- void draw() {
- background(0);
- if (keyPressed) {
- //= and - to zoom in and out on center of screen
- if (key == '-') sckeys *= 1/1.05;
- if (key == '=') sckeys *= 1.05;
- //r to reset all zooms and translates
- if (key == 'r') {
- sckeys=1;
- scmouse=1;
- tx=0;
- ty=0;
- }
- }
- /*this code works it not used in conjunction with the mouse zoom
- it zooms in and out on the center of the screen
- */
- translate(width/2, height/2);
- scale(sckeys);
- translate(-width/2, -height/2);
- /*Xmouse and Ymouse are just mouseX and mouseY constrained to the screen.
- This code _sort of_ works if not used in conjunction with the key zoom
- written right above. By sort of I mean if I choose one spot on the screen
- to zoom towards and keep zooming on that spot, it works. However, if I zoom
- on one spot and then on another the second zoom will not zoom towards
- the second spot I've chosen. If I leave the third zoom in the same spot
- as the second zoom, the third zoom _will_ zoom in on the correct spot.
- So it's the change from one zoom location to another that's causing problems.
- */
- translate(Xmouse, Ymouse);
- scale(scmouse);
- translate(-Xmouse, -Ymouse);
- //mouseDragged translation
- translate(tx, ty);
- //some test rectangles
- fill(255);
- rect(225, 225, 50, 50);
- fill(255, 0, 0);
- rect(300, 300, 20, 20);
- fill(255, 255, 0);
- rect(50, 50, 10, 10);
- }
- //getting mouse coordinates for debugging just type c
- void keyReleased() {
- if (key == 'c') {
- println();
- println("X = " + mouseX);
- println("Y = " + mouseY);
- }
- }
- //every time mouse is double clicked, the variable scmouse, the mouse scale,
- //is multipled by 2
- void mousePressed() {
- // mouseEvent variable contains the current event information
- if (mouseEvent.getClickCount()==2) {
- Xmouse=constrain(mouseX, 0, width);
- Ymouse=constrain(mouseY, 0, height);
- scmouse*=2;
- println("<double click>");
- }
- //if (mouseEvent.getButton()==MouseEvent.BUTTON3) println("<right button>");
- // this prints out the event in descriptive form
- }
- void mouseDragged() {
- tx += (mouseX - pmouseX) / (sckeys * scmouse);
- ty += (mouseY - pmouseY) / (sckeys * scmouse);
- }
1