PDF image capture

edited October 2013 in Questions about Code

Hello, A newbe question on why I am not able to save single PDF with mouse click to file with this code? Get unexpected token:void? Thanks.

//draw trailing circles from the mouse position
//how to save single animated frame? Try below did not work?

import processing.pdf.*;
boolean record;
int numFrames = 100;

float myX[] = new float[numFrames];
float myY[] = new float[numFrames];

void setup() {

    size (500, 500);
  background(255);
  fill(0, 120, 255, 150);
  stroke(255);
}

void draw() {
  if (record) {

    beginRecord(PDF, "frame-####.pdf");
  }

  background(255);

  int replace = frameCount % numFrames;
  myX[replace] = mouseX;
  myY[replace] = mouseY;

  for (int i = 0; i < numFrames; i++) {
    int index = (replace + 1 + i)% numFrames;
    fill(0, 120, 255*(width-mouseX)/width);

    ellipse(myX[index], myY[index], i, i);

    if (record) {
      endRecord();
      record = false;
    }
  }
  void mousePressed() { 
    record = true;
  }
}

Answers

  • edited October 2013
    //draw trailing circles from the mouse position 
    // modified by Luhai Cui luhai.cui@qq.com
    
    import processing.pdf.*; 
    
    boolean record; 
    int numFrames = 100;
    
    float myX[] = new float[numFrames]; 
    float myY[] = new float[numFrames];
    
    void setup() {
      size (500, 500);
      background(255); 
      fill(0, 120, 255, 150); 
      stroke(255);
    }
    
    void draw() { 
      if (record) {
        beginRecord(PDF, "frame-####.pdf");
      }
    
      background(255);
    
      int replace = frameCount % numFrames; 
      myX[replace] = mouseX; 
      myY[replace] = mouseY;
    
      for (int i = 0; i < numFrames; i++) { 
        int index = (replace + 1 + i)% numFrames; 
        fill(0, 120, 255*(width-mouseX)/width);
        ellipse(myX[index], myY[index], i, i);
      }
      if (record) {
        endRecord();
        record = false;
      }
    }
    
    void mousePressed() { 
      record = true;
    } 
    
  • I was not able to cut and paste my sketch on this forum and keep the formatting so there must be a correct way to do this? That said, the comment I believe was to format the sketch the same way I already have it written, unless I am missing something? So I still have the same question.

  • edited October 2013 Answer ✓

    Select your code and hit the C button (or Ctrl+K) to format it properly.

    And your endRecord() block should be outside of the for loop! (it ends the record on the first iteration...)

    Oh, and the mousePressed() method should be outside of draw() too.

    Looks like a brace is out of place.

    luhai shows the correct code (I had to format it too...)

  • sorry, I'll format my code by myself next time :)

  • Answer ✓

    @welder Ctrl-T is very good basic way to see what's wrong with your code.

  • Thanks you guys I see the missing bracket now and it works.

Sign In or Register to comment.