caleb531 / jcanvas

A jQuery plugin that makes the HTML5 canvas easy to work with.
https://projects.calebevans.me/jcanvas/
MIT License
626 stars 192 forks source link

I need a way to download JCanvas to a Image please. #336

Closed kevinvarela closed 6 years ago

kevinvarela commented 6 years ago

I tried a million ways for download Canvas but i failed.

If you have any way to resolve this. I will lot of grateful.

Regards.

caleb531 commented 6 years ago

@kevinvarela Can you please provide the code you are trying to use? I need more context about what you mean and what you are trying to do.

kevinvarela commented 6 years ago

@caleb531 I use JCanvas for do a image with text. But now, i want download this image generated with JCanvas to file png/jpeg.

HTML <canvas id="canvas"></canvas> <a id="download" href=""><button type="button" class="btn-success btn btn-block" onClick="downloadCanvas()">Download</button></a>

JS function downloadCanvas() { var download = document.getElementById("download"); var image = document.getElementById("canvas").toDataURL("image/png").replace("image/png", "image/octet-stream"); download.setAttribute("download", "IMAGEl-" + fileNameGenerator()); download.setAttribute("href", image); }

caleb531 commented 6 years ago

@kevinvarela First, convert the data URL produced by getCanvasImage() to a Blob. See https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript and pick your favorite solution for doing so.

Then, once you have the blob, you can use the following code to trigger a file download:

let a = document.createElement('a');
a.href = URL.createObjectURL(myBlobFromAbove, {type: 'image/png'});
a.download = 'myimage.png';
a.click();

Please let me know if you're able to get this working.

kevinvarela commented 6 years ago

Thank you very much.

And thank you for JCanvas.