From d101cc4829248cb2b7dc38ce5195e7ee4c534d2a Mon Sep 17 00:00:00 2001 From: Deveshi Dwivedi Date: Thu, 20 Jun 2024 20:10:59 +0530 Subject: [PATCH] handle undo --- modules/room/helpers/Canvas.helpers.ts | 33 +++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/modules/room/helpers/Canvas.helpers.ts b/modules/room/helpers/Canvas.helpers.ts index 4303383..61ca91f 100644 --- a/modules/room/helpers/Canvas.helpers.ts +++ b/modules/room/helpers/Canvas.helpers.ts @@ -13,9 +13,40 @@ export const drawFromSocket = ( tempCtx.beginPath(); socketMoves.forEach(([x,y])=>{ tempCtx.lineTo(x,y); - tempCtx.stroke(); }); + tempCtx.stroke(); tempCtx.closePath(); afterDraw(); } }; + + export const drawOnUndo = ( + ctx: CanvasRenderingContext2D, + savedMoves: [number, number][][], + users: {[key:string]: [number, number][][]} + ) => { + ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); + + Object.values(users).forEach((user)=>{ + user.forEach((userMove)=>{ + ctx.beginPath(); + userMove.forEach(([x,y])=>{ + ctx.lineTo(x,y); + }); + ctx.stroke(); + ctx.closePath(); + }); + }); + + savedMoves.forEach((movesArr)=> { + ctx.beginPath(); + movesArr.forEach(([x,y])=>{ + ctx.lineTo(x,y); + }); + ctx.stroke(); + ctx.closePath(); + }); + + + }; + \ No newline at end of file