We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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! [-XThere are other inconsistencies in your sketch too: :-?
canvas.beginDraw();
&canvas.endDraw();
outside itsif ()
block?noSmooth()
,stroke(1)
&strokeWeight(0)
, it's a total waste of CPU repeating those within draw()! Define those PGraphics's settings within setup() instead!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.
Didn't work, but adding the line function inside the push and popMatrix like this works.
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!