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

js

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);
}

html

<button onclick="exportCanvasAsPNG(‘pieChart‘,‘nianling‘)">保存</button>

 

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

上一篇:html的z-index


下一篇:photoshop教程:物体倒影制作方法介绍