I have made simple example of using canvas and then I saw that my code doesn’t work when I use jQuery selector.
Examples:
window.onload = function() {
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
ctx.fillRect(10,50,100,200);
};
window.onload = function() {
var canvas = $('#myCanvas');
var ctx = canvas.getContext('2d');
ctx.fillRect(10,50,100,200);
};
So I have no idea why it happened. Is there any limitations about it?
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 usingvar canvas = $('#myCanvas')[0]NOTE:
var canvas = document.getElementById('myCanvas');is equivalent tovar canvas = $('#myCanvas')[0]