Skip to content

Commit

Permalink
Ensure correct image aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
marceltschoppch committed Feb 15, 2018
1 parent 40a50b5 commit ac6d682
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/js/items-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ var _getItemAt,
var w = maxRes ? item.w : Math.round(item.w * item.fitRatio),
h = maxRes ? item.h : Math.round(item.h * item.fitRatio);

// ensure correct aspect ratio
if(img.naturalHeight && img.naturalWidth) {
var new_h = w * (img.naturalHeight / img.naturalWidth);
if(new_h > h) {
var new_w = h * (img.naturalWidth / img.naturalHeight);
img.style.marginLeft = (w - new_w) / 2 + 'px';
w = new_w;
} else {
img.style.marginTop = (h - new_h) / 2 + 'px';
h = new_h;
}
}

if(item.placeholder && !item.loaded) {
item.placeholder.style.width = w + 'px';
item.placeholder.style.height = h + 'px';
Expand Down Expand Up @@ -490,4 +503,4 @@ _registerModule('Controller', {
}

}
});
});

0 comments on commit ac6d682

Please sign in to comment.