canvas bug | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

canvas bug

cannot read property "getContext" of null https://code.sololearn.com/Wk6691oIxFwX/?ref=app

1st Feb 2021, 5:47 PM
Mark Davis
Mark Davis - avatar
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"; });
1st Feb 2021, 6:02 PM
Josh Greig
Josh Greig - avatar
0
thanks Josh, highly appreciated
1st Feb 2021, 6:10 PM
Mark Davis
Mark Davis - avatar