new functions and fixes

This commit is contained in:
Mateusz Gruszczyński
2026-08-06 09:08:02 +02:00
parent 1e80fb2bcb
commit 7989a873e7
11 changed files with 191 additions and 42 deletions
+12 -2
View File
@@ -9,6 +9,9 @@
function clamp(value, min, max) { return Math.min(max, Math.max(min, value)); }
function stem(name) { return name.replace(/\.[^.]+$/, "") || "image"; }
function imageNeedsProcessing(width, height, aspect, maxSize) {
return aspect !== "original" || (maxSize > 0 && Math.max(width, height) > maxSize);
}
export async function prepareImageFile(file) {
if (!file.type.startsWith("image/")) return file;
@@ -61,8 +64,15 @@ export async function prepareImageFile(file) {
const result = new Promise(resolve => {
dialog.addEventListener("close", () => { URL.revokeObjectURL(url); dialog.remove(); resolve(accepted); }, { once: true });
dialog.querySelector("[data-apply]").addEventListener("click", async () => {
const box = cropBox(), maxSize = Number(sizeSelect.value), out = document.createElement("canvas");
if (aspectSelect.value === "original") {
const box = cropBox(), maxSize = Number(sizeSelect.value);
const wholeImage = aspectSelect.value === "original";
if (!imageNeedsProcessing(image.naturalWidth, image.naturalHeight, aspectSelect.value, maxSize)) {
accepted = file;
dialog.close();
return;
}
const out = document.createElement("canvas");
if (wholeImage) {
const ratio = Math.min(1, maxSize ? maxSize / Math.max(image.naturalWidth, image.naturalHeight) : 1);
out.width = Math.max(1, Math.round(image.naturalWidth * ratio)); out.height = Math.max(1, Math.round(image.naturalHeight * ratio));
out.getContext("2d").drawImage(image, 0, 0, out.width, out.height);