0

I am trying to export my HTML Canvas to an Image. But when I export the image, I keep getting the img src = "undefined". I printed the canvas to the page directly and confirmed I am getting the correct canvas. Here's the code that I'm using. Please let me know what you see is wrong. May be a problem with CORS (This is HTML5)?

scope.getShuttleImage = function () {
            var tmpPng
            var imageElement
            html2canvas(document.body.getElementsByClassName("shuttle-truck"), {
                useCORS: true, 
                onrendered: function (canvas) {
                    tmpPng = canvas.toDataURL("image/png");

                }
            });
            document.write('<img src= "' + tmpPng+'"/>"') // Just for testings sake
        };
user4493284
  • 21
  • 1
  • 5

1 Answers1

2

I believe the problem is with html2canvas being asynchronous. Try changing your code to the following. Like @Archer said

scope.getShuttleImage = function () {
        var tmpPng
        var imageElement
        html2canvas(document.body.getElementsByClassName("shuttle-truck"), {
            useCORS: true, 
            onrendered: function (canvas) {
                tmpPng = canvas.toDataURL("image/png");
                document.write('<img src= "' + tmpPng+'"/>"') // Just for testings sake

            }
        });
    };
bobjoe
  • 673
  • 6
  • 11