[2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?
in
Core Library Questions
•
9 months ago
Kudos to the Processing team for working towards the big 2.0.
I've been testing Processing 2.0b7 for the past few days and have been running into some issues in the combination of PGraphics and transparency. I am unsure if these are due to my own coding errors, changes in Processing, remaining bugs or hardware incompatibilities. So I'm using the forum first to see if filing an issue is necessary.
I am trying to set pixels in a P2D/P3D PGraphics to transparent. The pixels also need to stay transparent when another begin-endDraw is called on the PGraphics. In the example below, setting the color to red (or any color) is possible. However setting it to transparent doesn't work. Instead it's set to grey. Note that the code does work when it's a JAVA2D PGraphics.
Can others reproduce this behavior?
And if so...
How can I set the pixels in a P2D/P3D PGraphics to transparent?
Code Example
![](zoho-static/ze/images/laughing.gif)
I am trying to set pixels in a P2D/P3D PGraphics to transparent. The pixels also need to stay transparent when another begin-endDraw is called on the PGraphics. In the example below, setting the color to red (or any color) is possible. However setting it to transparent doesn't work. Instead it's set to grey. Note that the code does work when it's a JAVA2D PGraphics.
Can others reproduce this behavior?
And if so...
How can I set the pixels in a P2D/P3D PGraphics to transparent?
Code Example
- PGraphics pg;
- void setup() {
- size(500, 500, P2D);
- pg = createGraphics(width/2, height/2, P2D);
- noFill();
- }
- void draw() {
- setTransparent();
- background(0, 255, 0);
- image(pg, width/4, height/4);
- rect(width/4, height/4, width/2, height/2);
- }
- void setTransparent() {
- color transparent = color(0, 0);
- color red = color(255, 0, 0);
- pg.beginDraw();
- pg.loadPixels();
- for (int y=0; y<pg.height; y++) {
- for (int x=0; x<pg.width; x++) {
- if (x>pg.width/2) {
- pg.pixels[x+y*pg.width] = transparent; // doesn't work with P2D/P3D
- } else {
- pg.pixels[x+y*pg.width] = red; // works
- }
- }
- }
- pg.updatePixels();
- pg.endDraw();
- }
1