From b9c377368361b3b5c4666271b4dba068270088f3 Mon Sep 17 00:00:00 2001 From: appala venkata avinash Date: Fri, 7 Jun 2024 19:03:03 +0530 Subject: [PATCH] Adjusted White Space in Download Meme --- src/components/HashtagTool/MemeEditor.js | 47 +++++++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/components/HashtagTool/MemeEditor.js b/src/components/HashtagTool/MemeEditor.js index ff12dc4..b8d95ce 100644 --- a/src/components/HashtagTool/MemeEditor.js +++ b/src/components/HashtagTool/MemeEditor.js @@ -12,6 +12,7 @@ const MemeEditor = () => { const [currentFont, setCurrentFont] = useState('Arial'); const [lines, setLines] = useState([]); const [isDrawing, setIsDrawing] = useState(false); + const [editedText, setEditedText] = useState(''); const stageRef = useRef(null); const fileInputRef = useRef(null); @@ -81,8 +82,50 @@ const MemeEditor = () => { transformer.getLayer().batchDraw(); } - const uri = stage.toDataURL({ pixelRatio: 2 }); - saveAs(uri, 'my-meme.png'); + const boundingBox = stage.getChildren()[0].getClientRect(); + + const scale = stage.scaleX(); + const width = boundingBox.width * scale; + const height = boundingBox.height * scale; + + const x = boundingBox.x * scale; + const y = boundingBox.y * scale; + + const pixelRatio = 2; + const dataURL = stage.toDataURL({ + x: boundingBox.x, + y: boundingBox.y, + width: boundingBox.width, + height: boundingBox.height, + pixelRatio: pixelRatio, + }); + + // Create an off-screen canvas to draw the cropped image + const offScreenCanvas = document.createElement('canvas'); + offScreenCanvas.width = width * pixelRatio; + offScreenCanvas.height = height * pixelRatio; + const ctx = offScreenCanvas.getContext('2d'); + + const img = new window.Image(); + img.onload = () => { + ctx.drawImage( + img, + x * pixelRatio, + y * pixelRatio, + width * pixelRatio, + height * pixelRatio, + 0, + 0, + width * pixelRatio, + height * pixelRatio, + ); + + // Convert the off-screen canvas to a data URL and download it + offScreenCanvas.toBlob(blob => { + saveAs(blob, 'my-meme.png'); + }); + }; + img.src = dataURL; }; const handleTextChange = e => {