@@ -31,6 +31,7 @@ function SignPad({
31
31
const [ isSignImg , setIsSignImg ] = useState ( "" ) ;
32
32
const [ signValue , setSignValue ] = useState ( "" ) ;
33
33
const [ textWidth , setTextWidth ] = useState ( null ) ;
34
+ const [ textHeight , setTextHeight ] = useState ( null ) ;
34
35
const fontOptions = [
35
36
{ value : "Fasthand" } ,
36
37
{ value : "Dancing Script" } ,
@@ -96,7 +97,7 @@ function SignPad({
96
97
} else {
97
98
if ( isTab === "type" ) {
98
99
setIsSignImg ( "" ) ;
99
- onSaveSign ( false , textWidth , 25 ) ;
100
+ onSaveSign ( false , textWidth , textHeight ) ;
100
101
} else {
101
102
setIsSignImg ( "" ) ;
102
103
onSaveSign ( ) ;
@@ -155,40 +156,41 @@ function SignPad({
155
156
canvasRef . current . fromDataURL ( isSignImg ) ;
156
157
}
157
158
if ( isTab === "type" ) {
158
- convertToImg ( ) ;
159
+ convertToImg ( fontSelect , signValue ) ;
159
160
}
160
161
} , [ isTab ] ) ;
161
162
//function for convert input text value in image
162
- const convertToImg = async ( fontStyle ) => {
163
+ const convertToImg = async ( fontStyle , text ) => {
163
164
//get text content to convert in image
164
- const textContent = signValue ;
165
+ const textContent = text ;
165
166
const fontfamily = fontStyle
166
167
? fontStyle
167
168
: fontSelect
168
169
? fontSelect
169
170
: "Fasthand" ;
170
- console . log ( "font family" , fontfamily ) ;
171
+
171
172
// Calculate the width of the text content
172
- const textWidth = getTextWidth ( textContent , fontfamily ) ;
173
+ const { width , height } = getTextWidth ( textContent , fontfamily ) ;
173
174
// Increase pixel ratio for higher resolution
174
175
const pixelRatio = window . devicePixelRatio || 1 ;
175
176
// Create a canvas with the calculated width
176
177
const canvas = document . createElement ( "canvas" ) ;
177
- canvas . width = textWidth * pixelRatio + 10 * pixelRatio ;
178
- canvas . height = 20 * pixelRatio ; // You can adjust the height as needed
179
- setTextWidth ( textWidth * pixelRatio ) ;
178
+ canvas . width = width * 2 ;
179
+ canvas . height = height ;
180
+ // canvas.height = height; // You can adjust the height as needed
181
+ setTextWidth ( width ) ;
182
+ setTextHeight ( 25 ) ;
180
183
181
184
// Draw the text content on the canvas
182
185
const context = canvas . getContext ( "2d" ) ;
183
186
184
187
context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
185
188
context . scale ( pixelRatio , pixelRatio ) ;
186
189
context . font = `13px ${ fontfamily } ` ; // You can adjust the font size and style
187
- context . fillText ( textContent , 0 , 10 ) ; // Adjust the y-coordinate as needed
190
+ context . fillText ( textContent , 4 , 10 ) ; // Adjust the y-coordinate as needed
188
191
189
192
// Convert the canvas to image data
190
193
const dataUrl = canvas . toDataURL ( "image/png" ) ;
191
-
192
194
setSignature ( dataUrl ) ;
193
195
} ;
194
196
@@ -199,9 +201,10 @@ function SignPad({
199
201
tempSpan . style . visibility = "hidden" ;
200
202
tempSpan . style . fontFamily = fontfamily ;
201
203
document . body . appendChild ( tempSpan ) ;
202
- const width = tempSpan . getBoundingClientRect ( ) . width ;
204
+ const width = tempSpan . offsetWidth ;
205
+ const height = tempSpan . offsetHeight ;
203
206
document . body . removeChild ( tempSpan ) ;
204
- return width ;
207
+ return { width, height } ;
205
208
} ;
206
209
return (
207
210
< div >
@@ -228,7 +231,7 @@ function SignPad({
228
231
>
229
232
{ isStamp ? (
230
233
< span style = { { color : themeColor ( ) } } className = "signTab" >
231
- Upload Image
234
+ Upload stamp image
232
235
</ span >
233
236
) : (
234
237
< >
@@ -462,15 +465,15 @@ function SignPad({
462
465
>
463
466
< span className = "signatureText" > Signature:</ span >
464
467
< input
465
- maxLength = { 15 }
468
+ maxLength = { 30 }
466
469
style = { { fontFamily : fontSelect } }
467
470
type = "text"
468
471
className = "signatureInput"
469
472
placeholder = "Your signature"
470
473
value = { signValue }
471
474
onChange = { ( e ) => {
472
475
setSignValue ( e . target . value ) ;
473
- convertToImg ( ) ;
476
+ convertToImg ( fontSelect , e . target . value ) ;
474
477
} }
475
478
/>
476
479
</ div >
@@ -486,7 +489,7 @@ function SignPad({
486
489
} }
487
490
onClick = { ( ) => {
488
491
setFontSelect ( font . value ) ;
489
- convertToImg ( font . value ) ;
492
+ convertToImg ( font . value , signValue ) ;
490
493
} }
491
494
>
492
495
< div
0 commit comments