fengyuanchen / cropper

⚠️ [Deprecated] No longer maintained, please use https://github.com/fengyuanchen/jquery-cropper
MIT License
7.75k stars 1.74k forks source link

Prevent zoom in when width or height reached #961

Closed Zyostes closed 6 years ago

Zyostes commented 6 years ago

I actually have this configuration :

var width = 340;
var height = 340;
var cropImage = $('.crop-image:visible');
                        cropImage.cropper({
                            aspectRatio: width/height,
                            minCropBoxWidth: width,
                            minCropBoxHeight: height,
                            viewMode: 1,
                            preview: '.preview-image',
                            crop: function(e) {
                                $('#team_photoCrop').val(e.x+';'+e.y+';'+e.width+';'+e.height);
                            }
                        }).on('ready', function() {
                            centerModals();
                        }).on('zoom', function(e) {
                            if(e.oldRatio < e.ratio) {
                                var data = cropImage.cropper('getData');
                                if(data.width <= width || data.height <= height) e.preventDefault();
                            }
                        });

I am trying to prevent zoom in when width or height is reached but with this code, when zoom stops, the data width/height is 317, it stops too late.

Thank you per advance.

fengyuanchen commented 6 years ago

Restrict the width and height of the crop box doesn't make sense. Why not just resize the cropped image to your wanted sizes after cropped?

fengyuanchen commented 6 years ago
var width = 340;
var height = 340;
var cropImage = $('.crop-image:visible');

cropImage.cropper({
    aspectRatio: width/height,
    viewMode: 1,
    preview: '.preview-image',
}).on('ready', function() {
  // Resize the cropped image to 340x340
  cropImage.cropper('getCroppedCanvas', {
    width: width,
    height: height,
  });
});