简体   繁体   English

使用html2canvas保存图像-纯Javascript

[英]Save image with html2canvas - Pure Javascript

I'm trying to make a button to capture and save my page in png. 我正在尝试创建一个按钮来捕获我的页面并将其保存为png。

For now, I can duplicate it with the resolution I need, but instead of showing it need to show a dialog and save it like "Save as..." to rename the file. 现在,我可以使用所需的分辨率来复制它,但无需显示它,而是需要显示一个对话框并将其保存为“另存为...”,以重命名该文件。

function myRenderFunction(canvas) {
  destination.appendChild(canvas);
}

var element = document.getElementById('element');
var destination = document.getElementById('destination');



html2canvas(element, {
  scale: 3,
    onrendered: myRenderFunction
});

Here is a JSFiddle of my current process. 这是我当前流程的一个JSFiddle

To save the image locally you can change your render function to the following: 要在本地保存图像,可以将渲染功能更改为以下内容:

function myRenderFunction(canvas){
    var a = document.createElement('a');
    // toDataURL defaults to png, so we need to request a jpeg, then convert for file download.
    a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
    a.download = 'somefilename.jpg';
    a.click();
}

This is from an answer of stackoverflow How to save img to user's local computer using HTML2canvas 这是来自stackoverflow的答案如何使用HTML2canvas将img保存到用户的本地计算机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM