We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Graphic as an image mask
Page Index Toggle Pages: 1
Graphic as an image mask (Read 976 times)
Graphic as an image mask
Jan 14th, 2009, 12:49pm
 
Is this possible? im trying to use an ellipse as a mask for a PImage. Sounds easy but the mask() function requires an image as a mask


thanks

fed
Re: Graphic as an image mask
Reply #1 - Jan 14th, 2009, 2:14pm
 
A PGraphics is a PImage too.
Example: Code:
PImage niceImage;
PImage maskImage;
PGraphics graphicalMask;
int iw, ih;
int dw, dh;

void setup()
{
 size(500, 500);
 niceImage = loadImage("SomeNiceImage.png");
 iw = niceImage.width;
 ih = niceImage.height;
 dw = width - iw;
 dh = height - ih;
 graphicalMask = createGraphics(iw, ih, JAVA2D);
}

void draw()
{
 background(200);

 graphicalMask.beginDraw();
 // Erase graphics
 graphicalMask.background(0);
 // Draw the mask
 int x = mouseX - dw/2;
 int y = mouseY - dh/2;
 graphicalMask.fill(255);
 graphicalMask.noStroke();
 graphicalMask.ellipse(x, y, 100, 50);
 graphicalMask.stroke(128);
 graphicalMask.strokeWeight(5);
 graphicalMask.line(0, y, iw, y);
 graphicalMask.line(x, 0, x, ih);
 graphicalMask.endDraw();

 // Copy the original image (kept as reference)
 maskImage = niceImage.get();
 // Apply the mask
 maskImage.mask(graphicalMask);
 // Display the result
 image(maskImage, dw/2, dh/2);
}
Page Index Toggle Pages: 1