From dd66ed184806de1f9af93f4ece40ac87255ff1b8 Mon Sep 17 00:00:00 2001 From: LSvekis Date: Fri, 2 Jul 2021 13:14:48 -0400 Subject: [PATCH] Update bouncing ball --- Chapter 13/bouncing ball | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Chapter 13/bouncing ball b/Chapter 13/bouncing ball index c3b82d3..546f857 100644 --- a/Chapter 13/bouncing ball +++ b/Chapter 13/bouncing ball @@ -18,8 +18,8 @@ const ctx = canvas.getContext("2d"); const ballSize = 10; let x = canvas.width/2; let y = canvas.height/2; -let dx = 1; -let dy = 1; +let dirX = 1; +let dirY = 1; function drawBall() { ctx.beginPath(); ctx.arc(x, y, ballSize, 0, Math.PI*2); @@ -27,19 +27,19 @@ function drawBall() { ctx.fill(); ctx.closePath(); } -function draw() { +function move() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawBall(); - if(x + dx > canvas.width-ballSize || x + dx < ballSize) { - dx *= -1; + if(x > canvas.width-ballSize || x < ballSize) { + dirX *= -1; } - if(y + dy > canvas.height-ballSize || y + dy < ballSize) { - dy *= -1; + if(y > canvas.height-ballSize || y < ballSize) { + dirY *= -1; } - x += dx; - y += dy; + x += dirX; + y += dirY; } -setInterval(draw, 10); +setInterval(move, 10);