Can someone check my html/javascript code, i tried it on mobile aide web and error occurs. The draw() cannot bo called. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone check my html/javascript code, i tried it on mobile aide web and error occurs. The draw() cannot bo called.

<!DOCTYPE html> <html> <head> <link href="css/styles.css" rel="stylesheet"> <script type="text/javascript"> function init(){ bg=document.getElementById("can1"); bgctx=bg.getContext("2d"); ob=document.getElementById("can2"); obctx=ob.getContext("2d"); } function draw(){ obctx.fillRect(0,0,40,50); } init(); draw(); </script> </head> <body> <canvas id="can1">This browser does not support canvas</canvas> <canvas id="can2">This browser does not support canvas</canvas> </body> </html>

21st Dec 2016, 11:31 AM
CJM
CJM - avatar
2 Answers
+ 1
When you call init(), you get an error ( and the script stop before calling draw() ) because of the call to the document.getElementById function where the targets ids dont exists yet ( parsing of html is in head, not yet in body ). You can put your 2 cals ( init and draw ) in a script tag in body after yours canvas ( so the ids exists ) and it would go right ( I didnt verif' -- if you want, I need your css code ^^ )...
21st Dec 2016, 11:42 AM
visph
visph - avatar
0
So what you mean is write the script between <body> tag. Thanks for your help :)
22nd Dec 2016, 2:57 AM
CJM
CJM - avatar