+ 1
Why isnāt this variable working
Iām trying to practice using the canvas since Iāve never used it before, so I tried just drawing a line. When I run it though, it says it canāt find a variable context in line 6. window.onload = function(){ var canvas= document.getElementById("canvas"); var context= canvas.getContext("2d") }; context.moveTo(10,10); context.lineTo(100,10); context.stroke();
3 Answers
+ 5
Put everything inside the onload function
+ 4
ElizabethM
Code outside the window.onload function is executing first. That's why.
0
// alert 2, alert 1
window.onload = function(){
alert(1);
var canvas=
document.getElementById("canvas");
var context= canvas.getContext("2d")
};
alert(2);
context.moveTo(10,10);
context.lineTo(100,10);
context.stroke();