Skip to content

Commit 81d0aea

Browse files
fix: text-input signatureissue
1 parent 153c5db commit 81d0aea

File tree

1 file changed

+36
-29
lines changed
  • microfrontends/SignDocuments/src/Component/component

1 file changed

+36
-29
lines changed

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

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function SignPad({
2626
const [penColor, setPenColor] = useState("blue");
2727
const allColor = [bluePen, redPen, blackPen];
2828
const canvasRef = useRef(null);
29+
const spanRef = useRef(null);
2930
const [isDefaultSign, setIsDefaultSign] = useState(false);
3031
const [isTab, setIsTab] = useState("draw");
3132
const [isSignImg, setIsSignImg] = useState("");
@@ -169,43 +170,46 @@ function SignPad({
169170
? fontSelect
170171
: "Fasthand";
171172

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
183179

180+
//create canvas to render text in canvas and convert in image
181+
const canvasElement = document.createElement("canvas");
184182
// 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;
186193

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);
191197

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);
192208
// Convert the canvas to image data
193-
const dataUrl = canvas.toDataURL("image/png");
209+
const dataUrl = canvasElement.toDataURL("image/png");
194210
setSignature(dataUrl);
195211
};
196212

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-
};
209213
return (
210214
<div>
211215
{/*isSignPad */}
@@ -464,6 +468,7 @@ function SignPad({
464468
}}
465469
>
466470
<span className="signatureText">Signature:</span>
471+
467472
<input
468473
maxLength={30}
469474
style={{ fontFamily: fontSelect }}
@@ -477,12 +482,14 @@ function SignPad({
477482
}}
478483
/>
479484
</div>
485+
{/* <div ref={spanRef}>nwbfmb</div> */}
480486
<div className="fontOptionContainer">
481487
{fontOptions.map((font, ind) => {
482488
return (
483489
<div
484490
key={ind}
485491
style={{
492+
cursor: "pointer",
486493
fontFamily: font.value,
487494
backgroundColor:
488495
fontSelect === font.value && "rgb(206 225 247)"

0 commit comments

Comments
 (0)