0
canvas bug
cannot read property "getContext" of null https://code.sololearn.com/Wk6691oIxFwX/?ref=app
2 Answers
+ 2
I have trouble reproducing that bug. When I run, there is no JavaScript error in the console. I see that id="can1" is in the HTML and the getContext is running fine.
I would do the following anyway, though. I would wrap your document manipulation code so it is invoked only after the whole document loads.
document.addEventListener('DOMContentLoaded', function() {
var can = document.getElementById("can1");
// cannot read property "getContext" of null
var ctx = can.getContext("2d");
can.style.backgroundColor = "blue";
});
0
thanks Josh, highly appreciated