[p5.js] Is there another way to print to Excel (or pdf)?

  1. You can find out how to print through ‘JS’ (JAVA Script) by searching ‘Google’.
  1. excel
    Export html table data to Excel using JavaScript / JQuery is not working properly in chrome browser - Stack Overflow
  2. pdf
    jspdf - Generate pdf from HTML in div using Javascript - Stack Overflow
  1. Is there a way to print ‘pdf’ or ‘excel’ among the methods provided by ‘P5’?
    Or should it be implemented in ‘JAVA Script’?

FYI, the programming language in question is usually referred as “JavaScript” with no space, or sometimes “ECMAScript.” This is to avoid confusion with the programming language “Java.”

I’m not sure what printing has to do with Excel or why you would associate p5js with Excel in any way.

That said, the simplest way to print in JavaScript is just to call the built in window.print() function (not to be confused with the p5js print() function. This will open the browser’s print dialog and allow the user to select how they would like to print (either to an actual printer, or saving as a PDF). Here’s an example:

function setup() {
  createCanvas(400, 400);
  background(220);
}

function draw() {
  circle(mouseX, mouseY, 30)
}

function doubleClicked() {
  window.print();
}

The only native function that p5js includes that relates would be the save() function which saves the content of the canvas as a PNG image and initiates a download.

function setup() {
  createCanvas(400, 400);
  background(220);
}

function draw() {
  circle(mouseX, mouseY, 30)
}

function doubleClicked() {
  // The file name here can be whatever you want, but it should end in .png
  // The user can choose to save the file with a different name if they want.
  save("DefaultFileName.png");
}
3 Likes

Dear KumuPaul

Good!

Thank you.