Skip to content

Commit c47ff1f

Browse files
committed
Implement review comments
1 parent 099e948 commit c47ff1f

File tree

13 files changed

+49
-26
lines changed

13 files changed

+49
-26
lines changed

example/src/Examples/API/Gradients.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from "react";
22
import { StyleSheet, Dimensions, ScrollView } from "react-native";
3-
import type { SkRect } from "@shopify/react-native-skia";
43
import {
4+
bottomRight,
5+
center,
6+
topLeft,
57
Skia,
68
rect,
79
Canvas,
@@ -32,10 +34,6 @@ const r5 = rect(0, 2 * SIZE, SIZE, SIZE);
3234
const r6 = rect(SIZE, 2 * SIZE, SIZE, SIZE);
3335
const r7 = rect(0, 3 * SIZE, SIZE, SIZE);
3436

35-
const topLeft = (r: SkRect) => vec(r.x, r.y);
36-
const bottomRight = (r: SkRect) => vec(r.x + r.width, r.y + r.height);
37-
const center = (r: SkRect) => vec(r.x + r.width / 2, r.y + r.height / 2);
38-
3937
export const Gradients = () => {
4038
return (
4139
<ScrollView>

package/src/renderer/Canvas.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ReactReconciler from "react-reconciler";
1818

1919
import { SkiaView, useDrawCallback } from "../views";
2020
import type { TouchHandler } from "../views";
21+
import { defaultSkiaPaint } from "../skia/types";
2122
import type { FontMgr } from "../skia/types";
2223
import { useValue } from "../values/hooks/useValue";
2324
import { Skia } from "../skia/Skia";
@@ -26,7 +27,6 @@ import { debug as hostDebug, skHostConfig } from "./HostConfig";
2627
// import { debugTree } from "./nodes";
2728
import { Container } from "./nodes";
2829
import { DependencyManager } from "./DependencyManager";
29-
import { SkiaPaint } from "./processors/Paint";
3030
import { CanvasProvider } from "./useCanvas";
3131

3232
export const skiaReconciler = ReactReconciler(skHostConfig);
@@ -97,7 +97,7 @@ export const Canvas = forwardRef<SkiaView, CanvasProps>(
9797
) {
9898
canvasCtx.size.current = { width, height };
9999
}
100-
const paint = SkiaPaint(Skia);
100+
const paint = defaultSkiaPaint(Skia.Paint());
101101
const ctx = {
102102
width,
103103
height,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ describe("Renderer", () => {
2525
processResult(surface, "snapshots/drawings/lightblue-rect.png");
2626
});
2727
it("Scaled light blue rectangle", () => {
28-
const scaled = size / 2;
28+
const scale = 2;
29+
const scaled = size / scale;
2930
const surface = drawOnNode(
30-
<Group transform={[{ scale: 2 }]} origin={center}>
31+
<Group transform={[{ scale }]} origin={center}>
3132
<Rect
3233
x={(width - scaled) / 2}
3334
y={(width - scaled) / 2}

package/src/renderer/components/Mask.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { ReactNode } from "react";
22
import React, { useMemo } from "react";
33

4-
import { BlendMode } from "../../skia/types";
5-
import { SkiaPaint } from "../processors";
4+
import { BlendMode, defaultSkiaPaint } from "../../skia/types";
65
import { useCanvas } from "../useCanvas";
76

87
import { Group } from "./Group";
@@ -17,15 +16,15 @@ interface MaskProps {
1716
export const Mask = ({ children, mask, mode, clip }: MaskProps) => {
1817
const { Skia } = useCanvas();
1918
const maskPaint = useMemo(() => {
20-
const paint = SkiaPaint(Skia);
19+
const paint = defaultSkiaPaint(Skia.Paint());
2120
paint.setBlendMode(BlendMode.Src);
2221
if (mode === "luminance") {
2322
paint.setColorFilter(Skia.ColorFilter.MakeLumaColorFilter());
2423
}
2524
return paint;
2625
}, [Skia, mode]);
2726
const clippingPaint = useMemo(() => {
28-
const paint = SkiaPaint(Skia);
27+
const paint = defaultSkiaPaint(Skia.Paint());
2928
paint.setBlendMode(BlendMode.DstIn);
3029
return paint;
3130
}, [Skia]);

package/src/renderer/components/Paint.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import React, { useRef, useMemo, forwardRef, useImperativeHandle } from "react";
33

44
import type { SkPaint } from "../../skia/types";
55
import type { CustomPaintProps, AnimatedProps } from "../processors";
6-
import { SkiaPaint, processPaint } from "../processors";
6+
import { processPaint } from "../processors";
77
import { createDeclaration } from "../nodes";
88
import { useCanvas } from "../useCanvas";
9+
import { defaultSkiaPaint } from "../../skia/types";
910

1011
export const usePaintRef = () => useRef<SkPaint>(null);
1112

@@ -16,7 +17,7 @@ export interface PaintProps extends Omit<CustomPaintProps, "paint"> {
1617
export const Paint = forwardRef<SkPaint, AnimatedProps<PaintProps>>(
1718
(props, ref) => {
1819
const { Skia } = useCanvas();
19-
const paint = useMemo(() => SkiaPaint(Skia), [Skia]);
20+
const paint = useMemo(() => defaultSkiaPaint(Skia.Paint()), [Skia]);
2021
useImperativeHandle(ref, () => paint, [paint]);
2122
const onDeclare = useMemo(
2223
() =>

package/src/renderer/components/shapes/Box.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import type {
1010
import { ClipOp, BlurStyle } from "../../../skia/types";
1111
import { createDrawing } from "../../nodes";
1212
import type { AnimatedProps, CustomPaintProps } from "../../processors";
13-
import { isRRect } from "../../processors/Rects";
1413
import { createDeclaration } from "../../nodes/Declaration";
1514
import { processColor } from "../../processors/Color";
15+
import { isRRect } from "../../../skia/types/RRect";
1616

1717
const inflate = (
1818
Skia: Skia,

package/src/renderer/processors/Clips.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { isRRect } from "../../skia/types";
12
import type { SkCanvas, ClipOp, SkRect, SkRRect, Skia } from "../../skia/types";
23

34
import type { PathDef } from "./Paths";
45
import { processPath, isPathDef } from "./Paths";
5-
import { isRRect } from "./Rects";
66

77
export type ClipDef = SkRRect | SkRect | PathDef;
88

package/src/renderer/processors/Paint.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ import type { DeclarationResult } from "../nodes";
1616

1717
import { processColor } from "./Color";
1818
export type SkEnum<T> = Uncapitalize<keyof T extends string ? keyof T : never>;
19-
export const SkiaPaint = (Skia: Skia) => {
20-
const paint = Skia.Paint();
21-
paint.setAntiAlias(true);
22-
return paint;
23-
};
19+
2420
export interface ChildrenProps {
2521
children?: ReactNode | ReactNode[];
2622
}

package/src/renderer/processors/Rects.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ const isRRectCtor = (def: RRectDef): def is RRectCtor =>
1313
// We have an issue to check property existence on JSI backed instances
1414
const isRectCtor = (def: RectDef): def is RectCtor =>
1515
(def as any).rect === undefined;
16-
// We have an issue to check property existence on JSI backed instances
17-
export const isRRect = (def: SkRect | SkRRect): def is SkRRect =>
18-
(def as any).rect !== undefined;
1916

2017
export interface RectCtor {
2118
x: number;

package/src/skia/core/Rect.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Skia } from "../Skia";
2-
import type { SkRect } from "../types";
2+
import type { SkRect, SkRRect } from "../types";
3+
import { isRRect } from "../types";
4+
5+
import { vec } from "./Vector";
36

47
export const rect = (x: number, y: number, width: number, height: number) =>
58
Skia.XYWHRect(x, y, width, height);
@@ -11,3 +14,20 @@ export const bounds = (rects: SkRect[]) => {
1114
const height = Math.max(...rects.map((r) => r.y + r.height));
1215
return rect(x, y, width, height);
1316
};
17+
18+
export const topLeft = (r: SkRect | SkRRect) =>
19+
isRRect(r) ? vec(r.rect.x, r.rect.y) : vec(r.x, r.y);
20+
export const topRight = (r: SkRect | SkRRect) =>
21+
isRRect(r) ? vec(r.rect.x + r.rect.width, r.rect.y) : vec(r.x + r.width, r.y);
22+
export const bottomLeft = (r: SkRect | SkRRect) =>
23+
isRRect(r)
24+
? vec(r.rect.x, r.rect.y + r.rect.height)
25+
: vec(r.x, r.y + r.height);
26+
export const bottomRight = (r: SkRect | SkRRect) =>
27+
isRRect(r)
28+
? vec(r.rect.x + r.rect.width, r.rect.y + r.rect.height)
29+
: vec(r.x + r.width, r.y + r.height);
30+
export const center = (r: SkRect | SkRRect) =>
31+
isRRect(r)
32+
? vec(r.rect.x + r.rect.width / 2, r.rect.y + r.rect.height / 2)
33+
: vec(r.x + r.width / 2, r.y + r.height / 2);

0 commit comments

Comments
 (0)