Performance gets worse when using PGraphics

edited November 2016 in Questions about Code

Hello, i have a sketch where I draw "concentric" polygons and rotate them. It runs at ~45fps when I draw on the canvas, but it goes to 9 fps when I changed the code to draw on a PGraphics.

this is my function where I rotate and draw the polygons:

   void plotPolygonArray(PGraphics pg, Polygon[] parray) {
          pg.beginDraw();
          pg.background(0);
          pg.stroke(255);
          pg.noFill();
          for (int i=0; i<parray.length; i++) {
            pg.pushMatrix();
            pg.translate(parray[0].x,parray[0].y);
            pg.rotate(parray[0].phi);                                     
            parray[0].phi+=parray[0].pinc;                                       
            pg.beginShape();
            pg.stroke(255);
            float angle = TWO_PI / parray[i].sides;
            for (float a = 0; a < TWO_PI; a += angle) {
              float sx =   cos(a) * parray[i].radius;
              float sy =  sin(a) * parray[i].radius;
              pg.vertex(sx, sy);
            }
            pg.endShape(CLOSE);
            pg.popMatrix();

          }
          pg.endDraw();
        }

then in draw() i go:

void draw() { plotPolygonArray(pg,parray); image(pg,0,0); }

Any help on how can I improve the framerate when using PGraphics in this sketch?

PD: this is a little .gif I made based on the sketch

Tagged:

Answers

  • edited November 2016

    •When I draw directly on the canvas, the sketch runs as smooth as in the .gif

    •I draw 150 polygons in the sketch. This is not the problem, as I tested it with only one polygon and it is as slow as with 150

Sign In or Register to comment.