Skip to content

Commit

Permalink
implement MousePosition component to track, emit mouse movements
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshidwivedi committed Jun 20, 2024
1 parent c6dea34 commit d89e2d0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions modules/room/components/MousePosition.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useRef } from "react";
import { useBoardPosition } from "../hooks/useBoardPosition";
import { useInterval, useMouse } from "react-use";
import {socket} from "@/common/lib/socket";
import {motion} from "framer-motion";

export const MousePosition = () => {
//setting initial values as 0
const prevPosition = useRef<{x:number, y:number}>({x:0, y:0});
const {x,y} = useBoardPosition();
const ref= useRef<HTMLDivElement>(null);

const {docX, docY} = useMouse(ref);

useInterval(()=>{
if(prevPosition.current.x !== docX || prevPosition.current.y !== docY){
socket.emit("mouse_move", docX- x.get(), docY- y.getY());
prevPosition.current = {x:docX, y:docY};
}
}, 300);

return (
<motion.div
ref={ref}
className="absolute top-2 left-0 z-50 select-none"
animate={{x:docX+15, y:docY+15}}
transition={{duration:0.05, ease:"linear"}}
>
{docX - x.get()} | {docY - y.get()}
</motion.div>
)

}

0 comments on commit d89e2d0

Please sign in to comment.