Skip to content

Instantly share code, notes, and snippets.

@AfterThreeYears
Created February 15, 2020 14:31
Show Gist options
  • Save AfterThreeYears/bbb1e784930f5b285c1984280942c67d to your computer and use it in GitHub Desktop.
Save AfterThreeYears/bbb1e784930f5b285c1984280942c67d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="button">下载</button>
<script src="https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.js"></script>
<script>
const img = new Image()
img.onload = () => {
// 画图片
const canvas = document.createElement("canvas");
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
canvas.toBlob(function(blob) {
console.log(blob);
saveAs(blob, '下载');
// const newImg = document.createElement("img"),
// url = URL.createObjectURL(blob);
// newImg.onload = function() {
// // no longer need to read the blob so it's revoked
/通过一个链接下载文件/ URL.revokeObjectURL(url);
// };
// newImg.src = url;
// document.body.appendChild(newImg);
});
}
img.crossOrigin = 'anonymous';
button.onclick = () => {
img.src = 'https://kano.guahao.cn/0yh262685325?token=YTAzN2Y4MDhlZmE4OTE0NmQzNDFkZGY4ZTc2N2RjNzRfTUQ1COUSTOM&v=1.0'
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment