After translating and turning the context that you want to translate back:
context.translate(canvas.width / 2, canvas.height / 2); context.rotate(180 * Math.PI / 180);
(you do not need to crop and / or specify the width and height if the destination size matches the size of the source).
But if you just want to flip the image or rotate it 180, you can instead use the scale canvas instead of a negative value - just remember to change the coordinates ( here is a demonstration of this method ):
context.scale(-1, -1); context.drawImage(tempCanvas, -tempCanvas.width, -tempCanvas.height);
More information about the conversion as a whole (update)
To better explain how transformations work, you can go through various stages.
Origo ( see definition here ) is always the point [0, 0] in the coordinate system (or, more precisely, the point where the axes are). When you use transforms, this is the point that translates into context for the canvas.
This is also the reason you are directly manipulating the context, not the canvas, because the transforms apply to the context, not to the element (otherwise the element would be rotated by itself when you applied the rotary transformation to it - which you can do with CSS).
Initially, the coordinate system looks like this (white mixing is the invisible areas of the canvas, the central square is the canvas, and the blue grid is the context and the current coordinate system):
(Note: ignore the negative Y axis - I somehow managed to miss this detail when creating graphics - the y axis, of course, is positively vertically down).

If you now translate canvas 3.5 to 3.5 points, to get Origo in the center, the context and coordinate system will now look like this:

If now we want to apply the rotation that will occur at Origo . That is why we need to translate before rotation, as when applying rotation we get the following result:

If now just to draw an image, it will not be centered. drawImage draws an image from the upper left corner of the image. This means that it will look like this:

That's why we need to move Origo back so that we compensate for the angle of the image.
However, since we also apply rotation, the orig will move relative to the current orientation of the system.
We translate back (half the size of the image to get the center point of the image at the previous Origo point) - the rotation remains unchanged:

Now we can call drawImage , since now you can see that Origo seems to be in the right position for this operation:
