-
Notifications
You must be signed in to change notification settings - Fork 37
/
jquery.aim.js
286 lines (236 loc) · 8.91 KB
/
jquery.aim.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
(function($) {
var elementList = [];
/**
* v Velocity of the mouse pointer
* vd Magnitude of velocity
* p Position of the mouse pointer
* t Average delta time for a simple calculation of new position, x = x0 + v * t
* mouseX the last retrived x coordinate of mouse cursor
* mouseY the last retrived y coordinate of mouse cursor
* anticipator a jquery object to debug where mouse is aiming
* anticipator.size, anticipator.radius, anticipator.center, anticipator.rect anticipator related properties
* anRad Radius (or size) of the anticipator, increases as mouse move faster
*/
var v = {x: 0, y: 0},
vd = 0,
p = {x: 0, y: 0},
t = 12,
mouseX = 0,
mouseY = 0,
DEBUG = false,
anticipator = {
size: 50,
center: {x: 0, y: 0},
effectiveSize: 1
};
anticipator.rect = {x0: 0, y0: 0, x1: anticipator.size, y1: anticipator.size};
/*
* Default anticipate function
*
* @function anticipateFunc
*
* @param {type} p position of anticipator
* @param {type} v velocity of anticipator
* @param {type} mouseX mouse X coordinate
* @param {type} mouseY mouse Y coordinate
* @param {type} anticipator anticipator object
* @returns {undefined}
*/
function anticipateFunc(p, v, mouseX, mouseY, anticipator) {
var a = anticipator;
//smoothen velocity values with ratio 0.7/0.3
if (p.x && p.y) {
v.x = v.x * 0.7 + (mouseX - p.x) * 0.3;
v.y = v.y * 0.7 + (mouseY - p.y) * 0.3;
}
p.x = mouseX;
p.y = mouseY;
//find velocity magnitude
vd = Math.sqrt(v.x * v.x + v.y * v.y);
vd < 0.1 && (v.x = 0, v.y = 0);
//change radius according to velocity magnitude
a.effectiveSize = Math.sqrt(a.size * vd + 1);
//assign anticipator coordinates according to new velocity values and smoothen it with ratio 0.7/0.3
a.center.x = a.center.x * 0.7 + (p.x + v.x * t) * 0.3;
a.center.x < 0 && (a.center.x = 0);
(a.center.x > $(window).width() - a.effectiveSize) && (a.center.x = $(window).width() - a.effectiveSize);
a.rect.x0 = a.center.x - a.effectiveSize;
a.rect.x1 = a.center.x + a.effectiveSize;
a.center.y = a.center.y * 0.7 + (p.y + v.y * t) * 0.3;
a.center.y < 0 && (a.center.y = 0);
(a.center.y > $(window).height() - a.effectiveSize) && (a.center.y = $(window).height() - a.effectiveSize);
a.rect.y0 = a.center.y - a.effectiveSize;
a.rect.y1 = a.center.y + a.effectiveSize;
}
$.fn.aim = function(opts) {
// Initialize menu-aim for all elements in jQuery collection
this.each(function() {
init.call(this, opts);
});
return this;
};
/*
* Sets debug mode to true or false. If debug mode is set to true, a circle showing the
* position and radius of anticipator will be created
*
* @param {type} val
* @returns {undefined}
*/
$.aim = {};
$.aim.setDebug = function(val) {
if (val) {
if ($('#jquery-aim-debug').length) {
return;
}
anticipator.elem = createDebugObject();
} else {
$('#jquery-aim-debug').remove();
anticipator.elem = null;
}
DEBUG = val;
};
$.aim.setAnticipateFunction = function(func) {
if (typeof func === 'function') {
anticipateFunc = func;
}
};
/*
* Adds properties with jquery `.data()` function so each time it doesn't recalculate every property
*
* @param {type} elem Jquery element to add properties
* @returns {undefined} none
*/
function addProperties($elem) {
var percent = 0.25;
var w = $elem.outerWidth();
var h = $elem.outerHeight();
var x = $elem.offset().left;
var y = $elem.offset().top;
var max = Math.sqrt(w * w + h * h);
var r = max / 2 * (1 + percent);
$elem.data('aim-data', {
rect: {
x0: x,
y0: y,
x1: x + w,
y1: y + h
},
center: {x: x, y: y},
increment: 0
}
);
}
/*
* Creates a circle jquery object which is to be used to
* show where the anticipator is at any time
*
* @returns {Object}
*/
function createDebugObject() {
var s = anticipator.size;
var elem = $('<div>')
.attr({
id: 'jquery-aim-debug'
})
.css({
width: 2 * s + 'px',
height: 2 * s + 'px',
'margin-left': -s + 'px',
'margin-top': -s + 'px',
top: 0,
left: 0,
border: '2px solid #333',
opacity: 0.3,
'background-color': 'yellowgreen',
position: 'absolute',
'pointer-events':'none'
})
.appendTo($('body'));
return elem;
}
/*
* Tests rectangle - rectangle intersection and gives the ratio of intersection. Max 1, min 0.
*
* @param {type} rect The first rectange
* @param {type} rect2 The second rectange
* @returns {Number} Ratio of intersection area to area of tailblazer
*/
function intersectRatio(rect, rect2) {
var x_overlap = Math.max(0, Math.min(rect.x1, rect2.x1) - Math.max(rect.x0, rect2.x0));
var y_overlap = Math.max(0, Math.min(rect.y1, rect2.y1) - Math.max(rect.y0, rect2.y0));
return x_overlap * y_overlap / (anticipator.effectiveSize * anticipator.effectiveSize);
}
function init(opts) {
var $this = $(this);
if ($.inArray($this, elementList) > -1)
return;
elementList.push($this);
addProperties($this);
$this.data('aim-data').options = opts;
}
$().ready(function() {
document.addEventListener('mousemove', function(e) {
mouseX = e.clientX,
mouseY = e.clientY;
}, false);
});
var timer = setInterval(function() {
var a = anticipator;
if (!elementList.length)
return;
anticipateFunc(p, v, mouseX, mouseY, a);
var prop = 'translate(' + a.center.x + 'px,' + a.center.y + 'px) scale(' + a.effectiveSize / a.size + ')';
DEBUG && a.elem.css({
'-webkit-transform': prop,
'-moz-transform': prop,
'-ms-transform': prop,
'transform': prop
/*width: tbRad * 2,
height: tbRad * 2,
marginLeft: -tbRad + 'px',
marginTop: -tbRad + 'px'*/
});
/*
* Iterate over each elements and calculate increment for all
* In each cycle, it increases by a value between 0 - 0.2 (reaches max if it fully intersects) and decreases by 0.05
* Increment can be between 0 and 2
* If it's greater than 1, aimEnter function will be called
* if it's less than or equal to 0, aimExit function will be called
*/
for (var i = 0; i < elementList.length; i++) {
var target = elementList[i];
var data = target.data('aim-data');
var isctRat = intersectRatio(data.rect, a.rect);
//check if they intersects and mouse is not on the element
if (isctRat && vd !== 0) {
data.increment = data.increment + isctRat * 0.2;
if (data.increment > 1 && data.increment < 2) {
if (data.options.className)
target.addClass(data.options.className);
else if (data.options.aimEnter && typeof data.options.aimEnter === 'function')
data.options.aimEnter.call(target, true);
if (data.increment > 2) {
data.increment = 2;
}
DEBUG && a.elem.css('background-color', 'tomato');
} else if (data.increment > 2) {
data.increment = 2;
DEBUG && a.elem.css('background-color', 'tomato');
}
break;
} else {
DEBUG && a.elem.css('background-color', 'yellowgreen');
}
if (data.increment !== 0) {
data.increment = data.increment - 0.05;
if (data.increment < 0) {
data.increment = 0;
if (data.options.className)
target.removeClass(data.options.className);
else if (data.options.aimExit && typeof data.options.aimExit === 'function')
data.options.aimExit.call(target, true);
}
}
}
}, 16); //~60 FPS
})(jQuery);