Skip to content

Commit 4d2df82

Browse files
committed
Change the processing the minimum width and height restrictions
1 parent 681c719 commit 4d2df82

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

src/Cropper.vue

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { CropperWrapper } from './components/service';
88
import { ResizeEvent, MoveEvent } from './core/events';
99
import { isLocal, isCrossOriginURL, isUndefined, addTimestamp, getSettings } from './core/utils';
1010
import { arrayBufferToDataURL, getImageTransforms, getStyleTransforms, prepareSource, parseImage } from './core/image';
11-
import { ALL_DIRECTIONS } from './core/constants';
11+
import { ALL_DIRECTIONS, MINIMAL_PERCENT_SIZE } from './core/constants';
1212
import * as algorithms from './core/algorithms';
1313
1414
const cn = bem('vue-advanced-cropper');
@@ -67,11 +67,9 @@ export default {
6767
},
6868
minWidth: {
6969
type: [Number, String],
70-
default: 10,
7170
},
7271
minHeight: {
7372
type: [Number, String],
74-
default: 10,
7573
},
7674
maxWidth: {
7775
type: [Number, String],
@@ -139,6 +137,10 @@ export default {
139137
type: [Boolean, Object],
140138
default: true,
141139
},
140+
imageRestriction: {
141+
type: [String],
142+
default: 'area',
143+
}
142144
},
143145
data() {
144146
return {
@@ -275,15 +277,17 @@ export default {
275277
props: this.$props
276278
});
277279
278-
if (this.minWidth > this.imageSize.width) {
279-
console.warn(`Warning: minimum width (${restrictions.minWidth}px) greater that the image width (${this.imageSize.width}px). It is set equal to the image width and width resizing was blocked`);
280-
restrictions.minWidth = this.imageSize.width;
281-
restrictions.widthFrozen = true;
280+
const minSize = Math.max(
281+
MINIMAL_PERCENT_SIZE * this.imageSize.width / this.coefficient / this.worldTransforms.scale,
282+
MINIMAL_PERCENT_SIZE * this.imageSize.height / this.coefficient / this.worldTransforms.scale,
283+
);
284+
285+
if (restrictions.minWidth < minSize) {
286+
restrictions.minWidth = Math.floor(minSize);
282287
}
283-
if (this.minHeight > this.imageSize.height) {
284-
console.warn(`Warning: minimum height (${restrictions.minHeight}px) greater that the image height (${this.imageSize.height}px). It is set equal to the image height and height resizing was blocked`);
285-
restrictions.minHeight = this.imageSize.height;
286-
restrictions.heightFrozen = true;
288+
289+
if (restrictions.minHeight < minSize) {
290+
restrictions.minHeight = Math.floor(minSize);
287291
}
288292
289293
if (restrictions.minWidth > restrictions.maxWidth) {
@@ -298,11 +302,13 @@ export default {
298302
restrictions.heightFrozen = true;
299303
}
300304
301-
if (!restrictions.maxWidth || restrictions.maxWidth > this.imageSize.width) {
302-
restrictions.maxWidth = this.imageSize.width;
303-
}
304-
if (!restrictions.maxHeight || restrictions.maxHeight > this.imageSize.height) {
305-
restrictions.maxHeight = this.imageSize.height;
305+
if (this.imageRestriction === 'area') {
306+
if (!restrictions.maxWidth || (restrictions.maxWidth > this.imageSize.width)) {
307+
restrictions.maxWidth = this.imageSize.width;
308+
}
309+
if (!restrictions.maxHeight || (restrictions.maxHeight > this.imageSize.height)) {
310+
restrictions.maxHeight = this.imageSize.height;
311+
}
306312
}
307313
308314
return restrictions;

src/core/algorithms.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function getBrokenLimits(coordinates, directions, allowedArea) {
2727
function fitConditions({ directions: oldDirections, coordinates, restrictions, preserveAspectRatio, stopOnBreak, mode = 'crop', allowedArea = {}}) {
2828
const directions = { ...oldDirections, };
2929

30-
3130
let currentWidth = getCurrentWidth(coordinates, directions);
3231
let currentHeight = getCurrentHeight(coordinates, directions);
3332

@@ -360,8 +359,8 @@ export function defaultSize ({ cropper, image, minWidth, minHeight, maxWidth, ma
360359

361360
export function percentRestrictions({ minWidth, minHeight, maxWidth, maxHeight, imageWidth, imageHeight }) {
362361
return {
363-
minWidth: minWidth / 100 * imageWidth,
364-
minHeight: minHeight / 100 * imageHeight,
362+
minWidth: minWidth ? minWidth / 100 * imageWidth : 0,
363+
minHeight: minHeight? minHeight / 100 * imageHeight : 0,
365364
maxWidth: maxWidth ? maxWidth / 100 * imageWidth : imageWidth,
366365
maxHeight: maxHeight ? maxHeight / 100 * imageHeight : imageHeight,
367366
};

src/core/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export const ALL_DIRECTIONS = ['left', 'right', 'top', 'bottom'];
55
export const COORDINATES_TYPES = ['width', 'height', 'left', 'top'];
66

77
export const XHR_DONE = 4;
8+
9+
export const MINIMAL_PERCENT_SIZE = 0.1;

0 commit comments

Comments
 (0)