22const canvas = document . getElementById ( "whiteboard" ) ;
33const ctx = canvas . getContext ( "2d" ) ;
44
5- // Set canvas to fill available space dynamically
5+ // Canvas setup
66function resizeCanvas ( ) {
7- canvas . width = canvas . parentElement . offsetWidth ;
8- canvas . height = canvas . parentElement . offsetHeight ;
9- }
10- 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 ( ) ;
1118window . addEventListener ( "resize" , resizeCanvas ) ;
1219
1320// Variables for drawing
1421let isDrawing = false ;
1522let brushColor = "#000000" ;
1623let brushSize = 10 ;
17-
18- // Event listeners for drawing
19- canvas . addEventListener ( "mousedown" , startDrawing ) ;
20- canvas . addEventListener ( "mousemove" , draw ) ;
21- canvas . addEventListener ( "mouseup" , stopDrawing ) ;
22- canvas . addEventListener ( "mouseout" , stopDrawing ) ;
24+ let currentTool = "pen" ; // Tracks the active tool (pen, eraser, shapes)
2325
2426// Event listeners for controls
2527document . getElementById ( "color" ) . addEventListener ( "input" , ( e ) => {
@@ -43,43 +45,107 @@ document.getElementById("fullscreen").addEventListener("click", () => {
4345 document . documentElement . requestFullscreen ( ) ;
4446} ) ;
4547
48+ // Side menu tools
49+ document . getElementById ( "pen-tool" ) . addEventListener ( "click" , ( ) => {
50+ currentTool = "pen" ;
51+ canvas . className = "pen" ;
52+ } ) ;
53+
54+ document . getElementById ( "eraser-tool" ) . addEventListener ( "click" , ( ) => {
55+ currentTool = "eraser" ;
56+ canvas . className = "eraser" ;
57+ } ) ;
58+
59+ document . getElementById ( "select-tool" ) . addEventListener ( "click" , ( ) => {
60+ currentTool = "select" ;
61+ canvas . className = "select" ;
62+ } ) ;
63+
64+ document . getElementById ( "draw-line" ) . addEventListener ( "click" , ( ) => {
65+ currentTool = "line" ;
66+ } ) ;
67+
68+ document . getElementById ( "draw-rect" ) . addEventListener ( "click" , ( ) => {
69+ currentTool = "rectangle" ;
70+ } ) ;
71+
72+ document . getElementById ( "draw-circle" ) . addEventListener ( "click" , ( ) => {
73+ currentTool = "circle" ;
74+ } ) ;
75+
4676// Drawing functions
47- function startDrawing ( e ) {
77+ let startX , startY ;
78+
79+ canvas . addEventListener ( "mousedown" , ( e ) => {
4880 isDrawing = true ;
49- ctx . beginPath ( ) ;
50- ctx . moveTo ( e . offsetX , e . offsetY ) ;
51- }
81+ startX = e . offsetX ;
82+ startY = e . offsetY ;
5283
53- function draw ( e ) {
54- if ( ! isDrawing ) return ;
84+ if ( currentTool === "pen" || currentTool === "eraser" ) {
85+ ctx . beginPath ( ) ;
86+ ctx . moveTo ( startX , startY ) ;
87+ }
88+ } ) ;
5589
56- ctx . lineWidth = brushSize ;
57- ctx . lineCap = "round" ;
58- ctx . strokeStyle = brushColor ;
90+ canvas . addEventListener ( "mousemove" , ( e ) => {
91+ if ( ! isDrawing ) return ;
5992
60- ctx . lineTo ( e . offsetX , e . offsetY ) ;
61- ctx . stroke ( ) ;
62- }
93+ if ( currentTool === "pen" ) {
94+ ctx . lineWidth = brushSize ;
95+ ctx . lineCap = "round" ;
96+ ctx . strokeStyle = brushColor ;
97+ ctx . lineTo ( e . offsetX , e . offsetY ) ;
98+ ctx . stroke ( ) ;
99+ } else if ( currentTool === "eraser" ) {
100+ ctx . lineWidth = brushSize ;
101+ ctx . lineCap = "round" ;
102+ ctx . strokeStyle = "#ffffff" ; // Eraser color
103+ ctx . lineTo ( e . offsetX , e . offsetY ) ;
104+ ctx . stroke ( ) ;
105+ }
106+ } ) ;
63107
64- function stopDrawing ( ) {
108+ canvas . addEventListener ( "mouseup" , ( e ) => {
109+ if ( ! isDrawing ) return ;
65110 isDrawing = false ;
66- ctx . closePath ( ) ;
67- }
68111
69- // Clear the canvas
112+ if ( currentTool === "line" ) {
113+ ctx . lineWidth = brushSize ;
114+ ctx . strokeStyle = brushColor ;
115+ ctx . beginPath ( ) ;
116+ ctx . moveTo ( startX , startY ) ;
117+ ctx . lineTo ( e . offsetX , e . offsetY ) ;
118+ ctx . stroke ( ) ;
119+ } else if ( currentTool === "rectangle" ) {
120+ const rectWidth = e . offsetX - startX ;
121+ const rectHeight = e . offsetY - startY ;
122+ ctx . lineWidth = brushSize ;
123+ ctx . strokeStyle = brushColor ;
124+ ctx . strokeRect ( startX , startY , rectWidth , rectHeight ) ;
125+ } else if ( currentTool === "circle" ) {
126+ const radius = Math . sqrt (
127+ Math . pow ( e . offsetX - startX , 2 ) + Math . pow ( e . offsetY - startY , 2 )
128+ ) ;
129+ ctx . lineWidth = brushSize ;
130+ ctx . strokeStyle = brushColor ;
131+ ctx . beginPath ( ) ;
132+ ctx . arc ( startX , startY , radius , 0 , Math . PI * 2 ) ;
133+ ctx . stroke ( ) ;
134+ }
135+ } ) ;
136+
137+ // Utility functions
70138function clearCanvas ( ) {
71139 ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
72140}
73141
74- // Save the canvas as an image
75142function saveImage ( ) {
76143 const link = document . createElement ( "a" ) ;
77144 link . download = "whiteboard.png" ;
78145 link . href = canvas . toDataURL ( "image/png" ) ;
79146 link . click ( ) ;
80147}
81148
82- // Upload an image to the canvas
83149function uploadImage ( e ) {
84150 const file = e . target . files [ 0 ] ;
85151 if ( ! file ) return ;
0 commit comments