[英]How do I export a HTML canvas to an image that can be downloaded?
function downloadCanvasContentFn(canvasId = "", fileName = "") {
let id = "canvasDownloadLinkId";
//get the link if already exisit in page
let link = document.getElementById(id);
//if not create one
if (link == null) {
//Create a link
link = document.createElement('a');
link.id = id;
}
//set file name
link.download = fileName + '.png';
//get canvas as data URL
link.href = document.getElementById(canvasId).toDataURL()
//click -- this will download
link.click();
}
上面提到的 function 可以是一個可重復使用的 function 以幫助將 canvas 下載為文件。 現在只需傳遞圖像的 canvasid 和文件名。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.