Skip to content

Commit afd7884

Browse files
committed
feat: originalSize
Add `originalSize` option, which allows to display a downscaled version of the image in the cropping interface, but use the original size for crop attributes.
1 parent ffbdcd4 commit afd7884

6 files changed

Lines changed: 80 additions & 77 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ cropper.on('change', function(crop) {
7373
| `actions` | Object | {pan, zoomOnDoubleClick, resize } Allowed user interactions. By default they are all set to `true`. |
7474
| `showSurroundingImage` | String | {always, never, panning } Shows the uncropped part of the image. By default set to `never`. |
7575
| `surroundingImageOpacity` | Number | {0.0 - 1.0} Sets the opacity when showing the uncropped part of the image. By default set to `0.2`. |
76+
| `originalSize` | Object | Original image size, can be used to display a downscaled version of the image in the cropping interface, but use the original size for crop attributes; e.g. `{width: 4000, height: 3000}`. |
7677

7778
### HTML
7879

src/crop.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Events = require('./events')
55
module.exports = class Crop {
66
constructor ({
77
arena, view, img, outline, url, fixedWidth, fixedHeight,
8-
minViewWidth, minViewHeight, minViewRatio, maxViewRatio, crop,
8+
minViewWidth, minViewHeight, minViewRatio, maxViewRatio, originalSize, crop,
99
zoomStep, maxArea, actions, minResolution, surroundingImageOpacity,
1010
showSurroundingImage
1111
}) {
@@ -21,6 +21,7 @@ module.exports = class Crop {
2121
this.minViewHeight = minViewHeight
2222
this.minViewRatio = minViewRatio
2323
this.maxViewRatio = maxViewRatio
24+
this.originalSize = originalSize
2425
this.actions = actions
2526
this.minResolution = minResolution
2627
this.surroundingImageOpacity = surroundingImageOpacity
@@ -102,7 +103,13 @@ module.exports = class Crop {
102103
this.zoomAllOut()
103104
}
104105

105-
onPreviewReady ({width, height}) {
106+
onPreviewReady (previewImageSize) {
107+
this.checkRatio(previewImageSize)
108+
const {width, height} = this.originalSize || previewImageSize
109+
110+
// console.log(this.originalSize, previewImageSize, {width, height})
111+
this.preview.updateImageDimensions({width, height})
112+
106113
let keepDimension
107114
if (!this.isInitialized) {
108115
this.events = new Events({
@@ -532,6 +539,18 @@ module.exports = class Crop {
532539
return {width, height}
533540
}
534541

542+
checkRatio (previewImageSize) {
543+
if (this.originalSize) {
544+
const expectedRatio = this.originalSize.width / this.originalSize.height
545+
const actualRatio = previewImageSize.width / previewImageSize.height
546+
const percentageChange = ((actualRatio - expectedRatio) / expectedRatio) * 100
547+
if (Math.abs(percentageChange) > 1) {
548+
throw new Error(`srcissors: Displayed image has a different image ratio than the ` +
549+
`one configured in 'originalRatio': ${expectedRatio} vs ${actualRatio}`)
550+
}
551+
}
552+
}
553+
535554
// Calculations
536555
// ------------
537556
//

src/preview.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ module.exports = class Preview {
1515
const height = this.img.height()
1616
this.ratio = width / height
1717

18-
this.updateImageDimensions({width, height})
19-
this.onReady({width: this.width, height: this.height})
18+
this.onReady({width, height})
2019
this.img.show()
2120
})
2221
}

src/preview_css_zoom.js

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/srcissors.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Crop = require('./crop')
44
module.exports = {
55
new ({
66
arena, url, fixedWidth, fixedHeight, minWidth, minHeight,
7-
minRatio, maxRatio, maxArea, zoomStep, crop, actions, minResolution,
7+
minRatio, maxRatio, maxArea, originalSize, zoomStep, crop, actions, minResolution,
88
surroundingImageOpacity, showSurroundingImage
99
}) {
1010
arena = $(arena)
@@ -46,6 +46,9 @@ module.exports = {
4646
minViewRatio: minRatio, // {Number} e.g. 1.5/2
4747
maxViewRatio: maxRatio, // {Number} e.g. 2/1
4848
maxArea, // {Number} 0.8 -> max 80% of arena area are covered by the preview
49+
originalSize, // {Object} Original image size, can be used to display a downscaled
50+
// version of the image in the cropping interface, but use the original
51+
// size for crop attributes; e.g. {width: 4000, height: 3000}
4952
zoomStep, // {Number} e.g. 1.25 -> 125%
5053
actions: allowedActions,
5154
minResolution

test/specs/srcissors_spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,57 @@ describe('srcissors', function () {
320320
expect(outline.get(0).style.opacity).to.equal('0')
321321
})
322322
})
323+
324+
describe('with 4000x3000 originalSize', function () {
325+
326+
beforeEach(function (done) {
327+
this.arena = $(template)
328+
this.arena.css({width: 100, height: 100})
329+
$(document.body).append(this.arena)
330+
331+
// Crop a 400x300 image with 4000x3000 originalSize
332+
this.crop = srcissors.new({
333+
arena: this.arena,
334+
originalSize: {width: 4000, height: 3000},
335+
url: '/base/test/images/diagonal.jpg'
336+
})
337+
this.crop.on('ready', done)
338+
})
339+
340+
afterEach(function () {
341+
this.arena.remove()
342+
})
343+
344+
it('has initialized the image correctly', function () {
345+
expect(this.crop.imageWidth).to.equal(4000)
346+
expect(this.crop.imageHeight).to.equal(3000)
347+
})
348+
349+
it('fires a change event after ready', function (done) {
350+
this.crop.on('change', function (crop) {
351+
expect(crop).to.deep.equal({
352+
x: 0,
353+
y: 0,
354+
width: 4000,
355+
height: 3000
356+
})
357+
358+
done()
359+
})
360+
})
361+
362+
it('scales crop coordinates on zoom', function (done) {
363+
this.crop.zoomIn()
364+
this.crop.on('change', function (crop) {
365+
expect(crop).to.deep.equal({
366+
x: 400,
367+
y: 300,
368+
width: 3200,
369+
height: 2400
370+
})
371+
372+
done()
373+
})
374+
})
375+
})
323376
})

0 commit comments

Comments
 (0)