diff --git a/packages/react-orbit/src/components/orbit.item.tsx b/packages/react-orbit/src/components/orbit.item.tsx index 0a35e34..76287e9 100644 --- a/packages/react-orbit/src/components/orbit.item.tsx +++ b/packages/react-orbit/src/components/orbit.item.tsx @@ -25,7 +25,8 @@ export const OrbitItem = ({ ...rest }: OrbitItemProps) => { const [angle, setAngle] = React.useState(startAngle); - + const orbitItemRef = React.useRef(null); + useEffect(() => { const interval = setInterval(() => { setAngle((prevAngle) => (direction === 'clockwise' ? prevAngle + (step % 360) : prevAngle - (step % 360))); @@ -33,8 +34,8 @@ export const OrbitItem = ({ return () => clearInterval(interval); }, [step, delay]); - - const { x, y } = calculateCoordinatesOnCircle(radius, angle); + + const { x, y } = calculateCoordinatesOnCircle(radius, angle, orbitItemRef.current?.getBoundingClientRect() ?? new DOMRect()); return (
+ ref={orbitItemRef} {children}
); diff --git a/packages/react-orbit/src/utils/geometry.utils.ts b/packages/react-orbit/src/utils/geometry.utils.ts index 9654e32..7164ede 100644 --- a/packages/react-orbit/src/utils/geometry.utils.ts +++ b/packages/react-orbit/src/utils/geometry.utils.ts @@ -1,10 +1,14 @@ const DEGREE_TO_RADIAN_FACTOR = Math.PI / 180; -export const calculateCoordinatesOnCircle = (radius: number, angle: number): { x: number; y: number } => { +export const calculateCoordinatesOnCircle = (radius: number, angle: number, boundingClientRect: DOMRect): { x: number; y: number } => { const angleInRadians = angle * DEGREE_TO_RADIAN_FACTOR; - + const { width, height } = boundingClientRect; + const x = radius + (radius * Math.cos(angleInRadians)); + const y = radius + (radius * Math.sin(angleInRadians)); + const centerX = x - width / 2; + const centerY = y - height / 2; return { - x: radius + (radius * Math.cos(angleInRadians)), - y: radius + (radius * Math.sin(angleInRadians)), + x: centerX, + y: centerY, }; };