A dw.Mask() uses the transparency of its child elements to mask another object.
- Opaque pixels in the mask make the corresponding parts of the masked object visible.
- Transparent pixels in the mask make the corresponding parts of the masked object invisible.
To use a mask:
- Create a
dw.Mask() object. .append() shapes to the mask. You can use color (where opacity determines visibility) or fill_opacity to control the mask effect.- Pass the
Mask instance to the mask argument of the element you want to mask.
Example using a gradient for transparency:
gradient = dw.LinearGradient(*[0,0], *[1,0], gradientUnits='objectBoundingBox')
gradient.add_stop(0, 'white')
gradient.add_stop(1, 'black')
mask = dw.Mask()
box = dw.Rectangle(30, 0, 100, 100, fill=gradient)
mask.append(box)
rect = dw.Rectangle(0, 0, 200, 100, fill='pink', stroke='red', stroke_width=2, mask=mask)
d.append(rect)
gradient = dw.LinearGradient(*[0,0], *[1,0], gradientUnits='objectBoundingBox')
gradient.add_stop(0, 'white')
gradient.add_stop(1, 'black')
mask = dw.Mask()
box = dw.Rectangle(30, 0, 100, 100, fill=gradient)
mask.append(box)
# Initial shape
rect = dw.Rectangle(0, 0, 200, 100,
fill='cyan', stroke='blue', stroke_width=2)
d.append(rect)
# After mask
rect = dw.Rectangle(0, 0, 200, 100,
fill='pink', stroke='red', stroke_width=2,
mask=mask)
d.append(rect)