Implement selecting template for collage

This commit is contained in:
Balakrishnan Balasubramanian 2023-09-06 23:44:29 -04:00
parent 8a56639920
commit 8034acb504
3 changed files with 186 additions and 80 deletions

View File

@ -7,12 +7,13 @@
background-color: lightgrey;
display: flex;
align-items: center;
justify-content: center;
flex: 30%;
justify-content: space-around;
flex-direction: column;
flex: 25%;
}
.imagebox {
padding: 2rem;
flex: 70%;
flex: 75%;
}
.image-surface {
@ -28,32 +29,32 @@
font-size: 2rem;
}
// paper sizes
.letter-portrait {
aspect-ratio: 85 / 110;
}
.letter-landscape {
aspect-ratio: 110 / 85;
}
.fiveseven-portrait {
aspect-ratio: 5 / 7;
}
.fiveseven-landscape {
aspect-ratio: 7 / 5;
}
.foursix-portrait {
aspect-ratio: 4 / 6;
}
.foursix-landscape {
aspect-ratio: 6 / 4;
.templates {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.half-landscape {
display:flex;
.templates li {
list-style-type: none;
}
.half-landscape .collage-img {
flex: 50%;
#page_size_selector {
width: 100%;
}
.template {
width: 100px;
height: 100px;
}
.template div {
border: 1px solid;
}
.single div {
width: 100%;
height: 100%;
}
.one-two {
@ -66,21 +67,38 @@
grid-area: one;
}
.letter-portrait .collage-img {
.half-leftright {
display:flex;
}
.half-leftright .img {
flex: 50%;
}
.half-topbottom .img {
width: 100%;
height: 50%;
}
.letter-landscape-2 {
display:flex;
// width: 80vh;
height: 100%;
// margin: auto;
aspect-ratio: 110 / 85;
// aspect-ratio: 7 / 5;
gap: 1rem;
.two-one-two-leftright {
display: grid;
grid-template-areas:
"one two two three"
"four two two five";
}
.letter-landscape-2 .img1 {
flex: 60%;
.two-one-two-leftright .img2 {
grid-area: two;
}
.letter-landscape-2 .img2 {
flex: 40%;
.two-one-two-topbottom {
display: grid;
grid-template-areas:
"one two"
"three three"
"three three"
"four five"
}
.two-one-two-topbottom .img3 {
grid-area: three;
}

View File

@ -17,10 +17,10 @@
<body>
<div class="container">
<div class="controls">
<div>
<div>
<label>
Page size
<span>Paper size</span>
<select id="page_size_selector" size=8 onChange="pageSizeChange(this)">
<option value="letter-portrait">Letter (Portrait)</option>
<option selected value="letter-landscape">Letter (Landscape)</option>
@ -30,20 +30,67 @@
<option value="foursix-landscape">4 X 6 (Landscape)</option>
</select>
</label>
<ul class="templates">
<li>
<div id="default_template" class="template single" data-collage-template="single">
<div class="img img1">
</div>
</div>
</li>
<li>
<div class="template one-two" data-collage-template="one-two">
<div class="img img1">
</div>
<div class="img img2">
</div>
<div class="img img3">
</div>
</div>
</li>
<li>
<div class="template half-leftright" data-collage-template="half-leftright">
<div class="img img1">
</div>
<div class="img img2">
</div>
</div>
</li>
<li>
<div class="template half-topbottom" data-collage-template="half-topbottom">
<div class="img img1">
</div>
<div class="img img2">
</div>
</div>
</li>
<li>
<div class="template two-one-two-leftright" data-collage-template="two-one-two-leftright">
<div class="img img1"></div>
<div class="img img2"></div>
<div class="img img3"></div>
<div class="img img4"></div>
<div class="img img5"></div>
</div>
</li>
<li>
<div class="template two-one-two-topbottom" data-collage-template="two-one-two-topbottom">
<div class="img img1"></div>
<div class="img img2"></div>
<div class="img img3"></div>
<div class="img img4"></div>
<div class="img img5"></div>
</div>
</li>
</ul>
<button id="snapper" class="showbuton">Snap Collage</button>
<p><a href="" target="_blank" id="collage-url"></a></p>
</div>
</div>
<div class="imagebox">
<div id="collage" class="image-surface one-two">
<div class="collage-img img1" data-collage-image-url="images/img1.jpg">
</div>
<div class="collage-img img2" data-collage-image-url="images/img2.jpg">
</div>
<div class="collage-img img3" data-collage-image-url="images/img3.jpg">
</div>
</div>
<div id="collage" class="image-surface"></div>
</div>
</div>
</body>

View File

@ -1,4 +1,6 @@
var pageSizes = {
"use strict";
const pageSizes = {
"letter-portrait" : {
"ap": "85 / 110",
"width": 3264,
@ -34,21 +36,32 @@ var pageSizes = {
// elements
/** @type {HTMLButtonElement} */
var snapButton
let snapButton
/** @type {HTMLDivElement} */
var collageDiv
let collageDiv
/** @type {HTMLSelectElement} */
var pageSizeSelect
let pageSizeSelect
/** @type {HTMLAnchorElement} */
var collageUrlA
let collageUrlA
// collage state
var crops = [];
var pageSize = "letter-landscape";
let crops = [];
let pageSize = "letter-landscape";
const imageUrls = [
, // images start with index 1
"images/img1.jpg",
"images/img2.jpg",
"images/img3.jpg",
"images/img4.jpg",
"images/img5.jpg",
"images/img6.jpg",
"images/img7.jpg",
]
function main() {
snapButton = document.getElementById("snapper")
@ -56,10 +69,14 @@ function main() {
pageSizeSelect = document.getElementById("page_size_selector")
collageUrlA = document.getElementById("collage-url")
initCollage()
snapButton.onclick = () => snap()
pageSizeSelect.onchange = () => pageSizeChange()
for(const tmpl of document.getElementsByClassName("template")) {
tmpl.onclick = () => applyTemplate(tmpl)
}
applyTemplate(document.getElementById("default_template"))
}
/**
@ -67,7 +84,7 @@ function main() {
* @param {string} imgUrl
*/
function makeCroppieElem(elem, imgUrl) {
cpie = new Croppie(elem, {
const cpie = new Croppie(elem, {
viewport: {
width: elem.clientWidth,
height: elem.clientHeight,
@ -85,11 +102,12 @@ function makeCroppieElem(elem, imgUrl) {
function initCollage() {
for(cpie of crops) {
for(const cpie of crops) {
cpie.destroy()
}
crops = []
for(elem of collageDiv.getElementsByClassName("collage-img")) {
for(const elem of collageDiv.getElementsByClassName("img")) {
const cpie = makeCroppieElem(elem, elem.dataset.collageImageUrl)
const lastLen = crops.push(cpie)
elem.dataset.collageCropieIndex = lastLen - 1
@ -116,10 +134,10 @@ async function makeCollage(req) {
}
function snap() {
col = collageDiv.offsetLeft;
cot = collageDiv.offsetTop;
const col = collageDiv.offsetLeft;
const cot = collageDiv.offsetTop;
console.log("----------------------")
req = {
const req = {
background_image: "",
aspect: {
width: pageSizes[pageSize]["width"],
@ -132,7 +150,7 @@ function snap() {
photos: [],
};
for(elem of collageDiv.getElementsByClassName("collage-img")) {
for(const elem of collageDiv.getElementsByClassName("img")) {
const cpie = crops[elem.dataset.collageCropieIndex]
// console.log(cpie.get().points)
// console.log(elem.offsetLeft - col)
@ -170,7 +188,7 @@ function snap() {
console.log(JSON.stringify(req));
(async () => {
collagFile = await makeCollage(req)
const collagFile = await makeCollage(req)
collageUrlA.href = `collages/${collagFile}`;
collageUrlA.text = `${collagFile} generated`;
})();
@ -182,4 +200,27 @@ function pageSizeChange() {
initCollage()
}
/**
* @param {HTMLDivElement} templateDiv
*/
function applyTemplate(templateDiv) {
/** @type {HTMLDivElement} */
const templateClone = templateDiv.cloneNode(true)
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()
}
main()