Skip to content

Commit 992ce00

Browse files
committed
Refine deprecations
1 parent 9e7c601 commit 992ce00

File tree

9 files changed

+47
-31
lines changed

9 files changed

+47
-31
lines changed

docs/docs/shapes/pictures.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ import {
2525
BlendMode
2626
} from "@shopify/react-native-skia";
2727

28-
const size = 256;
29-
3028
export const HelloWorld = () => {
3129
// Create a picture
3230
const picture = useMemo(() => createPicture(
33-
{ width: size, height: size },
3431
(canvas) => {
32+
const size = 256;
3533
const r = 0.33 * size;
3634
const paint = Skia.Paint();
3735
paint.setBlendMode(BlendMode.Multiply);
@@ -73,7 +71,6 @@ import {
7371
export const PictureExample = () => {
7472
// Create picture
7573
const picture = useMemo(() => createPicture(
76-
{ width: 100, height: 100 },
7774
(canvas) => {
7875
const paint = Skia.Paint();
7976
paint.setColor(Skia.Color("pink"));
@@ -82,7 +79,8 @@ export const PictureExample = () => {
8279
const circlePaint = Skia.Paint();
8380
circlePaint.setColor(Skia.Color("orange"));
8481
canvas.drawCircle(50, 50, 50, circlePaint);
85-
}
82+
},
83+
{ width: 100, height: 100 },
8684
), []);
8785

8886
// Serialize the picture

example/src/Examples/API/Touch.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ paint.setColor(Skia.Color("cyan"));
3838
paint.setStyle(PaintStyle.Stroke);
3939
paint.setStrokeWidth(10);
4040

41-
const drawTouches = (touches: TouchData[], size: SkSize, colors: string[]) => {
41+
const drawTouches = (touches: TouchData[], colors: string[]) => {
4242
"worklet";
4343
const recorder = Skia.PictureRecorder();
4444
const canvas = recorder.beginRecording(
45-
Skia.XYWHRect(0, 0, size.width, size.height)
45+
Skia.XYWHRect(0, 0, 2_000_000, 2_000_000)
4646
);
4747
touches.forEach((touch) => {
4848
const p = paint.copy();
@@ -54,16 +54,15 @@ const drawTouches = (touches: TouchData[], size: SkSize, colors: string[]) => {
5454

5555
export const Touch = () => {
5656
const picture = useSharedValue<SkPicture>(emptyPicture);
57-
const size = useSharedValue({ width: 0, height: 0 });
5857
const colors = useSharedValue<string[]>([]);
5958
const gesture = Gesture.Native()
6059
.onTouchesDown((event) => {
6160
const start = Math.round(Math.random() * 4);
6261
colors.value = Colors.slice(start, start + event.allTouches.length);
63-
picture.value = drawTouches(event.allTouches, size.value, colors.value);
62+
picture.value = drawTouches(event.allTouches, colors.value);
6463
})
6564
.onTouchesMove((event) => {
66-
picture.value = drawTouches(event.allTouches, size.value, colors.value);
65+
picture.value = drawTouches(event.allTouches, colors.value);
6766
})
6867
.onTouchesUp(() => {
6968
picture.value = emptyPicture;
@@ -72,7 +71,7 @@ export const Touch = () => {
7271
<View style={styles.container}>
7372
<Title>Touch handling</Title>
7473
<View style={{ flex: 1 }}>
75-
<Canvas style={styles.container} onSize={size}>
74+
<Canvas style={styles.container}>
7675
<Fill color="white" />
7776
<Group style="stroke" strokeWidth={8}>
7877
<Picture picture={picture} />

package/cpp/api/JsiSkPictureRecorder.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ class JsiSkPictureRecorder
2727
context, std::make_shared<SkPictureRecorder>()) {}
2828

2929
JSI_HOST_FUNCTION(beginRecording) {
30-
auto rect = JsiSkRect::fromValue(runtime, arguments[0]);
31-
SkRTreeFactory factory;
32-
auto canvas = getObject()->beginRecording(*rect, &factory);
30+
SkCanvas *canvas;
31+
if (count > 0) {
32+
auto rect = JsiSkRect::fromValue(runtime, arguments[0]);
33+
SkRTreeFactory factory;
34+
canvas = getObject()->beginRecording(*rect, &factory);
35+
} else {
36+
SkISize size = SkISize::Make(2'000'000, 2'000'000);
37+
SkRect rect = SkRect::Make(size);
38+
canvas = getObject()->beginRecording(rect, nullptr);
39+
}
3340
return jsi::Object::createFromHostObject(
3441
runtime, std::make_shared<JsiSkCanvas>(getContext(), canvas));
3542
}

package/src/renderer/Canvas.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
FunctionComponent,
1515
} from "react";
1616

17-
import { SkiaDomView, SkiaView } from "../views";
17+
import { SkiaDomView, SkiaPictureView } from "../views";
1818
import { Skia } from "../skia/Skia";
1919
import type { TouchHandler, SkiaBaseViewProps } from "../views";
2020
import type { SkiaValue } from "../values/types";
@@ -109,19 +109,23 @@ export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
109109
/>
110110
);
111111
} else {
112+
// This is for debugging
113+
const recorder = Skia.PictureRecorder();
114+
const canvas = recorder.beginRecording(
115+
Skia.XYWHRect(0, 0, 2_000_000, 2_000_000)
116+
);
117+
const ctx = new JsiDrawingContext(Skia, canvas);
118+
root.dom.render(ctx);
119+
const picture = recorder.finishRecordingAsPicture();
112120
return (
113-
<SkiaView
121+
<SkiaPictureView
114122
// eslint-disable-next-line @typescript-eslint/no-explicit-any
115123
ref={ref as any}
116124
style={style}
117125
mode={mode}
118126
debug={debug}
127+
picture={picture}
119128
onSize={onSize}
120-
onDraw={(canvas, info) => {
121-
onTouch && onTouch(info.touches);
122-
const ctx = new JsiDrawingContext(Skia, canvas);
123-
root.dom.render(ctx);
124-
}}
125129
{...props}
126130
/>
127131
);

package/src/renderer/__tests__/Picture.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const CheckPicture = ({}: EmptyProps) => {
1717
}, [Skia]);
1818
const picture = useMemo(
1919
() =>
20-
createPicture(Skia.XYWHRect(0, 0, r * 2, r * 2), (canvas) => {
20+
createPicture((canvas) => {
2121
const paint = Skia.Paint();
2222
paint.setColor(Skia.Color(color));
2323
canvas.drawCircle(r, r, r, paint);
@@ -45,11 +45,11 @@ const CheckPicture2 = ({}: EmptyProps) => {
4545
}, [Skia]);
4646
const picture = useMemo(
4747
() =>
48-
createPicture(Skia.XYWHRect(0, 0, r * 2, r * 2), (canvas) => {
48+
createPicture((canvas) => {
4949
const paint = Skia.Paint();
5050
paint.setColor(Skia.Color(color));
5151
canvas.drawCircle(r, r, r, paint);
52-
}),
52+
}, Skia.XYWHRect(0, 0, r * 2, r * 2)),
5353
// eslint-disable-next-line react-hooks/exhaustive-deps
5454
[]
5555
);

package/src/skia/core/Picture.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ import { isRect, type SkCanvas, type SkRect, type SkSize } from "../types";
88
* @returns SkPicture
99
*/
1010
export const createPicture = (
11-
rect: SkRect | SkSize,
12-
cb: (canvas: SkCanvas) => void
11+
cb: (canvas: SkCanvas) => void,
12+
rect?: SkRect | SkSize
1313
) => {
1414
const recorder = Skia.PictureRecorder();
1515
const canvas = recorder.beginRecording(
16-
isRect(rect) ? rect : Skia.XYWHRect(0, 0, rect.width, rect.height)
16+
// eslint-disable-next-line no-nested-ternary
17+
rect
18+
? isRect(rect)
19+
? rect
20+
: Skia.XYWHRect(0, 0, rect.width, rect.height)
21+
: undefined
1722
);
1823
cb(canvas);
1924
return recorder.finishRecordingAsPicture();

package/src/skia/types/Picture/PictureRecorder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface SkPictureRecorder {
99
*
1010
* @param bounds - a rect to cull the results.
1111
*/
12-
beginRecording(bounds: SkRect): SkCanvas;
12+
beginRecording(bounds?: SkRect): SkCanvas;
1313

1414
/**
1515
* Returns the captured draw commands as a picture and invalidates the canvas returned earlier.

package/src/skia/web/JsiSkPictureRecorder.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ export class JsiSkPictureRecorder
2020
this.ref.delete();
2121
};
2222

23-
beginRecording(bounds: SkRect) {
23+
beginRecording(bounds?: SkRect) {
2424
return new JsiSkCanvas(
2525
this.CanvasKit,
26-
this.ref.beginRecording(JsiSkRect.fromValue(this.CanvasKit, bounds))
26+
this.ref.beginRecording(
27+
bounds
28+
? JsiSkRect.fromValue(this.CanvasKit, bounds)
29+
: Float32Array.of(0, 0, 2_000_000, 2_000_000)
30+
)
2731
);
2832
}
2933

package/src/values/api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export const ValueApi = {
2626
* for animating Skia: https://shopify.github.io/react-native-skia/docs/animations/animations
2727
*/
2828
createValue<T>(initialValue: T): SkiaMutableValue<T> {
29-
deprecatedWarning();
3029
return SkiaValueApi.createValue(initialValue);
3130
},
3231
/**

0 commit comments

Comments
 (0)