Created
December 10, 2019 09:51
-
-
Save CodeDraken/48eec90eac563e40e141d57cbebaa6bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function update () { | |
// queue the next update | |
window.requestAnimationFrame(update) | |
// logic goes here | |
// bottom bound / floor | |
if (ball.y + ball.radius >= canvas.height) { | |
ball.velY = -ball.velY | |
ball.y = canvas.height - ball.radius | |
} | |
// top bound / ceiling | |
if (ball.y - ball.radius <= 0) { | |
ball.velY = -ball.velY | |
ball.y = ball.radius | |
} | |
// left bound | |
if (ball.x - ball.radius <= 0) { | |
ball.velX = -ball.velX | |
ball.x = ball.radius | |
} | |
// right bound | |
if (ball.x + ball.radius >= canvas.width) { | |
ball.velX = -ball.velX | |
ball.x = canvas.width - ball.radius | |
} | |
// add gravity | |
ball.velY += gravity | |
// update ball position | |
ball.x += ball.velX | |
ball.y += ball.velY | |
// draw after logic/calculations | |
draw() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment