Why canvas doesn't work with jQuery selector?

前端 未结 4 1406
太阳男子
太阳男子 2020-12-29 07:10

I have made simple example of using canvas and then I saw that my code doesn\'t work when I use jQuery selector.

Examples:

Javascript

<
4条回答
  •  感动是毒
    2020-12-29 07:42

    Check this updated version of your jQuery fiddle: http://jsfiddle.net/techfoobar/46VKa/3/

    The problem was:

    var canvas = $('#myCanvas') gets you a jQuery extended object and not a native DOM element object that has member functions like getContext etc. For this, you need to get the canvas element using var canvas = $('#myCanvas')[0]

    NOTE: var canvas = document.getElementById('myCanvas'); is equivalent to var canvas = $('#myCanvas')[0]

提交回复
热议问题