Export to PDF will not render vectors


#1

I am new to processing and was motivated to work on something I found interesting, things are coming along just fine but I can’t export vector based output from my sketch. I am using the Hype libraries hypeframework-dot-com and have tried two things. First following the examples at https://processing.org/reference/libraries/pdf/index.html anything I generate is only the solid background color. Second following the instructions from Joshua Davis I can save out a PDF but it is pixel based and not vector. I could really use a helping hand, not sure where I am going wrong and I apologize in advance for posting the whole code, I might remove something revealing if I try to trim things down. This code is based on Joshua’s example.

  import processing.pdf.*;
  import hype.*;
  import hype.extended.colorist.HColorPool;
  import hype.extended.behavior.HTimer;
  import hype.extended.behavior.HTween;
  import hype.extended.behavior.HOscillator;
  
  HColorPool    colors;
  HCanvas       canvas;
  HDrawablePool pool;
  HCallback     onTweenEnd;
  
  void mousePressed() { 
    looping = !looping;
  }
  
  void setup() {
    size(1900, 640, P3D);
    H.init(this).background(#242424).use3D(true);
  
    colors = new HColorPool(#FFFFFF, #F7F7F7, #ECECEC, #333333, #0095A8, #00616F, #FF3300, #FF6600);
  
    canvas = (HCanvas) H.add(new HCanvas(P3D).autoClear(false));
  
    onTweenEnd = new HCallback() {
      public void run(Object obj) {
        HTween tween = (HTween) obj;
        pool.release(tween.target());
      }
    };
  
    pool = new HDrawablePool(300);
    pool.autoParent(canvas)
      .add(new HRect(10).rounding(18))
      .onCreate(
      new HCallback() {
      public void run(Object obj) {
        HDrawable d = (HDrawable) obj;
        d
          .strokeWeight(2)
          .stroke(204, 102, 0)
          .fill(colors.getColor())
          .scale(0)
          .obj("tween", new HTween()
          .target(d)
          .property(H.Z)
          .ease(.0005)
          .spring(.97)
          .callback(onTweenEnd)
          )
          .obj("oscR", new HOscillator()
          .property(H.ROTATION)
          .waveform(H.SINE)
          .speed(0.3)
          .freq(22)
          .range( -(int)random(180, 360), (int)random(180, 360) )
          )
          .obj("oscS", new HOscillator()
          .property(H.SCALE)
          .waveform(H.SINE)
          .speed(.75)
          .freq(4)
          .range(.2, 2.0)
          )
          ;
      }
    }
    )
  
    .onRequest(
      new HCallback() {
      public void run(Object obj) {
        int i = pool.currentIndex();
        HDrawable d = (HDrawable) obj;
  
        HTween tween = (HTween) d.obj("tween");
        HOscillator oscR = (HOscillator) d.obj("oscR");
        HOscillator oscS = (HOscillator) d.obj("oscS");
  
        d.loc(random(width), random(height), random(-2000)).size(HMath.randomInt(1, 11)*5);
  
        tween.register().start(d.z()).end(100);
        oscR.register().currentStep(i*2).target(d);
        oscS.register().currentStep(i*2).target(d);
      }
    }
    )
  
    .onRelease(
      new HCallback() {
      public void run(Object obj) {
        HDrawable d = (HDrawable) obj;
        HOscillator oscR = (HOscillator) d.obj("oscR");
        HOscillator oscS = (HOscillator) d.obj("oscS");
  
        oscR.unregister();
        oscS.unregister();
      }
    }
    )
    ;
  
    new HTimer(100).callback(
      new HCallback() {
      public void run(Object obj) {
        pool.request();
        saveVector();
      }
    }
    );
  }
  
  void draw() {
    H.drawStage();
  }
  
  void saveVector() {
    PGraphics tmp = null;
    tmp = beginRecord(PDF, "render.pdf");
  
      H.stage().paintAll(tmp, false, 1);

    endRecord();
  }

PDF export doesn't work with Hyper framework
#2

hi, happy new year…

pls repost / repair above / your code posting using

</>

so i try to find where you could get that code from?
esp. about the
tmp = beginRecord(PDF, “render.pdf”);
as that is not from the examples in
https://processing.org/reference/libraries/pdf/index.html

now i tried to understand the

if (tmp == null)

what is this? a test if the file open worked?
anyhow in cases like that i recommend some diagnostic prints, so you know where
you end up when running the sketch.

without your main i found that as expected

H.stage().paintAll(tmp, false, 1);

should run?? is that what you want?

add:
why we not ask
@buraquex
as it looks like he solved exactly that ( but not posted his code here )


#3

Thanks for the response. I am learning by jumping in, unfortunately that means I can’t parse out the particulars you are asking about. . . I don’t know what "if (tmp == null) achieves since I am just following the instructions mid stream. I did reach out to buraquex so perhaps he will share some particulars. I did rework it using the standard PDF export method and all it does is render out the background as you can see below:

  import processing.pdf.*;
  import hype.*;
  import hype.extended.colorist.HColorPool;
  import hype.extended.behavior.HTimer;
  import hype.extended.behavior.HTween;
  import hype.extended.behavior.HOscillator;
  
  HColorPool    colors;
  HCanvas       canvas;
  HDrawablePool pool;
  HCallback     onTweenEnd;
  
  
  
  void setup() {
    size(1900, 640, P3D);
    H.init(this).background(#242424).use3D(true);
    beginRecord(PDF, "everything.pdf");
  
    colors = new HColorPool(#877ab4, #7e6584, #df9496, #875a71, #877ab4, #fcb1a7);
  
    canvas = (HCanvas) H.add(new HCanvas(P3D).autoClear(false));
  
    onTweenEnd = new HCallback() {
      public void run(Object obj) {
        HTween tween = (HTween) obj;
        pool.release(tween.target());
      }
    };
  
    pool = new HDrawablePool(1);
    pool.autoParent(canvas)
      .add(new HRect(10).rounding(18))
      .onCreate(
      new HCallback() {
      public void run(Object obj) {
        HDrawable d = (HDrawable) obj;
        d
          .strokeWeight(1)
          .stroke(252, 233, 170)
          .fill(colors.getColor())
          .scale(0)
          .obj("tween", new HTween()
          .target(d)
          .property(H.Z)
          .ease(.02)
          .spring(.4)
          .callback(onTweenEnd)
          )
          .obj("oscR", new HOscillator()
          .property(H.ROTATION)
          .waveform(H.SINE)
          .speed(3)
          .freq(1)
          .range( -(int)random(44, 250), (int)random(360, 360) )
          )
          .obj("oscS", new HOscillator()
          .property(H.SCALE)
          .waveform(H.SINE)
          .speed(0.75)
          .freq(4)
          .range(2, 2.0)
          )
          ;
      }
    }
    )
  
    .onRequest(
      new HCallback() {
      public void run(Object obj) {
        int i = pool.currentIndex();
        HDrawable d = (HDrawable) obj;
  
        HTween tween = (HTween) d.obj("tween");
        HOscillator oscR = (HOscillator) d.obj("oscR");
        HOscillator oscS = (HOscillator) d.obj("oscS");
  
        d.loc(random(width), random(height), random(-2000)).size(20, 40);
  
  
        tween.register().start(d.z()).end(100);
        oscR.register().currentStep(i*2).target(d);
        oscS.register().currentStep(i*2).target(d);
      }
    }
    )
  
    .onRelease(
      new HCallback() {
      public void run(Object obj) {
        HDrawable d = (HDrawable) obj;
        HOscillator oscR = (HOscillator) d.obj("oscR");
        HOscillator oscS = (HOscillator) d.obj("oscS");
  
        oscR.unregister();
        oscS.unregister();
      }
    }
    )
    ;
  
    new HTimer(100).callback(
      new HCallback() {
      public void run(Object obj) {
        pool.request();
      }
    }
    );
  }
  
  void draw() {
    H.drawStage();
  }
  
  void keyPressed() {
    if (key == 'q') {
      endRecord();
      exit();
    }
  }

#4

@kll I removed the code you were questioning (edited the above sketch) and it renders an image but it is not vector. How is it that an image is exported using the PDF function but that it is pixel based?

not%20vector


#5

sorry for the miss understanding, actually i asked you to put in some diagnostic prints

println("i not understand how i come here, but it is good to know the program flow ");

and not asked you to delete something.

//__________________________
and thank you for the code formatting and using screenshots/pictures
so the “experts” understand your question better.

and yes, this is a community, so we help each other
and good you reached out ( with the question at his thread )
but also just a mention of
@user
should be enough to give a message ( incl. notification by email )

//_________________________
and just to get more background info:
did you download?
( github.com/hype/HYPE_Processing )
no, i think its
( github.com/hype/HYPE_Processing/blob/lib_staging/distribution/HYPE.zip )
and after a other half hour i think you used
processing IDE /File/ Examples /
/Contributed Libraries / HYPE / HCanvas/ HCanvas04 /

//_________________________
i see you changed to

setup()
    size(1900, 640, P3D);
    beginRecord(PDF, "everything.pdf");
 //...
}

kepressed "q" endRecord();

and got a picture
( again, is it a picture from what you want to see? )
but it is still pix instead vector?

so i ask you to read again
https://processing.org/reference/libraries/pdf/
where it comes to the part

PDF Files from 3D Geometry (With Screen Display)
To create vectors from 3D data, use the beginRaw() and endRaw() commands.


#6

Yes, I have Hype downloaded and working fine.
I ran println with several variables and it worked but nothing was revealed to me, or I do not know what to look for. What variables do you recommend querying?
Yes, it is exporting the imagery I am expecting, just not as vectors
I have looked at PDF Files from 3D Geometry but since it describes breaking things down into lines and triangles I do not thing this is what I want.
Figuring out the forum, thanks for the patience.


#7

@kll I read and implemented an export method using beginRaw as you suggested, and I get a render but it is still pixel based. Is there something in the animation that is preventing it from rendering as a vector? At this point I feel like I have tried all of the variations from examples people have posted that seem related and they either give me a blank color background or pixels.


#8

well, that could be, good guess,

so take the program structure you have now, ( in a copy project )
erase/disable anything what draws something, use
// for line
and
/* for
code
sections
*/
and just replace it by one

line()

so you will see if the problem is your PDF printing or the
used drawing code.


#9

Ok, so I went through line by line and deleted everything until it started to break either the screen render or the PDF export. I still get only pixel-based export. I have used beginRecord/beginRaw (also “end”) with the same results. I have changed from P3D to P2D with the same results. I would be grateful for anything specific you might have to suggest, my time is running out for this concept and I will need to move on if I can’t export vectors soon.

  import processing.pdf.*;
  import hype.*;
  import hype.extended.colorist.HColorPool;
  import hype.extended.behavior.HTimer;
  
  HColorPool    colors;
  HCanvas       canvas;
  HDrawablePool pool;
  
  void mousePressed() { 
    looping = !looping;
  }
  
  void setup() {
    size(1900, 640, P3D);
    H.init(this).background(#242424).use3D(true);
  
    colors = new HColorPool(#FFFFFF);
    canvas = (HCanvas) H.add(new HCanvas(P3D).autoClear(false));
  
    pool = new HDrawablePool(300);
    pool.autoParent(canvas)
      .add(new HRect(10).rounding(18))
  
      .onCreate(
      new HCallback() {
      public void run(Object obj) {
        HDrawable d = (HDrawable) obj;
        d
          .strokeWeight(2)
          .stroke(204, 102, 0)
          .fill(colors.getColor())
          .scale(0)
          ;
      }
    }
    )
  
    .onRequest(
      new HCallback() {
      public void run(Object obj) {
  
        HDrawable d = (HDrawable) obj;
  
        d.loc(random(width), random(height), random(-2000)).size(100);
      }
    }
    )
    ;
  
    new HTimer(100).callback(
      new HCallback() {
      public void run(Object obj) {
        pool.request();
        saveVector();
      }
    }
    );
  }
  
  void draw() {
    H.drawStage();
  }
  
  void saveVector() {
    PGraphics tmp = null;
    tmp = beginRaw(PDF, "render.pdf");
  
    if (tmp == null) {
      H.drawStage();
    } else {
      H.stage().paintAll(tmp, false, 1);
    }
    endRaw();
  }

#10

no idea if that helps,
but try my example and tell me if that is in the format you want?

// https://processing.org/reference/libraries/pdf/index.html
// test vector outout to PDF on RPI

import processing.pdf.*;

boolean record;
String outfilename = "data/output.pdf";
void setup() {
  size(500, 500, P3D);
  println("press key [r] for print to PDF");
}

void draw() {
  if (record) {
    beginRaw(PDF, outfilename);
  }

  // Do all your drawing here
  background(204);
  translate(width/2, height/2, -200);
  rotateZ(0.2);
  rotateY(mouseX/500.0);
  box(200);

  if (record) {
    endRaw();
    record = false;
  }
}

// Hit 'r' to record a single frame
void keyPressed() {
  if (key == 'r') {
    println("print to "+outfilename);
    record = true;
  }
}


#11

When i run your code it works, obviously, when I inject it into my sketch one of several things happen. When the renderer is set to beginRaw / endRaw a blank pdf is generated and I get the message:

texture() is not available with this renderer.
vertex(x, y, u, v) is not available with this renderer.

When it is set to PDF or SVG using beginRecord / endRecord no error is triggered and the file that is generated is a500x500px color filled vector rectangle only.

  import processing.pdf.*;
  import hype.*;
  import hype.extended.colorist.HColorPool;
  import hype.extended.behavior.HTimer;
  
  boolean record;
  String outfilename = "data/output.pdf";
  
  HColorPool    colors;
  HCanvas       canvas;
  HDrawablePool pool;
  
  void mousePressed() { 
    looping = !looping;
  }
  
  void setup() {
    size(500, 500, P3D);
    H.init(this).background(#242424).use3D(true);
    println("press key [r] for print to PDF");
  
    colors = new HColorPool(#FFFFFF);
    canvas = (HCanvas) H.add(new HCanvas(P3D).autoClear(false));
  
    pool = new HDrawablePool(300);
    pool.autoParent(canvas)
      .add(new HRect(10).rounding(18))
  
      .onCreate(
      new HCallback() {
      public void run(Object obj) {
        HDrawable d = (HDrawable) obj;
        d
          .strokeWeight(2)
          .stroke(204, 102, 0)
          .fill(colors.getColor())
          .scale(0)
          ;
      }
    }
    )
  
    .onRequest(
      new HCallback() {
      public void run(Object obj) {
  
        HDrawable d = (HDrawable) obj;
  
        d.loc(random(width), random(height), random(-2000)).size(100);
      }
    }
    )
    ;
  
    new HTimer(100).callback(
      new HCallback() {
      public void run(Object obj) {
        pool.request();
  
      }
    }
    );
  }
  
  void draw() {
    
      if (record) {
      beginRaw(PDF, outfilename);
    }
    
    H.drawStage();
    
      if (record) {
      endRaw();
      record = false;
    }
    
  }
  
  // Hit 'r' to record a single frame
  void keyPressed() {
    if (key == 'r') {
      println("print to "+outfilename);
      record = true;
    }
  }

#12

so i understand:
H.drawStage()
gives one vector rectangle, but
H.stage().paintAll(tmp, false, 1);
gives all rectangles , but as pixel picture

  • what would mean that the
    HYPE / canvas / pool.add method actually is using PImage
    to add ( overdraw ) your filled / rounded rectangles.

//_________________________________
so as no expert jump in and i not understand that library,
better try to make your own stuff!
i got a “squircle” code for you where i added the PDF already.

// Squircle is a special case of a superellipse
// https://en.wikipedia.org/wiki/Squircle
//    ( x − a )**4 + ( y − b )**4 = r**4
//    x ( t ) = | cos t | 1/2 * r * sgn( cos t ) 
//    y ( t ) = | sin t | 1/2 * r * sgn( sin t )
// code by Dan Anderson

//rev for save to PDF as Vector?
import processing.pdf.*;

boolean record;
String outfilename = "data/output.pdf";


void setup() {
  size(300, 300);
  background(200, 200, 0);
  stroke(0, 200, 200); 
  strokeWeight(3); 
  fill(0, 100, 0);
  println("press key [r] for print to PDF");
}

void my_squircle(float r, float wx, float wy) {
  float x1, y1;  
  beginShape();
  for (float t = 0; t < TWO_PI; t += TWO_PI/160) {
    x1 = pow(abs(cos(t)), 0.5) * r*wx * sign(cos(t));
    y1 = pow(abs(sin(t)), 0.5) * r*wy * sign(sin(t));
    vertex(x1, y1);
  }
  endShape(CLOSE);
}

float sign(float input) {
  if (input < 0) { 
    return -1.0;
  }
  if (input > 0) { 
    return  1.0;
  }
  return 0.0;
}

void draw() {
  background(200, 200, 0);

  if (record) {
    //beginRaw(PDF, outfilename);
    beginRecord(PDF, outfilename);
  }
  translate(width/2, height/2);
  //my_squircle(mouseX/2.0, 1.0, float(mouseY)/float(height));
  my_squircle(90, 1, 1);        // test1
  translate(100, 40);
  my_squircle(20, 2, 4);        // test2
  // use more and with some random settings

  if (record) {
    //endRaw();
    endRecord();
    record = false;
  }
}

// Hit 'r' to record a single frame
void keyPressed() {
  if (key == 'r') {
    println("print to "+outfilename);
    record = true;
  }
}



#13

This is amazing, you are generous to help me thus far. Unfortunately I think the problems I am facing are greater than my understanding - I had originally hoped to simply tweak some code and save some vector files as a proof of concept for a project, but now I will need to learn not only processing but Hype in order to make this work, and I do not have the time. The original sketch used Oscilators, tweening and a timer to create animations, I have tried integrating your Squircles into the sketch but it is too complicated for my current ability. Its not that I don’t want to learn processing its that I am under a deadline to present a generalized concept. Its worth $ for me to have this working, not sure if that is taboo on the forum but that is where I am at.


#14

This is a animated segment from the original script
https://youtu.be/EkjsBNboH7c