Save canvas as jpg to desktop [duplicate]

Download attribute

There is a new download attribute in HTML5 that allow you to specify a file-name for links.

I made this for saving canvas. It has a link (“Download as image”) –

<a id="downloadLnk" download="YourFileName.jpg">Download as image</a>

And the function:

function download() {
    var dt = canvas.toDataURL('image/jpeg');
    this.href = dt;
};
downloadLnk.addEventListener('click', download, false);

You can even change the file-name dynamically by setting the attribute downloadLnk.download = 'myFilename.jpg'.

Demo from the archives.

Leave a Comment