Skip to content

Commit 153c5db

Browse files
fix: input signature image height issue and limit 30 character of input text signature
1 parent ac9367a commit 153c5db

File tree

1 file changed

+20
-17
lines changed
  • microfrontends/SignDocuments/src/Component/component

1 file changed

+20
-17
lines changed

microfrontends/SignDocuments/src/Component/component/signPad.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function SignPad({
3131
const [isSignImg, setIsSignImg] = useState("");
3232
const [signValue, setSignValue] = useState("");
3333
const [textWidth, setTextWidth] = useState(null);
34+
const [textHeight, setTextHeight] = useState(null);
3435
const fontOptions = [
3536
{ value: "Fasthand" },
3637
{ value: "Dancing Script" },
@@ -96,7 +97,7 @@ function SignPad({
9697
} else {
9798
if (isTab === "type") {
9899
setIsSignImg("");
99-
onSaveSign(false, textWidth, 25);
100+
onSaveSign(false, textWidth, textHeight);
100101
} else {
101102
setIsSignImg("");
102103
onSaveSign();
@@ -155,40 +156,41 @@ function SignPad({
155156
canvasRef.current.fromDataURL(isSignImg);
156157
}
157158
if (isTab === "type") {
158-
convertToImg();
159+
convertToImg(fontSelect, signValue);
159160
}
160161
}, [isTab]);
161162
//function for convert input text value in image
162-
const convertToImg = async (fontStyle) => {
163+
const convertToImg = async (fontStyle, text) => {
163164
//get text content to convert in image
164-
const textContent = signValue;
165+
const textContent = text;
165166
const fontfamily = fontStyle
166167
? fontStyle
167168
: fontSelect
168169
? fontSelect
169170
: "Fasthand";
170-
console.log("font family", fontfamily);
171+
171172
// Calculate the width of the text content
172-
const textWidth = getTextWidth(textContent, fontfamily);
173+
const { width, height } = getTextWidth(textContent, fontfamily);
173174
// Increase pixel ratio for higher resolution
174175
const pixelRatio = window.devicePixelRatio || 1;
175176
// Create a canvas with the calculated width
176177
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);
180183

181184
// Draw the text content on the canvas
182185
const context = canvas.getContext("2d");
183186

184187
context.clearRect(0, 0, canvas.width, canvas.height);
185188
context.scale(pixelRatio, pixelRatio);
186189
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
188191

189192
// Convert the canvas to image data
190193
const dataUrl = canvas.toDataURL("image/png");
191-
192194
setSignature(dataUrl);
193195
};
194196

@@ -199,9 +201,10 @@ function SignPad({
199201
tempSpan.style.visibility = "hidden";
200202
tempSpan.style.fontFamily = fontfamily;
201203
document.body.appendChild(tempSpan);
202-
const width = tempSpan.getBoundingClientRect().width;
204+
const width = tempSpan.offsetWidth;
205+
const height = tempSpan.offsetHeight;
203206
document.body.removeChild(tempSpan);
204-
return width;
207+
return { width, height };
205208
};
206209
return (
207210
<div>
@@ -228,7 +231,7 @@ function SignPad({
228231
>
229232
{isStamp ? (
230233
<span style={{ color: themeColor() }} className="signTab">
231-
Upload Image
234+
Upload stamp image
232235
</span>
233236
) : (
234237
<>
@@ -462,15 +465,15 @@ function SignPad({
462465
>
463466
<span className="signatureText">Signature:</span>
464467
<input
465-
maxLength={15}
468+
maxLength={30}
466469
style={{ fontFamily: fontSelect }}
467470
type="text"
468471
className="signatureInput"
469472
placeholder="Your signature"
470473
value={signValue}
471474
onChange={(e) => {
472475
setSignValue(e.target.value);
473-
convertToImg();
476+
convertToImg(fontSelect, e.target.value);
474477
}}
475478
/>
476479
</div>
@@ -486,7 +489,7 @@ function SignPad({
486489
}}
487490
onClick={() => {
488491
setFontSelect(font.value);
489-
convertToImg(font.value);
492+
convertToImg(font.value, signValue);
490493
}}
491494
>
492495
<div

0 commit comments

Comments
 (0)