document.getElementById("ctx").getContext("2d").fillStyle = "#FF0000";
document.getElementById("ctx").getContext("2d").fillRect(0, 0, 200, 200);
Your code works for me, assuming:
Make sure those 2 things are true about your setup...
Does your canvas element have an #id of
ctx? That's not fatal, but the canvas element contains a
context so it's a bit misleading.
If you have a canvas element like this:
Then you can get a reusable reference to the canvas's context like this:
// no need to constantly get a context reference ... // just do it once at the start of your app var canvas=document.getElementById('canvas'); var context=canvas.getContext('2d');
And you can reuse that context reference to do all your drawing calls: