Check this ? Please somebody tell why is the rectangle can't draw and why it showing error in 2 line?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Check this ? Please somebody tell why is the rectangle can't draw and why it showing error in 2 line??

https://code.sololearn.com/WFL3Qrvh43Nm/?ref=app

19th May 2019, 7:04 AM
Aman Saxena
Aman Saxena - avatar
8 Answers
+ 2
This code will output your rectangle. However, the fill colour will be black window.onload = function(){ var can=document.getElementById("canvas1"); var ctx = can.getContext("2d"); ctx.fillRect(100 , 70, 10, 10); ctx.fillStyle = "red"; }
19th May 2019, 7:41 AM
Emmanuel Joshua
Emmanuel Joshua - avatar
+ 1
Problem 1: you shouldn't put the variable declaration in two separate lines. Problem 2: you are executing this before the page loads, so your variable will also be undefined -> put everything in this function: window.onload = () => { //code here } (don't forget that to make the variable global scope you shouldn't use any keyword)
19th May 2019, 7:09 AM
Airree
Airree - avatar
+ 1
also, the rectangle will be black, because you changed the fillStyle after you made the rectangle, you should put that before filling it
19th May 2019, 7:11 AM
Airree
Airree - avatar
+ 1
Can u please show me how 🙏🙏
19th May 2019, 7:14 AM
Aman Saxena
Aman Saxena - avatar
+ 1
😙😙thanks
19th May 2019, 7:18 AM
Aman Saxena
Aman Saxena - avatar
+ 1
Bro what is the best resource to learn advanced canvas
19th May 2019, 7:34 AM
Aman Saxena
Aman Saxena - avatar
0
https://code.sololearn.com/Wy46yUt6ll7t/?ref=app Also, putting the canvas outside the body tag is just a bad idea
19th May 2019, 7:16 AM
Airree
Airree - avatar