P5.js: P5.image to base64 POSTable image

Created on 1 Nov 2017  路  3Comments  路  Source: processing/p5.js

Nature of issue?

  • [x] New feature request

Most appropriate sub-area of p5.js?

  • [x] Image

New feature details:

Is there a way on how to convert P5.image object into base64 png image object?

I would like to:

  • extract a canvas part (crop) and post it per ajax (base 64)
  • get() would be great for this, but that returns P5.image and I dont know how to convert and post it

Note:
If I wanted to post my whole canvas (already solved) I simply grab the canvas contents with canvas.toDataURL() and use that - but since cropped result is what I need - this isnt the way.

Thank you

Oliver

Most helpful comment

something like this should work:

var img;

function setup() { 
  img = loadImage("cat.jpg");
} 

function mousePressed() {
  img.loadPixels();
  console.log( img.canvas.toDataURL() );

}

All 3 comments

something like this should work:

var img;

function setup() { 
  img = loadImage("cat.jpg");
} 

function mousePressed() {
  img.loadPixels();
  console.log( img.canvas.toDataURL() );

}

Worked like a charm - thank you.

@lmccart - Lauren, a question: Do you think it would be helpful to propose and create some functions for beginners to get() the canvas encoded images - or parts of it (with get()) in case they would like to upload them somewhere per POST?

_Like lets say - upload sketches by users to a server?_

I just did it for one project and it wasn't easy for me to figure everything out.. What do you think?

Anyway big thank you :)

This is nowhere in the documentation. Thanks for this.

Was this page helpful?
0 / 5 - 0 ratings