A Pen by Anupam Jain on CodePen.
Last active
June 24, 2018 07:21
-
-
Save jainanupam/16cdd8455896a5fb7af92ad6a82f32aa to your computer and use it in GitHub Desktop.
Moving Ball
This file contains 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
<body> | |
Hello World! | |
<canvas id="my_canvas" width="300" height="300">Get Chrome...</canvas> | |
</body> |
This file contains 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 getRandomInt(max, min){ | |
return Math.floor(Math.random() * (+max - +min)) + +min; | |
} | |
window.onload = function(){ | |
var canvas = document.getElementById("my_canvas"); | |
c = canvas.getContext("2d"); | |
TWO_PI = 2 * Math.PI; | |
radius = 5; | |
var randomX =getRandomInt(0,1); | |
var randomY =getRandomInt(0,1); | |
// document.write("Random Number Generated : " + random ); | |
var posX = getRandomInt(0, canvas.width); | |
var posY = getRandomInt(0, canvas.height); | |
var incX = randomX == 0 ? -1 : 1; | |
var incY = randomY == 0 ? -1 : 1; | |
setInterval(function(){ | |
c.fillStyle = "rgba(0,0,0,0.1)"; | |
c.fillRect(0, 0, canvas.width, canvas.height); | |
if(posX + radius > canvas.width || (posX < radius && incX == -1)){ | |
incX = -incX; | |
} | |
if(posY + radius > canvas.height || (posY < radius && incY == -1)){ | |
incY = -incY; | |
} | |
posX += incX; | |
posY += incY; | |
c.fillStyle = "white"; | |
c.beginPath(); | |
c.arc(posX,posY,radius,0,TWO_PI,false); | |
c.fill(); | |
}, 10); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment