I may be dumb, but... how come this pgraphics image is cut off?

edited August 2018 in Questions about Code
PGraphics p;
void setup() {
  size(600, 600);
  p = createGraphics(600, 600);
  p.beginDraw();
  p.rectMode(CENTER);
  translate(width/2, height/2);
  p.rect(0, 0, 50, 50);
  p.endDraw();
  image(p, 0, 0);
}

I have just begun and I'm already stuck :/

Answers

  • If you remove the translate call, would it be cut off?

    Kf

  • That's what has been creating it, yes. I'm just wondering why is it not rendering anything in any other quadrant, past the origin.

  • Answer ✓

    Instead of translate maybe p.translate...?

  • Oh thanks, I didn't know that was an option. For some reason, I thought translating functions such as translate, rotate and push and pop affect all PGraphics, along with the main sketch, ergo that they don't require the dot syntax at the beginning.

  • p. is required before translate otherwise it applies only to line 10

    Solved now...?

  • translate() moves the canvas origin to 300, 300. It is reset when draw exits. Now image(p, -300, -300) aligns the image to the top corner of the canvas.

    p.translate(300, 300) moves the drawing origin of the PGraphics or PImage. No matter where you choose to draw it with image(), all its content is offset by that amount. Now p.rect(-300,-300, 600, 600) aligns a rect to the top corner of the image.

    So, you are either moving the piece of paper on the table, or moving where you start drawing on that piece of paper.

Sign In or Register to comment.