|
| 1 | +--- |
| 2 | +id: mask |
| 3 | +title: Mask |
| 4 | +sidebar_label: Mask |
| 5 | +slug: /mask |
| 6 | +--- |
| 7 | + |
| 8 | +The `Mask` component hides an element by masking the content at specific points. |
| 9 | +Just like its [CSS counterpart](https://developer.mozilla.org/en-US/docs/Web/CSS/mask), there are two modes available: |
| 10 | +* `alpha`: This mode indicates that the transparency (alpha channel) values of the mask layer image should be used as the mask values. This is how masks work in Figma. |
| 11 | +* `luminance`: This mode indicates that the luminance values of the mask layer image should be used as the mask values. This is how masks work in SVG. |
| 12 | + |
| 13 | +The first child of `Mask` is the drawing to be used as a mask, and the remaining children are the drawings to mask. |
| 14 | + |
| 15 | +By default, the mask is not clipped. If you want to clip the mask with the bounds of the content it is masking, use the `bounds` property. |
| 16 | + |
| 17 | +| Name | Type | Description | |
| 18 | +|:----------|:--------------------------|:--------------------------------------------------------------| |
| 19 | +| mode? | `alpha` or `luminance` | Is it a luminance or alpha mask (default is `alpha`) | |
| 20 | +| bounds? | `SkRect` | Optional rectangle to clip the mask with | |
| 21 | +| mask | `ReactNode[] | ReactNode` | Mask definition | |
| 22 | +| children | `ReactNode[] | ReactNode` | Content to mask | |
| 23 | + |
| 24 | +## Alpha Mask |
| 25 | + |
| 26 | +Opaque pixels will be visible and transparent pixels invisible. |
| 27 | + |
| 28 | +```tsx twoslash |
| 29 | +import {Canvas, Mask, Group, Circle, Rect} from "@shopify/react-native-skia"; |
| 30 | + |
| 31 | +const Demo = () => ( |
| 32 | + <Canvas style={{ width: 256, height: 256 }}> |
| 33 | + <Mask |
| 34 | + mask={ |
| 35 | + <Group> |
| 36 | + <Circle cx={128} cy={128} r={128} opacity={0.5} /> |
| 37 | + <Circle cx={128} cy={128} r={64} /> |
| 38 | + </Group> |
| 39 | + } |
| 40 | + > |
| 41 | + <Rect x={0} y={0} width={256} height={256} color="lightblue" /> |
| 42 | + </Mask> |
| 43 | + </Canvas> |
| 44 | +); |
| 45 | +``` |
| 46 | + |
| 47 | +### Result |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +## Luminance Mask |
| 52 | + |
| 53 | +White pixels will be visible and black pixels invisible. |
| 54 | + |
| 55 | +```tsx twoslash |
| 56 | +import {Canvas, Mask, Group, Circle, Rect} from "@shopify/react-native-skia"; |
| 57 | + |
| 58 | +const Demo = () => ( |
| 59 | + <Canvas style={{ width: 256, height: 256 }}> |
| 60 | + <Mask |
| 61 | + mode="luminance" |
| 62 | + mask={ |
| 63 | + <Group> |
| 64 | + <Circle cx={128} cy={128} r={128} color="white" /> |
| 65 | + <Circle cx={128} cy={128} r={64} color="black" /> |
| 66 | + </Group> |
| 67 | + } |
| 68 | + > |
| 69 | + <Rect x={0} y={0} width={256} height={256} color="lightblue" /> |
| 70 | + </Mask> |
| 71 | + </Canvas> |
| 72 | +); |
| 73 | +``` |
| 74 | + |
| 75 | +### Result |
| 76 | + |
| 77 | + |
0 commit comments