jQuery equivalent of getting the context of a Canvas

Claudiu picture Claudiu · May 27, 2010 · Viewed 92.7k times · Source

I have the following working code:

ctx = document.getElementById("canvas").getContext('2d');

Is there any way to re-write it to use $? Doing this fails:

ctx = $("#canvas").getContext('2d');

Answer

Matt picture Matt · May 27, 2010

Try:

$("#canvas")[0].getContext('2d');

jQuery exposes the actual DOM element in numeric indexes, where you can perform normal JavaScript/DOM functions.