-
Notifications
You must be signed in to change notification settings - Fork 7
/
jquery.spotlight.js
175 lines (145 loc) · 4.66 KB
/
jquery.spotlight.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
/**
* jQuery Spotlight
*
* Project Page: http://github.com/
* Original Plugin code by Gilbert Pellegrom (2009)
* Licensed under the GPL license (http://www.gnu.org/licenses/gpl-3.0.html)
* Version 2.0.1 (2014)
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var currentOverlay;
$.fn.spotlight = function (options) {
var method = 'create';
// Default settings
settings = $.extend({}, {
opacity: .5,
speed: 400,
color: '#333',
animate: true,
easing: '',
exitEvent: 'click',
onShow: function () {
// do nothing
},
onHide: function () {
// do nothing
},
spotlightZIndex: 9999,
spotlightElementClass: 'spotlight-background',
parentSelector: 'html',
paddingX: 0,
paddingY: 0
}, options);
function closeOverlay () {
if (!currentOverlay) {
return;
}
if (settings.animate) {
currentOverlay.animate({opacity: 0}, settings.speed, settings.easing, function () {
currentOverlay.remove();
currentOverlay = null;
// Trigger the onHide callback
settings.onHide.call(this);
});
} else {
currentOverlay.remove();
currentOverlay = null;
// Trigger the onHide callback
settings.onHide.call(this);
}
}
if (typeof options === 'string') {
method = options;
options = arguments[1];
}
switch (method) {
case 'close':
case 'destroy':
closeOverlay();
return;
}
var elements = $(this),
overlay,
parent,
context;
/**
* Colour in the overlay and clear all element masks
*/
function fillOverlay () {
context.fillStyle = settings.color;
context.fillRect(0, 0, parent.innerWidth(), parent.innerHeight());
// loop through elements and clear their position
elements.each(function (i, e) {
e = $(e);
var currentPos = e.position();
context.clearRect(
currentPos.left - settings.paddingX,
currentPos.top - settings.paddingY,
e.outerWidth() + (settings.paddingX * 2),
e.outerHeight() + (settings.paddingY * 2)
);
});
}
/**
* Handle resizing the window
*
* @param e
*/
function handleResize (e) {
overlay.attr('width', parent.innerWidth());
overlay.attr('height', parent.innerHeight());
if (typeof context !== 'undefined') {
fillOverlay();
}
}
closeOverlay();
// Add the overlay element
overlay = $('<canvas></canvas>');
overlay.addClass(settings.spotlightElementClass);
currentOverlay = overlay;
parent = $(settings.parentSelector);
parent.append(overlay);
// Get our elements
var element = $(this);
// Set the CSS styles
var cssConfig = {
position: 'absolute',
top: 0,
left: 0,
height: '100%',
width: '100%',
zIndex: settings.spotlightZIndex
};
if (settings.parentSelector == 'html') {
parent.css('height', '100%');
}
overlay.css(cssConfig);
handleResize();
$(window).resize(handleResize);
context = overlay[0].getContext('2d');
fillOverlay();
// Fade in the spotlight
if (settings.animate) {
overlay.animate({opacity: settings.opacity}, settings.speed, settings.easing, function () {
// Trigger the onShow callback
settings.onShow.call(this);
});
} else {
overlay.css('opacity', settings.opacity);
// Trigger the onShow callback
settings.onShow.call(this);
}
// Set up click to close
$(document).on(settings.exitEvent, overlay, closeOverlay);
// Returns the jQuery object to allow for chainability.
return this;
};
}));