-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitobject.js
228 lines (192 loc) · 6.78 KB
/
fitobject.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @preserve fitobject - v0.4.7 - 2017-05-11
* Size and position an object to fit its container.
* https://github.com/dannydb/fitobject
* Copyright (c) 2017 Danny DeBelius; Licensed MIT
*/
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(['jquery'], factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory(require("jquery"));
} else {
root.fitObject = factory(root.jQuery);
}
}(this, function($) {
var fitObject = function (params) {
var $els = $(this);
$els.each(function(index, el) {
var $object = $(this);
var $container = null;
var $objectWrapper = null;
var objectRatio = null;
var containerRatio = null;
var isShallowObject = null;
var fullObject = {};
var offset = {};
var cropped = {};
var newCss = null;
var options = {
container: null,
fit: 'cover',
safeArea: {
'top': 0,
'right': 0,
'bottom': 0,
'left': 0
}
}
if (params && typeof(params) === 'object') {
options = $.extend(options, params);
}
if (params && typeof(params) === 'string') {
options['fit'] = params;
}
if ($object.data('safe-area')) {
options['safeArea'] = $object.data('safe-area');
}
if ($object.data('object-fit')) {
options['fit'] = $object.data('object-fit');
}
if ($object.data('fit-container')) {
options['container'] = $object.data('fit-container');
}
$objectWrapper = $object.parents('.fit-object-wrapper');
$container = getContainer($object, $objectWrapper, options);
// Make sure the container has dimensions before starting calculations.
$container.css({
'display': 'block',
'visibility': 'visible'
});
// Calculate object and container aspect ratios
objectRatio = $object.innerHeight() / $object.width();
containerRatio = $container.height() / $container.width();
isShallowObject = objectRatio < containerRatio;
// Calculate full object dimensions to be cropped
fullObject['height'] = $container.width() * objectRatio;
fullObject['width'] = $container.height() / objectRatio;
// Calculate the pixels hidden by the cover fit crop
cropped['x'] = fullObject['width'] - $container.width();
cropped['y'] = fullObject['height'] - $container.height();
// Calculate the centering offset
offset['x'] = ($container.width() - fullObject['width']) / 2;
offset['y'] = ($container.height() - fullObject['height']) / 2;
// Cover image cropped horizontally
if (options['fit'] === 'cover' && isShallowObject){
newCss = {
height: $container.height(),
width: fullObject['width'],
left: getSafeOffset('horizontal', offset['x'], cropped['x'], options),
top: 0
}
}
// Cover image cropped vertically
if (options['fit'] === 'cover' && !isShallowObject){
newCss = {
height: fullObject['height'],
width: $container.width(),
left: 0,
top: getSafeOffset('vertical', offset['y'], cropped['y'], options)
}
}
// Contained image compressed horizontally
if (options['fit'] === 'contain' && isShallowObject){
newCss = {
height: fullObject['height'],
width: $container.width(),
left: 0,
top: offset['y']
}
}
// Contained image compressed vertically
if (options['fit'] === 'contain' && !isShallowObject){
newCss = {
height: $container.height(),
width: fullObject['width'],
left: offset['x'],
top: 0
}
}
// Remove the inline style applied before calculations.
$container.attr('style', null);
updatePosition($object, $objectWrapper, $container, newCss);
})
}
/**
* Get the element that will act as the object's container
* @param {Object} $object The object
* @param {Object} $objectWrapper The object fit wrapper
* @param {Object} options The object fit params
* @return {Object} The object's container
*/
var getContainer = function($object, $objectWrapper, options) {
var $container = null;
if ($objectWrapper.length === 0 && options['container'] === null) {
$container = $object.parent();
} else if (options['container'] === null) {
$container = $objectWrapper.parent();
} else {
$container = $(options['container']);
}
return $container;
}
/**
* Apply the new position CSS to the object
* @param {Object} $object The object
* @param {Object} $objectWrapper The object fit wrapper
* @param {Object} $container The object's container
* @param {Object} newCss The new object position styles
*/
var updatePosition = function($object, $objectWrapper, $container, newCss) {
$.extend(newCss, {
'position': 'absolute'
})
// Wrap the object in our positioning wrapper and apply the new size and
// position CSS values.
if ($objectWrapper.length < 1) {
$objectWrapper = $('<div class="fit-object-wrapper"></div>');
$objectWrapper.css(newCss);
$object.wrap($objectWrapper);
} else {
$objectWrapper.css(newCss);
}
// Make sure object will be positioned relative to its container
if ($container.css('position') === 'static') {
$container.css('position', 'relative');
}
// Make sure the container overflow is hidden
if ($container.css('overflow') !== 'hidden') {
$container.css('overflow', 'hidden');
}
}
/**
* Modify the offset to avoid cropping into a safe area
* @param {String} direction Either 'vertical' or 'horizontal'
* @param {Number} offset The centering offset for the overflow dimension
* @param {Number} cropped The pixels cropped on the overflow dimension
* @param {Object} options The fitObject params
* @return {Number} The modified offset
*/
var getSafeOffset = function (direction, offset, cropped, options) {
var safeArea = options['safeArea'];
var verticalBias = (safeArea['bottom'] - safeArea['top']) / 100;
var horizontalBias = (safeArea['right'] - safeArea['left']) / 100;
if (direction === 'horizontal') {
offset += cropped * horizontalBias;
}
if (direction === 'vertical') {
offset += cropped * verticalBias;
}
// Make sure the biased offset covers the top and left
if (offset > 0) {
offset = 0;
}
// Make sure the biased offset covers the right and bottom
if (offset < -cropped) {
offset = -cropped
}
return offset;
};
$.fn.fitObject = fitObject;
// return fitObject;
}));