SZanlongo
12/9/2014 - 7:37 PM

Ball animation with different effects

Ball animation with different effects

// Based off code from here - http://www.reddit.com/r/gifs/comments/2on8si/connecting_to_server_so_mesmerizing/cmow0sz

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

var numBalls = 24; // numb balls
var timeStep = 0;
var ballYTravel = 100;
var ballRadius = 5;
var ballSpace = 20;

var animationPosX = 100;

function clearCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
}

function drawBall(x, y, radius) {
	ctx.beginPath();
	ctx.arc(x, y, radius, 0, 2 * Math.PI);
	ctx.stroke();
}

function animateBalls() {
	clearCanvas();
  
	for (var i = 0; i < numBalls; i++) {
		drawBall(animationPosX + ballSpace * i, getY(i, timeStep), ballRadius);
	}
  
	timeStep++;
	requestAnimationFrame(animateBalls);
}

function getY(i, timeStep) {
	return 200 + ballYTravel / 2 * (Math.sin(timeStep * (i / 200 + 0.08)));
}

animateBalls();