Learn Create Your Own Split Screen

Next Page

This is pretty easy in code.

To bounce off the left side of the canvas, we check if the left side of the ball is less than the left side of the canvas (zero), and if it is, we reverse the x-velocity.

if (this.x < 0) {this.velocity_x= -this.velocity_x};
To bounce off the right side of the canvas, we check if the right side of the ball (which is it's x-position plus its width) is greater than the width of the canvas, and if it is, we reverse the x-velocity.
if (this.x + this.MyImg.width > myCanvas.width) {this.velocity_x= -this.velocity_x};

Works pretty well, right?