From 5ae7cb3aa87161ffa4eaa1059ef5f96d41e73524 Mon Sep 17 00:00:00 2001 From: Ollie Date: Fri, 11 Oct 2024 14:42:36 +0800 Subject: [PATCH 1/2] Add method intersects in Rect object. --- packages/skia/cpp/api/JsiSkRect.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/skia/cpp/api/JsiSkRect.h b/packages/skia/cpp/api/JsiSkRect.h index d4298cd71f..c1ac4d7a83 100644 --- a/packages/skia/cpp/api/JsiSkRect.h +++ b/packages/skia/cpp/api/JsiSkRect.h @@ -53,9 +53,16 @@ class JsiSkRect : public JsiSkWrappingSharedPtrHostObject { return jsi::Value::undefined(); } + JSI_HOST_FUNCTION(intersects) { + auto otherRect = JsiSkRect::fromValue(runtime, arguments[0]); + bool result = getObject()->intersects(*otherRect); + return jsi::Value(result); + } + JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkRect, setXYWH), JSI_EXPORT_FUNC(JsiSkRect, setLTRB), - JSI_EXPORT_FUNC(JsiSkRect, dispose)) + JSI_EXPORT_FUNC(JsiSkRect, dispose), + JSI_EXPORT_FUNC(JsiSkRect, intersects)) /** Constructor From fbb83189d02f4112ae97ce0ababfc3a13514f7e4 Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 28 Oct 2024 17:05:18 +0800 Subject: [PATCH 2/2] feat: add intersect method types declaration --- packages/skia/src/skia/types/Rect.ts | 6 ++++++ packages/skia/src/skia/web/JsiSkRect.ts | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/packages/skia/src/skia/types/Rect.ts b/packages/skia/src/skia/types/Rect.ts index bf0275a30f..74d3af5372 100644 --- a/packages/skia/src/skia/types/Rect.ts +++ b/packages/skia/src/skia/types/Rect.ts @@ -9,6 +9,7 @@ export interface SkRect { export interface SkHostRect extends SkRect, SkJSIInstance<"Rect"> { setXYWH(x: number, y: number, width: number, height: number): void; + intersects(other: SkRect): boolean; } export const isRect = (def: unknown): def is SkRect => { @@ -24,3 +25,8 @@ export const isRect = (def: unknown): def is SkRect => { } return false; }; + +export const intersects = (rectA: SkRect, rectB: SkRect): boolean =>{ + 'worklet'; + return (rectA as SkHostRect).intersects(rectB); +} \ No newline at end of file diff --git a/packages/skia/src/skia/web/JsiSkRect.ts b/packages/skia/src/skia/web/JsiSkRect.ts index c5fd1cf3e6..784dea7871 100644 --- a/packages/skia/src/skia/web/JsiSkRect.ts +++ b/packages/skia/src/skia/web/JsiSkRect.ts @@ -30,6 +30,16 @@ export class JsiSkRect this.ref[3] = y + height; } + + intersects(other: SkRect): boolean { + return ( + this.x < other.x + other.width && + this.x + this.width > other.x && + this.y < other.y + other.height && + this.y + this.height > other.y + ); + } + get x() { return this.ref[0]; }