Created
April 24, 2019 13:07
-
-
Save digi0ps/322151c790475b02746b681242de61ee 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
const initFalling = ball => { | |
/* Constants */ | |
const ballHeight = 100 | |
const acceleration = 9.8 / 60; | |
const { innerHeight } = window; | |
/* Variable to denote the speed of the ball */ | |
let fallingSpeed = 0; | |
const animateFall = () => { | |
const top = parseInt(ball.style.top); | |
const newTop = `${top + fallingSpeed}px`; | |
/* To break the fall, when the ball is near the surface */ | |
if (parseInt(newTop) >= innerHeight - ballHeight) { | |
ball.style.top = this.innerHeight - ballHeight + "px"; | |
ball.style.background = "red"; | |
return null; | |
} | |
/* Else set the top to the new value */ | |
ball.style.top = newTop; | |
fallingSpeed = fallingSpeed + acceleration; | |
requestAnimationFrame(animateFall); | |
}; | |
/* Fire the first animation */ | |
requestAnimationFrame(animateFall); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment