Zoom in and out.

Hello, I am a total processing/programming newbie. I tried out this code for zooming in and out, but nothing happens. It is an exact code i downloaded from a tutorial. Can anyone try it on their processing to see if it works? can anyone tell me a good way to zoom in/out?

int d = 40;

float xo;
float yo;
float zoom = 1;
float angle = 0;

void setup () {
  size (600, 200);
  xo = width/2;
  yo = height/2;
  smooth();
  noStroke();
}

void draw() {
  background(#1F7B9B);
  translate (xo, yo);
  scale (zoom);
  rotate (angle);

  fill(120);
  ellipse(-200, 0, d, d);
  fill(180);
  ellipse(-100, 0, d, d);
  fill(220);
  ellipse (0, 0, d, d);
  fill (180);
  ellipse (100,0,d,d);
  fill (120);
  ellipse (200,0,d,d);
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      zoom += .03;
    }
    else if (keyCode == DOWN) {
      zoom -= .03;
    }
    else if (keyCode == RIGHT) {
      angle += .03;
    }
    else if (keyCode == LEFT) {
      angle -= .03;
    }
  }
  if (key == 32); 
  {
    angle = 0;
    zoom = 1;
    xo = width/2;
    yo = height/2;
  }
}

void mouseDragged(){
  xo= xo + (mouseX - pmouseX);
  yo = yo + (mouseY - pmouseY);
}

Answers

  • Answer ✓

    Remove the semicolon on line 49.

    This was indeed quite puzzling at first, but there is a simple error in the code. On line 49, there is a semicolon:

    if (key == 32);
    

    ...so the code that follows it (which resets the variables that would be changed) is executed as a code block rather than as part of the if statement. Removing the semicolon causes the code block to be executed only if the condition is true... and fix the problem.

  • edited December 2013

    too late, calsign beat me to it 8)

  • the trick to finding this is to add println() statements to check that the bits you think are executing are executing and, more importantly in this case, that the bits you aren't expecting to be executed aren't executed.

  • Thank you very much, calsign, removing the semicolon indeed fixed it to complete satisfaction.

Sign In or Register to comment.