-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
final chat,undo and eraser functionality added
- Loading branch information
1 parent
dc06600
commit 3479e05
Showing
5 changed files
with
51 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,46 @@ | ||
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<CANVAS_SIZE.height;i+=25){ | ||
ctx.beginPath(); | ||
ctx.moveTo(0,i); | ||
ctx.lineTo(CANVAS_SIZE.width,i); | ||
ctx.stroke(); | ||
} | ||
for(let i=0;i<CANVAS_SIZE.width;i+=25){ | ||
ctx.beginPath(); | ||
ctx.moveTo(i,0); | ||
ctx.lineTo(i, CANVAS_SIZE.height); | ||
ctx.stroke(); | ||
} | ||
ctx.stroke(); | ||
ctx.closePath(); | ||
|
||
ctx.globalCompositeOperation= "source-over"; | ||
|
||
}; | ||
|
||
|
||
|
||
export const drawAllMoves = ( | ||
ctx: CanvasRenderingContext2D, | ||
room: ClientRoom | ||
) => { | ||
const {usersMoves, movesWithoutUser, myMoves} = room; | ||
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); | ||
|
||
drawBackground(ctx); | ||
const moves= [...movesWithoutUser, ...myMoves]; | ||
|
||
movesWithoutUser.forEach((move)=>{ | ||
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); | ||
}) | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters