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.
Page Index Toggle Pages: 1
cut a hole (Read 276 times)
cut a hole
Nov 4th, 2008, 12:24am
 
hello

i have got a ellipse and a image. via mouseclick i wan to cut a round hole in he image.
but i don´t know to put into practice

PGraphics pg;  
PImage img;
void setup() {  
 size(300,300);  
 pg = createGraphics(300, 300, JAVA2D);  
 img = loadImage("s.jpg");
}  

void draw(){  
 pg.background(0);
 pg.beginDraw();  
 pg.fill(120,0,0);  
 pg.ellipse(mouseX, mouseY, 50,50);  
 pg.modified = true;  
 pg.endDraw();
image(img, 300, 300);
 image(pg,0,0);
if (mousePressed == true) {
   //cut a hole?
}

}  
Re: cut a hole
Reply #1 - Nov 4th, 2008, 12:44am
 
The trick is to draw the image only in the places that are not over the "hole"

Copy is your man:
http://processing.org/reference/copy_.html

Basically just draw the image normally when the mouse isn't pressed, but don't draw the entire image when it is, and you can specify the source rects from the image to copy from.

Well, at least that's one way to do it.

Another would be using a mask or something if you need it to be a non-rectangular hole.
Re: cut a hole
Reply #2 - Nov 4th, 2008, 12:51am
 
but with copy() i can just copy a rectangle and not a ellipse
Re: cut a hole
Reply #3 - Nov 4th, 2008, 1:40am
 
Well, then you can go the mask route...

Just create a mask image that's the same size as your image, and set the pixels you want to be visible to 255, and the pixels you want to be transparent to 0. You can use that same PGraphics to render the ellipse to the mask image.

http://processing.org/reference/PImage_mask_.html



Page Index Toggle Pages: 1