@@ -95,7 +95,7 @@ function SignPad({
95
95
} else {
96
96
if ( isTab === "type" ) {
97
97
setIsSignImg ( "" ) ;
98
- onSaveSign ( false , textWidth , 30 ) ;
98
+ onSaveSign ( false , textWidth , 25 ) ;
99
99
} else {
100
100
setIsSignImg ( "" ) ;
101
101
onSaveSign ( ) ;
@@ -142,37 +142,42 @@ function SignPad({
142
142
}
143
143
} , [ isTab ] ) ;
144
144
//function for convert input text value in image
145
- const convertToImg = async ( ) => {
145
+ const convertToImg = async ( fontStyle ) => {
146
146
//get text content to convert in image
147
147
const textContent = signValue ;
148
+ const fontfamily = fontStyle ? fontStyle : fontSelect ;
148
149
// Calculate the width of the text content
149
- const textWidth = getTextWidth ( textContent ) ;
150
+ const textWidth = getTextWidth ( textContent , fontfamily ) ;
150
151
// Increase pixel ratio for higher resolution
151
152
const pixelRatio = window . devicePixelRatio || 1 ;
152
-
153
153
// Create a canvas with the calculated width
154
154
const canvas = document . createElement ( "canvas" ) ;
155
- canvas . width = textWidth * pixelRatio + 30 * pixelRatio ;
155
+ canvas . width = textWidth * pixelRatio + 10 * pixelRatio ;
156
156
canvas . height = 20 * pixelRatio ; // You can adjust the height as needed
157
157
setTextWidth ( textWidth * pixelRatio ) ;
158
+
158
159
// Draw the text content on the canvas
159
160
const context = canvas . getContext ( "2d" ) ;
161
+
162
+ context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
160
163
context . scale ( pixelRatio , pixelRatio ) ;
161
- context . font = `13px ${ fontSelect } ` ; // You can adjust the font size and style
164
+ context . font = `13px ${ fontfamily } ` ; // You can adjust the font size and style
162
165
context . fillText ( textContent , 0 , 10 ) ; // Adjust the y-coordinate as needed
163
166
164
167
// Convert the canvas to image data
165
168
const dataUrl = canvas . toDataURL ( "image/png" ) ;
169
+
166
170
setSignature ( dataUrl ) ;
167
171
} ;
168
172
169
173
//for getting text content width render text in span tag and get width
170
- const getTextWidth = ( text ) => {
174
+ const getTextWidth = ( text , fontfamily ) => {
171
175
const tempSpan = document . createElement ( "span" ) ;
172
176
tempSpan . innerText = text ;
173
177
tempSpan . style . visibility = "hidden" ;
178
+ tempSpan . style . fontFamily = fontfamily ;
174
179
document . body . appendChild ( tempSpan ) ;
175
- const width = tempSpan . offsetWidth ;
180
+ const width = tempSpan . getBoundingClientRect ( ) . width ;
176
181
document . body . removeChild ( tempSpan ) ;
177
182
return width ;
178
183
} ;
@@ -426,9 +431,16 @@ function SignPad({
426
431
)
427
432
) : isTab === "type" ? (
428
433
< div >
429
- < div style = { { display : "flex" , justifyContent : "space-between" } } >
430
- < span > Signature: </ span >
434
+ < div
435
+ style = { {
436
+ display : "flex" ,
437
+ justifyContent : "space-between" ,
438
+ alignItems : "center"
439
+ } }
440
+ >
441
+ < span className = "signatureText" > Signature:</ span >
431
442
< input
443
+ maxLength = { 15 }
432
444
style = { { fontFamily : fontSelect } }
433
445
type = "text"
434
446
className = "signatureInput"
@@ -450,9 +462,17 @@ function SignPad({
450
462
backgroundColor :
451
463
fontSelect === font . value && "rgb(206 225 247)"
452
464
} }
453
- onClick = { ( ) => setFontSelect ( font . value ) }
465
+ onClick = { ( ) => {
466
+ setFontSelect ( font . value ) ;
467
+ convertToImg ( font . value ) ;
468
+ } }
454
469
>
455
- < div style = { { padding : "5px 10px 5px 10px" } } >
470
+ < div
471
+ style = { {
472
+ padding : "5px 10px 5px 10px" ,
473
+ fontSize : "20px"
474
+ } }
475
+ >
456
476
{ signValue ? signValue : "Your signature" }
457
477
</ div >
458
478
</ div >
0 commit comments