Scaling/flipping PGraphics

G_DG_D
edited May 2016 in Questions about Code

I am trying to get two copies of a PGraphics - one displayed normally, the other flipped. I then want to convert my PGraphics to a PImage. I've got the following, but the second image isn't flipped...

  PGraphics g;
  g = createGraphics(width, height, P3D);
  g.beginDraw();
  g.image(srcImg, 0, 0);
  g.endDraw();
  PImage output = g.get();

  PGraphics g2; 
  g2 = createGraphics(width, height, P3D);
  g2.beginDraw();
  g2.image(srcImg, 0, 0);
  g2.scale(-1,1);
  g2.endDraw();
  PImage output2 = g2.get();

Answers

  • edited May 2016

    AFaIK scale(), just like fill() and textSize(), isn't retroactive.
    That is, it doesn't affect previous drawings: /:)
    https://Processing.org/reference/scale_.html

  • Well, I am sure I did have .scale() working at some point to flip PGraphics. Just can't remember how I made it work.

  • Answer ✓

    You probably set the scale before you drew your image. You might also need to translate(width,0); in there. You would want to do that translate first. Then change the scale. Then draw the image.

  • Thanks TfGuy44. Works like a charm. I ended up with:

      PGraphics g;
      g = createGraphics(width, height, P3D);
      g.beginDraw();
      g.pushMatrix();
      g.scale(1, -1);
      g.translate(0, -g.height);
      g.image(img, 0, 0);
      g.popMatrix();
      g.endDraw();
      PImage output = g.get();
    
Sign In or Register to comment.