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 inherits now from StencilView
, instead of RelativeLayout
. The StencilView
class doesn't use relative coordinates (as the RelativeLayout
class does) but we would like to keep relative coordinates in the drawing space because they are convenient...