I can’t get this to work?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I can’t get this to work??

Whenever I try to use the canvas to draw a shape or something it never works. I always do something like this... var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); ctx.beginPath(); ctx.moveTo(300, 100); ctx.lineTo(100, 200); ctx.stroke(); Please tell me how to set it up so that I can draw a line like a tried to above???

28th Jan 2018, 6:37 PM
Someone Else
Someone Else - avatar
7 Answers
+ 2
Try embedding your code in a window.onload function
28th Jan 2018, 6:48 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 1
window.onload normally is worse than window.addEventListener("DOMContentLoaded",function(){});
28th Jan 2018, 7:31 PM
Draphar
0
Ok
28th Jan 2018, 6:48 PM
Someone Else
Someone Else - avatar
0
What do you mean @Brains by embedding?? Can you write it out for me
28th Jan 2018, 6:49 PM
Someone Else
Someone Else - avatar
0
window.onload=function(){ //all your code is here }
28th Jan 2018, 6:52 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
0
Ok
28th Jan 2018, 6:53 PM
Someone Else
Someone Else - avatar
0
Draphar can you help me. Please show me how to animate on canvas
28th Jan 2018, 7:31 PM
Someone Else
Someone Else - avatar