-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageScaler.js
97 lines (92 loc) · 2.21 KB
/
ImageScaler.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
/*
Scale the background of an element to always fill the screen
regardless of aspect ratio
Usage:
var imageScaler = new ImageScaler($("selector"));
Kris McCann, 2013
*/
var ImageScaler = function(element, $)
{
// allow for passed in jQuery object, default to jQuery if not passed
$ = $ || jQuery;
// store reference to self
var self = this;
// orientation defaults to blank until we work out where we are
this.orientation = "";
// did we get an element to watch?
if(element === undefined)
{
// nope, nothing to do, error out
throw("FATAL: Expected input element");
}
$.when(ImageInfo($(element).css("background-image").replace(/\"|\'|\)|\(|url/g, ""))).then
(
function(e)
{
// calculate image aspect
self.aspect = e.width / e.height;
// store reference to element
self.element = element;
// init object
self.init();
},
function(err)
{
throw("FATAL: Could not load background image");
}
);
};
ImageScaler.prototype =
{
orientations:
{
PORTRAIT: "portrait",
LANDSCAPE: "landscape"
},
init: function()
{
// store ref to this
var self = this;
// set up listener for resize or orientation change
$(window).bind
(
"resize orientationchange",
function(e)
{
// pass on rather than direct chain so we don't lose context
self.size(e);
}
);
// and call once for initial size
this.size();
},
size: function(e)
{
// store current window aspect
var currentAspect = $(this.element).width() / $(this.element).height();
// did we run out of vertical space?
if(currentAspect < this.aspect)
{
// are we already in portrait?
if(this.orientation !== this.orientations.PORTRAIT)
{
// no, set to scale vertically to 100%
$(this.element).css("background-size", "auto 100%");
// and now note that we're in portrait
this.orientation = this.orientations.PORTRAIT;
}
}
// or horizontal space?
else if(currentAspect > this.aspect)
{
// are we already in landscape?
if(this.orientation !== this.orientations.LANDSCAPE)
{
// no, set to scale horizontally to 100%
$(this.element).css("background-size", "100% auto");
// and now note that we're in landscape
this.orientation = this.orientations.LANDSCAPE;
}
}
}
};