Skip to content

Instantly share code, notes, and snippets.

@digi0ps
Created April 24, 2019 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digi0ps/322151c790475b02746b681242de61ee to your computer and use it in GitHub Desktop.
Save digi0ps/322151c790475b02746b681242de61ee to your computer and use it in GitHub Desktop.
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