Search code examples
html2canvas

Blank canvas when getting element by ID html2canvas


Is it possible to use getElementByID to only print a specific element to canvas?

Using the latest version and it will only work with document.body

$("#myButton").click(function () {
    html2canvas(document.getElementById('myTable'), {
        allowTaint: true,
        imageTimeout: 15000,
        logging: true,
        useCORS: true
    }).then(function (canvas) {
        document.body.appendChild(canvas);
        const b64 = canvas.toDataURL("image/png").replace(/.*,/, '');
        console.log("data:image/png;base64, " + b64)
    });
});

Solution

  • Fixed by adding the following options

    scrollX: -window.scrollX,
    scrollY: -window.scrollY