collage-maker/web/index.js

224 lines
5.8 KiB
JavaScript
Raw Normal View History

"use strict";
const pageSizes = {
2023-09-05 18:42:47 -04:00
"letter-portrait" : {
"ap": "85 / 110",
"width": 3264,
"height": 4224,
},
"letter-landscape" : {
"ap": "110 / 85",
"width": 4224,
"height": 3264,
},
"fiveseven-portrait" : {
"ap": "5 / 7",
"width": 2250,
"height": 3150,
},
"fiveseven-landscape" : {
"ap": "7 / 5",
"width": 3150,
"height": 2250,
},
"foursix-portrait" : {
"ap": "4 / 6",
"width": 1800,
"height": 2700,
},
"foursix-landscape" : {
"ap": "6 / 4",
"width": 2700,
"height": 1800,
},
}
2023-09-06 18:56:02 -04:00
// elements
/** @type {HTMLButtonElement} */
let snapButton
2023-09-06 18:56:02 -04:00
/** @type {HTMLDivElement} */
let collageDiv
2023-09-06 18:56:02 -04:00
/** @type {HTMLSelectElement} */
let pageSizeSelect
2023-09-06 18:56:02 -04:00
2024-08-12 17:21:25 -04:00
/** @type {HTMLDialogElement} */
let noticeDialog
2023-09-06 18:56:02 -04:00
/** @type {HTMLAnchorElement} */
let collageUrlA
2023-09-05 18:42:47 -04:00
// collage state
2023-09-06 18:56:02 -04:00
let crops = [];
let pageSize = "letter-landscape";
/** @type {String[]} */
let imageUrls = []
2023-08-27 19:37:05 -04:00
2023-08-27 22:22:17 -04:00
function main() {
2023-09-05 18:42:47 -04:00
snapButton = document.getElementById("snapper")
collageDiv = document.getElementById("collage")
pageSizeSelect = document.getElementById("page_size_selector")
2024-08-12 17:21:25 -04:00
noticeDialog = document.getElementById("notice_dialog")
2023-09-05 18:42:47 -04:00
collageUrlA = document.getElementById("collage-url")
2023-09-06 18:56:02 -04:00
2023-09-05 18:42:47 -04:00
snapButton.onclick = () => snap()
2023-09-06 18:56:02 -04:00
pageSizeSelect.onchange = () => pageSizeChange()
for(const tmpl of document.getElementsByClassName("template")) {
// Assumes second class in the template is the collage's template
const [_, collageTemplate] = tmpl.classList
tmpl.dataset.collageTemplate = collageTemplate
tmpl.onclick = () => applyTemplate(tmpl)
}
2024-08-08 12:05:40 -04:00
const queryUrls = loadImageUrlsFromQuery()
// Skipping first entry in array to make the images start with index 1
2024-08-08 12:05:40 -04:00
imageUrls = [,].concat(queryUrls)
2024-08-12 17:21:25 -04:00
pageSizeChange()
applyTemplate(document.getElementById("default_template"))
2023-08-27 22:22:17 -04:00
}
2023-08-27 19:37:05 -04:00
2023-09-06 18:56:02 -04:00
/**
* @param {HTMLDivElement} elem
* @param {string} imgUrl
*/
2023-08-27 19:37:05 -04:00
function makeCroppieElem(elem, imgUrl) {
const cpie = new Croppie(elem, {
2023-08-27 19:37:05 -04:00
viewport: {
width: elem.clientWidth,
height: elem.clientHeight,
type: 'square'
},
showZoomer: false,
});
cpie.bind({
url: imgUrl
});
2023-08-27 22:22:17 -04:00
return cpie
2023-08-27 19:37:05 -04:00
}
2023-08-27 22:22:17 -04:00
2023-09-05 18:42:47 -04:00
function initCollage() {
for(const cpie of crops) {
2023-09-05 18:42:47 -04:00
cpie.destroy()
}
crops = []
for(const elem of collageDiv.getElementsByClassName("img")) {
2023-08-27 22:22:17 -04:00
const cpie = makeCroppieElem(elem, elem.dataset.collageImageUrl)
const lastLen = crops.push(cpie)
elem.dataset.collageCropieIndex = lastLen - 1
2023-08-27 19:37:05 -04:00
}
}
async function makeCollage(req) {
const resp = await fetch("make-collage", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(req),
})
return await resp.text();
2023-08-27 22:22:17 -04:00
}
function snap() {
const col = collageDiv.offsetLeft;
const cot = collageDiv.offsetTop;
const req = {
2023-08-27 22:22:17 -04:00
background_image: "",
aspect: {
2023-09-05 18:42:47 -04:00
width: pageSizes[pageSize]["width"],
height: pageSizes[pageSize]["height"],
2023-08-27 22:22:17 -04:00
},
dimension: {
width: collageDiv.clientWidth,
height: collageDiv.clientHeight,
},
photos: [],
};
for(const elem of collageDiv.getElementsByClassName("img")) {
2023-08-27 22:22:17 -04:00
const cpie = crops[elem.dataset.collageCropieIndex]
const fsx = elem.offsetLeft - col
const fsy = elem.offsetTop - cot
const [sx, sy, ex, ey] = cpie.get().points;
const photo = {
image: elem.dataset.collageImageUrl.slice("/photos/".length),
2023-08-27 22:22:17 -04:00
crop: {
start: {
x: parseInt(sx),
y: parseInt(sy),
},
end: {
x: parseInt(ex),
y: parseInt(ey),
},
},
frame: {
start: {
x: fsx,
y: fsy,
},
end: {
x: fsx + elem.clientWidth,
y: fsy + elem.clientHeight,
},
},
};
req.photos.push(photo)
}
(async () => {
2024-08-12 17:21:25 -04:00
noticeDialog.show()
collageUrlA.text = "Collage is being generated...";
const collagFile = await makeCollage(req)
collageUrlA.href = `collages/${collagFile}`;
collageUrlA.text = `${collagFile} generated`;
})();
2023-08-27 22:22:17 -04:00
}
2023-09-05 18:42:47 -04:00
2023-09-06 18:56:02 -04:00
function pageSizeChange() {
2024-08-12 17:21:25 -04:00
// https://stackoverflow.com/a/37802204
document.documentElement.style.setProperty('--collage-ap', pageSizes[pageSizeSelect.value]["ap"])
2023-09-06 18:56:02 -04:00
pageSize = pageSizeSelect.value
2023-09-05 18:42:47 -04:00
initCollage()
}
/** @param {HTMLDivElement} templateDiv */
function applyTemplate(templateDiv) {
2023-09-14 14:57:33 -04:00
document.getElementsByClassName("template-selected").item(0)?.classList.remove("template-selected")
/** @type {HTMLDivElement} */
const templateClone = templateDiv.cloneNode(true)
2023-09-14 14:57:33 -04:00
templateDiv.classList.add("template-selected")
for (const index in imageUrls) {
const url = imageUrls[index]
const imgClass = `img${index}`
const [imgDiv] = templateClone.getElementsByClassName(imgClass)
if(imgDiv === undefined) {
break;
}
imgDiv.dataset.collageImageUrl = url;
}
collageDiv.replaceChildren(...templateClone.children)
collageDiv.classList.remove(collageDiv.dataset.collageTemplate)
collageDiv.classList.add(templateDiv.dataset.collageTemplate)
collageDiv.dataset.collageTemplate = templateDiv.dataset.collageTemplate
initCollage()
}
2024-08-08 12:05:40 -04:00
function loadImageUrlsFromQuery() {
const params = new URLSearchParams(window.location.search)
const urlsstr = params.get('urls')
return JSON.parse(urlsstr)
}
2023-09-05 18:42:47 -04:00
main()