StencilView – limiting the drawing space

In Chapter 3, Widget Events – Binding Actions, we avoided drawing outside of the drawing space by using simple mathematics and collide_points. It was far from perfect (for example, it fails in the group mode or when we resize it), and it was tedious and prone to programming mistakes.

That was sufficient for a first example, however, StencilView is the easier way to go here. StencilView limits the drawing area to the space occupied by itself. Anything drawn outside that area is hidden. First, let's modify the file drawingspace.py with the following header:

97. # File name: drawingspace.py
98. from kivy.uix.stencilview import StencilView
99. 
100. class DrawingSpace(StencilView):
101.     ...

The DrawingSpace instance ...

Get Kivy – Interactive Applications and Games in Python - Second Edition 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.