Processing 1 vs 2, PGraphics, and Transparency

Running a program (included below) in Processing 1.5.1 on Windows generates a png file that has a transparent background - precisely what I want.

However, when I run the same program under Processing 2, the png is generated with a solid gray background. Is this a know issue, a deliberate change, or something else? I'm assuming this change in behavior is a consequence of the various Processing changes with respect to openGL.

So my question is how do I use PGraphics to create a png with a transparent background? Do I need to access the pixels array and manually set the transparency of every pixel before beginning to draw?

Example code:

PGraphics png;       
void setup() {
  size(50,50,P3D);
  png = createGraphics(1200, 800, P3D);
}
void draw() {
  png.beginDraw();
  png.smooth(); 
  png.strokeWeight(4);   
  png.noFill();   
  png.stroke(255,10,10);
  png.pushMatrix();
  png.translate(600, 400);
  png.rotate(PI);
  png.line(0,0,   300,200);
  png.line(0,0, -400, 400);
  png.line(0,0, 500, -550);
  png.line(0,0, 500, 550);
  png.popMatrix();
  png.save("C:/a_PDE_Output/testingPGraphicsTransparency151.png");
  png.endDraw();
  exit();
}

Answers

  • edited August 2014 Answer ✓

    Hi Jim you need a blank line either side of your code to ensure it gets displayed properly. I did it for you this time :)

    Long time since I used 1.5.1 but you can still get transparentcy with v2 add the following statement between lines 7 and 8

    png.background(0,0);

    This will set the background to transparent black.

  • Hi Quark, Thanks for the tip re code. I did do the Ctrl-K but wasn't aware that it wouldn't work without intervening blank lines.

    Also, thanks for the tip re using background. I have a number of older sketches that were last run under v1.5.1 some of which I've revisited since migrating to v2.x It can be a challenge figuring out why something that performs in a certain way in one version acquires a different behavior in a subsequent version.

    Thanks again.

  • In the example you show, you don't use 3D, do you? So, I think if you just remove the ,P3D part, it can work as it was previously. If you really need 3D in a less trivial code, I suppose the tip from quark can do the job.

  • I missed the fact that it was using P3D renderer so I would be interested in hearing if it worked in JAVAMODE. Also P3D in v2 is based on OpenGL but in v1.5.1 is was a 3D renderer built in Processing.

  • Function clear() also works:
    http://processing.org/reference/clear_.html

    Even on Processing 2+, PImage objects are instantiated w/ a 100% transparent black when using JAVA2D! <):)

  • Hi, FYI I had tried the program in both P2D and P3D modes. I'm guessing that whereas in v1 PGraphics was defaulting to a transparent background, in v2 it's defaulting to the same gray background as the default for the canvas and consequently must be directed to create a transparent background.

  • Again, try the default mode, also known as JAVA2D. P2D and P3D in v. 2 both are OpenGL modes.

Sign In or Register to comment.