import React, { useState, useRef, useEffect } from "react"; import { ShapeType, Size, OrientationType, Thickness, LineType, PlacementType } from "basicprimitives"; import ShapeAnnotationComponent from "../Diagrams/ShapeAnnotationComponent"; const initialPosition = { x: 100, y: 100, width: 200, height: 100 }; export default function ShapeAnnotationComponentSample() { const [position, setPosition] = useState(initialPosition); const dragState = useRef({ dragging: false, resizing: false, startX: 0, startY: 0, origX: 0, origY: 0, origWidth: 0, origHeight: 0, }); function onRectMouseDown(e) { e.preventDefault(); dragState.current = { dragging: true, resizing: false, startX: e.clientX, startY: e.clientY, origX: position.x, origY: position.y, origWidth: position.width, origHeight: position.height, }; window.addEventListener("mousemove", onMouseMove); window.addEventListener("mouseup", onMouseUp); } function onResizerMouseDown(e) { e.stopPropagation(); e.preventDefault(); dragState.current = { dragging: false, resizing: true, startX: e.clientX, startY: e.clientY, origX: position.x, origY: position.y, origWidth: position.width, origHeight: position.height, }; window.addEventListener("mousemove", onMouseMove); window.addEventListener("mouseup", onMouseUp); } function onMouseMove(e) { const ds = dragState.current; const deltaX = e.clientX - ds.startX; const deltaY = e.clientY - ds.startY; if (ds.dragging) { setPosition((r) => ({ ...r, x: ds.origX + deltaX, y: ds.origY + deltaY, })); } else if (ds.resizing) { setPosition((r) => ({ x: r.x, y: r.y, width: Math.max(20, ds.origWidth + deltaX), height: Math.max(20, ds.origHeight + deltaY), })); } } function onMouseUp() { dragState.current.dragging = false; dragState.current.resizing = false; window.removeEventListener("mousemove", onMouseMove); window.removeEventListener("mouseup", onMouseUp); } return (