diff --git a/modules/room/components/Room.tsx b/modules/room/components/Room.tsx index 6930b89..06a5ebf 100644 --- a/modules/room/components/Room.tsx +++ b/modules/room/components/Room.tsx @@ -8,6 +8,7 @@ import { ToolBar } from "./toolbar/ToolBar"; import NameInput from "./NameInput"; import UsersList from "./UsersList"; import { useRef } from "react"; +import Chat from "./chat/Chat"; @@ -25,6 +26,7 @@ const Room = () => { + ); diff --git a/modules/room/components/toolbar/ToolBar.tsx b/modules/room/components/toolbar/ToolBar.tsx index 2eaa472..329030b 100644 --- a/modules/room/components/toolbar/ToolBar.tsx +++ b/modules/room/components/toolbar/ToolBar.tsx @@ -1,22 +1,25 @@ +import { RefObject } from "react"; import ColorPicker from "./ColorPicker"; import Eraser from "./Eraser"; import LineWidthPicker from "./LineWidthPicker"; import { BsFillChatFill, BsFillImageFill, BsThreeDots } from "react-icons/bs"; import { HiOutlineDownload } from "react-icons/hi"; +import { FaUndo } from "react-icons/fa"; -export const ToolBar = () => { +export const ToolBar = ({undoRef}: {undoRef: RefObject}) => { return ( -
+ +
- diff --git a/modules/room/helpers/Canvas.helpers.ts b/modules/room/helpers/Canvas.helpers.ts index 0941c2e..6e23b95 100644 --- a/modules/room/helpers/Canvas.helpers.ts +++ b/modules/room/helpers/Canvas.helpers.ts @@ -1,45 +1,28 @@ -import { CANVAS_SIZE } from "@/common/constants/canvasSize"; export const handleMove = ( move: Move, ctx: CanvasRenderingContext2D, )=>{ const {options, path} = move; - const tempCtx = ctx; + + ctx.lineWidth= options.lineWidth; + ctx.strokeStyle= options.lineColor; - if(tempCtx){ - tempCtx.lineWidth= options.lineWidth; - tempCtx.strokeStyle= options.lineColor; + if(move.eraser) ctx.globalCompositeOperation = "destination-out"; - tempCtx.beginPath(); + ctx.beginPath(); path.forEach(([x,y])=>{ - tempCtx.lineTo(x,y); + ctx.lineTo(x,y); }); - tempCtx.stroke(); - tempCtx.closePath(); - } - }; - - //checks on the canvas - export const drawBackground = (ctx: CanvasRenderingContext2D) => { - ctx.lineWidth = 1; - ctx.strokeStyle = "#ccc"; - - for(let i=0;i{ - handleMove(move,ctx); - }); usersMoves.forEach((userMoves)=>{ - userMoves.forEach((move)=> handleMove(move,ctx)); - }); - - myMoves.forEach((move)=> { - handleMove(move,ctx); + moves.push(...userMoves); }); + moves.sort((a,b)=> a.timestamp - b.timestamp); + moves.forEach((move)=> { + handleMove(move, ctx); + }) }; diff --git a/modules/room/hooks/useDraw.ts b/modules/room/hooks/useDraw.ts index 7c9c743..a870d8f 100644 --- a/modules/room/hooks/useDraw.ts +++ b/modules/room/hooks/useDraw.ts @@ -1,5 +1,4 @@ import { useOptionsValue } from "@/common/recoil/options"; -import { useUsers } from "@/common/recoil/users"; import { useState, useEffect, useCallback} from "react"; import { useBoardPosition } from "./useBoardPosition"; import { socket } from "@/common/lib/socket"; @@ -31,9 +30,20 @@ export const useDraw = ( ctx.lineCap = "round"; ctx.lineWidth = options.lineWidth; ctx.strokeStyle = options.lineColor; + if(options.erase) ctx.globalCompositeOperation = "destination-out"; } }); + useEffect(()=> { + socket.on("your_move", (move)=>{ + handleAddMyMove(move); + }); + + return ()=> { + socket.off("your_move"); + }; + }); + const handleUndo = useCallback(() => { if (ctx) { handleRemoveMyMove(); @@ -82,11 +92,13 @@ export const useDraw = ( const move: Move = { path: tempMoves, - options + options, + timestamp: 0, + eraser: options.erase, }; - handleAddMyMove(move); tempMoves = []; + ctx.globalCompositeOperation= "source-over" socket.emit("draw", move); diff --git a/server/index.ts b/server/index.ts index e095d6d..ccd6fad 100644 --- a/server/index.ts +++ b/server/index.ts @@ -120,9 +120,11 @@ nextApp.prepare().then(async () => { socket.on("draw", (move) => { const roomId = getRoomId(); - addMove(roomId, socket.id, move); - socket.broadcast.to(roomId).emit("user_draw", move, socket.id); + const timestamp= Date.now(); + addMove(roomId, socket.id, {...move, timestamp}); + io.to(socket.id).emit("your_move", {...move, timestamp}); + socket.broadcast.to(roomId).emit("user_draw", {...move,timestamp}, socket.id); }); socket.on("undo", () => { @@ -132,6 +134,10 @@ nextApp.prepare().then(async () => { socket.broadcast.to(roomId).emit("user_undo", socket.id); }); + socket.on("send_msg", (msg)=> { + io.to(getRoomId()).emit("new_msg", socket.id, msg); + }); + socket.on("mouse_move", (x, y) => {