Learn Create Your Own Split Screen

Next Page

We achieve this by calling ctx.translate, moving the zero point to where we want the image drawn. Then we draw the image at 0,0.

Instead of this:

if (this.visible) ctx.drawImage(this.MyImg, this.x, this.y);
We do this:
ctx.translate(this.x, this.y); if (this.visible) ctx.drawImage(this.MyImg, 0, 0); // Note: zero, zero!

If you look at the code below, you'll see we also do a save/restore, so the warping of the context we're doing for this sprite doesn't have a bad effect on any other sprites, or accumulate on the same sprite next frame.

Click on the sample, then try the Left and Right keys to rotate the image.

It's still not quite right, but we'll deal with that next.