Skip to content

Commit 3697f4c

Browse files
committed
Minor update to the headless API
1 parent 44e561d commit 3697f4c

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

docs/docs/getting-started/headless.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ You will notice in the example below that the import URL looks different than th
1515

1616
```tsx
1717
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";
1919

2020
(async () => {
2121
const width = 256;
2222
const height = 256;
2323
const r = size * 0.33;
2424
await LoadSkiaWeb();
25-
const {image, surface} = draw(
25+
const surface = makeOffscreenSurface(width, height);
26+
const image = draw(surface,
2627
<Group blendMode="multiply">
2728
<Circle cx={r} cy={r} r={r} color="cyan" />
2829
<Circle cx={size - r} cy={r} r={r} color="magenta" />
@@ -32,7 +33,7 @@ import { Fill, draw } from "@shopify/react-native-skia/lib/commonjs/headless";
3233
r={r}
3334
color="yellow"
3435
/>
35-
</Group>, width, height);
36+
</Group>);
3637
console.log(image.encodeToBase64());
3738
// Cleaning up CanvasKit resources
3839
image.dispose();

package/src/headless/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@ import type { ReactNode } from "react";
66
import { JsiSkApi } from "../skia/web";
77
import { SkiaRoot } from "../renderer/Reconciler";
88
import { JsiDrawingContext } from "../dom/types";
9+
import type { SkSurface } from "../skia";
910

1011
export * from "../renderer/components";
1112

1213
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1314
let Skia: any;
1415

15-
export const draw = (element: ReactNode, width: number, height: number) => {
16+
export const makeOffscreenSurface = (width: number, height: number) => {
1617
if (!Skia) {
1718
Skia = JsiSkApi(CanvasKit);
1819
}
1920
const surface = Skia.Surface.MakeOffscreen(width, height);
2021
if (surface === null) {
2122
throw new Error("Couldn't create surface!");
2223
}
24+
return surface;
25+
};
26+
27+
export const drawOffscreen = (surface: SkSurface, element: ReactNode) => {
2328
const root = new SkiaRoot(Skia);
2429
root.render(element);
2530
const canvas = surface.getCanvas();
2631
const ctx = new JsiDrawingContext(Skia, canvas);
2732
root.dom.render(ctx);
2833
surface.flush();
29-
const image = surface.makeImageSnapshot();
30-
return { image, surface };
34+
return surface.makeImageSnapshot();
3135
};

0 commit comments

Comments
 (0)