Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

[2.0xx] How to set P2D/P3D PGraphics pixels to transparent via the pixel array?

$
0
0
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
  1. PGraphics pg;
  2.  
  3. void setup() {
  4.   size(500, 500, P2D);
  5.   pg = createGraphics(width/2, height/2, P2D);
  6.   noFill();
  7. }
  8.  
  9. void draw() {
  10.   setTransparent();
  11.   background(0, 255, 0);
  12.   image(pg, width/4, height/4);
  13.   rect(width/4, height/4, width/2, height/2);
  14. }
  15.  
  16. void setTransparent() {
  17.   color transparent = color(0, 0);
  18.   color red = color(255, 0, 0);
  19.  
  20.   pg.beginDraw();
  21.   pg.loadPixels();
  22.   for (int y=0; y<pg.height; y++) {
  23.     for (int x=0; x<pg.width; x++) {
  24.       if (x>pg.width/2) {
  25.         pg.pixels[x+y*pg.width] = transparent; // doesn't work with P2D/P3D
  26.       } else {
  27.         pg.pixels[x+y*pg.width] = red; // works
  28.       }
  29.     }
  30.   }
  31.   pg.updatePixels();
  32.   pg.endDraw();
  33. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles