Hello, i need to ask a JavaScript question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hello, i need to ask a JavaScript question

Why when i use getcontext it gives me that "cannot read properties of null"

6th Jul 2023, 3:30 PM
Fatima Ghanawi
2 Answers
+ 5
Don't ask to ask, just ask. You can only get the canvas, and hence its context, once the document is fully loaded. If you need help with your code, just will need to actually show your code.
6th Jul 2023, 3:35 PM
Lisa
Lisa - avatar
+ 5
hey there! If you got stuck with coding errors, please send it here, so we can check your code and the output! getContext always use canvas(used for drawing object and shapes) to work with, usually 2d. In order to make it to not show "cannot read properties of null", your HTML must first provide a canvas with an ID/Class name..(Giving it a width and height is a SHOULD) <canvas id = "myCanvas" width = 600 height = 400> No support </canvas> Next, in the JS part, you need to get its ID and assign it to a variable, with the document.getElementById()/document.getElementByClassName() function. var canvas = document.getElementById("myCanvas"); Now, you should use the [name].getContext("2d") function, where [name] is the variable you declared FOR THE CANVAS(ID/CLASS NAME). var context = canvas.getContext("2d"); Done, now the drawing part is up to you.
6th Jul 2023, 4:01 PM
Dragon RB
Dragon RB - avatar