import { useEffect, useRef } from "react"; import { Image as KonvaImage, Group, Rect } from "react-konva"; import Konva from "konva"; import useImage from "use-image"; import type { ShapeProps } from "./types"; import { clampPositionToStage } from "../../utils/stageBounds"; import { createRoundedRectClipFunc, getObjectBorderRadius } from "../../utils/borderRadius"; const ImageObject = ({ obj, isSelected, onSelect, onUpdate, draggable, stageWidth, stageHeight, onImageReady, }: ShapeProps) => { const [image, status] = useImage(obj.imageUrl || ""); const shapeRef = useRef(null); const groupRef = useRef(null); useEffect(() => { if (status === "loaded" && onImageReady) { onImageReady(); } }, [status, onImageReady]); if (!image) return null; const width = obj.width || image.width; const height = obj.height || image.height; const hasStageBounds = stageWidth != null && stageHeight != null; const cornerRadius = getObjectBorderRadius(obj.borderRadius); const clipFunc = createRoundedRectClipFunc(width, height, cornerRadius); const dragBoundFunc = draggable && hasStageBounds ? (pos: { x: number; y: number }) => clampPositionToStage( pos.x, pos.y, width, height, stageWidth!, stageHeight!, ) : undefined; const handleDragEnd = (e: Konva.KonvaEventObject) => { const node = e.target; let x = node.x(); let y = node.y(); if (hasStageBounds) { ({ x, y } = clampPositionToStage( x, y, width, height, stageWidth!, stageHeight!, )); node.position({ x, y }); } onUpdate(obj.id, { x, y }); }; const handleClick = (e: Konva.KonvaEventObject) => { const node = cornerRadius > 0 ? groupRef.current : shapeRef.current; if (node) { onSelect(obj.id, node, e); } }; if (cornerRadius > 0) { return ( {isSelected && ( )} ); } return ( ); }; export default ImageObject;