# Painting/revealing a layer

**URL:** <https://discourse.processing.org/t/painting-revealing-a-layer/38000>\
**Category:** Coding Questions\
**Created:** [July 17, 2022, 10:04am UTC](https://discourse.processing.org/t/painting-revealing-a-layer/38000 "2022-07-17T10:04:36Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [July 17, 2022, 11:32am UTC](https://discourse.processing.org/t/painting-revealing-a-layer/38000/2 "2022-07-17T11:32:46Z")

</div>

Hi @bringmeflowers,

better use [the mask function](https://processing.org/reference/PImage_mask_.html) for this … check out…

```auto
PGraphics maskimg;
PImage underlyingimg;

void setup() {
  size(800,800,P2D);
  underlyingimg = loadImage("image.jpg");

  // create mask (should be same as the underlying image size)
  maskimg = createGraphics(underlyingimg.width,underlyingimg.height,P2D);

  // initialize the whole mask with black (everything hidden)
  maskimg.beginDraw();
  maskimg.background(0);
  maskimg.noStroke();
  maskimg.fill(255);
  maskimg.endDraw();
}

void draw() {
  background(0);

  if (mousePressed) {
    // if mousePressed draw the area with white pixels (revealed)
    // just for example using a circle brush (ie ellipse) 
    maskimg.beginDraw();
    maskimg.ellipse(mouseX,mouseY,50,50);
    maskimg.endDraw();     
  }  
  // apply the mask to the underlying image and paint it
  underlyingimg.mask(maskimg);
  image(underlyingimg, 0, 0);
}

```

Cheers  
— mnse

---

_[View the full topic](https://discourse.processing.org/t/painting-revealing-a-layer/38000)._
