Skip to content

Commit 5b88808

Browse files
wcandillonchrfalch
andauthored
Fix example regressions with float rect values (#500)
Co-authored-by: Christian Falch <[email protected]>
1 parent 1ffc273 commit 5b88808

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

example/src/Examples/Aurora/Aurora.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { CoonsPatchMeshGradient } from "./components/CoonsPatchMeshGradient";
44

55
export const Aurora = () => {
66
return (
7-
<CoonsPatchMeshGradient rows={3} cols={3} colors={palette.otto} play />
7+
<CoonsPatchMeshGradient
8+
rows={3}
9+
cols={3}
10+
colors={palette.otto}
11+
lines
12+
handles
13+
/>
814
);
915
};
1016

example/src/Examples/Aurora/components/CoonsPatchMeshGradient.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type {
44
SkiaReadonlyValue,
55
} from "@shopify/react-native-skia";
66
import {
7+
Skia,
8+
isEdge,
79
Group,
810
useClockValue,
911
add,
@@ -24,6 +26,7 @@ import { Curves } from "./Curves";
2426
import { useHandles } from "./useHandles";
2527

2628
const { width, height } = Dimensions.get("window");
29+
const window = Skia.XYWHRect(0, 0, width, height);
2730

2831
const rectToTexture = (
2932
vertices: CubicBezierHandle[],
@@ -130,12 +133,7 @@ export const CoonsPatchMeshGradient = ({
130133
.flat();
131134
const meshNoise = useDerivedValue(() => {
132135
return defaultMesh.map((pt, i) => {
133-
const isEdge =
134-
pt.pos.x === 0 ||
135-
pt.pos.y === 0 ||
136-
pt.pos.x === Math.fround(width) ||
137-
pt.pos.y === Math.fround(height);
138-
if (isEdge) {
136+
if (isEdge(pt.pos, window)) {
139137
return pt;
140138
}
141139
const noisePos = new SimplexNoise(`${i}-pos`);
@@ -169,7 +167,7 @@ export const CoonsPatchMeshGradient = ({
169167

170168
const meshGesture = useValue(defaultMesh);
171169

172-
const onTouch = useHandles(meshGesture, defaultMesh, width, height);
170+
const onTouch = useHandles(meshGesture, defaultMesh, window);
173171
const mesh = play ? meshNoise : meshGesture;
174172
if (image === null) {
175173
return null;
@@ -193,9 +191,7 @@ export const CoonsPatchMeshGradient = ({
193191
})}
194192
</Group>
195193
{defaultMesh.map(({ pos }, index) => {
196-
const edge =
197-
pos.x === 0 || pos.y === 0 || pos.x === width || pos.y === height;
198-
if (edge || !handles) {
194+
if (isEdge(pos, window) || !handles) {
199195
return null;
200196
}
201197
return (

example/src/Examples/Aurora/components/useHandles.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
import type { SkiaValue, CubicBezierHandle } from "@shopify/react-native-skia";
2-
import { sub, useTouchHandler, useValue } from "@shopify/react-native-skia";
1+
import type {
2+
SkiaValue,
3+
CubicBezierHandle,
4+
SkRect,
5+
} from "@shopify/react-native-skia";
6+
import {
7+
isEdge,
8+
sub,
9+
useTouchHandler,
10+
useValue,
11+
} from "@shopify/react-native-skia";
312

413
import { inRadius, symmetric } from "./Math";
514

@@ -11,8 +20,7 @@ type TouchSelection = null | {
1120
export const useHandles = (
1221
mesh: SkiaValue<CubicBezierHandle[]>,
1322
defaultMesh: CubicBezierHandle[],
14-
width: number,
15-
height: number
23+
window: SkRect
1624
) => {
1725
const selection = useValue<TouchSelection>(null);
1826
return useTouchHandler({
@@ -34,9 +42,7 @@ export const useHandles = (
3442
}
3543
} else {
3644
defaultMesh.every(({ pos: p }, index) => {
37-
const edge =
38-
p.x === 0 || p.y === 0 || p.x === width || p.y === height;
39-
if (!edge) {
45+
if (!isEdge(p, window)) {
4046
const { pos, c1, c2 } = mesh.current[index];
4147
const c3 = symmetric(c1, pos);
4248
const c4 = symmetric(c2, pos);

example/src/Examples/Vertices/Vertices.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import {
77
useDerivedValue,
88
Vertices,
99
useImage,
10+
Skia,
11+
isEdge,
1012
} from "@shopify/react-native-skia";
1113
import cdt2d from "cdt2d";
1214
import SimplexNoise from "simplex-noise";
1315

1416
import "./cdt2d.d";
1517

1618
const { width, height } = Dimensions.get("window");
19+
const window = Skia.XYWHRect(0, 0, width, height);
1720
const N = 3;
1821
const n = new Array(N + 1).fill(0).map((_, i) => i);
1922
const hSize = width / N;
@@ -34,15 +37,14 @@ export const Demo = () => {
3437
const clock = useClockValue();
3538
const vertices = useDerivedValue(
3639
() =>
37-
defaultVertices.map(({ x, y }, i) => {
38-
const isEdge = x === 0 || y === 0 || x === width || y === height;
39-
if (isEdge) {
40-
return { x, y };
40+
defaultVertices.map((vertex, i) => {
41+
if (isEdge(vertex, window)) {
42+
return vertex;
4143
}
4244
const noise = new SimplexNoise(i);
4345
return {
44-
x: x + AX * noise.noise2D(clock.current / F, 0),
45-
y: y + AY * noise.noise2D(0, clock.current / F),
46+
x: vertex.x + AX * noise.noise2D(clock.current / F, 0),
47+
y: vertex.y + AY * noise.noise2D(0, clock.current / F),
4648
};
4749
}),
4850
[clock]

package/src/renderer/processors/Rects.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { SkRect, SkRRect } from "../../skia";
33
import { Skia } from "../../skia";
44

5+
import type { Vector } from "./math/Vector";
56
import { vec } from "./math/Vector";
67
import type { Radius } from "./Radius";
78
import { processRadius } from "./Radius";
@@ -22,6 +23,9 @@ export const bounds = (rects: SkRect[]) => {
2223
return rect(x, y, width, height);
2324
};
2425

26+
export const isEdge = (pos: Vector, b: SkRect) =>
27+
pos.x === b.x || pos.y === b.y || pos.x === b.width || pos.y === b.height;
28+
2529
export const topLeft = (r: SkRect | SkRRect) =>
2630
isRRect(r) ? vec(r.rect.x, r.rect.y) : vec(r.x, r.y);
2731
export const topRight = (r: SkRect | SkRRect) =>

0 commit comments

Comments
 (0)