Skip to content

Commit

Permalink
add hook to listen for user draw and undo events
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshidwivedi committed Jun 20, 2024
1 parent 520467d commit bde2d23
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions modules/room/hooks/Canvas.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { useCallback, useEffect, useState } from "react";
import { socket } from "@/common/lib/socket";
import { useOptions } from "@/common/recoil/options";
import { drawOnUndo } from "../helpers/Canvas.helpers";
import { useUsers } from "@/common/recoil/users";
import usersAtom, { useUsers } from "@/common/recoil/users";
import { useBoardPosition } from "./useBoardPosition";
import { getPos } from "@/common/lib/getPos";
import { useSetRecoilState } from "recoil";


const savedMoves: [number, number][][]= [];
Expand All @@ -15,7 +16,7 @@ export const useDraw = (
blocked: boolean,
handleEnd: () => void
) => {
const users = useUsers();
const users= useUsers();
const options = useOptions();
const [drawing, setDrawing] = useState(false);

Expand All @@ -35,7 +36,7 @@ export const useDraw = (
const handleUndo = useCallback(()=> {
if(ctx){
savedMoves.pop();
//may cause error

socket.emit("undo");

drawOnUndo(ctx, savedMoves, users);
Expand Down Expand Up @@ -92,7 +93,42 @@ export const useDraw = (
handleEndDrawing,
handleDraw,
handleStartDrawing,
handleUndo,
drawing,
}

};
};

//hook to listen for user draw & undo events
export const useSocketDraw = (
ctx: CanvasRenderingContext2D | undefined,
handleEnd: () => void
) => {

const setUsers = useSetRecoilState(usersAtom);

useEffect(()=>{
socket.on("user_draw", (newMoves, options, userId)=> {
if(ctx){
ctx.lineWidth= options.lineWidth;
ctx.strokeStyle= options.lineColor;
ctx.beginPath();

newMoves.forEach(([x,y])=> {
ctx.lineTo(x,y);
});
ctx.stroke();
ctx.closePath();
handleEnd();
setUsers((prevUsers)=> {
const newUsers= {...prevUsers};
newUsers[userId] = [...newUsers[userId], newMoves];
return newUsers;
});
}
});

socket.on
})

}

0 comments on commit bde2d23

Please sign in to comment.