canvas getContext , onload | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

canvas getContext , onload

I have problem with canvas - method getContext. Code returns error: Can not read property 'getContext' of null. I think that the reson is not full load html document. When I make this code on my laptop I only need to put: <script type = "text / javascript" src = "script.js"> </ script> behind canvas tag but when I copy code to playground sololearn it doesn't work, any easy solution?

2nd Jul 2018, 12:03 PM
Dawid
Dawid - avatar
9 Answers
+ 6
sololearn requires a window.onload=function(){ //insert code here } ritual.
2nd Jul 2018, 12:15 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
2nd Jul 2018, 12:48 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
0
Make sure that js its loaded AFTER html
2nd Jul 2018, 12:06 PM
KrOW
KrOW - avatar
0
Thanks Brainsng but I thried already that: window.onload = function(){} and body onload leads to lack of access to several functions. In html I have 3 button with onclick that starts functions. When I close this functions in another I lose access.
2nd Jul 2018, 12:23 PM
Dawid
Dawid - avatar
0
Dawid Post your code if want more help
2nd Jul 2018, 12:24 PM
KrOW
KrOW - avatar
2nd Jul 2018, 12:26 PM
Dawid
Dawid - avatar
0
Functions definition aparts, clock its drawed like expected
2nd Jul 2018, 12:29 PM
KrOW
KrOW - avatar
0
KrOW - Yes, with window.onload, but functions "start","stop","reset" doesnt works.
2nd Jul 2018, 12:32 PM
Dawid
Dawid - avatar
0
Thanks, Brainsng - I put all on function not only: window.onload = function(){ var canvas = document.getElementById("canvas"); context = canvas.getContext("2d"); schwimmer1 = 0; schwimmer2 = 15; schwimmer3 = 30; schwimmer4 = 45; drawClock(); }
2nd Jul 2018, 12:56 PM
Dawid
Dawid - avatar