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

canvas is undefined

html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Page Title</title> </head> <body> <canvas id="game" width="600" height="400"></canvas> </body> </html> js: var canvas = document.getElementById("game"); var ctx = canvas.getContext("2d"); <--- canvas is undefined why?

21st Sep 2018, 8:16 AM
Duoxx
Duoxx - avatar
1 Answer
+ 6
Have you tried wrapping your JS code within: window.onload=function() { /* your code */ }; ? I'm not seeing any errors with this sample: https://code.sololearn.com/WoA6i4WuXlp3/?ref=app
21st Sep 2018, 8:19 AM
Hatsy Rei
Hatsy Rei - avatar