Skip to content

Instantly share code, notes, and snippets.

@jz5
Created September 6, 2015 03:15
Show Gist options
  • Save jz5/022a4b17b968a8fdb2a0 to your computer and use it in GitHub Desktop.
Save jz5/022a4b17b968a8fdb2a0 to your computer and use it in GitHub Desktop.
DrawingBoard.Board.prototype.downloadImg = function () {
var canvas = $("canvas").get(0);
var base64 = canvas.toDataURL();
var blob = Base64toBlob(base64);
saveBlob(blob, "pronama-chan-tshirt.png");
};
function Base64toBlob(_base64) {
var i;
var tmp = _base64.split(',');
var data = atob(tmp[1]);
var mime = tmp[0].split(':')[1].split(';')[0];
var arr = new Uint8Array(data.length);
for (i = 0; i < data.length; i++) { arr[i] = data.charCodeAt(i); }
var blob = new Blob([arr], { type: mime });
return blob;
}
function saveBlob(_blob, _file) {
if ( /*@cc_on ! @*/ false) {
window.navigator.msSaveBlob(_blob, _file);
}
else {
var url = (window.URL || window.webkitURL);
var data = url.createObjectURL(_blob);
var e = document.createEvent("MouseEvents");
e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var a = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
a.href = data;
a.download = _file;
a.dispatchEvent(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment