import React from "react"; import { useRef, useState } from "react"; import SignatureCanvas from "react-signature-canvas"; import redPen from "../../assests/redPen.png"; import bluePen from "../../assests/bluePen.png"; import blackPen from "../../assests/blackPen.png"; import { themeColor } from "../../utils/ThemeColor/backColor"; import { useEffect } from "react"; function SignPad({ isSignPad, isStamp, setIsImageSelect, onSaveSign, setIsSignPad, setImage, isImageSelect, imageRef, onImageChange, onSaveImage, image, defaultSign, setSignature }) { const [penColor, setPenColor] = useState("blue"); const allColor = [bluePen, redPen, blackPen]; const canvasRef = useRef(null); const [isDefaultSign, setIsDefaultSign] = useState(false); const [isTab, setIsTab] = useState("draw"); 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" }, { value: "Cedarville Cursive" }, { value: "Delicious Handrawn" } // Add more font options as needed ]; const [fontSelect, setFontSelect] = useState(fontOptions[0].value); useEffect(() => { const senderUser = localStorage.getItem( `Parse/${localStorage.getItem("parseAppId")}/currentUser` ) && localStorage.getItem( `Parse/${localStorage.getItem("parseAppId")}/currentUser` ); const jsonSender = JSON.parse(senderUser); const currentUserName = jsonSender && jsonSender.name; //function for clear signature setSignValue(currentUserName); setFontSelect("Fasthand"); }, []); //function for clear signature image const handleClear = () => { if (isTab === "draw") { if (canvasRef.current) { canvasRef.current.clear(); } else if (isStamp) { setImage(""); } setIsSignImg(""); } else if (isTab === "uploadImage") { } }; //function for set signature url const handleSignatureChange = (dataURL) => { // canvasRef.current.backgroundColor = 'rgb(165, 26, 26)' setSignature(canvasRef.current.toDataURL()); setIsSignImg(canvasRef.current.toDataURL()); }; //save button component const SaveBtn = () => { return (
{(isTab === "draw" || isTab === "uploadImage") && ( )}
); }; useEffect(() => { const loadFont = async () => { try { await document.fonts.load(`20px ${fontSelect}`); const selectFontSTyle = fontOptions.find( (font) => font.value === fontSelect ); setFontSelect(selectFontSTyle?.value || fontOptions[0].value); } catch (error) { console.error("Error loading font:", error); } }; loadFont(); }, [fontSelect]); useEffect(() => { // Load the default signature after the component mounts if (canvasRef.current) { canvasRef.current.fromDataURL(isSignImg); } if (isTab === "type") { convertToImg(fontSelect, signValue); } }, [isTab]); //function for convert input text value in image const convertToImg = async (fontStyle, text) => { //get text content to convert in image const textContent = text; const fontfamily = fontStyle ? fontStyle : fontSelect ? fontSelect : "Fasthand"; //creating span for getting text content width const span = document.createElement("span"); span.textContent = textContent; span.style.font = `20px ${fontfamily}`; // here put your text size and font family span.style.display = "hidden"; document.body.appendChild(span); // Replace 'container' with the ID of the container element //create canvas to render text in canvas and convert in image const canvasElement = document.createElement("canvas"); // Draw the text content on the canvas const ctx = canvasElement.getContext("2d"); const pixelRatio = window.devicePixelRatio || 1; const width = span.offsetWidth; const height = span.offsetHeight; setTextWidth(width); setTextHeight(height); const font = span.style["font"]; // Set the canvas dimensions to match the span canvasElement.width = width * pixelRatio; canvasElement.height = height * pixelRatio; // Render the content of the span onto the canvas // ctx.fillStyle = "white"; // Set the background color of the canvas // ctx.fillRect(0, 0, width*pixelRatio, height*pixelRatio); // You can customize text styles if needed ctx.font = font; ctx.fillStyle = "black"; // Set the text color ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.scale(pixelRatio, pixelRatio); // Draw the content of the span onto the canvas ctx.fillText(span.textContent, width / 2, height / 2); // Adjust the x,y-coordinate as needed //remove span tag document.body.removeChild(span); // Convert the canvas to image data const dataUrl = canvasElement.toDataURL("image/png"); setSignature(dataUrl); }; return (
{isSignPad && (
{isStamp ? ( Upload stamp image ) : ( <>
{ setIsDefaultSign(false); setIsImageSelect(false); setIsTab("draw"); setImage(); }} style={{ color: isTab === "draw" ? themeColor() : "#515252", marginLeft: "2px" }} className="signTab" > Draw
{ setIsDefaultSign(false); setIsImageSelect(true); setIsTab("uploadImage"); }} style={{ color: isTab === "uploadImage" ? themeColor() : "#515252" }} className="signTab" > Upload Image
{ setIsDefaultSign(false); setIsImageSelect(false); setIsTab("type"); setImage(); }} style={{ color: isTab === "type" ? themeColor() : "#515252", marginLeft: "2px" }} className="signTab" > Type
{defaultSign && (
{ setIsDefaultSign(true); setIsImageSelect(true); setIsTab("mysignature"); setImage(); }} style={{ color: isTab === "mysignature" ? themeColor() : "#515252" }} className="signTab" > My Signature
)} )}
{ setPenColor("blue"); setIsSignPad(false); setIsImageSelect(false); setIsDefaultSign(false); setImage(); setIsTab("draw"); }} > ×
{isDefaultSign ? ( <>
stamp img
) : isImageSelect || isStamp ? ( !image ? (
imageRef.current.click()} >
Upload
) : ( <>
print img
) ) : isTab === "type" ? (
Signature: { setSignValue(e.target.value); convertToImg(fontSelect, e.target.value); }} />
{fontOptions.map((font, ind) => { return (
{ setFontSelect(font.value); convertToImg(font.value, signValue); }} >
{signValue ? signValue : "Your signature"}
); })}
) : ( <> handleSignatureChange(canvasRef.current.toDataURL()) } dotSize={1} />
{allColor.map((data, key) => { return ( pen img { if (key === 0) { setPenColor("blue"); } else if (key === 1) { setPenColor("red"); } else if (key === 2) { setPenColor("black"); } }} src={data} width={20} height={20} /> ); })}
)}
)}
); } export default SignPad;