Merge two PGraphics into one

edited November 2013 in How To...

Is it possible to merge one transparent PGraphic with another one?

Answers

  • _vk_vk
    edited November 2013 Answer ✓

    like this?

    PGraphics [] pgs = new PGraphics [2];
    
    
    void setup(){
      size(400,400);
      pgs[0] = createGraphics(width, height);
      pgs[1] = createGraphics(width, height);
    
      pgs[0].beginDraw();
      pgs[0].background(255,0);
      pgs[0].fill(190,100,200);
      pgs[0].noStroke();
      pgs[0].ellipse(200, 134 ,100, 100);// ellipse
      pgs[0].endDraw();
    
    
      pgs[1].beginDraw();
      pgs[1].background(255,0);
      pgs[1].fill(200,190,100);
      pgs[1].rect(150, 218 ,100, 100);// rect
      pgs[1].image(pgs[0], 0, 0);// merge
      pgs[1].endDraw();  
    
      background(77);
      image(pgs[1], 0, 0);// display only one
      strokeWeight(4);
      line(0, 200, 400, 200);
    
    }
    
  • I didn't thought about image() within the PGraphic. Thanks. I only knew arrayCopy(pg[0], pg[1]) -using that example- which would copy every pixel and replace them all.

  • That's why showing a bit of code often helps in pointing out errors...

    Indeed, it is little known that a PGraphics is actually a PImage and can be treated like one: it just has additional drawing capabilities.

Sign In or Register to comment.