Skip to content

Commit 02e60b7

Browse files
feat: export icon at actual preview scale size
1 parent 35bb562 commit 02e60b7

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

src/pages/index.astro

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,8 @@ import { locales, defaultLocale, translations as i18nStrings } from "../i18n/tra
757757
}
758758

759759
downloadBtn.addEventListener("click", () => {
760-
const svgEl = getCleanSvg(exportSize);
760+
const iconSize = Math.round(exportSize * previewScale);
761+
const svgEl = getCleanSvg(iconSize);
761762

762763
const svgData = new XMLSerializer().serializeToString(svgEl);
763764
const svgBlob = new Blob([svgData], { type: "image/svg+xml;charset=utf-8" });
@@ -781,12 +782,13 @@ import { locales, defaultLocale, translations as i18nStrings } from "../i18n/tra
781782
ctx.fillRect(0, 0, exportSize, exportSize);
782783
}
783784

784-
ctx.drawImage(img, 0, 0, exportSize, exportSize);
785+
const offset = Math.round((exportSize - iconSize) / 2);
786+
ctx.drawImage(img, offset, offset, iconSize, iconSize);
785787
URL.revokeObjectURL(url);
786788

787789
const pngUrl = canvas.toDataURL("image/png");
788790
const link = document.createElement("a");
789-
link.download = `apatch-icon-${currentColor.replace("#", "")}-${exportSize}x${exportSize}${bgEnabled ? "-bg" : ""}.png`;
791+
link.download = `apatch-icon-${currentColor.replace("#", "")}-${exportSize}x${exportSize}-${iconSize}px${bgEnabled ? "-bg" : ""}.png`;
790792
link.href = pngUrl;
791793
link.click();
792794
setStatus(tr("pngExported", `${exportSize}x${exportSize}`));
@@ -800,13 +802,16 @@ import { locales, defaultLocale, translations as i18nStrings } from "../i18n/tra
800802

801803
downloadSvgBtn.addEventListener("click", () => {
802804
try {
803-
const svgEl = getCleanSvg(exportSize);
805+
const iconSize = Math.round(exportSize * previewScale);
806+
const svgEl = getCleanSvg(iconSize);
804807
const svgData = new XMLSerializer().serializeToString(svgEl);
805-
const filename = `apatch-icon-${currentColor.replace("#", "")}-${exportSize}x${exportSize}${bgEnabled ? "-bg" : ""}.svg`;
808+
const offset = Math.round((exportSize - iconSize) / 2);
809+
const filename = `apatch-icon-${currentColor.replace("#", "")}-${exportSize}x${exportSize}-${iconSize}px${bgEnabled ? "-bg" : ""}.svg`;
806810

807-
if (bgEnabled) {
808-
const bgColor = darkenColor(currentColor, 0.82);
809-
const wrappedSvg = `<svg xmlns="http://www.w3.org/2000/svg" width="${exportSize}" height="${exportSize}" viewBox="0 0 ${exportSize} ${exportSize}"><rect width="100%" height="100%" fill="${bgColor}"/>${svgData}</svg>`;
811+
if (bgEnabled || previewScale < 1) {
812+
const bgColor = bgEnabled ? darkenColor(currentColor, 0.82) : "none";
813+
const rect = bgColor !== "none" ? `<rect width="100%" height="100%" fill="${bgColor}"/>` : "";
814+
const wrappedSvg = `<svg xmlns="http://www.w3.org/2000/svg" width="${exportSize}" height="${exportSize}" viewBox="0 0 ${exportSize} ${exportSize}">${rect}<g transform="translate(${offset}, ${offset})">${svgData}</g></svg>`;
810815
downloadBlob(new Blob([wrappedSvg], { type: "image/svg+xml;charset=utf-8" }), filename);
811816
} else {
812817
downloadBlob(new Blob([svgData], { type: "image/svg+xml;charset=utf-8" }), filename);

0 commit comments

Comments
 (0)