Html2canvas: Blank canvas when getting element by ID

Created on 28 Sep 2020  路  3Comments  路  Source: niklasvh/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)
        });
    });

Most helpful comment

Maybe your element is scrolled up or down. Try by adding scrollX: -window.scrollX and scrollY: -window.scrollY

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

All 3 comments

Maybe your element is scrolled up or down. Try by adding scrollX: -window.scrollX and scrollY: -window.scrollY

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

Perfect, that's solved the issue thanks.

Please please please write these two properties to the main examples according to often scrolling while screenshotting. See comment above https://github.com/niklasvh/html2canvas/issues/2368#issuecomment-701920277

scrollX: -window.scrollX,
scrollY: -window.scrollY
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlphaDu picture AlphaDu  路  4Comments

yasergh picture yasergh  路  5Comments

stevencherry1 picture stevencherry1  路  3Comments

kunal886496 picture kunal886496  路  3Comments

dking3876 picture dking3876  路  4Comments