I am new to p5.js and I encountered a problem with a simple code:
var a=10;
var b;
function setup() {
createCanvas(400, 400);
//some expressions using variables go here
}
function draw() {
background(220);
//more expressions using variables go here
print()
}
If I add a print() function, before I am able to type in any parameters printer dialog
opens and goes into an infinite loop. No matter I press print or cancel buttons, the dialog comes back.It does not happen all the time. Please advise!
By default, the draw() function is called repeatedly. Therefore your call to print() occurs within an infinite loop. Add a call to noLoop() within the setup() function to prevent this, as follows:
function setup() {
createCanvas(400, 400);
//some expressions using variables go here
noLoop();
}
Thank you, I am aware of that, but according to p5.js reference reference | p5.js, print() also supposed to work. And it works, but not all the times. Is it a bug?
Thank you for your comment. The problem is not in print () peritedly working in draw().
The problem is that print() with no arguments specified opens a dialog with my printer before I can add arguments to print().
After having stopped all instances of my copy of your program from running in the browser, I was able to add an argument to print(), as follows:
var a=10;
var b;
function setup() {
createCanvas(400, 400);
//some expressions using variables go here
noLoop();
}
function draw() {
background(220);
//more expressions using variables go here
print("Hello, World!");
}
The following was displayed in the JavaScript console when the program was started again:
Hey @sirin , I suppose you’re using the p5 web editor with auto refresh enabled. As you mentionned, print aims to open a print dialog if you don’t pass a string or a variable in parameters. So, you can disabled the auto-refresh option while you type your code, give a string in your print() and then enable it again.
Not convenient, I have to admit it.