@@ -26,6 +26,7 @@ function SignPad({
26
26
const [ penColor , setPenColor ] = useState ( "blue" ) ;
27
27
const allColor = [ bluePen , redPen , blackPen ] ;
28
28
const canvasRef = useRef ( null ) ;
29
+ const spanRef = useRef ( null ) ;
29
30
const [ isDefaultSign , setIsDefaultSign ] = useState ( false ) ;
30
31
const [ isTab , setIsTab ] = useState ( "draw" ) ;
31
32
const [ isSignImg , setIsSignImg ] = useState ( "" ) ;
@@ -169,43 +170,46 @@ function SignPad({
169
170
? fontSelect
170
171
: "Fasthand" ;
171
172
172
- // Calculate the width of the text content
173
- const { width, height } = getTextWidth ( textContent , fontfamily ) ;
174
- // Increase pixel ratio for higher resolution
175
- const pixelRatio = window . devicePixelRatio || 1 ;
176
- // Create a canvas with the calculated width
177
- const canvas = document . createElement ( "canvas" ) ;
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 ) ;
173
+ //creating span for getting text content width
174
+ const span = document . createElement ( "span" ) ;
175
+ span . textContent = textContent ;
176
+ span . style . font = `20px ${ fontfamily } ` ; // here put your text size and font family
177
+ span . style . display = "hidden" ;
178
+ document . body . appendChild ( span ) ; // Replace 'container' with the ID of the container element
183
179
180
+ //create canvas to render text in canvas and convert in image
181
+ const canvasElement = document . createElement ( "canvas" ) ;
184
182
// Draw the text content on the canvas
185
- const context = canvas . getContext ( "2d" ) ;
183
+ const ctx = canvasElement . getContext ( "2d" ) ;
184
+ const pixelRatio = window . devicePixelRatio || 1 ;
185
+ const width = span . offsetWidth ;
186
+ const height = span . offsetHeight ;
187
+ setTextWidth ( width ) ;
188
+ setTextHeight ( height ) ;
189
+ const font = span . style [ "font" ] ;
190
+ // Set the canvas dimensions to match the span
191
+ canvasElement . width = width * pixelRatio ;
192
+ canvasElement . height = height * pixelRatio ;
186
193
187
- context . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
188
- context . scale ( pixelRatio , pixelRatio ) ;
189
- context . font = `13px ${ fontfamily } ` ; // You can adjust the font size and style
190
- context . fillText ( textContent , 4 , 10 ) ; // Adjust the y-coordinate as needed
194
+ // Render the content of the span onto the canvas
195
+ // ctx.fillStyle = "white"; // Set the background color of the canvas
196
+ // ctx.fillRect(0, 0, width*pixelRatio, height*pixelRatio);
191
197
198
+ // You can customize text styles if needed
199
+ ctx . font = font ;
200
+ ctx . fillStyle = "black" ; // Set the text color
201
+ ctx . textAlign = "center" ;
202
+ ctx . textBaseline = "middle" ;
203
+ ctx . scale ( pixelRatio , pixelRatio ) ;
204
+ // Draw the content of the span onto the canvas
205
+ ctx . fillText ( span . textContent , width / 2 , height / 2 ) ; // Adjust the x,y-coordinate as needed
206
+ //remove span tag
207
+ document . body . removeChild ( span ) ;
192
208
// Convert the canvas to image data
193
- const dataUrl = canvas . toDataURL ( "image/png" ) ;
209
+ const dataUrl = canvasElement . toDataURL ( "image/png" ) ;
194
210
setSignature ( dataUrl ) ;
195
211
} ;
196
212
197
- //for getting text content width render text in span tag and get width
198
- const getTextWidth = ( text , fontfamily ) => {
199
- const tempSpan = document . createElement ( "span" ) ;
200
- tempSpan . innerText = text ;
201
- tempSpan . style . visibility = "hidden" ;
202
- tempSpan . style . fontFamily = fontfamily ;
203
- document . body . appendChild ( tempSpan ) ;
204
- const width = tempSpan . offsetWidth ;
205
- const height = tempSpan . offsetHeight ;
206
- document . body . removeChild ( tempSpan ) ;
207
- return { width, height } ;
208
- } ;
209
213
return (
210
214
< div >
211
215
{ /*isSignPad */ }
@@ -464,6 +468,7 @@ function SignPad({
464
468
} }
465
469
>
466
470
< span className = "signatureText" > Signature:</ span >
471
+
467
472
< input
468
473
maxLength = { 30 }
469
474
style = { { fontFamily : fontSelect } }
@@ -477,12 +482,14 @@ function SignPad({
477
482
} }
478
483
/>
479
484
</ div >
485
+ { /* <div ref={spanRef}>nwbfmb</div> */ }
480
486
< div className = "fontOptionContainer" >
481
487
{ fontOptions . map ( ( font , ind ) => {
482
488
return (
483
489
< div
484
490
key = { ind }
485
491
style = { {
492
+ cursor : "pointer" ,
486
493
fontFamily : font . value ,
487
494
backgroundColor :
488
495
fontSelect === font . value && "rgb(206 225 247)"
0 commit comments