From 153c5db4b518939f91e7b3460d82dd938a2c16a6 Mon Sep 17 00:00:00 2001 From: RaktimaNXG Date: Wed, 3 Jan 2024 16:17:32 +0530 Subject: [PATCH] fix: input signature image height issue and limit 30 character of input text signature --- .../src/Component/component/signPad.js | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/microfrontends/SignDocuments/src/Component/component/signPad.js b/microfrontends/SignDocuments/src/Component/component/signPad.js index 66355fd05..11b21e935 100644 --- a/microfrontends/SignDocuments/src/Component/component/signPad.js +++ b/microfrontends/SignDocuments/src/Component/component/signPad.js @@ -31,6 +31,7 @@ function SignPad({ const [isSignImg, setIsSignImg] = useState(""); const [signValue, setSignValue] = useState(""); const [textWidth, setTextWidth] = useState(null); + const [textHeight, setTextHeight] = useState(null); const fontOptions = [ { value: "Fasthand" }, { value: "Dancing Script" }, @@ -96,7 +97,7 @@ function SignPad({ } else { if (isTab === "type") { setIsSignImg(""); - onSaveSign(false, textWidth, 25); + onSaveSign(false, textWidth, textHeight); } else { setIsSignImg(""); onSaveSign(); @@ -155,28 +156,30 @@ function SignPad({ canvasRef.current.fromDataURL(isSignImg); } if (isTab === "type") { - convertToImg(); + convertToImg(fontSelect, signValue); } }, [isTab]); //function for convert input text value in image - const convertToImg = async (fontStyle) => { + const convertToImg = async (fontStyle, text) => { //get text content to convert in image - const textContent = signValue; + const textContent = text; const fontfamily = fontStyle ? fontStyle : fontSelect ? fontSelect : "Fasthand"; - console.log("font family", fontfamily); + // Calculate the width of the text content - const textWidth = getTextWidth(textContent, fontfamily); + const { width, height } = getTextWidth(textContent, fontfamily); // Increase pixel ratio for higher resolution const pixelRatio = window.devicePixelRatio || 1; // Create a canvas with the calculated width const canvas = document.createElement("canvas"); - canvas.width = textWidth * pixelRatio + 10 * pixelRatio; - canvas.height = 20 * pixelRatio; // You can adjust the height as needed - setTextWidth(textWidth * pixelRatio); + canvas.width = width * 2; + canvas.height = height; + // canvas.height = height; // You can adjust the height as needed + setTextWidth(width); + setTextHeight(25); // Draw the text content on the canvas const context = canvas.getContext("2d"); @@ -184,11 +187,10 @@ function SignPad({ context.clearRect(0, 0, canvas.width, canvas.height); context.scale(pixelRatio, pixelRatio); context.font = `13px ${fontfamily}`; // You can adjust the font size and style - context.fillText(textContent, 0, 10); // Adjust the y-coordinate as needed + context.fillText(textContent, 4, 10); // Adjust the y-coordinate as needed // Convert the canvas to image data const dataUrl = canvas.toDataURL("image/png"); - setSignature(dataUrl); }; @@ -199,9 +201,10 @@ function SignPad({ tempSpan.style.visibility = "hidden"; tempSpan.style.fontFamily = fontfamily; document.body.appendChild(tempSpan); - const width = tempSpan.getBoundingClientRect().width; + const width = tempSpan.offsetWidth; + const height = tempSpan.offsetHeight; document.body.removeChild(tempSpan); - return width; + return { width, height }; }; return (
@@ -228,7 +231,7 @@ function SignPad({ > {isStamp ? ( - Upload Image + Upload stamp image ) : ( <> @@ -462,7 +465,7 @@ function SignPad({ > Signature: { setSignValue(e.target.value); - convertToImg(); + convertToImg(fontSelect, e.target.value); }} />
@@ -486,7 +489,7 @@ function SignPad({ }} onClick={() => { setFontSelect(font.value); - convertToImg(font.value); + convertToImg(font.value, signValue); }} >