@@ -78,6 +78,116 @@ const textStyle = {
78
78
};
79
79
```
80
80
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
+
81
191
## Paragraph Bounding Box
82
192
83
193
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.
0 commit comments