Canvas.toDataURL() Uncaught TypeError: undefined is not a function

前端 未结 1 713
天涯浪人
天涯浪人 2021-01-20 01:26

I\'m using a plugin called html2canvas to convert some html on my page into a canvas element. I then want to save that canvas as an image. Unfortunately I keep encountering

相关标签:
1条回答
  • 2021-01-20 01:57

    Ok, I found my problem was I was trying to call toDataURL() on my jQuery object rather than my canvas element. To fix this I used .get(0). Full code below:

    function generate(){
            html2canvas($('#b2_1'), {
                onrendered: function(canvas) {
                    canvas.setAttribute("id", "canvas");
                    document.body.appendChild(canvas);
                }
            });//this all works, the canvas appears as expected
    
            var myCanvas = $(document).find('#canvas');
            myCanvas.css("margin-left", "50px");
            var myImg = myCanvas.get(0).toDataURL();//have to get the canvas element from the jquery object
        }
    
    0 讨论(0)
提交回复
热议问题