Please upgrade your browser to something newer, like Google Chrome

PImage img;
PGraphics pg;
boolean loaded = false;

void setup(){
	size(600,320);
	background(255);
	img = requestImage("data/trame.jpg");
}

void initMask(){
	println("img loded");
	println("img.width: "+img.width+"  img.height: "+img.height);
	pg = createGraphics(img.width, img.height, JAVA2D);
	println("pg.width: "+pg.width+"  pg.height: "+pg.height);
}

void draw(){
	background(150);
	if(img.width>1){
		if(!loaded){
			initMask();
			loaded=true;
		}

		updatePg();

		img.mask(pg); // apply PGraphics as PImage.mask()
		image(img,0,0);

		image(pg,300,0);

		fill(0);
		text("PImage with PGraphics mask", 5, 315);
		text("PGraphics", 305, 315);
	}
}

void updatePg() {
	pg.beginDraw();
	pg.background(0);
	pg.noStroke();
	pg.fill(255);
	pg.ellipse(pg.width/2, pg.height/2, 200+cos(radians(frameCount))*100, 200+cos(radians(frameCount))*100);
	pg.endDraw();
}