# Using PGraphics to create a mask

**URL:** https://discourse.processing.org/t/using-pgraphics-to-create-a-mask/16740
**Category:** Processing.py
**Created:** [December 30, 2019, 8:06pm UTC](https://discourse.processing.org/t/using-pgraphics-to-create-a-mask/16740 "2019-12-30T20:06:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![hinomupi](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@hinomupi](https://discourse.processing.org/u/hinomupi)
#### Post date: [December 30, 2019, 8:06pm UTC](https://discourse.processing.org/t/using-pgraphics-to-create-a-mask/16740/1 "2019-12-30T20:06:33Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

### Author: ![GSA\_IxD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gsa_ixd/32/7554_2.png) [@GSA\_IxD](https://discourse.processing.org/u/GSA_IxD)
#### Post date: [December 30, 2019, 9:44pm UTC](https://discourse.processing.org/t/using-pgraphics-to-create-a-mask/16740/2 "2019-12-30T21:44:47Z")

</div>

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

```auto
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();
}

```

---

<div class="post-metadata">

### Author: ![hinomupi](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@hinomupi](https://discourse.processing.org/u/hinomupi)
#### Post date: [December 30, 2019, 10:17pm UTC](https://discourse.processing.org/t/using-pgraphics-to-create-a-mask/16740/3 "2019-12-30T22:17:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [December 31, 2019, 12:33am UTC](https://discourse.processing.org/t/using-pgraphics-to-create-a-mask/16740/4 "2019-12-31T00:33:08Z")

</div>

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.

> [@hinomupi](#):
>
> if either main or mask is a black screen with a hole in it it might help

It would be:

```auto
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

---

<div class="post-metadata">

### Author: ![hinomupi](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@hinomupi](https://discourse.processing.org/u/hinomupi)
#### Post date: [December 31, 2019, 12:47am UTC](https://discourse.processing.org/t/using-pgraphics-to-create-a-mask/16740/5 "2019-12-31T00:47:39Z")

</div>

> [@GSA\_IxD](#):
>
> main.mask(mask);

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

> [@kfrajer](#):
>
> You are referring to a **generalized form** which it is not clear from the information you have provided so far.

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