Skip to content

Commit

Permalink
Added fill/fit minZoom option, close #20
Browse files Browse the repository at this point in the history
When minZoom is set to "fit", the image can be zoomed out until it fits
the preview area. minZoom defaults to "fill".

Removed fitWidth/fitHeight options.
  • Loading branch information
Scott Cheng committed Feb 9, 2015
1 parent f197844 commit 34f79e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
32 changes: 10 additions & 22 deletions src/cropit.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ class Cropit
imageState: null
allowCrossOrigin: false
allowDragNDrop: true
fitWidth: false
fitHeight: false
freeMove: false
minZoom: 'fill'

@PREVIEW_EVENTS: do ->
[
Expand Down Expand Up @@ -257,19 +256,15 @@ class Cropit
ret = x: offset.x, y: offset.y

unless @options.freeMove
if @imageSize.w * @zoom <= @previewSize.w
ret.x = 0
else if ret.x > 0
ret.x = 0
else if ret.x + @imageSize.w * @zoom < @previewSize.w
ret.x = @previewSize.w - @imageSize.w * @zoom

if @imageSize.h * @zoom <= @previewSize.h
ret.y = 0
else if ret.y > 0
ret.y = 0
else if ret.y + @imageSize.h * @zoom < @previewSize.h
ret.y = @previewSize.h - @imageSize.h * @zoom
if @imageSize.w * @zoom >= @previewSize.w
ret.x = Math.min 0, Math.max ret.x, @previewSize.w - @imageSize.w * @zoom
else
ret.x = Math.max 0, Math.min ret.x, @previewSize.w - @imageSize.w * @zoom

if @imageSize.h * @zoom >= @previewSize.h
ret.y = Math.min 0, Math.max ret.y, @previewSize.h - @imageSize.h * @zoom
else
ret.y = Math.max 0, Math.min ret.y, @previewSize.h - @imageSize.h * @zoom

ret.x = @round ret.x
ret.y = @round ret.y
Expand Down Expand Up @@ -341,13 +336,6 @@ class Cropit
w: @previewSize.w
h: @previewSize.h

if @options.fitHeight and not @options.fitWidth and
@imageSize.w * @zoom < @previewSize.w
croppedSize.w = @imageSize.w * @zoom
else if @options.fitWidth and not @options.fitHeight and
@imageSize.h * @zoom < @previewSize.h
croppedSize.h = @imageSize.h * @zoom

exportZoom = if exportOptions.originalSize then 1 / @zoom else @options.exportZoom

canvas = $ '<canvas />'
Expand Down
6 changes: 1 addition & 5 deletions src/zoomer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ class Zoomer
widthRatio = previewSize.w / imageSize.w
heightRatio = previewSize.h / imageSize.h

if options?.fitWidth && !options?.fitHeight
@minZoom = widthRatio
else if options?.fitHeight && !options?.fitWidth
@minZoom = heightRatio
else if options?.fitWidth && options?.fitHeight
if options?.minZoom is 'fit'
@minZoom = if widthRatio < heightRatio then widthRatio else heightRatio
else
@minZoom = if widthRatio < heightRatio then heightRatio else widthRatio
Expand Down

0 comments on commit 34f79e3

Please sign in to comment.