advanced-cropper
Version:
The core of the advanced cropper libraries family
44 lines (39 loc) • 2.16 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib = require('tslib');
var utils = require('./utils.js');
var helpers = require('./helpers.js');
var copyState = require('../state/copyState.js');
require('../types/index.js');
require('../state/setCoordinates.js');
var approximateSize = require('./approximateSize.js');
function fitCoordinates(state, settings) {
if (state.coordinates) {
var result = copyState.copyState(state);
var aspectRatio = helpers.getAspectRatio(state, settings);
var sizeRestrictions = helpers.getSizeRestrictions(state, settings);
// Fit the size of coordinates to existing size restrictions and visible area
result.coordinates = tslib.__assign(tslib.__assign({}, state.coordinates), approximateSize.approximateSize({
width: state.coordinates.width,
height: state.coordinates.height,
aspectRatio: aspectRatio,
sizeRestrictions: state.visibleArea
? {
maxWidth: Math.min(state.visibleArea.width, sizeRestrictions.maxWidth),
maxHeight: Math.min(state.visibleArea.height, sizeRestrictions.maxHeight),
minHeight: Math.min(state.visibleArea.height, sizeRestrictions.minHeight),
minWidth: Math.min(state.visibleArea.width, sizeRestrictions.minWidth),
}
: sizeRestrictions,
}));
// Return the coordinates to the previous center
result.coordinates = utils.applyMove(result.coordinates, utils.diff(utils.getCenter(state.coordinates), utils.getCenter(result.coordinates)));
// Fit the coordinates to position restrictions and visible area
result.coordinates = utils.moveToPositionRestrictions(result.coordinates, state.visibleArea
? utils.mergePositionRestrictions(utils.coordinatesToPositionRestrictions(state.visibleArea), helpers.getPositionRestrictions(result, settings))
: helpers.getPositionRestrictions(result, settings));
return result;
}
return state;
}
exports.fitCoordinates = fitCoordinates;