advanced-cropper
Version:
The core of the advanced cropper libraries family
120 lines (115 loc) • 5.48 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib = require('tslib');
var index = require('../../utils/index.js');
var utils = require('../../service/utils.js');
var helpers = require('../../service/helpers.js');
var copyState = require('../../state/copyState.js');
require('../../types/index.js');
require('../../state/setCoordinates.js');
var approximateSize = require('../../service/approximateSize.js');
var defaultStencilConstraints = require('../../defaults/defaultStencilConstraints.js');
function fixedStencilConstraints(rawSettings, stencilOptions) {
var defaultConstraints = defaultStencilConstraints.defaultStencilConstraints({}, stencilOptions);
return {
stencilSize: function (state, settings) {
var previousSize = index.isFunction(rawSettings.stencilSize)
? rawSettings.stencilSize(state, settings)
: rawSettings.stencilSize;
return approximateSize.approximateSize(tslib.__assign(tslib.__assign({}, previousSize), { aspectRatio: utils.aspectRatioIntersection(defaultConstraints.aspectRatio, utils.createAspectRatio(utils.ratio(previousSize))) }));
},
};
}
function getStencilSize(state, settings) {
var boundary = state.boundary;
var size = index.isFunction(settings.stencilSize) ? settings.stencilSize(state, settings) : settings.stencilSize;
if (size.width > boundary.width || size.height > boundary.height) {
size = approximateSize.approximateSize({
sizeRestrictions: {
maxWidth: boundary.width,
maxHeight: boundary.height,
minWidth: 0,
minHeight: 0,
},
width: size.width,
height: size.height,
aspectRatio: {
minimum: utils.ratio(size),
maximum: utils.ratio(size),
},
});
}
return size;
}
function sizeRestrictions(state, settings) {
var stencilSize = getStencilSize(state, tslib.__assign(tslib.__assign({}, settings), { stencilSize: settings.stencilSize }));
var areaRestrictions = helpers.getAreaSizeRestrictions(state, settings);
return {
maxWidth: (areaRestrictions.maxWidth * stencilSize.width) / state.boundary.width,
maxHeight: (areaRestrictions.maxHeight * stencilSize.height) / state.boundary.height,
minWidth: 0,
minHeight: 0,
};
}
function defaultSize(state, settings) {
var imageSize = state.imageSize, visibleArea = state.visibleArea, boundary = state.boundary;
var sizeRestrictions = helpers.getSizeRestrictions(state, settings);
var aspectRatio = helpers.getAspectRatio(state, settings);
var stencilSize = index.isFunction(settings.stencilSize) ? settings.stencilSize(state, settings) : settings.stencilSize;
var area = (visibleArea || imageSize);
var height, width;
if (utils.ratio(area) > utils.ratio(boundary)) {
height = (stencilSize.height * area.height) / boundary.height;
width = height * utils.ratio(stencilSize);
}
else {
width = (stencilSize.width * area.width) / boundary.width;
height = width / utils.ratio(stencilSize);
}
return approximateSize.approximateSize({
width: width,
height: height,
aspectRatio: aspectRatio,
sizeRestrictions: sizeRestrictions,
});
}
function aspectRatio(state, settings) {
var value = utils.ratio(getStencilSize(state, settings));
return {
minimum: value,
maximum: value,
};
}
function fixedStencilAlgorithm(state, settings) {
if (helpers.isInitializedState(state)) {
var result = copyState.copyState(state);
var stencil = getStencilSize(state, settings);
// First of all try to resize visible area as much as possible:
result.visibleArea = utils.applyScale(result.visibleArea, (result.coordinates.width * result.boundary.width) / (result.visibleArea.width * stencil.width));
// Check that visible area doesn't break the area restrictions:
var scale = utils.fitToSizeRestrictions(result.visibleArea, helpers.getAreaSizeRestrictions(result, settings));
if (scale !== 1) {
result.visibleArea = utils.applyScale(result.visibleArea, scale);
result.coordinates = utils.applyScale(result.coordinates, scale);
}
result.visibleArea = utils.applyMove(result.visibleArea, utils.diff(utils.getCenter(result.coordinates), utils.getCenter(result.visibleArea)));
// Center stencil in visible area:
result.visibleArea = utils.moveToPositionRestrictions(result.visibleArea, helpers.getAreaPositionRestrictions(result, settings));
result.coordinates = utils.moveToPositionRestrictions(result.coordinates, utils.mergePositionRestrictions(utils.coordinatesToPositionRestrictions(result.visibleArea), helpers.getAreaPositionRestrictions(result, settings)));
return result;
}
return state;
}
function fixedStencil(state, settings, action) {
if (action && action.immediately) {
return fixedStencilAlgorithm(state, settings);
}
return state;
}
exports.aspectRatio = aspectRatio;
exports.defaultSize = defaultSize;
exports.fixedStencil = fixedStencil;
exports.fixedStencilAlgorithm = fixedStencilAlgorithm;
exports.fixedStencilConstraints = fixedStencilConstraints;
exports.getStencilSize = getStencilSize;
exports.sizeRestrictions = sizeRestrictions;