Masking one PGraphic with another PGraphic, possible?
in
Programming Questions
•
5 months ago
Hi
I've read a couple of forum posts that come close to this topic but no explicit examples. I'm trying to mask the contents of one PGraphic with a mask image generated dynamically in another. I've put together some simple test code below. I'd expect to see a red circle moving over a black background but I just get a sea of red. Enabling/Disabling the commented lines gets a bit closer inasmuch as you can see some black fringing around a red circle on a red background, so something is going on!
Any pointers as to why this isn't working or how to achieve the desired effect?
Thanks in advance
Matt
I've read a couple of forum posts that come close to this topic but no explicit examples. I'm trying to mask the contents of one PGraphic with a mask image generated dynamically in another. I've put together some simple test code below. I'd expect to see a red circle moving over a black background but I just get a sea of red. Enabling/Disabling the commented lines gets a bit closer inasmuch as you can see some black fringing around a red circle on a red background, so something is going on!
Any pointers as to why this isn't working or how to achieve the desired effect?
Thanks in advance
Matt
- PGraphics maskGraphic;
PGraphics displayGraphic;
void setup() {
size(1000, 600, P2D);
maskGraphic = createGraphics(1000, 600, P2D);
displayGraphic = createGraphics(1000, 600, P2D);
}
void draw() {
background(0);
maskGraphic.beginDraw();
maskGraphic.background(0);
maskGraphic.noStroke();
maskGraphic.fill(255);
maskGraphic.ellipse(mouseX, mouseY, 300, 300);
maskGraphic.endDraw();
displayGraphic.beginDraw();
displayGraphic.background(255, 0, 0);
//displayGraphic.mask(maskGraphic); //implementing the mask line here reveals the mask shape but doesn't exclude the background red
displayGraphic.endDraw();
displayGraphic.mask(maskGraphic); //I'd expect the line here to work but it doesn't mask anything
image(displayGraphic, 0, 0);
}
1