what is wrong with my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is wrong with my code?

Bellow is my code: var ctx = canvas.getContext("2d"); var ballRadius = 10; var x = canvas.width/2; var y = canvas.height-30; var dx = 2; JS says cant read getContext is there something wrong?

1st Nov 2018, 1:25 PM
Lincoln
Lincoln - avatar
1 Answer
+ 4
That happens most likely because the DOM has not fully loaded when the script is executed, so the canvas variable is still undefined. Try wrapping the whole initialization process in window.onload = function() { ... }
1st Nov 2018, 1:47 PM
Shadow
Shadow - avatar