PDF Export - Generative Typography - particles

edited August 2014 in How To...

Hey everybody, i am totally new to processing and i was playing around with this generative Typography Tutorial. My Problem is that the pdf export that i tried to add to it won't work. Well, it just shows the regular Text in de PDF File but i want to have the particles. I guess that i am overlocking the class of particles somehow. Can anybody please help me out?

This is the Code which consists of two tabs.

FIRST TAB

import processing.pdf.*;

boolean dosave=false;
int maxParticles = 1000; 
ArrayList <Particle> particles = new ArrayList <Particle> (); 
color BACKGROUND_COLOR = color(255);
color PGRAPHICS_COLOR = color(0);
float globalRotation;
PGraphics pg;

void setup() {
  size(1280, 700, P2D);
  smooth(16);
  pg = createGraphics(width, height, JAVA2D);
  pg.beginDraw();
  pg.textSize(200);
  pg.textAlign(CENTER, CENTER);
  pg.fill(PGRAPHICS_COLOR);
  pg.text("Test", pg.width/2, pg.height/2); 
  pg.endDraw();
  background(BACKGROUND_COLOR);
}

void draw() {

  if(dosave) {

   PGraphicsPDF pdf = (PGraphicsPDF)beginRaw(PDF, "pdf_complex_out.pdf"); 


    pdf.beginDraw();
    pdf.textSize(200);
    pdf.textAlign(CENTER, CENTER);
    pdf.fill(PGRAPHICS_COLOR);

    pdf.endDraw();
  }

  addRemoveParticles();
  // update and display each particle in the list
  for (Particle p : particles) {
    p.update();
    p.display();
  }

 if(dosave) {
    endRaw();
    }

}



void mousePressed() {
  particles.clear(); // remove all particles
  globalRotation = random(TWO_PI); // randomly set the global rotation/direction of the Particles
  }

void keyPressed() {
  if (key == 'd') {
    dosave=false;
  }
}

void addRemoveParticles() {
  // remove particles with no life left
  for (int i=particles.size()-1; i>=0; i--) {
    Particle p = particles.get(i);
    if (p.life <= 0) {
      particles.remove(i);
    }
  }
  // add particles until the maximum
  while (particles.size () < maxParticles) {
    particles.add(new Particle());
  }

}

SECOND TAB

class Particle {
  PVector loc;
  float life, lifeRate;

  Particle() {
    getPosition();
    life = random(0.9, 1.25);
    lifeRate = random(0.07, 0.08);
  }

  void update() {
    float angle = noise(loc.x * 0.01, loc.y * 0.01) * TWO_PI;
    PVector vel = PVector.fromAngle(angle + globalRotation);
    loc.add(vel);
    life -= lifeRate;
  }

  void display() {
    boolean special = random(1) < 0.001;
    strokeWeight(special ? random(0.75, 3) : 0.75);
    stroke(0, special ? random(175, 255) : 65);
    point(loc.x, loc.y);
  }

  // get a random position inside the text
  void getPosition() {
    while (loc == null || !isInText (loc)) loc = new PVector(random(width), random(height));
  }

  // return if point is inside the text
  boolean isInText(PVector v) {
    return pg.get(int(v.x), int(v.y)) == PGRAPHICS_COLOR;
  }
}

Answers

  • Let see...

    • There is a endRaw() which seems misplaced, since you don't have a beginRaw().
    • I don't see when doSave becomes true...
    • Supposing it becomes true, then you create a PDF file and draw in it, then end the drawing and are done (except you don't reset doSave to false, so each next frame will be saved too). Indeed, nothing of the real sketch is saved.
    • There are several ways to save PDF files, as you probably saw. It can be confusing to choose which one... The way you choose only draw in the PDF (not on screen) and only graphical instructions prefixed by pdf. (the created PDF object) will go in the PDF file.
    • Since you seem to want to save a chosen frame to PDF (actually will be the next one), you have to take the one triggered by keyboard and capturing all drawing instructions of a draw() call to the PDF.
Sign In or Register to comment.