Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P5.image to base64 POSTable image #2326

Closed
2 tasks done
ogoossens opened this issue Nov 1, 2017 · 4 comments
Closed
2 tasks done

P5.image to base64 POSTable image #2326

ogoossens opened this issue Nov 1, 2017 · 4 comments

Comments

@ogoossens
Copy link

Nature of issue?

  • New feature request

Most appropriate sub-area of p5.js?

  • 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

@lmccart
Copy link
Member

lmccart commented Nov 1, 2017

something like this should work:

var img;

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

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

@ogoossens
Copy link
Author

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 :)

@cledesma
Copy link

cledesma commented Jun 7, 2019

This is nowhere in the documentation. Thanks for this.

@nikensss
Copy link

nikensss commented Sep 3, 2021

something like this should work:

var img;

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

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

Is it possible to add the canvas property to the p5.Image type in the typings for TypeScript? I currenly need this feature and I have to do the following:

const { canvas } = p5.get() as unknown as {
  canvas: HTMLCanvasElement;
}

which feels very hacky...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants