Translation

Learn about the translation method in Pycairo.

We'll cover the following

Translation

Translation shifts the entire coordinate system. It is useful if we want to draw a shape in a different position, but without changing the drawing code.

We use the translate method of the Context to perform a translation:

ctx.translate(200, 300)

This moves the entire user space 200 units to the right, and 300 units down, relative to device space. This means that the origin in user space, now, corresponds to the point (200, 300) in device space.

Note: In simpler terms, if we draw something at point (x, y) in user space after translating it by (200, 300), it will appear at point (x+200, y+300) in device space.

The diagram below shows the transformed user space. The portion of user space that maps onto the entire device space is shown, so that the red square appears in the same place in both the user and device space. But, if we look at the coordinates for user space we will see that they shifted relative to device space.

The device space is unchanged by the transformation. Notice that some of the user space coordinates are now negative, because of the translation. This is fine, because those coordinates map onto non-negative values in device space. We can see this in the illustration below:

Get hands-on with 1200+ tech skills courses.