So I'm new to javascript and trying to make a ball I designed bounce off the canvas so far I've gotten the code to make the ball bounce up and down now I'm just wondering how I would be able to do so horizontally.
I have tried making an xPosistion as I assumed a ypostion is vertital and that by using x must mean it's horizontal but trying this it's like my code just gets frozen, I've commented it out so that when you try the code you can see what I'm trying to achieve as the ball is bouncing up and down perfectly
// Gets a handle to the element with id canvasOne.
var canvas = document.getElementById("canvas-for-ball");
// Get a 2D context for the canvas.
var ctx = canvas.getContext("2d");
// The vertical location of the ball.
var yPos = 10;
var yVel = 2;
// A function to repeat every time the animation loops.
function repeatme() {
ctx.clearRect(0, 0, 300, 300)
// Draw the ball (stroked, not filled).
ctx.beginPath();
ctx.arc(70, yPos, 7, 0, 2 * Math.PI);
ctx.stroke();
// Update the y location.
yPos += yVel;
//console.log(yPos);
/*xPos += xVel;
console.log(xPos);*/
// Thos of statement shows if ball hits off this position bounce back
if (yPos > 290)
yVel *= -1;
if (yPos < 10)
yVel *= -1;
/* if (xPos > 200)
xVel *= -1;
if (xPos < 100)
xVel *= -1;*/
window.requestAnimationFrame(repeatme);
}
// Get the animation going.
repeatme();
background-color: white;
<h1>Akeem Jokosenumi</h1>
<canvas id="canvas-for-ball" width="300" height="300" style="border:1px solid"></canvas>