make local file
authorPatrick McGuire <insidenothing@gmail.com>
Mon, 27 Apr 2020 15:13:25 +0000 (11:13 -0400)
committerGitHub <noreply@github.com>
Mon, 27 Apr 2020 15:13:25 +0000 (11:13 -0400)
index.html

index a3c15a1..4a7fe26 100644 (file)
         document.getElementById("canvasimg").src = dataURL;
         document.getElementById("canvasimg").style.display = "inline";
         document.getElementById("canvasimg_bk").style.display = "inline";
+        dlCanvas();
     }
+/* REGISTER DOWNLOAD HANDLER */
+/* Only convert the canvas to Data URL when the user clicks. 
+   This saves RAM and CPU ressources in case this feature is not required. */
+function dlCanvas() {
+  var dt = canvas.toDataURL('image/png');
+  /* Change MIME type to trick the browser to downlaod the file instead of displaying it */
+  dt = dt.replace(/^data:image\/[^;]*/, 'data:application/octet-stream');
+
+  /* In addition to <a>'s "download" attribute, you can define HTTP-style headers */
+  dt = dt.replace(/^data:application\/octet-stream/, 'data:application/octet-stream;headers=Content-Disposition%3A%20attachment%3B%20filename=Canvas.png');
+
+  this.href = dt;
+};
 
 </script>