This repository was archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustgallery.js
More file actions
72 lines (57 loc) · 1.64 KB
/
justgallery.js
File metadata and controls
72 lines (57 loc) · 1.64 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Does the necessary calculations for a justified gallery. */
var adjustrow = function(row, overflow) {
var remainingOverflow = overflow;
// sum up the total width
var totalWidth = 0;
for (var i=0; i<row.length; i++) {
totalWidth += row[i].width;
}
// calculate adjusted width
for (var i=0; i<row.length; i++) {
var img = row[i];
var rowWeight = (img.width*1.0) / totalWidth;
var currentOverflow = Math.floor(rowWeight * overflow);
remainingOverflow -= currentOverflow;
img["jg-width"] = img.width - currentOverflow;
img["jg-height"] = img.height;
}
// distribute remaining overflow
while (remainingOverflow > 0) {
for (var i=0; i<row.length; i++) {
var img = row[i];
img["jg-width"] = img["jg-width"] - 1;
remainingOverflow--;
if (remainingOverflow <= 0) break;
}
}
// calculate margins
for (var i=0; i<row.length; i++) {
var img = row[i];
img["jg-marginleft"] = -Math.floor((img.width - img["jg-width"]) / 2.0);
}
return row;
}
exports.justgallery = function(images, width, padding) {
var currentWidth = 0,
currentRow = [],
rows = [];
for (var i=0; i<images.length; i++) {
var img = images[i];
currentRow.push(img);
currentWidth += img.width;
if (currentWidth >= width) {
var overflow = currentWidth - width;
var adjustedRow = adjustrow(currentRow, overflow);
rows.push(adjustedRow);
currentRow = [];
currentWidth = 0;
} else {
currentWidth += padding;
}
}
if (currentRow.length > 0) {
var adjustedRow = adjustrow(currentRow, 0);
rows.push(adjustedRow);
}
return rows;
}