Skip to content

Commit

Permalink
Merge pull request #36 from kovacicmilan/use-exact-values
Browse files Browse the repository at this point in the history
Using exact values
  • Loading branch information
pdokas authored Nov 6, 2017
2 parents 07b7a58 + 2287627 commit a4ace4c
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 a4ace4c

Please sign in to comment.