File tree Expand file tree Collapse file tree 2 files changed +22
-14
lines changed
docs/docs/getting-started Expand file tree Collapse file tree 2 files changed +22
-14
lines changed Original file line number Diff line number Diff line change @@ -15,25 +15,29 @@ You will notice in the example below that the import URL looks different than th
15
15
16
16
``` tsx
17
17
import { LoadSkiaWeb } from " @shopify/react-native-skia/lib/commonjs/web/LoadSkiaWeb" ;
18
- import { Fill , draw } from " @shopify/react-native-skia/lib/commonjs/headless" ;
18
+ import { Fill , makeOffscreenSurface , drawOffscreen } from " @shopify/react-native-skia/lib/commonjs/headless" ;
19
19
20
20
(async () => {
21
21
const width = 256 ;
22
22
const height = 256 ;
23
23
const r = size * 0.33 ;
24
24
await LoadSkiaWeb ();
25
- const image = draw (
25
+ const surface = makeOffscreenSurface (width , height );
26
+ const image = draw (surface ,
26
27
<Group blendMode = " multiply" >
27
- <Circle cx = { r } cy = { r } r = { r } color = " cyan" />
28
- <Circle cx = { size - r } cy = { r } r = { r } color = " magenta" />
29
- <Circle
30
- cx = { size / 2 }
31
- cy = { size - r }
32
- r = { r }
33
- color = " yellow"
34
- />
35
- </Group >, width , height );
28
+ <Circle cx = { r } cy = { r } r = { r } color = " cyan" />
29
+ <Circle cx = { size - r } cy = { r } r = { r } color = " magenta" />
30
+ <Circle
31
+ cx = { size / 2 }
32
+ cy = { size - r }
33
+ r = { r }
34
+ color = " yellow"
35
+ />
36
+ </Group >);
36
37
console .log (image .encodeToBase64 ());
38
+ // Cleaning up CanvasKit resources
39
+ image .dispose ();
40
+ surface .dispose ();
37
41
})();
38
42
```
39
43
Original file line number Diff line number Diff line change @@ -6,26 +6,30 @@ import type { ReactNode } from "react";
6
6
import { JsiSkApi } from "../skia/web" ;
7
7
import { SkiaRoot } from "../renderer/Reconciler" ;
8
8
import { JsiDrawingContext } from "../dom/types" ;
9
+ import type { SkSurface } from "../skia" ;
9
10
10
11
export * from "../renderer/components" ;
11
12
12
13
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13
14
let Skia : any ;
14
15
15
- export const draw = ( element : ReactNode , width : number , height : number ) => {
16
+ export const makeOffscreenSurface = ( width : number , height : number ) => {
16
17
if ( ! Skia ) {
17
18
Skia = JsiSkApi ( CanvasKit ) ;
18
19
}
19
20
const surface = Skia . Surface . MakeOffscreen ( width , height ) ;
20
21
if ( surface === null ) {
21
22
throw new Error ( "Couldn't create surface!" ) ;
22
23
}
24
+ return surface ;
25
+ } ;
26
+
27
+ export const drawOffscreen = ( surface : SkSurface , element : ReactNode ) => {
23
28
const root = new SkiaRoot ( Skia ) ;
24
29
root . render ( element ) ;
25
30
const canvas = surface . getCanvas ( ) ;
26
31
const ctx = new JsiDrawingContext ( Skia , canvas ) ;
27
32
root . dom . render ( ctx ) ;
28
33
surface . flush ( ) ;
29
- const image = surface . makeImageSnapshot ( ) ;
30
- return image ;
34
+ return surface . makeImageSnapshot ( ) ;
31
35
} ;
You can’t perform that action at this time.
0 commit comments