Skip to content

Instantly share code, notes, and snippets.

@oshoham oshoham/maskTest.pde
Last active Oct 4, 2017

Embed
What would you like to do?
maskTest
PGraphics img1;
PGraphics img2;
void setup() {
size(400, 400);
background(255);
img1 = createGraphics(400, 400);
img2 = createGraphics(400, 400);
// draw the circle that will be masked
img1.beginDraw();
img1.clear();
img1.noStroke();
img1.fill(0);
img1.ellipse(200, 200, 100, 100);
img1.endDraw();
// draw the mask
img2.beginDraw();
img2.background(0);
for (int x = 0; x < width; x += 10) {
img2.stroke(255);
img2.line(x, 0, x, height);
}
img2.endDraw();
alternateMask(img1, img2);
image(img1, 0, 0);
}
void alternateMask(PImage img1, PImage img2) {
img1.loadPixels();
img2.loadPixels();
for (int i = 0; i < img2.pixels.length; i++) {
float a1 = alpha(img1.pixels[i]);
if (a1 != 0) {
float a2 = brightness(img2.pixels[i]);
float r = red(img1.pixels[i]);
float g = green(img1.pixels[i]);
float b = blue(img1.pixels[i]);
img1.pixels[i] = color(r, g, b, a2);
}
}
img1.updatePixels();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.