Skip to content
mask

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_mask paints a color through it, and fill_mask_source any ColorSource – a gradient or a pattern – so a mask is what makes a gradient take an arbitrary soft shape.
  • push_clip_mask puts it on the clip stack, where it nests with clip paths and rectangles the way push_clip_path does.
  • apply_mask scales a canvas’s alpha by it, and the draw_canvas overload 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: A width x height grid of 0-255 coverage, row-major, one byte per pixel: 255 lets everything through, 0 nothing.

Functions