question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Way to increase resolution of the image generated via toDataURL

See original GitHub issue

This is more of a feature request. I am easily able to generate the PNG by calling toDataURL on the returned canvas. But the quality of the image is rather blurry/poor. I did some googling & found out that by default it just returns an image at 96 dpi. And there doesnt seem to be a standard way of improving this. Also the toDataURLHD is experimental and does not work any ways.

Is there any way html2canvas can return an image at a higher resolution? Or even if it can provide a way to get the DOM being rendered, I can use some library that uses the DOM (with all the computed styles applied to it) and then generate whatever image I want.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:2
  • Comments:58 (6 by maintainers)

github_iconTop GitHub Comments

25reactions
airdrummingfoolcommented, Sep 19, 2016

I figured out another way to get a high resolution image that doesn’t need to resize the body. However, it does have to absolutely position the source element due to a bug in html2canvas (#790, #820, #893, #922, etc). Based on @MisterLamb’s retina code.

function takeHighResScreenshot(srcEl, destIMG, scaleFactor) {
    // Save original size of element
    var originalWidth = srcEl.offsetWidth;
    var originalHeight = srcEl.offsetHeight;
    // Force px size (no %, EMs, etc)
    srcEl.style.width = originalWidth + "px";
    srcEl.style.height = originalHeight + "px";

    // Position the element at the top left of the document because of bugs in html2canvas. The bug exists when supplying a custom canvas, and offsets the rendering on the custom canvas based on the offset of the source element on the page; thus the source element MUST be at 0, 0.
    // See html2canvas issues #790, #820, #893, #922
    srcEl.style.position = "absolute";
    srcEl.style.top = "0";
    srcEl.style.left = "0";

    // Create scaled canvas
    var scaledCanvas = document.createElement("canvas");
    scaledCanvas.width = originalWidth * scaleFactor;
    scaledCanvas.height = originalHeight * scaleFactor;
    scaledCanvas.style.width = originalWidth + "px";
    scaledCanvas.style.height = originalHeight + "px";
    var scaledContext = scaledCanvas.getContext("2d");
    scaledContext.scale(scaleFactor, scaleFactor);

    html2canvas(srcEl, { canvas: scaledCanvas })
    .then(function(canvas) {
        destIMG.src = canvas.toDataURL("image/png");
        srcEl.style.display = "none";
    });
};

Usage:

var src = document.getElementById("screenshot-src");
var img = document.getElementById("screenshot-img");
takeHighResScreenshot(src, img, 2); // This time we provide desired scale factor directly, no more messing with DPI

EDIT: Reduced hackiness using absolute positioning instead of actually moving the element and adjusting margins, thanks to @jason-o-matic for the suggestion.

22reactions
IvoPereiracommented, Dec 23, 2014

+1 for this. Looking to get an image with html2canvas to insert into a PDF, but getting a blurry quality.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any way to increase resolution of the image generated ...
I am easily able to generate the PNG by calling toDataURL on the returned canvas. But the quality of the image is rather...
Read more >
HTMLCanvasElement.toDataURL() - Web APIs | MDN
The HTMLCanvasElement.toDataURL() method returns a data URL containing a representation of the image in the format specified by the type ...
Read more >
HTML5 Canvas Export to High Quality Image Tutorial - Konva
How to export a high quality image from a stage?If you need to export a stage as an image or as base64 then...
Read more >
HTMLCanvasElement.toDataURL()
The HTMLCanvasElement.toDataURL() method returns a data URI containing a representation of the image in the format specified by the type ...
Read more >
Generating Images Using HTML Canvas in JavaScript
The story image should be even simpler because we don't need to resize it. From here, you could use the toDataURL to add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found