最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Save image with html2canvas - Pure Javascript - Stack Overflow

programmeradmin2浏览0评论

I'm trying to make a button to capture and save my page in 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.

I'm trying to make a button to capture and save my page in 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.

Share Improve this question asked Jun 3, 2017 at 12:29 Léo DurandLéo Durand 2131 gold badge4 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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 puter using HTML2canvas

发布评论

评论列表(0)

  1. 暂无评论