Learn Create Your Own Split Screen

Next Page

The magic happens using the following line of code:
rect = myCanvas.getBoundingClientRect();
It's effect is to tell us the actual coordinates of the canvas within the browser window.

By subtracting that, we get exactly perfect coordinates within the canvas.

Try moving around the canvas below, and ensure everything works as you'd expect.

The code now is a bit ugly and unwieldy, but we can fix that.