Skip to content

Commit 318c4ba

Browse files
committed
add: jpg save format
Test Notes: n/a
1 parent 4a184e1 commit 318c4ba

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<div class="toolbar-left">
1414
<button id="selectFolderBtn" class="btn"><i class="fas fa-folder-open"></i> Select Folder</button>
1515
<button id="addFilesBtn" class="btn"><i class="fas fa-file"></i> Add Files</button>
16+
<select id="atlasFormat" class="select">
17+
<option value="png" selected>PNG</option>
18+
<option value="jpg">JPG</option>
19+
</select>
1620
<select id="atlasSizeSelect" class="select"></select>
1721
<select id="paddingSelect" class="select">
1822
<option value="0" selected>0px</option>

main.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const sharp = require("sharp");
55

66
let store;
77
let main_window;
8+
let format = "png";
89

910
async function initialize_store() {
1011
const { default: Store } = await import("electron-store");
@@ -164,16 +165,24 @@ app.whenReady().then(async () => {
164165
}
165166
});
166167

167-
ipcMain.handle("save-atlas", async (event, { data_url, default_path, quality }) => {
168+
ipcMain.handle("save-atlas", async (event, { data_url, default_path, quality, format }) => {
169+
if (!format) {
170+
format = "png"; // Default to PNG if format is not provided
171+
}
168172
const result = await dialog.showSaveDialog({
169173
default_path,
170-
filters: [{ name: "PNG", extensions: ["png"] }]
174+
filters: [{ name: format.toUpperCase(), extensions: [format] }]
171175
});
172176
if (!result.canceled) {
173-
const base64_data = data_url.replace(/^data:image\/png;base64,/, "");
177+
const base64_data = data_url.replace(/^data:image\/(png|jpeg);base64,/, "");
174178
const buffer = Buffer.from(base64_data, "base64");
175-
const png_buffer = await sharp(buffer).png({ quality: Math.round(parseFloat(quality) * 100) }).toBuffer();
176-
await fs.writeFile(result.filePath, png_buffer, "base64");
179+
let image_buffer;
180+
if (format === "jpg") {
181+
image_buffer = await sharp(buffer).jpeg({ quality: Math.round(parseFloat(quality) * 100) }).toBuffer();
182+
} else {
183+
image_buffer = await sharp(buffer).png({ quality: Math.round(parseFloat(quality) * 100) }).toBuffer();
184+
}
185+
await fs.writeFile(result.filePath, image_buffer, "base64");
177186
return result.filePath;
178187
}
179188
});
@@ -226,7 +235,6 @@ app.whenReady().then(async () => {
226235
return store.get("atlas_zoom_factor", 0.6); // Change default to 0.6 (60%)
227236
});
228237

229-
// Add these new IPC handlers
230238
ipcMain.handle("save-project", async (event, project_data) => {
231239
const result = await dialog.showSaveDialog({
232240
title: "Save Project",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sprite-packer",
33
"productName": "Sprite Packer",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"main": "main.js",
66
"scripts": {
77
"start": "electron .",

preload.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ contextBridge.exposeInMainWorld("electronAPI", {
1919
save_project: (projectData) => ipcRenderer.invoke("save-project", projectData),
2020
load_project: () => ipcRenderer.invoke("load-project"),
2121
open_external: (url) => ipcRenderer.invoke("open-external", url),
22+
select_format: () => ipcRenderer.invoke("select-format"),
2223
});

renderer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const preview_canvas = document.getElementById("previewCanvas");
99
const ctx = preview_canvas.getContext("2d");
1010
const quality_select = document.getElementById("qualitySelect");
1111
const canvas_container = document.getElementById("canvasContainer");
12+
const atlas_format_select = document.getElementById("atlasFormat");
1213

1314
const save_atlas_btn = document.getElementById("saveAtlasBtn");
1415
const save_project_btn = document.getElementById("saveProjectBtn");
@@ -585,8 +586,13 @@ if (image_files.length > 0) {
585586
save_atlas_btn.addEventListener("click", async () => {
586587
if (packed_atlas_data_url) {
587588
try {
588-
const default_path = "sprite_atlas.png";
589-
const saved_path = await window.electronAPI.save_atlas({ data_url: packed_atlas_data_url, default_path, quality: quality_select.value });
589+
const default_path = `sprite_atlas.${atlas_format_select.value}`;
590+
const saved_path = await window.electronAPI.save_atlas({
591+
data_url: packed_atlas_data_url,
592+
default_path,
593+
quality: quality_select.value,
594+
format: atlas_format_select.value
595+
});
590596
if (saved_path) {
591597

592598
}

0 commit comments

Comments
 (0)