Skip to content

Commit 9326caf

Browse files
authored
Document using paints for the paragraph API (#2119)
1 parent c755355 commit 9326caf

25 files changed

+460
-3
lines changed

.github/workflows/android.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ jobs:
174174
if: failure()
175175
with:
176176
path: package/src/__tests__/snapshots/
177-
name: snapshots-screenshots
177+
name: ${{ matrix.working-directory }}-snapshots-screenshots
178178

179179
- uses: actions/upload-artifact@v2
180180
if: failure()
181181
with:
182182
path: docs/static
183-
name: docs-screenshots
183+
name: ${{ matrix.working-directory }}-docs-screenshots
184184

docs/docs/text/paragraph.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,116 @@ const textStyle = {
7878
};
7979
```
8080

81+
## Using Paints
82+
83+
You can use paint objects for the foreground and the background of a text style.
84+
85+
<img src={require("/static/img/paragraph/background-node.png").default} width="256" height="256" />
86+
87+
Below we use a foreground and a background paint on a text style:
88+
89+
```tsx twoslash
90+
import { useMemo } from "react";
91+
import { Paragraph, Skia, useFonts, Canvas, Rect, TileMode } from "@shopify/react-native-skia";
92+
93+
// Our background shader
94+
const source = Skia.RuntimeEffect.Make(`
95+
uniform vec4 position;
96+
uniform vec4 colors[4];
97+
98+
vec4 main(vec2 pos) {
99+
vec2 uv = (pos - vec2(position.x, position.y))/vec2(position.z, position.w);
100+
vec4 colorA = mix(colors[0], colors[1], uv.x);
101+
vec4 colorB = mix(colors[2], colors[3], uv.x);
102+
return mix(colorA, colorB, uv.y);
103+
}`)!;
104+
105+
// Define an array of colors for the gradient to be used in shader uniform
106+
const colors = ["#dafb61", "#61DAFB", "#fb61da", "#61fbcf"].flatMap(
107+
(c) => Array.from(Skia.Color(c))
108+
);
109+
110+
const MyParagraph = () => {
111+
const paragraph = useMemo(() => {
112+
113+
// Create a foreground paint.
114+
const backgroundPaint = Skia.Paint();
115+
backgroundPaint.setShader(
116+
source.makeShader([0, 0, 256, 256, ...colors])
117+
);
118+
119+
// Create a foreground paint. We use a radial gradient.
120+
const foregroundPaint = Skia.Paint();
121+
foregroundPaint.setShader(
122+
Skia.Shader.MakeRadialGradient(
123+
{ x: 0, y: 0 },
124+
256,
125+
[Skia.Color("magenta"), Skia.Color("yellow")],
126+
null,
127+
TileMode.Clamp
128+
)
129+
);
130+
131+
const para = Skia.ParagraphBuilder.Make()
132+
.pushStyle(
133+
{
134+
fontFamilies: ["Roboto"],
135+
fontSize: 72,
136+
fontStyle: { weight: 500 },
137+
color: Skia.Color("black"),
138+
},
139+
foregroundPaint,
140+
backgroundPaint
141+
)
142+
.addText("Say Hello to React Native Skia")
143+
.pop()
144+
.build();
145+
return para;
146+
}, []);
147+
return (
148+
<Canvas style={{ width: 256, height: 256 }}>
149+
<Paragraph paragraph={paragraph} x={0} y={0} width={256} />
150+
</Canvas>
151+
);
152+
};
153+
```
154+
155+
### Applying Effects
156+
157+
The `Paragraph` component doesn't follow the same painting rules as other components.
158+
However you can apply effets using the `layer` property.
159+
For instance, in the example below, fopr we apply a blur image filter.
160+
161+
```tsx twoslash
162+
import React from "react";
163+
import { Canvas, ImageSVG, Skia, Group, Paint, Blur, Paragraph } from "@shopify/react-native-skia";
164+
165+
const width = 256;
166+
const height = 256;
167+
168+
export const Demo = () => {
169+
const paragraph = Skia.ParagraphBuilder.Make()
170+
.pushStyle({
171+
color: Skia.Color("black"),
172+
fontSize: 25,
173+
})
174+
.addText("Hello Skia")
175+
.build();
176+
return (
177+
<Canvas style={{ flex: 1 }}>
178+
<Group layer={<Paint><Blur blur={10} /></Paint>}>
179+
<Paragraph paragraph={paragraph} x={0} y={0} width={width} />
180+
</Group>
181+
</Canvas>
182+
);
183+
};
184+
```
185+
186+
### Result
187+
188+
<img src={require("/static/img/blurred-paragraph-node.png").default} width="256" height="256" />
189+
190+
81191
## Paragraph Bounding Box
82192

83193
Before getting the paragraph height and width, you need to compute its layout using `layout()` and and once done, you can invoke `getHeight()` for the height and `getLongestLine()` for the width.
18.7 KB
Loading
17.3 KB
Loading
18.2 KB
Loading
11.2 KB
Loading
11.6 KB
Loading
11 KB
Loading
118 KB
Loading
120 KB
Loading

0 commit comments

Comments
 (0)