Léo Durand
Léo Durand

Reputation: 213

Save image with html2canvas - Pure Javascript

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.

Upvotes: 0

Views: 6634

Answers (1)

Merunas Grincalaitis
Merunas Grincalaitis

Reputation: 859

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

Upvotes: 4

Related Questions