22const canvas = document . getElementById ( "whiteboard" ) ;
33const ctx = canvas . getContext ( "2d" ) ;
44
5-
65// Canvas setup
76function resizeCanvas ( ) {
8- // Save the current canvas content
9- const savedContent = ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
10-
11- // Resize the canvas
12- canvas . width = canvas . parentElement . offsetWidth ;
13- canvas . height = canvas . parentElement . offsetHeight ;
14-
15- // Restore the saved content
16- ctx . putImageData ( savedContent , 0 , 0 ) ;
17- }
18- resizeCanvas ( ) ;
7+ // Save the current canvas content
8+ const savedContent = ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ;
9+
10+ // Resize the canvas
11+ canvas . width = canvas . parentElement . offsetWidth ;
12+ canvas . height = canvas . parentElement . offsetHeight ;
13+
14+ // Restore the saved content
15+ ctx . putImageData ( savedContent , 0 , 0 ) ;
16+ }
17+ resizeCanvas ( ) ;
1918window . addEventListener ( "resize" , resizeCanvas ) ;
2019
2120// Variables for drawing
@@ -49,17 +48,14 @@ document.getElementById("fullscreen").addEventListener("click", () => {
4948// Side menu tools
5049document . getElementById ( "pen-tool" ) . addEventListener ( "click" , ( ) => {
5150 currentTool = "pen" ;
52- canvas . className = "pen" ;
5351} ) ;
5452
5553document . getElementById ( "eraser-tool" ) . addEventListener ( "click" , ( ) => {
5654 currentTool = "eraser" ;
57- canvas . className = "eraser" ;
5855} ) ;
5956
6057document . getElementById ( "select-tool" ) . addEventListener ( "click" , ( ) => {
6158 currentTool = "select" ;
62- canvas . className = "select" ;
6359} ) ;
6460
6561document . getElementById ( "draw-line" ) . addEventListener ( "click" , ( ) => {
@@ -74,6 +70,10 @@ document.getElementById("draw-circle").addEventListener("click", () => {
7470 currentTool = "circle" ;
7571} ) ;
7672
73+ document . getElementById ( "text-tool" ) . addEventListener ( "click" , ( ) => {
74+ currentTool = "text" ;
75+ } ) ;
76+
7777// Drawing functions
7878let startX , startY ;
7979
@@ -132,6 +132,13 @@ canvas.addEventListener("mouseup", (e) => {
132132 ctx . beginPath ( ) ;
133133 ctx . arc ( startX , startY , radius , 0 , Math . PI * 2 ) ;
134134 ctx . stroke ( ) ;
135+ } else if ( currentTool === "text" ) {
136+ const text = prompt ( "Enter the text:" ) ;
137+ if ( text ) {
138+ ctx . fillStyle = brushColor ;
139+ ctx . font = `${ brushSize * 2 } px Arial` ;
140+ ctx . fillText ( text , startX , startY ) ;
141+ }
135142 }
136143} ) ;
137144
0 commit comments