diff --git a/modules/room/components/Canvas.tsx b/modules/room/components/Canvas.tsx index b6daaae..275dd96 100644 --- a/modules/room/components/Canvas.tsx +++ b/modules/room/components/Canvas.tsx @@ -3,7 +3,7 @@ import { useViewportSize } from "@/common/hooks/useViewportSize"; import { useMotionValue, motion } from "framer-motion"; import { useRef, useState, useEffect } from "react"; import { useKeyPressEvent } from "react-use"; -import { useDraw } from "../hooks/Canvas.hooks"; +import { useDraw, useSocketDraw } from "../hooks/Canvas.hooks"; import { socket } from "@/common/lib/socket"; import { drawFromSocket } from "../helpers/Canvas.helpers"; import Minimap from "./Minimap"; @@ -43,11 +43,9 @@ const Canvas = () => { } }; - const { handleDraw, handleEndDrawing, handleStartDrawing, drawing } = useDraw( + const { handleDraw, handleEndDrawing, handleStartDrawing, drawing, handleUndo } = useDraw( ctx, dragging, - -x.get(), - -y.get(), copyCanvasToSmall ); @@ -66,31 +64,12 @@ const Canvas = () => { } }, [dragging]); - useEffect(() => { - let movesToDrawLater: [number, number][] = []; - let optionsToUseLater: CtxOptions = { - lineColor: "", - lineWidth: 0, - }; - socket.on("socket_draw", (movesToDraw, socketOptions) => { - if (ctx && !drawing) { - drawFromSocket(movesToDraw, socketOptions, ctx, copyCanvasToSmall); - } else { - movesToDrawLater = movesToDraw; - optionsToUseLater = socketOptions; - } - }); - - return () => { - socket.off("socket_draw"); - if (movesToDrawLater.length && ctx) { - drawFromSocket(movesToDrawLater, optionsToUseLater, ctx, copyCanvasToSmall); - } - }; - }, [drawing, ctx]); - + useSocketDraw(ctx, copyCanvasToSmall); return ( -
+
+
) diff --git a/modules/room/components/MousePosition.tsx b/modules/room/components/MousePosition.tsx index 4356339..8cdf470 100644 --- a/modules/room/components/MousePosition.tsx +++ b/modules/room/components/MousePosition.tsx @@ -4,6 +4,7 @@ import { useInterval, useMouse } from "react-use"; import { socket } from "@/common/lib/socket"; import { motion } from "framer-motion"; import { BsCursorFill } from "react-icons/bs"; +import { getPos } from "@/common/lib/getPos"; // To track and emit mouse position changes via socket export const MousePosition = () => { @@ -15,7 +16,7 @@ export const MousePosition = () => { useInterval(() => { if (prevPosition.current.x !== docX || prevPosition.current.y !== docY) { - socket.emit("mouse_move", docX - x.get(), docY - y.get()); + socket.emit("mouse_move", getPos(docX,x), getPos(docY, y)); prevPosition.current = { x: docX, y: docY }; } }, 300); @@ -23,12 +24,12 @@ export const MousePosition = () => { return ( - {docX - x.get()},{docY - y.get()} + {getPos(docX, x).toFixed(0)} | {getPos(docY, y).toFixed(0)} ); };