0
Same problem
Every time I use btn In my code the same ReferenceError comes up and every time someone on here helps me with it. I have watched some yt videos so I know what the error means but I don’t know how to solve it. Can some one explain how to solve it https://code.sololearn.com/WJ8PMSOr31Cb/?ref=app
2 odpowiedzi
0
Try the following code:
let btn;
window.onload = function() {btn=
document.getElementById("jump");let count=0
btn.onclick = function() {count+=1;};
var canvas =
document.getElementById("canvas");
var context =
canvas.getContext("2d");
var x = 300;
var y = 350;
context.arc(x, y, 50, 0, 2 *
Math.PI);
context.fillStyle="red";
context.fill();
btn.addEventListener("click",function() {
count += 1;
y -= 25;
context.clearRect (0, 0, 600, 400);
context.beginPath ();
context.arc (x, y, 50, 0, 2 *
Math.PI);
context.fillStyle="red";
context.fill ();
context.font = '25px Arial';
context.fillStyle = 'white';
context.fillText ("count: " +
count, 20, 30);});
btn.onclick = function () {
count += 1;
y -= 25;
function draw () {
context.clearRect = (0, 0, 600, 400);
context.beginPath();
context.arc(x, y, 50, 0, 2 *
Math.PI);
context.fillStyle="red";
context.fill();
context.font = "25 px Arial";
context.fillStyle = "white";
context.fillText( "Count : " +
count, 20, 30);
window.requestAnimationFrame(draw);}}}
0
I placed the btn variable in global scope and the btn.onclick code block inside the window.onload code block