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 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]; }