Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add download name as custom name #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions canvas2image.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,19 @@ var Canvas2Image = function () {
return canvas.toDataURL(type);
}

function saveFile (strData) {
document.location.href = strData;
function saveFile (strData, type) {
// Download file with filename as image.png
var link = document.createElement('a');
if (typeof link.download === 'string')
{
link.href = strData;
link.download = "image."+type;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}else{
window.open(uri);
}
}

function genImage(strData) {
Expand Down Expand Up @@ -201,14 +212,15 @@ var Canvas2Image = function () {
if ($support.canvas && $support.dataURL) {
if (typeof canvas == "string") { canvas = document.getElementById(canvas); }
if (type == undefined) { type = 'png'; }
var rawType = type;
type = fixType(type);
if (/bmp/.test(type)) {
var data = getImageData(scaleCanvas(canvas, width, height));
var strData = genBitmapImage(data);
saveFile(makeURI(strData, downloadMime));
saveFile(makeURI(strData, downloadMime), rawType);
} else {
var strData = getDataURL(canvas, type, width, height);
saveFile(strData.replace(type, downloadMime));
saveFile(strData.replace(type, downloadMime), rawType);
}
}
};
Expand Down