import { Dimensions, Image, LayoutRectangle, StyleSheet, Text, View, } from "react-native"; import React, { useEffect, useRef, useState } from "react"; import Canvas from "react-native-canvas"; import { BoundingBox } from "../../redux/canvasSlice"; import SafecamCircle from "../atoms/Circle"; import Icon from "react-native-vector-icons/MaterialCommunityIcons"; export interface Hotspot { bboxOfHotspot: BoundingBox; xyp: number[]; xyp_rel: number[]; xypn: number[]; xyp_reln: number[]; xyp_on_screen: number[]; hotspotIndex: number; } interface AnnotationToolProps { boundingBoxData?: BoundingBox[]; clearScreen: boolean; backgroundImageSrc?: string; hideBoundingBoxes?: boolean; onHotspotMark?: (e: (Hotspot | undefined)[]) => void; } const SafecamAnnotationsTool = (props: AnnotationToolProps) => { const canvasRef = useRef(null); const [viewLayout, setViewLayout] = useState(); const [canvasLayout, setCanvasLayout] = useState(); const [ viewLayoutModifiedBoundingBoxData, setViewLayoutModifiedBoundingBoxData, ] = useState([]); const [touchCoordinates, setTouchCoordinates] = useState<{ x: number; y: number; }>(); useEffect(() => { if (props.clearScreen) { setTouchCoordinates(undefined); } }, [props.clearScreen]); const { boundingBoxData = [], backgroundImageSrc = "", onHotspotMark = () => {}, } = props; useEffect(() => { if (canvasLayout) { const updateBoundingBoxes = boundingBoxData.map((box) => { return { ...box, x: box.x * canvasLayout.width, y: box.y * canvasLayout.height, width: box.width * canvasLayout.width, height: box.height * canvasLayout.height, }; }); setViewLayoutModifiedBoundingBoxData(updateBoundingBoxes); } }, [canvasLayout, boundingBoxData]); //* for drawing rectangles useEffect(() => { if (canvasRef.current && canvasLayout && viewLayout) { canvasRef.current.height = canvasLayout.height; canvasRef.current.width = canvasLayout.width; const ctx = canvasRef.current.getContext("2d"); if (viewLayoutModifiedBoundingBoxData) { viewLayoutModifiedBoundingBoxData.forEach((box) => { ctx.strokeStyle = "#FF0000"; ctx.lineWidth = 2; ctx.strokeRect(box.x, box.y, box.width, box.height); }); } // width and height have a unique offset } }, [viewLayoutModifiedBoundingBoxData, canvasLayout, viewLayout]); const renderTouchedCoordinatesAsUI = () => { if (touchCoordinates) { return ( ); } return <>; }; const checkIfPointInBoundingBox = ( locationX: number, locationY: number, boundingBox: BoundingBox ): boolean => { return ( locationX > boundingBox.x && locationX < boundingBox.x + boundingBox.width && locationY > boundingBox.y && locationY < boundingBox.y + boundingBox.height ); }; const handleBoundingBoxTouch = ( locationX: number, locationY: number, bboxOfHotspot: BoundingBox, touchCoordinatesLength: number ) => { if (canvasRef.current) { const xPosOnRealImg = (locationX / canvasRef.current.width) * bboxOfHotspot.image_size[1]; const yPosOnRealImg = (locationY / canvasRef.current.height) * bboxOfHotspot.image_size[0]; const xyp = [xPosOnRealImg, yPosOnRealImg]; let [x, y] = xyp; let xyp_rel = [ x - bboxOfHotspot["xywh"][0], y - bboxOfHotspot["xywh"][1], ]; let [h, w] = bboxOfHotspot["image_size"] || [1080, 1920]; let xypn = [x / w, y / h]; let xyp_reln = [ (x - bboxOfHotspot["xywh"][0]) / w, (y - bboxOfHotspot["xywh"][1]) / h, ]; return { bboxOfHotspot, xyp, xyp_rel, xypn, xyp_reln, xyp_on_screen: [locationX, locationY], hotspotIndex: touchCoordinatesLength, } as Hotspot; } }; return ( setViewLayout(e.nativeEvent.layout)} pointerEvents="box-none" > setCanvasLayout(e.nativeEvent.layout)} pointerEvents="box-only" onTouchStart={(e) => { const { locationX, locationY } = e.nativeEvent; if (locationX && locationY) { const boundingBoxOfHotspot = viewLayoutModifiedBoundingBoxData.filter((box) => checkIfPointInBoundingBox(locationX, locationY, box) ); let hotspotData = boundingBoxOfHotspot.map((boundingBox) => handleBoundingBoxTouch(locationX, locationY, boundingBox, 1) ); if (hotspotData.length > 0) { onHotspotMark(hotspotData); setTouchCoordinates({ x: locationX, y: locationY }); } } }} > {renderTouchedCoordinatesAsUI()} ); }; const styles = StyleSheet.create({ root: { width: "100%", height: "100%", flex: 1, position: "relative", backgroundColor: "black", alignItems: "center", justifyContent: "center", }, canvas: { alignSelf: "center", position: "absolute", height: "100%", top: 0, aspectRatio: 16 / 9, }, }); export default SafecamAnnotationsTool;