scottcheng / cropit

A "customizable crop and zoom" jQuery plugin.
http://scottcheng.github.io/cropit/
MIT License
875 stars 304 forks source link

Disable moving image, if image is smaller than container #221

Open Ali-Azmoud opened 7 years ago

Ali-Azmoud commented 7 years ago

Hi Thanks for your great plugin. by default, if I set minZoom to fit then image will fit and center. But I can move image left or right or top and down based on which edge is smaller. is there any feature to always keep image in center if it's not fully filled the preview-image box by both edges. Disable moving.

I hope you understand what I mean.

Ali-Azmoud commented 7 years ago

Okay, I made it myself. Here is the code if the author of this great plugin wants to add it as a feature to the plugin. Just replace these lines of code with the highlighted lines in file dist/jquery.cropit.js file.

}, {
    key: 'onMove',
    value: function onMove(e) {
        var eventPosition = this.getEventPosition(e);

        var imagePreviewWidth = this.imageWidth * this.zoom,
            imagePreviewHeight = this.imageHeight * this.zoom;

        if (this.moveContinue && eventPosition) {
            if (imagePreviewWidth < this.previewSize.width) {
                this.offset = {
                    x: (this.previewSize.width - imagePreviewWidth) / 2,
                    y: this.offset.y + eventPosition.y - this.origin.y
                };
            } else if (imagePreviewHeight < this.previewSize.height) {
                this.offset = {
                    y: (this.previewSize.height - imagePreviewHeight) / 2,
                    x: this.offset.x + eventPosition.x - this.origin.x
                };
            } else {
                this.offset = {
                    x: this.offset.x + eventPosition.x - this.origin.x,
                    y: this.offset.y + eventPosition.y - this.origin.y
                };
            }
        }

        this.origin = eventPosition;

        e.stopPropagation();
        return false;
    }
}, {

That's it.

Now if width or height of the previewImage is smaller than Preview Container then image will not move in that direction and jumps to center of Preview Container ( based on width or height )