3 ms·
Lovely code! Thanks for sharing. Oh cool, TIL you can dispatch a fake click event on an invisible anchor element pointing to a png object url to download a fil
by DonHopkins 8y ago
Lovely code! Thanks for sharing.
Oh cool, TIL you can dispatch a fake click event on an invisible anchor element pointing to a png object url to download a file, without even putting the anchor in the document! I presume it all just gets garbage collected with it's done. I would have never have thought to attempt that, but it works like a charm, in just a few lines.
https://github.com/portsmouth/fibre/blob/master/js/fibre.js#L1056 https://github.com/portsmouth/fibre/blob/master/js/fibre.js#...
case 80: // P key: save current image to disk
{
if (fibre.editing) break;
var link = document.createElement('a');
link.download = "fibre.png";
this.render_canvas.toBlob(function(blob){
link.href = URL.createObjectURL(blob);
var event = new MouseEvent('click');
link.dispatchEvent(event);
},'image/png', 1);
break;
}
- robin_reala 8y agoThe link.download is the important point: in DOM terms that’s <a download="fibre.png"> which specifies that the resource should be downloaded with a filename of fibre.png.