Skip to content

Instantly share code, notes, and snippets.

@CodeDraken
Created December 10, 2019 09:51
Show Gist options
  • Save CodeDraken/48eec90eac563e40e141d57cbebaa6bd to your computer and use it in GitHub Desktop.
Save CodeDraken/48eec90eac563e40e141d57cbebaa6bd to your computer and use it in GitHub Desktop.
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