Book Image

Kivy Cookbook

By : Hugo Solis
Book Image

Kivy Cookbook

By: Hugo Solis

Overview of this book

Table of Contents (16 chapters)
Kivy Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 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:
                  ...