@@ -27,8 +27,34 @@ function SignPad({
27
27
const allColor = [ bluePen , redPen , blackPen ] ;
28
28
const canvasRef = useRef ( null ) ;
29
29
const [ isDefaultSign , setIsDefaultSign ] = useState ( false ) ;
30
- const [ isTab , setIsTab ] = useState ( "signature " ) ;
30
+ const [ isTab , setIsTab ] = useState ( "draw " ) ;
31
31
const [ isSignImg , setIsSignImg ] = useState ( "" ) ;
32
+ const [ signValue , setSignValue ] = useState ( "" ) ;
33
+ const [ textWidth , setTextWidth ] = useState ( null ) ;
34
+ const fontOptions = [
35
+ { value : "Lucida Handwriting" } ,
36
+ { value : "Segoe Script" } ,
37
+ { value : "Harrington" } ,
38
+ { value : "cursive" }
39
+
40
+ // Add more font options as needed
41
+ ] ;
42
+ const [ fontSelect , setFontSelect ] = useState ( fontOptions [ 0 ] . value ) ;
43
+
44
+ useEffect ( ( ) => {
45
+ const senderUser =
46
+ localStorage . getItem (
47
+ `Parse/${ localStorage . getItem ( "parseAppId" ) } /currentUser`
48
+ ) &&
49
+ localStorage . getItem (
50
+ `Parse/${ localStorage . getItem ( "parseAppId" ) } /currentUser`
51
+ ) ;
52
+ const jsonSender = JSON . parse ( senderUser ) ;
53
+
54
+ const currentUserName = jsonSender && jsonSender . name ;
55
+ //function for clear signature
56
+ setSignValue ( currentUserName ) ;
57
+ } , [ ] ) ;
32
58
//function for clear signature
33
59
const handleClear = ( ) => {
34
60
if ( canvasRef . current ) {
@@ -68,8 +94,13 @@ function SignPad({
68
94
setIsSignImg ( "" ) ;
69
95
onSaveSign ( isDefaultSign ) ;
70
96
} else {
71
- setIsSignImg ( "" ) ;
72
- onSaveSign ( ) ;
97
+ if ( isTab === "type" ) {
98
+ setIsSignImg ( "" ) ;
99
+ onSaveSign ( false , textWidth , 30 ) ;
100
+ } else {
101
+ setIsSignImg ( "" ) ;
102
+ onSaveSign ( ) ;
103
+ }
73
104
}
74
105
75
106
setPenColor ( "blue" ) ;
@@ -82,13 +113,15 @@ function SignPad({
82
113
setIsImageSelect ( false ) ;
83
114
setIsDefaultSign ( false ) ;
84
115
setImage ( ) ;
85
- setIsTab ( "signature " ) ;
116
+ setIsTab ( "draw " ) ;
86
117
} }
87
118
style = { {
88
119
background : themeColor ( ) ,
89
120
color : "white"
90
121
} }
91
- disabled = { isSignImg || image || isDefaultSign ? false : true }
122
+ disabled = {
123
+ isSignImg || image || isDefaultSign || textWidth ? false : true
124
+ }
92
125
type = "button"
93
126
className = {
94
127
isSignImg || image ? "finishBtn saveBtn" : "disabledFinish saveBtn"
@@ -105,8 +138,46 @@ function SignPad({
105
138
if ( canvasRef . current ) {
106
139
canvasRef . current . fromDataURL ( isSignImg ) ;
107
140
}
141
+ if ( isTab === "type" ) {
142
+ convertToImg ( ) ;
143
+ }
108
144
} , [ isTab ] ) ;
145
+ //function for convert input text value in image
146
+ const convertToImg = async ( ) => {
147
+ //get text content to convert in image
148
+ const textContent = signValue ;
149
+ // Calculate the width of the text content
150
+ const textWidth = getTextWidth ( textContent ) ;
151
+ // Increase pixel ratio for higher resolution
152
+ const pixelRatio = window . devicePixelRatio || 1 ;
153
+
154
+ // Create a canvas with the calculated width
155
+ const canvas = document . createElement ( "canvas" ) ;
156
+ canvas . width = textWidth * pixelRatio + 20 * pixelRatio ;
157
+ canvas . height = 20 * pixelRatio ; // You can adjust the height as needed
158
+ setTextWidth ( textWidth * pixelRatio ) ;
159
+ // Draw the text content on the canvas
160
+ const context = canvas . getContext ( "2d" ) ;
161
+ context . scale ( pixelRatio , pixelRatio ) ;
162
+ context . font = `13px ${ fontSelect } ` ; // You can adjust the font size and style
163
+ context . fillText ( textContent , 0 , 10 ) ; // Adjust the y-coordinate as needed
109
164
165
+ // Convert the canvas to image data
166
+ const dataUrl = canvas . toDataURL ( "image/png" ) ;
167
+
168
+ setSignature ( dataUrl ) ;
169
+ } ;
170
+
171
+ //for getting text content width render text in span tag and get width
172
+ const getTextWidth = ( text ) => {
173
+ const tempSpan = document . createElement ( "span" ) ;
174
+ tempSpan . innerText = text ;
175
+ tempSpan . style . visibility = "hidden" ;
176
+ document . body . appendChild ( tempSpan ) ;
177
+ const width = tempSpan . offsetWidth ;
178
+ document . body . removeChild ( tempSpan ) ;
179
+ return width ;
180
+ } ;
110
181
return (
111
182
< div >
112
183
{ /*isSignPad */ }
@@ -143,22 +214,23 @@ function SignPad({
143
214
onClick = { ( ) => {
144
215
setIsDefaultSign ( false ) ;
145
216
setIsImageSelect ( false ) ;
146
- setIsTab ( "signature " ) ;
217
+ setIsTab ( "draw " ) ;
147
218
setImage ( ) ;
148
219
} }
149
220
style = { {
150
- color : isTab === "signature" ? themeColor ( ) : "#515252" ,
221
+ color : isTab === "draw" ? themeColor ( ) : "#515252" ,
222
+
151
223
marginLeft : "2px"
152
224
} }
153
225
className = "signTab"
154
226
>
155
- Signature
227
+ Draw
156
228
</ span >
157
229
158
230
< div
159
231
style = { {
160
232
border :
161
- isTab === "signature "
233
+ isTab === "draw "
162
234
? "1.5px solid #108783"
163
235
: "1.5px solid #ffffff"
164
236
} }
@@ -187,7 +259,33 @@ function SignPad({
187
259
} }
188
260
> </ div >
189
261
</ div >
262
+ < div >
263
+ < span
264
+ onClick = { ( ) => {
265
+ setIsDefaultSign ( false ) ;
266
+ setIsImageSelect ( false ) ;
267
+ setIsTab ( "type" ) ;
268
+ setImage ( ) ;
269
+ } }
270
+ style = { {
271
+ color : isTab === "type" ? themeColor ( ) : "#515252" ,
272
+
273
+ marginLeft : "2px"
274
+ } }
275
+ className = "signTab"
276
+ >
277
+ Type
278
+ </ span >
190
279
280
+ < div
281
+ style = { {
282
+ border :
283
+ isTab === "type"
284
+ ? "1.5px solid #108783"
285
+ : "1.5px solid #ffffff"
286
+ } }
287
+ > </ div >
288
+ </ div >
191
289
{ defaultSign && (
192
290
< div >
193
291
< span
@@ -234,7 +332,7 @@ function SignPad({
234
332
setIsImageSelect ( false ) ;
235
333
setIsDefaultSign ( false ) ;
236
334
setImage ( ) ;
237
- setIsTab ( "signature " ) ;
335
+ setIsTab ( "draw " ) ;
238
336
} }
239
337
>
240
338
X
@@ -254,6 +352,7 @@ function SignPad({
254
352
alignItems : "center" ,
255
353
marginBottom : 6 ,
256
354
cursor : "pointer"
355
+ // background:'rgb(255, 255, 255)'
257
356
} }
258
357
className = "signatureCanvas"
259
358
>
@@ -262,6 +361,7 @@ function SignPad({
262
361
style = { {
263
362
width : "100%" ,
264
363
height : "100%" ,
364
+ background : "rgb(255, 255, 255)" ,
265
365
objectFit : "contain"
266
366
} }
267
367
src = { defaultSign }
@@ -276,6 +376,7 @@ function SignPad({
276
376
< div
277
377
style = { {
278
378
border : "1px solid black" ,
379
+
279
380
display : "flex" ,
280
381
flexDirection : "column" ,
281
382
justifyContent : "center" ,
@@ -293,6 +394,7 @@ function SignPad({
293
394
accept = "image/*"
294
395
ref = { imageRef }
295
396
hidden
397
+ // style={{ display: "none" }}
296
398
/>
297
399
< i className = "fas fa-cloud-upload-alt uploadImgLogo" > </ i >
298
400
< div className = "uploadImg" > Upload</ div >
@@ -301,32 +403,71 @@ function SignPad({
301
403
< >
302
404
< div
303
405
style = { {
406
+ // position: "relative",
407
+
304
408
border : "1px solid black" ,
305
- display : "flex" ,
306
- flexDirection : "column" ,
307
- justifyContent : "center" ,
308
- alignItems : "center" ,
309
409
marginBottom : 6 ,
310
- cursor : "pointer"
410
+ // justifyContent:"center"
411
+ overflow : "hidden"
311
412
} }
312
413
className = "signatureCanvas"
313
414
>
314
415
< img
315
- alt = "stamp img"
416
+ alt = "print img"
417
+ ref = { imageRef }
418
+ // alt="preview image"
419
+ src = { image . src }
316
420
style = { {
317
- width : "100%" ,
318
- height : "100%" ,
421
+ //overflow:"hidden",
319
422
objectFit : "contain"
320
423
} }
321
- ref = { imageRef }
322
- src = { image . src }
323
424
/>
324
425
</ div >
325
426
< div style = { { display : "flex" , justifyContent : "flex-end" } } >
326
427
< SaveBtn />
327
428
</ div >
328
429
</ >
329
430
)
431
+ ) : isTab === "type" ? (
432
+ < div >
433
+ < div style = { { display : "flex" , justifyContent : "space-between" } } >
434
+ < span > Signature: </ span >
435
+ < input
436
+ style = { { fontFamily : fontSelect } }
437
+ type = "text"
438
+ className = "signatureInput"
439
+ placeholder = "Your signature"
440
+ value = { signValue }
441
+ onChange = { ( e ) => {
442
+ setSignValue ( e . target . value ) ;
443
+ convertToImg ( ) ;
444
+ } }
445
+ />
446
+ </ div >
447
+ < div className = "fontOptionContainer" >
448
+ { fontOptions . map ( ( font , ind ) => {
449
+ return (
450
+ < div
451
+ key = { ind }
452
+ style = { {
453
+ fontFamily : font . value ,
454
+ backgroundColor :
455
+ fontSelect === font . value && "rgb(206 225 247)"
456
+ } }
457
+ onClick = { ( ) => setFontSelect ( font . value ) }
458
+ >
459
+ < div style = { { padding : "5px 10px 5px 10px" } } >
460
+ { signValue ? signValue : "Your signature" }
461
+ </ div >
462
+ </ div >
463
+ ) ;
464
+ } ) }
465
+ </ div >
466
+
467
+ < div style = { { display : "flex" , justifyContent : "flex-end" } } >
468
+ < SaveBtn />
469
+ </ div >
470
+ </ div >
330
471
) : (
331
472
< >
332
473
< SignatureCanvas
@@ -381,9 +522,11 @@ function SignPad({
381
522
width = { 20 }
382
523
height = { 20 }
383
524
/>
525
+ // </button>
384
526
) ;
385
527
} ) }
386
528
</ div >
529
+
387
530
< SaveBtn />
388
531
</ div >
389
532
</ >
0 commit comments