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:
In the KV file, define the rule for the root widget.
Define a button with a label and a position.
In
canvas.before
, push the matrix onto the context's matrix stack.Rotate the matrix 45 degrees.
Use the translate instruction to translate the matrix.
In
canvas.after,
pop the matrix from the context's matrix stack onto the model view:<MyW>: Button: text: 'hello world' size_hint: None, None pos_hint: {'center_x': .5, 'center_y': .5} canvas.before: PushMatrix Rotate: ...