-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.fake_crop.coffee
46 lines (35 loc) · 1.37 KB
/
jquery.fake_crop.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Created by Flinto, LLC
# https://www.flinto.com
jQuery.fn.fakeCrop = () ->
fakeCrop = (wrap) ->
# ensure wrap can hide image extremities
wrap.css(overflow: 'hidden')
img = wrap.find('img')
# reset any existing margins/width
img.css(marginLeft: "", marginTop: "", width: "", height: "")
# initial dimensions based on the image's dimensions
initial =
width: img[0].width
height: img[0].height
# final dimensions are based on the container's dimensions
final =
width: wrap.width()
height: wrap.height()
initial.ratio = initial.width / initial.height
final.ratio = final.width / final.height
# use the width to height ratios to determine in which direction the image will be constrained
if initial.ratio == final.ratio
img.width(final.width)
else
if initial.ratio < final.ratio
[dimension, otherDimension, offsetDirection] = ['width', 'height', 'top']
else
[dimension, otherDimension, offsetDirection] = ['height', 'width', 'left']
# set the constrained dimension value
img[dimension](final[dimension])
# determine and set the necessary offset in the other dimension
offset = (img[otherDimension]() - final[otherDimension]) / -2
img.css "margin-#{offsetDirection}", "#{offset}px"
img.show()
$(this).each (i, e) => fakeCrop($(e))
return $(this)