I’ve run into a problem with transparency in the P2D renderer. When rendering an image in PG, the transparent pixels somehow blend into the background. I know I can use blendMode(REPLACE), but then the transparent pixels replace all the non-transparent pixels. Is there a way to render images with transparency so that they don’t blend into the background?
Here’s an example of the code:
PGraphics canvas;
PGraphics image;
void settings(){
size(720,720,P2D);
}
void setup(){
canvas = createGraphics(720,720,P2D);
image = createGraphics(360,360);
image.beginDraw();
image.noStroke();
image.fill(0,128);
image.rect(0,0,360,360);
image.fill(0);
image.rect(90,90,180,180);
image.endDraw();
canvas.beginDraw();
canvas.background(255,0,0,0); // red but transparent
canvas.image(image,180,180);
canvas.endDraw();
}
void draw(){
background(255);
image(canvas,0,0);
}
As you can see transparent pixels blends with red. I’ve set a red background just for clarity; by default, the background colour is set to (0,0,0,0), so the image blends into it.