最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Canvas.toDataURL() Uncaught TypeError: undefined is not a function - Stack Overflow

programmeradmin0浏览0评论

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 the error in the title. I have tried with different variable names, with different html, etc. But keep encountering the same error. Here is my code (triggered on a button click):

JS

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");//this was to test I was selecting the right element, the canvas moves
        var myImg = myCanvas.toDataURL();//code breaks here
    }

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 the error in the title. I have tried with different variable names, with different html, etc. But keep encountering the same error. Here is my code (triggered on a button click):

JS

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");//this was to test I was selecting the right element, the canvas moves
        var myImg = myCanvas.toDataURL();//code breaks here
    }
Share Improve this question asked Oct 9, 2014 at 17:07 gaynorvadergaynorvader 2,6573 gold badges20 silver badges36 bronze badges 1
  • 3 Don't do what i just did for an hour: make sure the URL part of toDataURL() is capitalized. I was positive all my code was correct but still getting this error....because toDataUrl() with lowercase Url isn't actually a function no matter how hard one may believe it is, haha. – dhaupin Commented Jun 17, 2016 at 19:45
Add a comment  | 

1 Answer 1

Reset to default 16

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)

  1. 暂无评论