Stroke opacity by temporary PGraphics
in
Programming Questions
•
2 years ago
Hi,
I'm trying to do opacity like in photoshop, where the stroke looks like homogenous object (individual cricles should nod be seen ). To do this I first render the whole stroke to PGraphics temp, and at the end of stroke I print it to Layer of image.
However, the it looks to be working only if I allocate new temp for each stroke. If I use "temp.background(0,0)" to clear and recycle temp, than it draws just the first storke, again and again.
It looks like Layer1.image(temp,0,0) does not know that temp has changed. Which is strange, bacause "image(temp,0,0)" on the applet directly paints correct (updated) temp.
Sure I can use createGraphics and let garbage collector do all the work, but this is not very nice solution.
- int sx = 500;
int sy = 500;
float opacity = 100;
PGraphics Layer1,temp;
//boolean strokeEnd = false;
boolean wasPressed = false;
void setup() {
size(sx, sy);
noFill();
smooth();
// frameRate(10);
Layer1 = createGraphics(sx, sy, JAVA2D);
Layer1.beginDraw();
Layer1.smooth();
Layer1.background(0,0);
Layer1.tint (255,255,255,opacity);
Layer1.endDraw();
temp = createGraphics(sx, sy, JAVA2D);
temp.beginDraw();
temp.fill(255, 0, 0, 255);
temp.noStroke();
temp.smooth();
temp.endDraw();
}
void draw() {
// println ("mousePressed:"+ mousePressed+" wasPressed: "+ wasPressed);
if (mousePressed){
wasPressed = true;
temp.beginDraw(); temp.ellipse(mouseX, mouseY, 50, 50); temp.endDraw();
}else{ if(wasPressed){
// println( " ======== EndOffStroke");
wasPressed = false;
Layer1.beginDraw(); Layer1.image(temp, 0, 0); Layer1.endDraw();
temp.beginDraw(); temp.background(0,0); temp.endDraw(); // this does not work - /*
- // this works fine
temp = createGraphics(sx, sy, JAVA2D);
temp.beginDraw();
temp.fill(255, 0, 0, 255);
temp.noStroke();
temp.smooth();
temp.endDraw(); - */
}
}
background(200);
noTint();
image(Layer1, 0, 0);
tint (255,255,255,opacity);
image(temp, 0, 0);
ellipse(mouseX, mouseY, 50, 50);
}
1