Trouble trying with beginRecord()

Hello people,

I am trying to export a processing artwork into a new PDF every time I press the letter 's'. The code works but it only export the next frame and not the current one displayed in the screen. How can I tweak this code in order to export exactly what I am seeing in the processing screen?

Code:

import processing.pdf.*;

int space = 10;
boolean savePDF = false;

void setup() {
  size(85, 545);
  colorMode(HSB, 360, 100, 100);
  smooth();
  newGrid();
}

void draw() {
}


void mouseClicked() {
  newGrid();
}

void newGrid() {
  if ( savePDF ) { // Begin record 
    beginRecord( PDF, "pdf/myartwork-####.pdf" );
  }

  background(random(360), 50, 100);
  stroke(random(360), 100, 50);
  space = int(random(4, height * 0.1));
  strokeWeight(space * 0.4);
  for (int x = 0; x < width; x += space) {
    for (int y = 0; y < height; y += space) {
      int r = int(random(3));
      if (r == 2) {
        line(x, y, x + space, y + space);
      }
      else if (r == 1) {
        line(x, y + space, x + space, y);
      }
      else continue;
    }
  }
  if ( savePDF ) { // End record
    endRecord(); 
    savePDF = false;
  }
}

void keyPressed() {
  if ( key == 's' ) {
    savePDF = true;
  }
}

Thank you in advance.

Answers

  • edited November 2013 Answer ✓

    beginRecord() doesn't pick up what was already drawn. Just like a camera doesn't record what has transpired! =;
    Since you render a new canvas only after mouseClicked(), you should use noLoop() in setup() to halt draw().
    Then use redraw() inside mouseClicked(). So your code becomes input event-driven! :ar!
    I believe if no endRecord() is issued, no actual file save occurs. So you should put that inside keyTyped()! B-)

  • Answer ✓

    The endRecord() is fine where it is... But indeed, the PDF renderer is just that... a renderer, ie. it can render only drawings done after the key is pressed, not what is on screen when the key is pressed. (Unlike the saveFrame() function, which doesn't do a rendering, but just save a state. PDF needs to capture drawing orders, not drawing result.)

  • Thanks guys. I am totally new to processing so I appreciate the help.

    @GoToLoop, I done it what you recommended and the code still works but I do not have a clue what it means an 'input event-driven' whats the difference between the new code and the old code? Does it improve the performance?

    @PhiLho, I understand that is not possible to achieve what I want however I wonder if it possible to create a single PDF containing multiple pages with the frames I choose to save. Is this possible?

  • Multiple page is addressed in the PDF library reference.

Sign In or Register to comment.