@@ -6,9 +6,9 @@ import debounce from 'debounce';
66import { RectangleStencil } from ' ./components/stencils' ;
77import { CropperWrapper } from ' ./components/service' ;
88import { ResizeEvent , MoveEvent } from ' ./core/events' ;
9- import { isLocal , isCrossOriginURL , isUndefined , addTimestamp , getSettings } from ' ./core/utils' ;
9+ import { isLocal , isCrossOriginURL , isUndefined , addTimestamp , getSettings , parseNumber } from ' ./core/utils' ;
1010import { arrayBufferToDataURL , getImageTransforms , getStyleTransforms , prepareSource , parseImage } from ' ./core/image' ;
11- import { ALL_DIRECTIONS , MINIMAL_PERCENT_SIZE } from ' ./core/constants' ;
11+ import { ALL_DIRECTIONS , MINIMAL_PERCENT_SIZE , IMAGE_RESTRICTIONS } from ' ./core/constants' ;
1212import * as algorithms from ' ./core/algorithms' ;
1313
1414const cn = bem (' vue-advanced-cropper' );
@@ -41,6 +41,10 @@ export default {
4141 type: String ,
4242 default: null ,
4343 },
44+ allowedArea: {
45+ type: Function ,
46+ default: algorithms .allowedArea ,
47+ },
4448 resizeAlgorithm: {
4549 type: Function ,
4650 default: algorithms .resize ,
@@ -140,6 +144,9 @@ export default {
140144 imageRestriction: {
141145 type: [String ],
142146 default: ' area' ,
147+ validator (value ) {
148+ return IMAGE_RESTRICTIONS .indexOf (value) !== - 1 ;
149+ }
143150 }
144151 },
145152 data () {
@@ -199,7 +206,7 @@ export default {
199206 };
200207
201208 // Disable some interactions for user convenience
202- settings .touchMove .enabled = settings .touchMove .enabled && this .worldTransforms .scale > 1 ;
209+ settings .touchMove .enabled = settings .touchMove .enabled && ( this .worldTransforms .scale !== 1 || this . imageRestriction === ' none ' ) ;
203210
204211 return settings;
205212 },
@@ -265,13 +272,20 @@ export default {
265272 return result;
266273 },
267274 stencilRestrictions () {
275+ const oldRestrictions = {
276+ minWidth: ! isUndefined (this .minWidth ) ? this .minWidth : 0 ,
277+ minHeight: ! isUndefined (this .minHeight ) ? this .minHeight : 0 ,
278+ maxWidth: ! isUndefined (this .maxWidth ) ? this .maxWidth : Infinity ,
279+ maxHeight: ! isUndefined (this .maxHeight ) ? this .maxHeight : Infinity ,
280+ };
281+
268282 const restrictions = migrateAlgorithm (this .restrictions , ' restrictions' , (args ) => [
269283 args .minWidth , args .minHeight , args .maxWidth , args .maxHeight , args .imageWidth , args .imageHeight
270284 ])({
271- minWidth: Number ( this .minWidth ),
272- minHeight: Number ( this .minHeight ),
273- maxWidth: Number ( this .maxWidth ),
274- maxHeight: Number ( this .maxHeight ),
285+ minWidth: parseNumber ( oldRestrictions .minWidth ),
286+ minHeight: parseNumber ( oldRestrictions .minHeight ),
287+ maxWidth: parseNumber ( oldRestrictions .maxWidth ),
288+ maxHeight: parseNumber ( oldRestrictions .maxHeight ),
275289 imageWidth: this .imageSize .width ,
276290 imageHeight: this .imageSize .height ,
277291 props: this .$props
@@ -302,7 +316,7 @@ export default {
302316 restrictions .heightFrozen = true ;
303317 }
304318
305- if (this .imageRestriction === ' area ' ) {
319+ if (this .imageRestriction !== ' none ' ) {
306320 if (! restrictions .maxWidth || (restrictions .maxWidth > this .imageSize .width )) {
307321 restrictions .maxWidth = this .imageSize .width ;
308322 }
@@ -330,6 +344,9 @@ export default {
330344 maxHeight () {
331345 this .onPropsChange ();
332346 },
347+ imageRestriction () {
348+ this .resetCoordinates ();
349+ },
333350 stencilProps (oldProps , newProps ) {
334351 const significantProps = [' aspectRatio' , ' minAspectRatio' , ' maxAspectRatio' ];
335352 if (significantProps .find (prop => oldProps[prop] !== newProps[prop])) {
@@ -386,6 +403,7 @@ export default {
386403 worldTransforms: this .worldTransforms ,
387404 coefficient: this .coefficient ,
388405 imageSize: this .imageSize ,
406+ allowedArea: this .getAllowedArea (true )
389407 });
390408
391409 this .worldTransforms = worldTransforms;
@@ -497,7 +515,7 @@ export default {
497515 this .resizeAlgorithm ({
498516 coordinates: this .coordinates ,
499517 restrictions: this .stencilRestrictions ,
500- allowedArea: this .allowedArea (),
518+ allowedArea: this .getAllowedArea (),
501519 aspectRatio: this .stencilAspectRatios (),
502520 resizeEvent
503521 })
@@ -520,6 +538,9 @@ export default {
520538 frozenDirections: this .frozenDirections ,
521539 stencilCoordinates: this .stencilCoordinates ,
522540 worldTransforms: this .worldTransforms ,
541+ allowedArea: this .getAllowedArea (true ),
542+ minScale: this .imageRestriction === ' none' ? MINIMAL_PERCENT_SIZE : 1 ,
543+ fitImage: this .imageRestriction === ' area'
523544 });
524545 this .worldTransforms = worldTransforms;
525546 this .onChangeCoordinates (coordinates);
@@ -532,7 +553,7 @@ export default {
532553 this .onChangeCoordinates (
533554 this .moveAlgorithm ({
534555 coordinates: this .coordinates ,
535- allowedArea: this .allowedArea (),
556+ allowedArea: this .getAllowedArea (),
536557 moveEvent
537558 })
538559 );
@@ -590,12 +611,8 @@ export default {
590611 },
591612 applyTransforms (transforms , autoZoom = false ) {
592613 const aspectRatio = this .stencilAspectRatios ();
593- const allowedArea = {
594- left: 0 ,
595- top: 0 ,
596- right: this .imageSize .width ,
597- bottom: this .imageSize .height
598- };
614+
615+ const allowedArea = this .getAllowedArea (true );
599616
600617 const moveAlgorithm = (prevCoordinates , newCoordinates ) => {
601618 return this .moveAlgorithm ({
@@ -649,7 +666,7 @@ export default {
649666 }
650667 });
651668
652- if (autoZoom && this . worldTransforms . scale > 1 ) {
669+ if (autoZoom) {
653670 this .autoZoom (coordinates);
654671 } else {
655672 if (this .worldTransforms .scale > 1 ) {
@@ -791,13 +808,13 @@ export default {
791808 }
792809 });
793810 },
794- allowedArea ( ) {
795- return {
796- left : - this . worldTransforms . shift . left / this . worldTransforms . scale ,
797- top : - this .worldTransforms . shift . top / this . worldTransforms . scale ,
798- right : ( this .boundarySize . width - this . worldTransforms . shift . left / this . coefficient ) / this . worldTransforms . scale * this . coefficient ,
799- bottom : ( this .boundarySize . height - this . worldTransforms . shift . top / this . coefficient ) / this . worldTransforms . scale * this . coefficient ,
800- };
811+ getAllowedArea ( breakBoundaries ) {
812+ return this . allowedArea ( {
813+ breakBoundaries ,
814+ imageSize : this .imageSize ,
815+ worldTransforms : this .worldTransforms ,
816+ imageRestriction : this .imageRestriction
817+ }) ;
801818 },
802819 stencilAspectRatios () {
803820 if (this .$refs .stencil .aspectRatios ) {
0 commit comments