Skip to content

Commit b1aff27

Browse files
authored
chore(πŸ“–): Add picture example (#3585)
1 parent 699b534 commit b1aff27

File tree

7 files changed

+56
-1
lines changed

7 files changed

+56
-1
lines changed

β€Žapps/docs/docs/pictures.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const size = 256;
3333
const n = 20;
3434

3535
const paint = Skia.Paint();
36+
const recorder = Skia.PictureRecorder();
3637

3738
export const HelloWorld = () => {
3839
const progress = useSharedValue(0);
@@ -43,7 +44,6 @@ export const HelloWorld = () => {
4344

4445
const picture = useDerivedValue(() => {
4546
"worklet";
46-
const recorder = Skia.PictureRecorder();
4747
const canvas = recorder.beginRecording(Skia.XYWHRect(0, 0, size, size));
4848
const numberOfCircles = Math.floor(progress.value * n);
4949
for (let i = 0; i < numberOfCircles; i++) {

β€Žapps/example/src/App.tsxβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
Video,
3434
Chat,
3535
LiquidGlass,
36+
Pictures,
3637
} from "./Examples";
3738
import { CI, Tests } from "./Tests";
3839
import { HomeScreen } from "./Home";
@@ -71,6 +72,7 @@ const linking: LinkingOptions<StackParamList> = {
7172
SpeedTest: "speedtest",
7273
Video: "video",
7374
Chat: "chat",
75+
Pictures: "pictures",
7476
},
7577
},
7678
prefixes: ["rnskia://"],
@@ -232,6 +234,7 @@ const App = () => {
232234
name="Performance"
233235
component={PerformanceDrawingTest}
234236
/>
237+
<Stack.Screen name="Pictures" component={Pictures} />
235238
</Stack.Navigator>
236239
</NavigationContainer>
237240
</GestureHandlerRootView>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { useEffect } from "react";
2+
import { Canvas, Picture, Skia } from "@shopify/react-native-skia";
3+
import {
4+
useDerivedValue,
5+
useSharedValue,
6+
withRepeat,
7+
withTiming,
8+
} from "react-native-reanimated";
9+
10+
const size = 256;
11+
const n = 20;
12+
13+
const paint = Skia.Paint();
14+
15+
export const Pictures = () => {
16+
const progress = useSharedValue(0);
17+
18+
useEffect(() => {
19+
progress.value = withRepeat(withTiming(1, { duration: 3000 }), -1, true);
20+
}, [progress]);
21+
22+
const picture = useDerivedValue(() => {
23+
"worklet";
24+
const recorder = Skia.PictureRecorder();
25+
const canvas = recorder.beginRecording(Skia.XYWHRect(0, 0, size, size));
26+
const angle = progress.value * Math.PI * 2;
27+
for (let i = 0; i < n; i++) {
28+
const alpha = Math.pow((i + 1) / n, 5) * 200;
29+
const r = ((i + 1) / n) * (size / 4);
30+
const offset = ((i + 1) / n) * (size / 4);
31+
const cx = size / 2 + Math.cos(angle + (i * Math.PI * 2) / n) * offset;
32+
const cy = size / 2 + Math.sin(angle + (i * Math.PI * 2) / n) * offset;
33+
paint.setColor(Skia.Color(`rgba(0, 122, 255, ${alpha / 255})`));
34+
canvas.drawCircle(cx, cy, r, paint);
35+
}
36+
return recorder.finishRecordingAsPicture();
37+
});
38+
39+
return (
40+
<Canvas style={{ flex: 1 }}>
41+
<Picture picture={picture} />
42+
</Canvas>
43+
);
44+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./Pictures";

β€Žapps/example/src/Examples/index.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ export * from "./FrostedCard";
2828
export * from "./SpeedTest";
2929
export * from "./Video";
3030
export * from "./Chat";
31+
export * from "./Pictures";

β€Žapps/example/src/Home/HomeScreen.tsxβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ export const HomeScreen = () => {
122122
/>
123123
<HomeScreenButton title="πŸ“Ή Video" description="Video" route="Video" />
124124
<HomeScreenButton title="πŸ’¬ Chat" description="Chat" route="Chat" />
125+
<HomeScreenButton
126+
title="πŸ–Ό Pictures"
127+
description="Animated circle trail using Pictures"
128+
route="Pictures"
129+
/>
125130
</ScrollView>
126131
);
127132
};

β€Žapps/example/src/types.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export type StackParamList = {
3030
SpeedTest: undefined;
3131
Video: undefined;
3232
Chat: undefined;
33+
Pictures: undefined;
3334
};

0 commit comments

Comments
Β (0)