-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e78e0d
commit 282629a
Showing
25 changed files
with
254 additions
and
59 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
id: mask | ||
title: Mask | ||
sidebar_label: Mask | ||
slug: /mask | ||
--- | ||
|
||
The `Mask` component hides an element by masking the content at specific points. | ||
Just like its [CSS counterpart](https://developer.mozilla.org/en-US/docs/Web/CSS/mask), there are two modes available: | ||
* `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. | ||
* `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. | ||
|
||
The first child of `Mask` is the drawing to be used as a mask, and the remaining children are the drawings to mask. | ||
|
||
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. | ||
|
||
| Name | Type | Description | | ||
|:----------|:--------------------------|:--------------------------------------------------------------| | ||
| mode? | `alpha` or `luminance` | Is it a luminance or alpha mask (default is `alpha`) | | ||
| bounds? | `SkRect` | Optional rectangle to clip the mask with | | ||
| mask | `ReactNode[] | ReactNode` | Mask definition | | ||
| children | `ReactNode[] | ReactNode` | Content to mask | | ||
|
||
## Alpha Mask | ||
|
||
Opaque pixels will be visible and transparent pixels invisible. | ||
|
||
```tsx twoslash | ||
import {Canvas, Mask, Group, Circle, Rect} from "@shopify/react-native-skia"; | ||
|
||
const Demo = () => ( | ||
<Canvas style={{ width: 256, height: 256 }}> | ||
<Mask | ||
mask={ | ||
<Group> | ||
<Circle cx={128} cy={128} r={128} opacity={0.5} /> | ||
<Circle cx={128} cy={128} r={64} /> | ||
</Group> | ||
} | ||
> | ||
<Rect x={0} y={0} width={256} height={256} color="lightblue" /> | ||
</Mask> | ||
</Canvas> | ||
); | ||
``` | ||
|
||
### Result | ||
|
||
 | ||
|
||
## Luminance Mask | ||
|
||
White pixels will be visible and black pixels invisible. | ||
|
||
```tsx twoslash | ||
import {Canvas, Mask, Group, Circle, Rect} from "@shopify/react-native-skia"; | ||
|
||
const Demo = () => ( | ||
<Canvas style={{ width: 256, height: 256 }}> | ||
<Mask | ||
mode="luminance" | ||
mask={ | ||
<Group> | ||
<Circle cx={128} cy={128} r={128} color="white" /> | ||
<Circle cx={128} cy={128} r={64} color="black" /> | ||
</Group> | ||
} | ||
> | ||
<Rect x={0} y={0} width={256} height={256} color="lightblue" /> | ||
</Mask> | ||
</Canvas> | ||
); | ||
``` | ||
|
||
### Result | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { ReactNode } from "react"; | ||
import React from "react"; | ||
|
||
import type { SkRect } from "../../skia/Rect"; | ||
|
||
import { usePaintRef, Paint } from "./Paint"; | ||
import { Defs } from "./Defs"; | ||
import { LumaColorFilter } from "./colorFilters/LumaColorFilter"; | ||
import { Group } from "./Group"; | ||
|
||
// Here we ask the user to provide the bounds of content | ||
// We could compute it ourselve but prefer not to unless | ||
// other similar use-cases come up | ||
interface MaskProps { | ||
mode: "luminance" | "alpha"; | ||
bounds?: SkRect; | ||
mask: ReactNode | ReactNode[]; | ||
children: ReactNode | ReactNode[]; | ||
} | ||
|
||
export const Mask = ({ children, mask, mode, bounds }: MaskProps) => { | ||
const paint = usePaintRef(); | ||
return ( | ||
<> | ||
<Defs> | ||
<Paint ref={paint}>{mode === "luminance" && <LumaColorFilter />}</Paint> | ||
</Defs> | ||
<Group rasterize={mode === "luminance" ? paint : undefined} clip={bounds}> | ||
{mask} | ||
</Group> | ||
<Group blendMode="srcIn">{children}</Group> | ||
</> | ||
); | ||
}; | ||
|
||
Mask.defaultProps = { | ||
mode: "alpha", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.