@evg-b/evg-ui
Version:
EVG-UI library inspired by Material Design.
69 lines (56 loc) • 2.04 kB
JavaScript
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
var RatioImg = /*#__PURE__*/function () {
function RatioImg(width, height) {
_classCallCheck(this, RatioImg);
this.coefficientAnamorphismW = Number((width / height).toFixed(5));
this.coefficientAnamorphismH = Number((height / width).toFixed(5));
}
/*
@param size - размер нового отрезка
mod - 'w' | 'h' - ширина или высота
@return {newWidth:int,newHeight:int}
*/
_createClass(RatioImg, [{
key: "reCaclNewRation",
value: function reCaclNewRation(size, mod) {
switch (mod) {
case 'w':
return {
newWidth: size,
newHeight: size / this.coefficientAnamorphismW | 0
};
case 'h':
return {
newWidth: size / this.coefficientAnamorphismH | 0,
newHeight: size
};
}
}
}]);
return RatioImg;
}();
function calcMaxSizeRatio (naturalWidth, naturalHeight, clientWidth, clientHeight) {
var img = new RatioImg(naturalWidth, naturalHeight);
var acceptableLimitWidth = clientWidth >= 1024 ? 1000 : clientWidth,
acceptableLimitHeight = clientHeight * 0.9 | 0;
var notFitLimitWidth = naturalWidth > acceptableLimitWidth,
notFitLimitHeight = naturalHeight > acceptableLimitHeight;
if (notFitLimitWidth && notFitLimitHeight) {
var newSizetoH = img.reCaclNewRation(acceptableLimitHeight, 'h');
if (newSizetoH.newWidth > acceptableLimitWidth) {
return img.reCaclNewRation(acceptableLimitWidth, 'w');
} else {
return newSizetoH;
}
} else if (!notFitLimitWidth && !notFitLimitHeight) {
return img.reCaclNewRation(naturalWidth, 'w');
}
if (notFitLimitWidth) {
return img.reCaclNewRation(acceptableLimitWidth, 'w');
}
if (notFitLimitHeight) {
return img.reCaclNewRation(acceptableLimitHeight, 'h');
}
}
export default calcMaxSizeRatio;