Why can't I change the color of my canvas? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why can't I change the color of my canvas?

I've tried everything and can't seem to get my canvas to change color https://code.sololearn.com/WEP8gu3YmRYh/?ref=app

3rd May 2022, 7:07 PM
Nate Washburn
3 Answers
+ 2
You got a typo at line 6, in JS section c.fillStyle = "red"; // c.fillStlye = "red" P.S. Can you please delete the duplicate of this one you posted earlier?
4th May 2022, 11:58 AM
Ipang
0
Still can't get it to work
4th May 2022, 6:53 PM
Nate Washburn
0
Don't use 'onload' attribute in <body> (line 6 HTML section) Instead, use window.onload event handler function directly in JS section window.onload = function() { const canvas = document.querySelector("canvas"); const c = canvas.getContext("2d"); canvas.width = 200; canvas.height = 200; c.fillStyle = "red"; c.fillRect(0,0,canvas.width,canvas.height); c.clearRect(100,100,50,50); }
5th May 2022, 2:49 AM
Ipang