Learn Create Your Own Split Screen

Next Page

We achieve this by adding the following two lines of code before drawImage but after translate.

The dimension we want to flip gets scaled by -1, reversing everything.

The dimension we don't want to flip gets scaled by 1, since multiplying something by one leaves it unchanged.

if (this.flipV) ctx.scale(1,-1); // Scale all vertical coordinates the other way around if (this.flipH) ctx.scale(-1,1); // Scale all horizontal coordinates the other way around

Try pressing V or H, also the Left and Right keys.

Because manipulations to the drawing context are cumulative, even though we seemingly haven't coded for the case of being flipped H and V at the same time, it works properly.

Now our sprite knows how to flip and rotate !