Pgraphic Scale On Event
in
Programming Questions
•
2 years ago
Hey Everyone,
I'm having a problem scaling Pgraphics on event. For example, when you press the 'a' key I want the Pgraphic to zoom in; simple enough but I am unable to get the Pgraphic to scale. I am using Matt Patey's example. I think it might be how I order the image and Pgraphic.
See below.
Thanks for any help....
j
- /**
- * Pan Demo
- *
- * Processing sketch illustrates how an off-screen buffer can be
- * used to create a panning tool to view an image larger than the
- * base canvas. Use the arrow keys to scroll to the limits of
- * the displayed image.
- *
- * @author Matt Patey
- */
- PImage bufSlice;
- PGraphics buf;
- int copyOffsetX;
- int copyOffsetY;
- int copyWidth;
- int copyHeight;
- float scaler = 1.0;
- void setup() {
- size(400, 400, P3D);
- // Create an off-screen buffer that will contain the entire image.
- buf = createGraphics(800, 800, JAVA2D);
- buf.beginDraw();
- buf.smooth();
- buf.scale(scaler);
- buf.background(255);
- for (int i = 0; i < buf.width; i+=10) {
- buf.line(i, 0, i + 50, buf.height);
- buf.line(0, i, buf.height, i + 30);
- }
- buf.endDraw();
- copyOffsetX = 0;
- copyOffsetY = 0;
- copyWidth = width;
- copyHeight = height;
- }
- void draw() {
- background(0);
- image(getBufSlice(), 0, 0);
- }
- /**
- * Updates the copied version of the off-screen buffer.
- */
- PImage getBufSlice() {
- buf.scale(scaler);
- return buf.get(copyOffsetX, copyOffsetY, copyWidth, copyHeight);
- }
- /**
- * Handle key presses.
- */
- void keyPressed() {
- if(key == 'a'){
- scaler += 0.1;
- }
- 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;
- }
- }
1