<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<canvas id="myCanvas"></canvas>
<div id="showPic" style="display: none">
<a href="" id="a_safe" download="板件"> <img src="" id="img_safe" alt=""></a>
</div>
<button id="canToPic">下载</button>
</body>
<script>
document.getElementById("canToPic").onclick = function () {
var a = document.getElementById('a_safe')
var img = document.getElementById('img_safe')
var image = document.getElementById("myCanvas").toDataURL("image/png");
a.href = image;
img.src = image;
if (document.all) {
a.click();
}
// 其它浏览器
else {
var e = document.createEvent("MouseEvents");
e.initEvent("click", true, true);
a.dispatchEvent(e);
}
}
</script>
</html>
转载于:https://my.oschina.net/u/3407699/blog/1563218