From 3e3598ae10aced276ad6dc5d2c5f55b0520ad54e Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Sat, 22 Apr 2023 19:58:10 +0000 Subject: [PATCH] Working prototype for Lychee like image grid --- onlylegs/__init__.py | 2 +- onlylegs/static/js/grid.js | 73 ++++++++++++++++++++++++++++++++++ onlylegs/templates/index.html | 4 +- onlylegs/templates/layout.html | 1 + 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 onlylegs/static/js/grid.js diff --git a/onlylegs/__init__.py b/onlylegs/__init__.py index 5e78f84..6da1ef1 100644 --- a/onlylegs/__init__.py +++ b/onlylegs/__init__.py @@ -102,7 +102,7 @@ def create_app(): # pylint: disable=R0914 assets.init_app(app) scripts = Bundle( - "js/*.js", filters="jsmin", output="gen/js.js", depends="js/*.js" + "js/*.js", output="gen/js.js", depends="js/*.js" ) # filter jsmin is broken :c styles = Bundle( "sass/style.sass", diff --git a/onlylegs/static/js/grid.js b/onlylegs/static/js/grid.js new file mode 100644 index 0000000..339dcd7 --- /dev/null +++ b/onlylegs/static/js/grid.js @@ -0,0 +1,73 @@ +function makeGrid() { + // Get the container and images + const container = document.querySelector('.gallery-grid'); + const images = document.querySelectorAll('.gallery-item'); + + // Set the gap between images + const gap = 0.6 * 16; + const maxWidth = container.clientWidth - gap; + const maxHeight = 13 * 16; + + // Calculate the width and height of each image + let calculated = {}; + for (let i = 0; i < images.length; i++) { + const image = images[i].querySelector('img'); + const width = image.naturalWidth; + const height = image.naturalHeight; + + let ratio = width / height; + const newWidth = maxHeight * ratio; + + if (newWidth > maxWidth) { + newWidth = maxWidth / 3 - gap; // 3 images per row max + ratio = newWidth / height; + } + + calculated[i] = {"width": newWidth, + "height": maxHeight, + "ratio": ratio}; + } + + // Next images position + let nextTop = gap; + let nextLeft = gap; + + for (let i = 0; i < images.length; i++) { + let currentRow = []; + let currectLength = 0; + + // While the row is not full add images to it + while (currectLength < maxWidth) { + currentRow.push(i); + currectLength += calculated[i]["width"]; + i++; + } + // currentRow.push(i); + // currectLength += calculated[i]["width"]; + + // Go back one image as it can't be added to the row + i--; + + // Calculate the amount of space required to fill the row + const currentRowDiff = (currectLength - maxWidth); + + // Cycle through the images in the row and adjust their width and left position + for (let j = 0; j < currentRow.length; j++) { + const image = images[currentRow[j]]; + const data = calculated[currentRow[j]]; + // Shrink compared to the % of the row it takes up + const shrink = currentRowDiff * (data["width"] / currectLength); + + image.style.width = data["width"] - shrink - gap + 'px'; + image.style.height = data["height"] + 'px'; + image.style.left = nextLeft + 'px'; + image.style.top = nextTop + 'px'; + + nextLeft += data["width"] - shrink; + } + + // Reset for the next row + nextTop += maxHeight + gap; + nextLeft = gap; + } +} \ No newline at end of file diff --git a/onlylegs/templates/index.html b/onlylegs/templates/index.html index 35d39db..e85cafb 100644 --- a/onlylegs/templates/index.html +++ b/onlylegs/templates/index.html @@ -34,9 +34,9 @@ {% if images %} -