mask
Mojo module 🡭
mask
Alpha masks: a per-pixel coverage, 0-255, that a fill is painted through, a layer is composited through, or drawing is clipped to – the shape of an operation separated from how the shape was made.
Canvas.push_clip_path already rasterizes a path into exactly this
kind of coverage and keeps it on the clip stack. A Mask is that
coverage as a value: built from a path (Mask.from_path), from a
canvas’s alpha channel (Mask.from_alpha, so a blurred disk becomes
a soft-edged stencil) or from its luminance (Mask.from_luminance,
so a white-on-black painting is one), then used any number of times:
fill_maskpaints a color through it, andfill_mask_sourceanyColorSource– a gradient or a pattern – so a mask is what makes a gradient take an arbitrary soft shape.push_clip_maskputs it on the clip stack, where it nests with clip paths and rectangles the waypush_clip_pathdoes.apply_maskscales a canvas’s alpha by it, and thedraw_canvasoverload taking a mask composites a layer through it.
A mask is in device space: it is placed at a pixel offset, and the
canvas transform does not apply to it. Masks are raster-only; a
consumer wanting the same silhouette on a vector backend clips to the
path the mask came from (push_clip_path, which all three have)
rather than painting through a mask. Coverage scales the alpha of
whatever passes through it, so a translucent color through a
half-covered pixel lands at a quarter, as a clip path’s edge does.
Structs
Mask: Awidth x heightgrid of 0-255 coverage, row-major, one byte per pixel: 255 lets everything through, 0 nothing.