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

添加额外参数,保存图片时增加文件名。 #35

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
30 changes: 18 additions & 12 deletions canvas2image.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ var Canvas2Image = function () {
return canvas.toDataURL(type);
}

function saveFile (strData) {
document.location.href = strData;
function saveFile (strData, name) {
// document.location.href = strData;
var a = document.createElement('a');
a.href = strData;
a.download = name;
a.click();
}

function genImage(strData) {
Expand Down Expand Up @@ -201,14 +205,16 @@ var Canvas2Image = function () {
if ($support.canvas && $support.dataURL) {
if (typeof canvas == "string") { canvas = document.getElementById(canvas); }
if (type == undefined) { type = 'png'; }
var name = canvas.getAttribute('data-name');
if (name == undefined) { name = "download." + type; } else { name += "." + 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), name);
} else {
var strData = getDataURL(canvas, type, width, height);
saveFile(strData.replace(type, downloadMime));
saveFile(strData.replace(type, downloadMime), name);
}
}
};
Expand All @@ -234,17 +240,17 @@ var Canvas2Image = function () {

return {
saveAsImage: saveAsImage,
saveAsPNG: function (canvas, width, height) {
return saveAsImage(canvas, width, height, 'png');
saveAsPNG: function (canvas, width, height, name) {
return saveAsImage(canvas, width, height, 'png', name);
},
saveAsJPEG: function (canvas, width, height) {
return saveAsImage(canvas, width, height, 'jpeg');
saveAsJPEG: function (canvas, width, height, name) {
return saveAsImage(canvas, width, height, 'jpeg', name);
},
saveAsGIF: function (canvas, width, height) {
return saveAsImage(canvas, width, height, 'gif');
saveAsGIF: function (canvas, width, height, name) {
return saveAsImage(canvas, width, height, 'gif', name);
},
saveAsBMP: function (canvas, width, height) {
return saveAsImage(canvas, width, height, 'bmp');
saveAsBMP: function (canvas, width, height, name) {
return saveAsImage(canvas, width, height, 'bmp', name);
},

convertToImage: convertToImage,
Expand Down