Rotating, translating, and scaling the canvas

The rotation, translation, and scaling canvas in Kivy is relatively easy with the use of matrices. In this recipe, we will create a traditional button and rotate its canvas.

Getting ready

Again the mathematics are important in graphics, so we should at least do a Wikipedia check about the matrix concept to get a real sense about what is happening with the canvas.

How to do it…

For this recipe, follow these steps:

  1. In the KV file, define the rule for the root widget.
  2. Define a button with a label and a position.
  3. In canvas.before, push the matrix onto the context's matrix stack.
  4. Rotate the matrix 45 degrees.
  5. Use the translate instruction to translate the matrix.
  6. In canvas.after, pop the matrix from the context's ...

Get Kivy Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.