9191 margin-top : auto;
9292 box-shadow : 0 2px 4px rgba (0 , 0 , 0 , 0.1 );
9393 }
94+ .tools {
95+ margin-bottom : 15px ;
96+ display : flex;
97+ gap : 10px ;
98+ align-items : center;
99+ }
100+
101+ .tool-button {
102+ padding : 5px 15px ;
103+ background-color : # 2d6e7f ;
104+ color : white;
105+ border : none;
106+ border-radius : 5px ;
107+ cursor : pointer;
108+ }
109+
110+ .tool-button .active {
111+ background-color : # 4CAF50 ;
112+ }
113+
114+ # canvas {
115+ border : 1px solid # ccc ;
116+ border-radius : 5px ;
117+ background-color : white;
118+ display : none;
119+ }
120+
121+ .color-picker {
122+ margin-left : 10px ;
123+ }
94124 </ style >
95125</ head >
96126< body >
@@ -105,7 +135,15 @@ <h1>Study Notes</h1>
105135 </ nav >
106136 < div class ="container ">
107137 < h2 > Your Notes</ h2 >
138+ < div class ="tools ">
139+ < button class ="tool-button active " onclick ="switchMode('text') "> Text Mode</ button >
140+ < button class ="tool-button " onclick ="switchMode('draw') "> Draw Mode</ button >
141+ < button class ="tool-button " onclick ="setTool('pen') "> Pen</ button >
142+ < button class ="tool-button " onclick ="setTool('eraser') "> Eraser</ button >
143+ < input type ="color " class ="color-picker " onchange ="setColor(this.value) " value ="#000000 ">
144+ </ div >
108145 < textarea id ="notes " placeholder ="Write your notes here... "> </ textarea >
146+ < canvas id ="canvas " width ="800 " height ="300 "> </ canvas >
109147 < div class ="button-container ">
110148 < button class ="button " onclick ="saveNotes() "> Save Notes</ button >
111149 < button class ="button " onclick ="clearNotes() "> Clear Notes</ button >
@@ -116,20 +154,105 @@ <h2>Your Notes</h2>
116154 </ footer >
117155
118156 < script >
157+ let isDrawing = false ;
158+ let currentTool = 'pen' ;
159+ let currentColor = '#000000' ;
160+ const canvas = document . getElementById ( 'canvas' ) ;
161+ const ctx = canvas . getContext ( '2d' ) ;
162+ const textarea = document . getElementById ( 'notes' ) ;
163+
119164 // Load saved notes from local storage
120165 document . addEventListener ( "DOMContentLoaded" , function ( ) {
121- document . getElementById ( "notes" ) . value = localStorage . getItem ( "userNotes" ) || "" ;
166+ const savedText = localStorage . getItem ( "userNotes" ) || "" ;
167+ const savedDrawing = localStorage . getItem ( "userDrawing" ) ;
168+
169+ textarea . value = savedText ;
170+
171+ if ( savedDrawing ) {
172+ const img = new Image ( ) ;
173+ img . onload = function ( ) {
174+ ctx . drawImage ( img , 0 , 0 ) ;
175+ }
176+ img . src = savedDrawing ;
177+ }
122178 } ) ;
123179
180+ function switchMode ( mode ) {
181+ const buttons = document . querySelectorAll ( '.tool-button' ) ;
182+ buttons . forEach ( btn => btn . classList . remove ( 'active' ) ) ;
183+
184+ if ( mode === 'text' ) {
185+ textarea . style . display = 'block' ;
186+ canvas . style . display = 'none' ;
187+ document . querySelector ( 'button[onclick="switchMode(\'text\')"]' ) . classList . add ( 'active' ) ;
188+ } else {
189+ textarea . style . display = 'none' ;
190+ canvas . style . display = 'block' ;
191+ document . querySelector ( 'button[onclick="switchMode(\'draw\')"]' ) . classList . add ( 'active' ) ;
192+ }
193+ }
194+
195+ function setTool ( tool ) {
196+ currentTool = tool ;
197+ const buttons = document . querySelectorAll ( '.tool-button' ) ;
198+ buttons . forEach ( btn => btn . classList . remove ( 'active' ) ) ;
199+ document . querySelector ( `button[onclick="setTool('${ tool } ')"]` ) . classList . add ( 'active' ) ;
200+ }
201+
202+ function setColor ( color ) {
203+ currentColor = color ;
204+ }
205+
206+ canvas . addEventListener ( 'mousedown' , startDrawing ) ;
207+ canvas . addEventListener ( 'mousemove' , draw ) ;
208+ canvas . addEventListener ( 'mouseup' , stopDrawing ) ;
209+ canvas . addEventListener ( 'mouseout' , stopDrawing ) ;
210+
211+ function startDrawing ( e ) {
212+ isDrawing = true ;
213+ draw ( e ) ;
214+ }
215+
216+ function draw ( e ) {
217+ if ( ! isDrawing ) return ;
218+
219+ const rect = canvas . getBoundingClientRect ( ) ;
220+ const x = e . clientX - rect . left ;
221+ const y = e . clientY - rect . top ;
222+
223+ ctx . lineWidth = 2 ;
224+ ctx . lineCap = 'round' ;
225+
226+ if ( currentTool === 'eraser' ) {
227+ ctx . strokeStyle = '#ffffff' ;
228+ ctx . lineWidth = 20 ;
229+ } else {
230+ ctx . strokeStyle = currentColor ;
231+ }
232+
233+ ctx . lineTo ( x , y ) ;
234+ ctx . stroke ( ) ;
235+ ctx . beginPath ( ) ;
236+ ctx . moveTo ( x , y ) ;
237+ }
238+
239+ function stopDrawing ( ) {
240+ isDrawing = false ;
241+ ctx . beginPath ( ) ;
242+ }
243+
124244 function saveNotes ( ) {
125- localStorage . setItem ( "userNotes" , document . getElementById ( "notes" ) . value ) ;
245+ localStorage . setItem ( "userNotes" , textarea . value ) ;
246+ localStorage . setItem ( "userDrawing" , canvas . toDataURL ( ) ) ;
126247 alert ( "Notes saved!" ) ;
127248 }
128249
129250 function clearNotes ( ) {
130251 if ( confirm ( "Are you sure you want to clear your notes?" ) ) {
131- document . getElementById ( "notes" ) . value = "" ;
252+ textarea . value = "" ;
253+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
132254 localStorage . removeItem ( "userNotes" ) ;
255+ localStorage . removeItem ( "userDrawing" ) ;
133256 }
134257 }
135258 </ script >
0 commit comments