Skip to content

Instantly share code, notes, and snippets.

@oshoham
Last active October 4, 2017 00:51
Show Gist options
  • Save oshoham/5148141d112ac1069c9ec6b4fc8d476c to your computer and use it in GitHub Desktop.
Save oshoham/5148141d112ac1069c9ec6b4fc8d476c to your computer and use it in GitHub Desktop.
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