手动下载canvas、地图的当前画面(下载为图片)

发布时间 2023-04-18 14:45:23作者: kaookiee
functionDownloadImg() {//使用前需要npm下载html2canvas并引入组件中才能使用
html2canvas(document.querySelector("#map"), {//参数为容器的dom
// 转换为图片
backgroundColor: "#ffffff", //设置截图的背景色
useCORS: true, // 解决资源跨域问题
allowTaint: false, //允许跨域(图片跨域相关)
taintTest: true,
}).then((canvas) => {
let link = document.createElement("a");
link.href = canvas.toDataURL(); //下载链接(base64格式)
link.setAttribute("download", "地图下载.png");
link.style.display = "none"; //a标签隐藏
document.body.appendChild(link);
link.click();
link.remove();
});
},