Erase part of PGraphics image to transparent

Any help cutting/cropping out part of a PGraphics image, leaving behind a transparent rectangle?

I tried the code below, but it just adds a transparent rectangle on top (no visual change to the underlying image). I did change the fill to (255, 100) to verify the coordinates and dimensions, it added a light white semi-transparent rectangle on top of what’s (still visible) underneath.

img.beginDraw();
img.noStroke();
img.fill(255, 0);
img.rect(100, 100, 200, 200);
img.endDraw();

Thanks for any help!

1 Like

You could probably use the set() function or one of the pixel-related functions in the reference.

If you’re still stuck, can you please post a more complete example that we can copy and paste to run ourselves?

Thanks for the help Kevin. Sorry to leave out the constructor and display:

size(500, 500);
PGraphics img = createGraphics(500, 500);
img.beginDraw();
img.noStroke();
img.fill(255, 0);
img.rect(100, 100, 200, 200);
img.endDraw();
image(img, 0, 0);

I am having some luck writing to the pixels directly:
java.util.Arrays.fill(img.pixels, 1000, 100000, color(255, 0));

Thanks again!

You could also play with the blendMode() function. I think blendMode(REPLACE) might be what you’re looking for, but I haven’t tried it out. More info can be found in the reference.

3 Likes

I’ve actually used blendMode(REPLACE) before in a (failed) project I worked on a while back.
link to the line i used it on with a bit of context on the surrounding lines

setting blendMode(REPLACE); and then setting your stroke/fill color to transparent color(0,0,0,0); will let you draw transparency with lines, rects, anything you want really.

It works well. Just don’t forget to send your blendMode back to the default blendMode(BLEND); when you’re done.

Love the link to the reference, Kevin!

1 Like