Translate PGraphics makes drawing app unresponsive

edited December 2014 in JavaScript Mode

I have the following code:

PGraphics canvas;

void setup() {
  size(500, 500);
  background(50);
  canvas= createGraphics(300, 300);
  canvas.beginDraw();
  canvas.background(255);
  canvas.endDraw();
}

void draw() {

  canvas.beginDraw();
  if (mousePressed==true && mouseButton==LEFT) {

    canvas.noSmooth();
    pushMatrix();
    canvas.translate(-150, 0);
    popMatrix();
    canvas.stroke(1);
    canvas.strokeWeight(0);
    canvas.line(mouseX, mouseY, pmouseX, pmouseY);
  }
  canvas.endDraw();

  image(canvas, 150, 0);
}

If I take out the canvas.translate(-150, 0); the sketch works fine, except that the mouse is offset, but at least I can draw one the canvas.

What's the correct way of translating a PGraphic in Javascript mode?

Answers

  • As soon as I posted this thread it dawned on me that I could just use mouseX-150 to translate the mouse.

    But still interested in an answer as to why translating a PGraphic won't work, though it's listed in processingjs reference.

  • edited December 2014 Answer ✓

    Any drawing function w/o a reference prefix affects sketch's own canvas only!
    Thus pushMatrix(); & popMatrix(); don't affect your PGraphics or any other! [-X

    There are other inconsistencies in your sketch too: :-?

    • Why canvas.beginDraw(); & canvas.endDraw(); outside its if () block?
    • Since your PGraphics is always noSmooth(), stroke(1) & strokeWeight(0), it's a total waste of CPU repeating those within draw()! Define those PGraphics's settings within setup() instead!
  • edited December 2014

    I tried canvas.pushMatrix() before, but it didn't work either.

    Thanks for the rest of the tips. I think stroke and strokeWeight are left the because it's a sketch fragment, and in the whole sketch the brushsize (strokeWeight) is changed.

  • edited December 2014
    canvas.pushMatrix();
    canvas.translate(-150, 0);
    canvas.popMatrix();
    

    Didn't work, but adding the line function inside the push and popMatrix like this works.

    canvas.pushMatrix();
    canvas.translate(-150, 0);
    canvas.line(mouseX, mouseY, pmouseX, pmouseY);
    canvas.popMatrix();
    

    Just makes sense, but as a beginner in the heat of the moment you are busy with other things. ;) Since the beginDraw() outside of if statement und the redrawing of stroke and strokeWeight, so bear with GoToLoop.

    Always appreciate the help here from the regular posters, it's a tough job with all the brash/repetitive questions und stupid mistakes. Thank you all!

Sign In or Register to comment.