Similar to 1000Bugy's answer but simpler because you don't have to make an anchor on the fly and dispatch a click event manually (plus an IE fix).
If you make your download button an anchor you can highjack it right before the default anchor functionality is run.
So onAnchorClick
you can set the anchor href to the canvas base64 image and the anchor download attribute to whatever you want to name your image.
This does not work in (the current) IE because it doesn't implement the download attribute and prevents download of data uris. But this can be fixed by using window.navigator.msSaveBlob
instead.
So your anchor click event handler would be as followed (where anchor
, canvas
and fileName
are scope lookups):
function onClickAnchor(e) {
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(canvas.msToBlob(), fileName);
e.preventDefault();
} else {
anchor.setAttribute('download', fileName);
anchor.setAttribute('href', canvas.toDataURL());
}
}
Here's a fiddle