Skip to content

Commit

Permalink
Update Download.jsx to force the width
Browse files Browse the repository at this point in the history
  • Loading branch information
dobri1408 authored Sep 26, 2024
1 parent ad5b0c0 commit 50a2a88
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Utils/Download.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,18 @@ export default function Download(props) {
// Make all elements inside SVG modify only x and width attributes (no scaling)
const elements = svgClone.querySelectorAll('*');
elements.forEach((el) => {
// Modify only x and width attributes
['x', 'width', 'cx'].forEach((attr) => {
if (el.hasAttribute(attr)) {
const originalValue = parseFloat(el.getAttribute(attr));
el.setAttribute(attr, originalValue); // No scaling, just keep original values
el.setAttribute(attr, originalValue);
}
});

// Center text for svg with index 1
if (index === 1 && el.tagName === 'text') {
el.setAttribute('x', '50%');
el.setAttribute('text-anchor', 'middle');
}

// Ensure text color is black
if (el.tagName === 'text') {
el.setAttribute('fill', 'black');
if (el.hasAttribute('style')) {
Expand All @@ -250,8 +247,7 @@ export default function Download(props) {
}
});

// Remove white background and set transparent
svgClone.style.background = 'none'; // Remove background color
svgClone.style.background = 'none';
svgClone.setAttribute('style', 'background-color: transparent');

if (svgWidth > maxWidth) maxWidth = svgWidth;
Expand All @@ -261,14 +257,23 @@ export default function Download(props) {

if (index !== 1) svgClone.setAttribute('y', newY + totalTextHeight);

totalHeight += svgHeight; // Keep height unchanged
totalHeight += svgHeight;

combinedSvg.appendChild(svgClone);
});

// Adjust the final combined SVG size based on the original dimensions
combinedSvg.setAttribute('width', maxWidth);
combinedSvg.setAttribute('height', totalHeight + totalTextHeight);
const forcedWidth = 700;
const aspectRatio = maxWidth / totalHeight;
const forcedHeight = forcedWidth / aspectRatio;

combinedSvg.setAttribute('width', forcedWidth);
combinedSvg.setAttribute('height', forcedHeight + totalTextHeight);

combinedSvg.setAttribute(
'viewBox',
`0 0 ${maxWidth} ${totalHeight + totalTextHeight}`,
);

if (type === 'svg') {
downloadSVG(combinedSvg, `${title}.${type.toLowerCase()}`);
Expand Down

0 comments on commit 50a2a88

Please sign in to comment.