We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to save a pdf when s is pressed... but so far I get blank pages. I think my problem is that I need a redraw when keyPressed... help for a newbie?
// Press n to generate new page, press p to print pdf
import processing.pdf.*;
import java.util.Calendar;
String timestamp() {
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", Calendar.getInstance());
}
boolean savePDF = false;
void penta(int x,int y){
strokeWeight(1);
line(x,y,800,y);
line(x,y+5,800,y+5);
line(x,y+10,800,y+10);
line(x,y+15,800,y+15);
line(x,y+20,800,y+20);
}
void setup(){
size(842,595);
smooth();
background(255);
strokeWeight(0.5);
penta(40,300);
noLoop();}
void draw(){
if(key == 'p' || key=='P'){
savePDF = true;
beginRecord(PDF, timestamp()+".pdf");}
if (savePDF) {
savePDF = false;
println("saving to pdf – finishing");
endRecord();
println("saving to pdf – done");
}
if(key == 'n' || key== 'N'){
background(255);
strokeWeight(1);
penta(40,300);
float Var= random(0,127);
int v=int(Var);
float ef= random(0,127);
int eff=int(ef);
println(v);
;
if ((v <= 127) && (v >=0)){
float j= random(1,4);
for (int i=0; i <= j; i++){
float x = random(40, 800);
float y = random(120, 380);
float r = random(2, 200);
fill(0);
ellipse (x,y,r,r);}
}
}
}
void keyPressed(){
redraw();}
Answers
A working PDF example I've got here. Hope it helps:
http://forum.processing.org/two/discussion/4275/how-to-compile-a-series-of-functions-into-one-variable#Item_13
GoToLoop thank you, I'm not that good to see at first sight what I'm doing wrong and how your kind answer could help me.. after all it's just my second week with processing
if somebody could make me see what is the problem with my code it would be more helpful. ^_^
I guess main diff. between my example & yours is the initCanvas() function.
You know, beginRecord() doesn't pick past procedures like fill(), stroke(), background(), etc.
P.S.: Hmm... I guess there are more to it! As aforementioned above, beginRecord() isn't retroactive!
You gotta start drawing after beginRecord()!
DOH!!
GoToLoop I've made it... I just have some problems with the fonts.. I still make some confusion.. I thought the best place to load font was void setup(){} am I wrong?
That's right! Load all resources inside setup() if possible! As you should know, textFont() sets which font to use:
https://processing.org/reference/textFont_.html
However I fear perhaps beginRecord() doesn't pick up the textFont() previously set for the canvas, just like other things like fill() & stroke()!
That's why I've made a separate initCanvas() in order to initialize the PGraphicsPDF after a beginRecord()!
Just make your own custom function for that task. Notice you won't re-loadFont() nor re-createFont() in there.
Merely re-use the PFont object you already got! *-:)
thank you sir for your patience...
I've made it!!!
the way was to use createFont() inside your idea of the initCanvas now exports perfectly YES!
More specifically the idea was to use textFont() inside initCanvas()!
While createFont() or loadFont() inside setup() instead!
That is so in order to avoid needless instantiation of PFont objects!
Thus, create/load once, reuse many! :P
Nonetheless, I'm glad it's already worked for ya! O:-)