photo-partition
A library to apply linear partitioning algorithm to a set of photos.
Usage
partitions(viewports, photoArray, desiredHeight, requestedRows)
- viewports: an object which maps a viewport width to a container width. For example,
in Bootstrap 3, @media queries for
.container
are setup in the following way:
{{{
Having the following structure,
<!-- photos go in here -->
the col-xs-12
element with 15px
padding on left and right will have width of 1140px
,
940px
and 720px
for given min-width
values.
Therefore, the viewports
argument will be:
1200: 1140 992: 940 768: 720
-
photoArray: an array of photos as objects. Each object must contain both
width
andheight
properties, oraspect
property. It can have other values, but they will be removed (todo: inherit properties in the returned object). -
[desiredHeight]: what required height each image is supposed to take. The value will be used to calculate the total width of all images given their aspect:
const summedWidth = lib.getSummedWidth(aspects, desiredHeight)
this option.
getCSS(partitions, prefix)
- partitions: an object generated with
partitions
function.
Example
const partitions = const photoList = url: '/files/cd24b45a-48c3-4f78-a029-7725128e82ef.jpg' width: 2448 height: 3264 aspect: 075 url: '/files/9dc13701-4975-4e45-bbfe-ea6d315b983e.jpg' width: 2448 height: 3264 aspect: 075 url: '/files/91143cde-f06c-4b0b-86e1-9a1dc182fb01.jpg' width: 2448 height: 3264 aspect: 075 url: '/files/6737002f-4fa6-4479-b7eb-bb90271ea359.jpg' width: 3264 height: 2448 aspect: 133 url: '/files/28aac5b5-9a60-445e-b4d6-a51e485745c5.jpg' width: 3264 height: 2448 aspect: 133 url: '/files/d42ee39d-1827-433f-bca0-25c9ddf4abfa.jpg' width: 3264 height: 2448 aspect: 133 const p = partitions // partition for bootstrap viewports with desired height of 250/*{ '768': [ { height: 201, width: 151 }, { height: 201, width: 151 }, { height: 201, width: 151 }, { height: 201, width: 267 }, { height: 271, width: 360 }, { height: 271, width: 360 } ], '992': [ { height: 263, width: 197 }, { height: 263, width: 197 }, { height: 263, width: 197 }, { height: 263, width: 349 }, { height: 353, width: 469 }, { height: 353, width: 471 } ], '1200': [ { height: 183, width: 137 }, { height: 183, width: 137 }, { height: 183, width: 137 }, { height: 183, width: 243 }, { height: 183, width: 243 }, { height: 183, width: 243 } ] }*/ const css = partitions/*'@media (min-width: 768px) { .s0 { width: 151px; height: 201px; } .s1 { width: 151px; height: 201px; } .s2 { width: 151px; height: 201px; } .s3 { width: 267px; height: 201px; } .s4 { width: 360px; height: 271px; } .s5 { width: 360px; height: 271px; } }@media (min-width: 992px) { .s0 { width: 197px; height: 263px; } .s1 { width: 197px; height: 263px; } .s2 { width: 197px; height: 263px; } .s3 { width: 349px; height: 263px; } .s4 { width: 469px; height: 353px; } .s5 { width: 471px; height: 353px; } }@media (min-width: 1200px) { .s0 { width: 137px; height: 183px; } .s1 { width: 137px; height: 183px; } .s2 { width: 137px; height: 183px; } .s3 { width: 243px; height: 183px; } .s4 { width: 243px; height: 183px; } .s5 { width: 243px; height: 183px; } }'*/ const outputList1 = photoList /*[ { url: '/files/cd24b45a-48c3-4f78-a029-7725128e82ef.jpg', class: 's0' }, { url: '/files/9dc13701-4975-4e45-bbfe-ea6d315b983e.jpg', class: 's1' }, { url: '/files/91143cde-f06c-4b0b-86e1-9a1dc182fb01.jpg', class: 's2' }, { url: '/files/6737002f-4fa6-4479-b7eb-bb90271ea359.jpg', class: 's3' }, { url: '/files/28aac5b5-9a60-445e-b4d6-a51e485745c5.jpg', class: 's4' }, { url: '/files/d42ee39d-1827-433f-bca0-25c9ddf4abfa.jpg', class: 's5' } ]*/ await ctx
nunjucks template:
{% extends "views/layouts/main.html" %}{% block header %} Photos{% endblock %}{% block body %} {% for photo in outputList %} {% endfor %} {% endblock %}
result html:
ES5
The package uses some newer language features. For your convenience, it's been transpiled to be compatible with older environments. You can use the following snippet.
const partitions =
Testing
Module is tested with zoroaster: npm test
.