how comes when i try using the getContext("2d") ...there is an output of cant get context of null??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

how comes when i try using the getContext("2d") ...there is an output of cant get context of null???

ill be glad for your positive response and what i need to have to make games with javascript ..html ...css

25th May 2020, 6:33 AM
Dev Ben
Dev Ben - avatar
2 Answers
+ 7
Please share your code link so we can see what's wrong. This happens when your code fails in selecting the canvas element. This may help you: https://www.sololearn.com/post/90825/?ref=app But, again, please let us see your code.
25th May 2020, 6:42 AM
Kevin ★
+ 3
The <script> you are running probably runs before the document is loaded and your canvas does not exist yet. Try this: document.addEventListener("DOMContentLoaded", () => { let mycanvas = document.getElementById("mycanvas"); let ctx = mycanvas.getContext("2d"); ... etc etc });
25th May 2020, 6:43 AM
Schindlabua
Schindlabua - avatar