0

How to download a canvas image?

22nd Sep 2018, 7:13 PM
Orsrozsondai
Orsrozsondai - avatar
4 Réponses
+ 17
I am not sure about download but try right click save as.
22nd Sep 2018, 7:38 PM
Nilavarasan
Nilavarasan  - avatar
9th Mar 2021, 7:49 AM
Sharique Khan
Sharique Khan - avatar
0
If you're okay with user action required (right click on an image), you could ask the canvas to output its content as base64-encoded data (it looks like this, internally): https://code.sololearn.com/W9yG8qZkeF7T/?ref=app ...but you would inject it into the page as a downloadable image: // create a dynamic image element var imgEl = document.createElement('img'); // transfer base64-encoded image data imgEl.src = canvas.toDataURL(); // insert and render the image document.body.appendChild(imgEl);
23rd Sep 2018, 1:01 AM
Kirk Schafer
Kirk Schafer - avatar
0
Returning because I got pinged, the issue to overcome with "downloads" is initiating a *protocol* (http) download via *scripting* (which is not http) -- because you need the browser to receive headers and a MIME type. So, two other solutions, because canvas doesn't support right-click: * Round-trip it to a server -- avoided in my previous answer as unnecessarily complicated -- but you could just xhr() or fetch() that. * Just occurred to me: an overlay image that uses toDataURL() but overlays a right-clickable image: https://weworkweplay.com/play/saving-html5-canvas-as-image/ That gets you right click with some blocks-client disadvantages, but it's more like a tutorial than my answers.
6th Oct 2018, 2:15 AM
Kirk Schafer
Kirk Schafer - avatar