js实现canvas保存图片为png格式并下载到本地

function exportCanvasAsPNG(id, fileName) {
    var canvasElement = document.getElementById(id);
    var MIME_TYPE = "image/png";
    var imgURL = canvasElement.toDataURL(MIME_TYPE);
    var dlLink = document.createElement('a');
    dlLink.download = fileName;
    dlLink.href = imgURL;
    dlLink.dataset.downloadurl = [MIME_TYPE, dlLink.download, dlLink.href].join(':');
    document.body.appendChild(dlLink);
    dlLink.click();
    document.body.removeChild(dlLink);
}
for(let i = 1; i< 23; i++) { exportCanvasAsPNG("cw-pdf-" + i, "cw-pdf-" + i +".png") }

  

posted @ 2023-06-17 15:05  笠航  阅读(404)  评论(0编辑  收藏  举报