-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
46 lines (38 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import BreakpointPartition from './lib/BreakpointPartition.js';
export default function perfectLayout(photos, screenWidth, screenHeight, opts) {
opts = opts || {};
opts.margin = opts.margin || 0;
const rows = _perfectRowsNumber(photos, screenWidth, screenHeight);
const idealHeight = parseInt(screenHeight / 2, 10);
if (rows < 1) {
return photos.map(img => {
return {
data: img.data,
src: img.src,
width: parseInt(idealHeight * img.ratio) - (opts.margin * 2),
height: idealHeight
};
});
} else {
const weights = photos.map(img => parseInt(img.ratio * 100, 10));
const partitions = BreakpointPartition(weights, rows);
let current = 0;
return partitions.map(row => {
const summedRatios = row.reduce((sum, el, i) => sum + photos[current + i].ratio, 0);
return row.map(() => {
const img = photos[current++];
return {
data: img.data,
src: img.src,
width: parseInt((screenWidth / summedRatios) * img.ratio, 10) - (opts.margin * 2),
height: parseInt(screenWidth / summedRatios, 10)
};
});
});
}
}
function _perfectRowsNumber(photos, screenWidth, screenHeight) {
const idealHeight = parseInt(screenHeight / 2);
const totalWidth = photos.reduce((sum, img) => sum + img.ratio * idealHeight, 0);
return Math.round(totalWidth / screenWidth);
}