MCPcopy Index your code
hub / github.com/OpenSignLabs/OpenSign / convertBase64ToImg

Function convertBase64ToImg

apps/OpenSign/src/constant/Utils.js:1717–1752  ·  view source on GitHub ↗
(base64Image, widgetDims)

Source from the content-addressed store, hash-verified

1715 * @returns {Promise<string>} A Promise that resolves to a data-URL of the new image
1716 */
1717export async function convertBase64ToImg(base64Image, widgetDims) {
1718 const { Width: maxWidth, Height: maxHeight } = widgetDims;
1719 // Load the image off-DOM
1720 const img = new Image();
1721 img.src = base64Image;
1722 await new Promise((resolve, reject) => {
1723 img.onload = resolve;
1724 img.onerror = reject;
1725 });
1726
1727 // 2. Compute scale to fit within widget (preserving aspect ratio)
1728 const { naturalWidth: imgW, naturalHeight: imgH } = img;
1729 const scale = Math.min(maxWidth / imgW, maxHeight / imgH, 1);
1730 const drawW = imgW * scale;
1731 const drawH = imgH * scale;
1732
1733 // 3. Prepare a high-DPI canvas
1734 const pxRatio = (window.devicePixelRatio || 1) * 2;
1735 const canvas = document.createElement("canvas");
1736 canvas.width = Math.ceil(maxWidth * pxRatio);
1737 canvas.height = Math.ceil(maxHeight * pxRatio);
1738 const ctx = canvas.getContext("2d");
1739 ctx.scale(pxRatio, pxRatio);
1740 ctx.clearRect(0, 0, maxWidth, maxHeight);
1741
1742 // 4. Center the image in the widget rectangle
1743 const x = (maxWidth - drawW) / 2;
1744 const y = (maxHeight - drawH) / 2;
1745 ctx.drawImage(img, x, y, drawW, drawH);
1746 // 5. Return new base64 in same format as input
1747 // const quality =
1748 // inputMime.includes("jpeg") || inputMime.includes("jpg") ? 0.9 : undefined;
1749 // return canvas.toDataURL(inputMime, quality);
1750 // 5. Always return PNG (lossless, no quality param needed)
1751 return canvas.toDataURL("image/png");
1752}
1753
1754//function to use After setting the signature URL for the first signature widget, clicking on subsequent
1755//signature widgets should automatically apply and display the signature. apply for initial,signature and stamp widget

Callers 1

embedWidgetsToDocFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected