We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello Processing community,
I am a little new with this whole thing, so bear with me:
Basically, for a project of mine, I wish to create a looking glass. During the scenes in this project (its an interactive story of sorts) two gifs will be running simutaneously, one on top of the other. The gifs are run using the GifAnimation library. The idea is that the user can move the looking glass over some area of the gif on top, and, in the area of the looking glass, the user will be able to see the gif underneath; within the area of the looking glass, the gif on top becomes transparent. The two gifs will be fairly similar, however the hidden one will have small, subtle changes that reveal additional information about the story.
What I have so far:
Firstly, the LookingGlass class which is called by draw() is defined like so. lg is defined as a global variable, LookingGlass lg in the main file.
`class LookingGlass {
float radius = 50;
// This is a method which determines which pixels on the screen are inside of the looking glass, and which ones are not.
void makeTransparent() {
verticies[0] = new Vect2(mouseX-(lg.radius)/2, mouseY);
verticies[1] = new Vect2(mouseX-(lg.radius)/3, mouseY-(lg.radius)/3);
verticies[2] = new Vect2(mouseX, mouseY-(lg.radius)/2);
verticies[3] = new Vect2(mouseX+(lg.radius)/3, mouseY-(lg.radius)/3);
verticies[4] = new Vect2(mouseX+(lg.radius)/2, mouseY);
verticies[5] = new Vect2(mouseX+(lg.radius)/3, mouseY+(lg.radius)/3);
verticies[6] = new Vect2(mouseX, mouseY+(lg.radius)/2);
verticies[7] = new Vect2(mouseX-(lg.radius)/3, mouseY+(lg.radius)/3);
// I have put various things here which have all failed, as described below
}
}
// Displays the looking glass, and has it gain transparency
void display() {
fill(255, 20);
ellipse(mouseX, mouseY, radius, radius);
}
}`
Originally, I though that you could use pixels[] to make each the pixels with the above listed verticies (which roughly encompass the same area as the circle) transparent, by using the point2line library's method insidePolygon, which takes a list of Vect2 (the polygon's verticies) and a Vect2 (the point which you wish to test whether it is inside the bounds defined by those verticies). Nevertheless, this is TERRIBLY inefficient - my window size is 690x388, so each time the looking glass moves, it has to recalcuate which of the 267720 pixels on the screen are inside the looking glass and which ones are not.
I don't know if I could use PGraphics to somehow resolve this issue - I don't know enough about Processing in general.
Regardless, any help would be appreciated.
EDIT: Formatting
Answers
It seems like you want to use Pimage.mask() and implement a mouse-controlled mask.
Leave your image layers A and B alone and only reapply a precalculated mask image layer each draw. You can simply draw the mask -- for example, draw an ellipse, or a filled polygon -- then apply the mask to image A or image B, depending on if you want a cutout or a reverse. To be clear: render the mask layer pixels once, in setup -- then only apply it, never recalculate it.
Note that if performance is still an issue, masking a small, cropped "under" image that is actually drawn on top of your base layer might be a more performance efficient way of achieving your effect -- but what will work for you in the end depends on your images and how you are handling transparency and compositing effects.
Thank you jeremydouglass, this was exactly what I was looking for.
If I could just annoy you with a further question, specifically applying (showing) the mask in a given area. Given the code:
Where foregroundImage and backgroundImage are just some PImages sized 1920x1200 and maskImage is a 1920x1200 image which is completely transparent.
I'm not certain where to go from here: if I uncomment foregroundImage.mask(maskImage); the foregroundImage effecively disappears, however I only want a circular section of the foreground image to disappear when the pg graphics object (the ellipse) moves over it.
@VD_D you are very close.
First study this example from PhiLo in 2009, which is a working example of the effect you want. It re-renders the graphicalMask mask every draw loop before using it to create the maskImage for display.
https://processing.org/discourse/beta/num_1231933751.html
Then, consider this working example, which is based on your work and his:
Now, how can you create a LookingGlass class and call it based on this working example?
image(lg.display(),0,0);
etc.