From c7186ab940119d8b366215c7348f7b87801bfefc Mon Sep 17 00:00:00 2001 From: Deveshi Dwivedi Date: Wed, 19 Jun 2024 01:28:50 +0530 Subject: [PATCH] update minimap to draw correctly --- modules/canvas/components/Minimap.tsx | 89 ++++++++++++++------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/modules/canvas/components/Minimap.tsx b/modules/canvas/components/Minimap.tsx index 7d96bd3..1d3ed1c 100644 --- a/modules/canvas/components/Minimap.tsx +++ b/modules/canvas/components/Minimap.tsx @@ -1,70 +1,75 @@ import { CANVAS_SIZE } from "@/common/constants/canvasSize"; import { useViewportSize } from "@/common/hooks/useViewportSize"; import { MotionValue, useMotionValue } from "framer-motion"; -import {Dispatch, SetStateAction, forwardRef, useEffect, useRef} from "react"; -import {motion} from "framer-motion"; - +import { Dispatch, SetStateAction, forwardRef, useEffect, useRef } from "react"; +import { motion } from "framer-motion"; const Minimap = forwardRef< -HTMLCanvasElement,{ + HTMLCanvasElement, + { x: MotionValue; y: MotionValue; dragging: boolean; setMovedMiniMap: Dispatch>; -} ->(({x, y, dragging, setMovedMiniMap}, ref)=>{ - const containerRef = useRef(null); - const {width, height} = useViewportSize(); + } +>(({ x, y, dragging, setMovedMiniMap }, ref) => { + const containerRef = useRef(null); + const { width, height } = useViewportSize(); + + const miniX = useMotionValue(0); + const miniY = useMotionValue(0); + + useEffect(() => { + const updateX = (newX: number) => { + if (!dragging) x.set(-newX * 10); + }; - const miniX = useMotionValue(0); - const miniY = useMotionValue(0); + const updateY = (newY: number) => { + if (!dragging) y.set(-newY * 10); + }; - useEffect(()=>{ - miniX.onChange((newX)=>{ - if(!dragging) x.set(-newX * 10); - }); - miniY.onChange((newY)=>{ - if(!dragging) y.set(-newY * 10); - }); + miniX.onChange(updateX); + miniY.onChange(updateY); - return () =>{ - miniX.clearListeners(); - miniY.clearListeners(); - }; + return () => { + miniX.clearListeners(); + miniY.clearListeners(); + }; + }, [dragging, miniX, miniY, x, y]); - }, [dragging, miniX, miniY, x, y]); -//the mini version of canvas on top right corner -return ( -
- + - + setMovedMiniMap((prev:boolean)=> !prev)} + dragTransition={{ power: 0, timeConstant: 0 }} + onDragEnd={() => setMovedMiniMap((prev: boolean) => !prev)} className="absolute top-0 left-0 cursor-grab border-2 border-red-500" style={{ - width: width / 10, - height: height / 10, - x: miniX, - y: miniY, + width: width / 10, + height: height / 10, + x: miniX, + y: miniY, }} - animate={{x: x.get() / -10, y: y.get() / -10}} - transition={{duration: 0.1}} - > - + />
-) + ); }); Minimap.displayName = "Minimap"; -export default Minimap; \ No newline at end of file +export default Minimap;