inst
YaBB Newbies
Offline
Posts: 13
Re: how to zoom in/out and move on a map
Reply #2 - May 13th , 2008, 9:04am
Hi, thanks to sw01.- I did find 2 ways to move the map and to zomm in/out.- Version 1 - Here´s my code: PImage a; // Declare variable "a" of type PImage int updown = 0; int leftright = 0; int zoom = 1; void setup(){ size(600,600); smooth(); frameRate(10); a = loadImage("kartec.jpg"); noStroke(); } void draw () { scale(zoom); translate (leftright,updown); image(a,0,0); } // Tastkommandos erkennen void keyPressed() { if (key == CODED) { if (keyCode == UP) { updown = updown-30; } else if (keyCode == DOWN) { updown = updown+30; } else if (keyCode == LEFT) { leftright = leftright-30; } else if (keyCode == RIGHT) { leftright = leftright+30; } } if(key == '+') { zoom = 2; println(zoom); } else if (key == '-') { zoom = 1; println(zoom); } } Version 2 - Matt Patey: Well this one is OK - but the quiet big map-image is diplayed bad, if I push the buttons to move the image quickly.- Now I found this solution from Matt Patey. It works fine, but the movement of the image is very slow.- Is there a possibility to change this? Or is this a problem of the hardware memory speed? Thanks for any help! /** * PanDemo.pde * @author Matt Patey */ PImage imgBorealis; PImage bufSlice; PGraphics buf; int copyOffsetX; int copyOffsetY; int copyWidth; int copyHeight; int zoom = 1; void setup() { size(600, 600, P3D); // Load our image. imgBorealis = loadImage("kartec.jpg"); // Create an off-screen buffer that will contain the entire image. buf = createGraphics(imgBorealis.width, imgBorealis.height, P3D); buf.beginDraw(); buf.image(imgBorealis, 0, 0); buf.endDraw(); copyOffsetX = 0; copyOffsetY = 0; copyWidth = width; copyHeight = height; } void draw() { background(0); scale(zoom); image(getBufSlice(), 0, 0); } /** * Updates the copied version of the off-screen buffer. */ PImage getBufSlice() { return buf.get(copyOffsetX, copyOffsetY, copyWidth, copyHeight); } /** * Handle key presses. */ void keyPressed() { switch(keyCode) { case LEFT: if(copyOffsetX < buf.width - width) { copyOffsetX++; } break; case RIGHT: if(copyOffsetX > 0) { copyOffsetX--; } break; case UP: if(copyOffsetY < buf.height - height) { copyOffsetY++; } break; case DOWN: if(copyOffsetY > 0) { copyOffsetY--; } break; } }