Skip to content

Commit

Permalink
Using exact values
Browse files Browse the repository at this point in the history
This solves the problem of row widths being slightly different in multiple line gallery (#34). The change is to use the exact values with out rounding them.
  • Loading branch information
kovacicmilan committed Jan 18, 2017
1 parent d79dac7 commit 2287627
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
14 changes: 5 additions & 9 deletions lib/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ Row.prototype = {
var itemWidthSum = this.left,
rowWidthWithoutSpacing = this.width - (this.items.length - 1) * this.spacing,
clampedToNativeRatio,
roundedHeight,
clampedHeight,
errorWidthPerItem,
roundedCumulativeErrors,
Expand All @@ -207,24 +206,21 @@ Row.prototype = {
widowLayoutStyle = 'left';
}

// Don't set fractional values in the layout.
roundedHeight = Math.round(newHeight);

// Clamp row height to edge case minimum/maximum.
clampedHeight = Math.max(this.edgeCaseMinRowHeight, Math.min(roundedHeight, this.edgeCaseMaxRowHeight));
clampedHeight = Math.max(this.edgeCaseMinRowHeight, Math.min(newHeight, this.edgeCaseMaxRowHeight));

if (roundedHeight !== clampedHeight) {
if (newHeight !== clampedHeight) {

// If row height was clamped, the resulting row/item aspect ratio will be off,
// so force it to fit the width (recalculate aspectRatio to match clamped height).
// NOTE: this will result in cropping/padding commensurate to the amount of clamping.
this.height = clampedHeight;
clampedToNativeRatio = (rowWidthWithoutSpacing / clampedHeight) / (rowWidthWithoutSpacing / roundedHeight);
clampedToNativeRatio = (rowWidthWithoutSpacing / clampedHeight) / (rowWidthWithoutSpacing / newHeight);

} else {

// If not clamped, leave ratio at 1.0.
this.height = roundedHeight;
this.height = newHeight;
clampedToNativeRatio = 1.0;

}
Expand All @@ -233,7 +229,7 @@ Row.prototype = {
this.items.forEach(function (item) {

item.top = this.top;
item.width = Math.round(item.aspectRatio * this.height * clampedToNativeRatio);
item.width = item.aspectRatio * this.height * clampedToNativeRatio;
item.height = this.height;

// Left-to-right.
Expand Down
1 change: 0 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ describe('justified-layout', function () {
});

expect(geometry.boxes[5].width).toEqual(1040);
expect(geometry.boxes[5].top).toEqual(713);
expect(geometry.boxes[6].top).toEqual(geometry.boxes[5].top + geometry.boxes[5].height + 10);

});
Expand Down

0 comments on commit 2287627

Please sign in to comment.