Skip to content

Commit a34e956

Browse files
authored
Merge branch 'main' into matrix4
2 parents be178bd + 9c2f039 commit a34e956

File tree

20 files changed

+228
-13
lines changed

20 files changed

+228
-13
lines changed

docs/docs/shapes/pictures.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,54 @@ A Picture renders a previously recorded list of drawing operations on the canvas
1111
| :------ | :---------- | :---------------- |
1212
| picture | `SkPicture` | Picture to render |
1313

14-
### Example
14+
15+
## Hello World
16+
17+
```tsx twoslash
18+
import React, { useMemo } from "react";
19+
import {
20+
createPicture,
21+
Canvas,
22+
Picture,
23+
Skia,
24+
Group,
25+
BlendMode
26+
} from "@shopify/react-native-skia";
27+
28+
const size = 256;
29+
30+
export const HelloWorld = () => {
31+
// Create a picture
32+
const picture = useMemo(() => createPicture(
33+
{ width: size, height: size },
34+
(canvas) => {
35+
const r = 0.33 * size;
36+
const paint = Skia.Paint();
37+
paint.setBlendMode(BlendMode.Multiply);
38+
39+
paint.setColor(Skia.Color("cyan"));
40+
canvas.drawCircle(r, r, r, paint);
41+
42+
paint.setColor(Skia.Color("magenta"));
43+
canvas.drawCircle(size - r, r, r, paint);
44+
45+
paint.setColor(Skia.Color("yellow"));
46+
canvas.drawCircle(size / 2, size - r, r, paint);
47+
}
48+
), []);
49+
return (
50+
<Canvas style={{ flex: 1 }}>
51+
<Picture picture={picture} />
52+
</Canvas>
53+
);
54+
};
55+
```
56+
57+
## Serialization
58+
59+
You can serialize a picture to a byte array.
60+
Serialized pictures are only compatible with the version of Skia it was created with.
61+
You can use serialized pictures with the [Skia debugger](https://skia.org/docs/dev/tools/debugger/).
1562

1663
```tsx twoslash
1764
import React, { useMemo } from "react";
@@ -26,7 +73,7 @@ import {
2673
export const PictureExample = () => {
2774
// Create picture
2875
const picture = useMemo(() => createPicture(
29-
{ x: 0, y: 0, width: 100, height: 100 },
76+
{ width: 100, height: 100 },
3077
(canvas) => {
3178
const paint = Skia.Paint();
3279
paint.setColor(Skia.Color("pink"));
7.64 KB
Loading
883 Bytes
Loading

package/src/animation/spring/runSpring.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { Spring } from "./Spring";
1010
import { createSpringEasing } from "./functions/spring";
1111

1212
/**
13+
* @deprecated Use Reanimated 3 for animations:
14+
* https://shopify.github.io/react-native-skia/docs/animations/animations
15+
*
1316
* Creates a new animation on an existing value that will be driven by
1417
* an animation value. The value will be run from / to the value in
1518
* params and modified by the provided easing curve for the length of

package/src/animation/spring/useSpring.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { Spring } from "./Spring";
1010
import { createSpringEasing } from "./functions/spring";
1111

1212
/**
13+
* @deprecated Use Reanimated 3 for animations:
14+
* https://shopify.github.io/react-native-skia/docs/animations/animations
15+
*
1316
* Creats a spring based animation value that will run whenever
1417
* the animation parameters change.
1518
* @param toOrParams

package/src/animation/timing/runTiming.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import type {
88
import { getResolvedParams } from "./functions";
99
import { createTiming } from "./createTiming";
1010
/**
11+
* @deprecated Use Reanimated 3 for animations:
12+
* https://shopify.github.io/react-native-skia/docs/animations/animations
13+
*
1114
* Creates a new animation on an existing value that will be driven by
1215
* an animation value. The value will be run from / to the value in
1316
* params and modified by the provided easing curve for the length of

package/src/animation/timing/useLoop.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import type { TimingConfig } from "../types";
33
import { useTiming } from "./useTiming";
44

55
/**
6+
* @deprecated Use Reanimated 3 for animations:
7+
* https://shopify.github.io/react-native-skia/docs/animations/animations
8+
*
69
* Configures a looped timing value. The value will go back and forth
710
* between 0 and 1 and back.
811
* @param config Timing configuration for easing and duration

package/src/animation/timing/useTiming.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { getResolvedParams } from "./functions";
1212
import { createTiming } from "./createTiming";
1313

1414
/**
15+
* @deprecated Use Reanimated 3 for animations:
16+
* https://shopify.github.io/react-native-skia/docs/animations/animations
17+
*
1518
* Creats an animation value that will run whenever
1619
* the animation parameters change. The animation start immediately.
1720
* @param toOrParams

package/src/dom/nodes/JsiSkDOM.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ export class JsiSkDOM implements SkDOM {
173173
}
174174

175175
CustomDrawing(props: CustomDrawingNodeProps) {
176+
console.warn(
177+
// eslint-disable-next-line max-len
178+
"The <Drawing /> component is deprecated and will be removed in the next release. For custom drawings, please sure the <Picture /> component: https://shopify.github.io/react-native-skia/docs/shapes/pictures/"
179+
);
176180
return NATIVE_DOM
177181
? global.SkiaDomApi.CustomDrawingNode(props ?? {})
178182
: new CustomDrawingNode(this.ctx, props);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { importSkia, surface } from "../setup";
2+
import { checkImage } from "../../../__tests__/setup";
3+
import { BlendMode } from "../../../skia/types";
4+
5+
describe("Pictures", () => {
6+
it("Should draw a simple picture", async () => {
7+
const { Skia } = importSkia();
8+
const str = await surface.eval((Sk) => {
9+
const size = 256;
10+
const surf = Sk.Surface.MakeOffscreen(size, size)!;
11+
const canvas = surf.getCanvas();
12+
const recorder = Sk.PictureRecorder();
13+
const canvas2 = recorder.beginRecording(Sk.XYWHRect(0, 0, size, size));
14+
canvas2.drawColor(Sk.Color("cyan"));
15+
const picture = recorder.finishRecordingAsPicture();
16+
canvas.drawPicture(picture);
17+
return surf.makeImageSnapshot().encodeToBase64();
18+
});
19+
const image = Skia.Image.MakeImageFromEncoded(Skia.Data.fromBase64(str))!;
20+
checkImage(image, "snapshots/pictures/simple-picture.png");
21+
});
22+
it("Should draw the hello world example as picture", async () => {
23+
const { Skia } = importSkia();
24+
const str = await surface.eval(
25+
(Sk, ctx) => {
26+
const { size } = ctx;
27+
const r = size * 0.33;
28+
const surf = Sk.Surface.MakeOffscreen(size, size)!;
29+
const canvas = surf.getCanvas();
30+
const recorder = Sk.PictureRecorder();
31+
const canvas2 = recorder.beginRecording(Sk.XYWHRect(0, 0, size, size));
32+
const paint = Sk.Paint();
33+
paint.setBlendMode(ctx.BlendMode);
34+
paint.setColor(Sk.Color("cyan"));
35+
canvas2.drawCircle(r, r, r, paint);
36+
paint.setColor(Sk.Color("magenta"));
37+
canvas2.drawCircle(size - r, r, r, paint);
38+
paint.setColor(Sk.Color("yellow"));
39+
canvas2.drawCircle(size / 2, size - r, r, paint);
40+
const picture = recorder.finishRecordingAsPicture();
41+
canvas.drawPicture(picture);
42+
surf.flush();
43+
return surf.makeImageSnapshot().encodeToBase64();
44+
},
45+
{ BlendMode: BlendMode.Multiply, size: surface.width }
46+
);
47+
const image = Skia.Image.MakeImageFromEncoded(Skia.Data.fromBase64(str))!;
48+
checkImage(image, "snapshots/pictures/hello-world.png");
49+
});
50+
});

0 commit comments

Comments
 (0)