6969let currentColorIndex = 0 ;
7070let isEraser = false ;
7171
72-
7372//brush size pics
7473const brushSources = [
7574"assets/sizeL.webp" , // large
108107"assets/colourGray.webp" , // gray
109108] ;
110109
111- function resizeCanvasToDisplay ( ) {
112- const canvas = document . getElementById ( 'c' ) ;
113- const ctx = canvas . getContext ( '2d' ) ;
114-
115- // Get viewport size
116- const displayWidth = window . innerWidth ;
117- const displayHeight = window . innerHeight ;
118-
119- // Set how much of the screen you want to use (optional)
120- const targetWidth = displayWidth ;
121- const targetHeight = displayHeight ;
122-
123- // Set canvas pixel size (drawing surface)
124- canvas . width = targetWidth ;
125- canvas . height = targetHeight ;
126-
127- // Set canvas display size (CSS)
128- canvas . style . width = targetWidth + 'px' ;
129- canvas . style . height = targetHeight + 'px' ;
130- }
131-
132-
133-
134-
135-
136-
110+ //canvas resize function
111+ function resizeCanvasToDisplay ( ) {
112+ const canvas = document . getElementById ( 'c' ) ;
113+ const ctx = canvas . getContext ( '2d' ) ;
114+ const displayWidth = window . innerWidth ;
115+ const displayHeight = window . innerHeight ;
116+ canvas . width = displayWidth ;
117+ canvas . height = displayHeight ;
118+ canvas . style . width = displayWidth + 'px' ;
119+ canvas . style . height = displayHeight + 'px' ;
120+ }
137121
122+ // color switch function
123+ let colors = [ "black" , "gray" , ] ;
124+ ctx . strokeStyle = colors [ currentColorIndex ] ;
125+ function toggleColor ( ) {
126+ currentColorIndex = ( currentColorIndex + 1 ) % colors . length ;
127+ ctx . strokeStyle = colors [ currentColorIndex ] ;
138128
139- // color switch function
140- let colors = [ "black" , "gray" , ] ;
141- ctx . strokeStyle = colors [ currentColorIndex ] ;
142- function toggleColor ( ) {
143- currentColorIndex = ( currentColorIndex + 1 ) % colors . length ;
144- ctx . strokeStyle = colors [ currentColorIndex ] ;
145-
146- const colorElement = document . getElementById ( 'colorswitch' ) ;
129+ const colorElement = document . getElementById ( 'colorswitch' ) ;
147130 colorElement . src = colorSources [ currentColorIndex ] ;
148- }
131+ }
149132
150133
151134 // Upload function
157140
158141 try {
159142 const res = await fetch ( "https://api.reimage.dev/upload/" , {
160- method : "POST" ,
161- headers : { Authorization : `Bearer ${ API_KEY } ` } ,
162- body : formData
143+ method : "POST" ,
144+ headers : { Authorization : `Bearer ${ API_KEY } ` } ,
145+ body : formData
163146 } ) ;
164147 const data = await res . json ( ) ;
165148 } catch ( err ) {
166149 console . error ( "Upload failed." ) ;
167150 alert ( "Upload failed." ) ;
168151 }
169152 } , "image/png" ) ;
170- const saveElement = document . getElementById ( 'savepic' ) ;
153+ const saveElement = document . getElementById ( 'savepic' ) ;
171154 currentSaveIndex = ( currentSaveIndex + 1 ) % saveSources . length ;
172155 saveElement . src = saveSources [ currentSaveIndex ] ;
173156 if ( currentSaveIndex === 1 ) {
174- setTimeout ( ( ) => {
175- currentSaveIndex = 0 ;
176- saveElement . src = saveSources [ 0 ] ;
177- } , 5000 ) ; }
178- undoStack . push ( ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ) ;
157+ setTimeout ( ( ) => {
158+ currentSaveIndex = 0 ;
159+ saveElement . src = saveSources [ 0 ] ;
160+ } , 5000 ) ; }
161+ undoStack . push ( ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ) ;
179162 if ( undoStack . length > 10 ) undoStack . shift ( ) ; // optional limit
180- ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
181- }
163+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
164+ }
182165
183166 //undo Function
184167 function undo ( ) {
190173 currentDogIndex = ( currentDogIndex + 1 ) % dogSources . length ;
191174 dogElement . src = dogSources [ currentDogIndex ] ;
192175 if ( currentDogIndex === 1 ) {
193- setTimeout ( ( ) => {
194- currentDogIndex = 0 ;
195- dogElement . src = dogSources [ 0 ] ;
196- } , 175 ) ;
197- }
176+ setTimeout ( ( ) => {
177+ currentDogIndex = 0 ;
178+ dogElement . src = dogSources [ 0 ] ;
179+ } , 175 ) ;
180+ }
198181 }
199-
200- // UNDO LIMIT
201- if ( undoStack . length > 3 ) {
202- undoStack . shift ( ) ;
203- }
204182
205183 //clear canvas function
206184 function clearCanvas ( ) {
215193 //confirmation for clearing
216194 function preClear ( ) {
217195 undoStack . push ( ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ) ;
218- if ( undoStack . length > 10 ) undoStack . shift ( ) ; // optional limit
196+ if ( undoStack . length > 10 ) undoStack . shift ( ) ;
219197 const clearElement = document . getElementById ( 'clearState' ) ;
220198 currentClearIndex = ( currentClearIndex + 1 ) % clearSources . length ;
221199 clearElement . src = clearSources [ currentClearIndex ] ;
252230 const dx = newX - lastX ;
253231 const dy = newY - lastY ;
254232 const distance = Math . sqrt ( dx * dx + dy * dy ) ;
255- const speed = distance / ( deltaTime || 1 ) ;
233+ const speed = distance / ( deltaTime * 2 ) ;
256234
257235 const minSize = 0 ;
258236 const velocity = Math . min ( speed , 100 ) ; // clamp max speed
285263
286264 canvas . onmousedown = e => {
287265 undoStack . push ( ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ) ;
288- if ( undoStack . length > 10 ) undoStack . shift ( ) ; // optional limit
266+ if ( undoStack . length > 10 ) undoStack . shift ( ) ;
289267 drawing = true ;
290268 x = e . offsetX ;
291269 y = e . offsetY ;
311289 canvas . addEventListener ( 'touchstart' , e => {
312290 e . preventDefault ( ) ;
313291 undoStack . push ( ctx . getImageData ( 0 , 0 , canvas . width , canvas . height ) ) ;
314- if ( undoStack . length > 10 ) undoStack . shift ( ) ; // optional limit
292+ if ( undoStack . length > 10 ) undoStack . shift ( ) ;
315293 const touch = e . touches [ 0 ] ;
316294 const rect = canvas . getBoundingClientRect ( ) ;
317295 x = touch . clientX - rect . left ;
355333 } ) ;
356334 </ script >
357335 </ body >
358- </ html >
336+ </ html >
0 commit comments