Skip to content

Commit

Permalink
When rejectSmallImage is false, allow small image to be zoomed down to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Cheng committed Jun 18, 2015
1 parent c747c79 commit 869a63e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
9 changes: 8 additions & 1 deletion dist/jquery.cropit.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ return /******/ (function(modules) { // webpackBootstrap
previewSize: this.previewSize,
exportZoom: this.options.exportZoom,
maxZoom: this.options.maxZoom,
minZoom: this.options.minZoom
minZoom: this.options.minZoom,
rejectSmallImage: this.options.rejectSmallImage
});
this.setZoom((0, _utils.exists)(zoom) ? zoom : this.zoom);

Expand Down Expand Up @@ -819,6 +820,8 @@ return /******/ (function(modules) { // webpackBootstrap
var maxZoom = _ref$maxZoom === undefined ? 1 : _ref$maxZoom;
var _ref$minZoom = _ref.minZoom;
var minZoom = _ref$minZoom === undefined ? 'fill' : _ref$minZoom;
var _ref$rejectSmallImage = _ref.rejectSmallImage;
var rejectSmallImage = _ref$rejectSmallImage === undefined ? false : _ref$rejectSmallImage;

var widthRatio = previewSize.w / imageSize.w;
var heightRatio = previewSize.h / imageSize.h;
Expand All @@ -829,6 +832,10 @@ return /******/ (function(modules) { // webpackBootstrap
this.minZoom = Math.max(widthRatio, heightRatio);
}

if (!rejectSmallImage) {
this.minZoom = Math.min(this.minZoom, 1);
}

this.maxZoom = Math.max(this.minZoom, maxZoom / exportZoom);
}
}, {
Expand Down
1 change: 1 addition & 0 deletions src/cropit.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ class Cropit {
exportZoom: this.options.exportZoom,
maxZoom: this.options.maxZoom,
minZoom: this.options.minZoom,
rejectSmallImage: this.options.rejectSmallImage,
});
this.setZoom(exists(zoom) ? zoom : this.zoom);

Expand Down
6 changes: 5 additions & 1 deletion src/zoomer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Zoomer {
this.minZoom = this.maxZoom = 1;
}

setup({ imageSize, previewSize, exportZoom=1, maxZoom=1, minZoom='fill' }) {
setup({ imageSize, previewSize, exportZoom=1, maxZoom=1, minZoom='fill', rejectSmallImage=false }) {
const widthRatio = previewSize.w / imageSize.w;
const heightRatio = previewSize.h / imageSize.h;

Expand All @@ -14,6 +14,10 @@ class Zoomer {
this.minZoom = Math.max(widthRatio, heightRatio);
}

if (!rejectSmallImage) {
this.minZoom = Math.min(this.minZoom, 1);
}

this.maxZoom = Math.max(this.minZoom, maxZoom / exportZoom);
}

Expand Down
20 changes: 20 additions & 0 deletions test/zoomer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ describe('Zoomer', () => {
zoomer.setup({
imageSize: { w: 4, h: 2 },
previewSize: { w: 1, h: 1 },
minZoom: 'fill',
});
expect(zoomer.minZoom).toBe(0.5);

zoomer.setup({
imageSize: { w: 2, h: 4 },
previewSize: { w: 1, h: 1 },
minZoom: 'fill',
});
expect(zoomer.minZoom).toBe(0.5);

zoomer.setup({
imageSize: { w: 2, h: 2 },
previewSize: { w: 1, h: 1 },
minZoom: 'fill',
});
expect(zoomer.minZoom).toBe(0.5);
});
Expand Down Expand Up @@ -57,6 +60,23 @@ describe('Zoomer', () => {
expect(zoomer.minZoom).toBe(0.5);
});

it('sets minZoom to 1 if image is small and rejectSmallImage is false', () => {
zoomer.setup({
imageSize: { w: 1, h: 1 },
previewSize: { w: 2, h: 2 },
rejectSmallImage: false,
});
expect(zoomer.minZoom).toBe(1);

zoomer.setup({
imageSize: { w: 1, h: 3 },
previewSize: { w: 2, h: 2 },
minZoom: 'fill',
rejectSmallImage: false,
});
expect(zoomer.minZoom).toBe(1);
});

it('sets maxZoom to minZoom if image is smaller than preview', () => {
zoomer.setup({
imageSize: { w: 4, h: 2 },
Expand Down

0 comments on commit 869a63e

Please sign in to comment.