Using PGraphics to create a mask

So, I want to create a PGraphics object with some transparent regions and everything else black, but I can only find the regions that are transparent. Is there any way for me to do something like:

Mask.createGraphics(width,height)
Mask.background(0)
Mask.fill(0,0,0,0)
Mask.ellipse(x,y,2r,2r)

Such that the ellipse replaces all the pixels in it’s region instead of being drawn on top of the black background? Or would I have to go through each pixel and change their colors individually?

Is this helpful? (sorry – just realised this is supposed to be Processing.py)

PGraphics main, mask;

void setup() {
  size(600, 600, P2D);
  main = createGraphics(width, height, P2D);
  mask = createGraphics(width, height, P2D);
}

void draw() {
  background(0);
  updateMask();
  updateMain();
  // apply mask
  main.mask(mask);
  image(main, 0, 0);
}

void updateMain() {
  main.beginDraw();
  main.background(128, 0, 0);
  main.stroke(255);
  main.strokeWeight(50);
  main.strokeCap(SQUARE);
  float y = noise(frameCount*0.01)*height;
  main.line(0, y, width, y);
  main.endDraw();
}

void updateMask() {
  mask.beginDraw();
  mask.background(0);
  mask.fill(255);
  float s = noise(frameCount*0.013);
  mask.ellipse(mask.width/2, mask.height/2, mask.width*s, mask.height*s);
  mask.endDraw();
}

if either main or mask is a black screen with a hole in it it might help

and I need it in sort of a generalized form, since I won’t be using only ellipse in my mask, that was just an example

Not sure were you are stuck at. The previous example provides a good demonstration of using mask on PGraphics. You are referring to a generalized form which it is not clear from the information you have provided so far.

It would be:

void updateMask() {
  mask.beginDraw();
  mask.background(0);
  mask.fill(255);
  float s = 0.2; // 20% of the dimension
  mask.ellipse(mask.width/2, mask.height/2, mask.width*s, mask.height*s);
  mask.endDraw();
}

Kf

I can’t find the .mask() function in the processing.py reference, how does it work?

I meant that it’s supposed to work with any shape drawn, not only ellipse()