Created
November 21, 2018 02:35
-
-
Save sursir/f2b9d93e6bfd7c29c3bdab7a9aa1b46d to your computer and use it in GitHub Desktop.
canvas to image png and download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function exportCanvasAsPNG(id, fileName) { | |
var canvasElement = document.getElementById(id); | |
var MIME_TYPE = "image/png"; | |
var imgURL = canvasElement.toDataURL(MIME_TYPE); | |
var dlLink = document.createElement('a'); | |
dlLink.download = fileName; | |
dlLink.href = imgURL; | |
dlLink.dataset.downloadurl = [MIME_TYPE, dlLink.download, dlLink.href].join(':'); | |
document.body.appendChild(dlLink); | |
dlLink.click(); | |
document.body.removeChild(dlLink); | |
} | |
// another | |
image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); //Convert image to 'octet-stream' (Just a downl | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment