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

Error on canvas

Why doesn't the following code work??? The id is correct on the HTML file const canvas = document.getElementById("c"); console.log(canvas); canvas.height = innerHeight; canvas.width = innerWidth; canvas.style.backgroundColor = "black"; var c = canvas.getContext("2d"); c.beginPath(); c.arc(50, 50, 100, 0, 2*Math.Pi,false); c.strokeStyle = "orange"; c.stroke();

30th Jan 2021, 3:56 PM
Alexandre
Alexandre - avatar
2 Answers
+ 1
Try wrapping the code inside an onload event listener on window. Something like window.onload = () = { code... } The script on Sololearn loads before the all HTML is parsed so the element with id "c" might not exist at the time your code runs.
30th Jan 2021, 4:24 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
It was the problem indeed now it works just fine Thank you a lot
30th Jan 2021, 4:42 PM
Alexandre
Alexandre - avatar