-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.xeyes-2.0.js
133 lines (106 loc) · 3.93 KB
/
jquery.xeyes-2.0.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
/**
jQuery xeyes
@version 2.0
@author Felix Milea-Ciobanu
@repo https://github.com/felixmc/jQuery-xeyes
*/
(function($) {
"use strict";
var defaultOptions = {
padding: 0,
reset: false,
radius: 'natural',
position: 'center',
trigger: window
};
var positions = {
top: 90,
bottom: -90,
left: 180,
right: 0,
topRight: 45,
topLeft: 135,
bottomRight: -45,
bottomLeft: -135
};
function Iris($iris) {
this.$iris = $iris;
$iris.css('position', 'absolute');
this.width = $iris.outerWidth();
this.height = $iris.outerHeight();
this.resetOffset = function() {
var offset = $iris.offset();
this.offset = {
x: offset.left + (this.width / 2) - parseInt($iris.css("left")),
y: offset.top + (this.height / 2) - parseInt($iris.css("top"))
};
};
}
function Eye($eye, $iris) {
this.$eye = $eye;
$eye.css('position', 'relative');
this.width = $eye.width();
this.height = $eye.height();
this.iris = new Iris($iris);
this.pos = {
x: (this.width - this.iris.width) / 2,
y: (this.height - this.iris.height) / 2
};
$iris.css("left", this.pos.x + "px").css("top", this.pos.y + "px");
this.padding = 0;
}
Eye.prototype.follow = function(mouse) {
mouse.x = mouse.x - this.pos.x;
mouse.y = mouse.y - this.pos.y;
this.iris.resetOffset();
var degree = Math.atan(( mouse.y - this.iris.offset.y) / ( mouse.x - this.iris.offset.x)),
direction = (this.iris.offset.x > mouse.x) ? -1 : 1,
newX = Math.cos(degree) * ((this.width - this.iris.width) / 2 - this.padding) * direction,
newY = Math.sin(degree) * ((this.height - this.iris.height) / 2 - this.padding) * direction,
radius = Math.sqrt(Math.pow(newX, 2) + Math.pow(newY, 2)),
distance = Math.sqrt(Math.pow(mouse.y - this.iris.offset.y, 2) + Math.pow(mouse.x - this.iris.offset.x, 2));
if (radius > distance) {
this.iris.$iris.css("left", ( mouse.x - this.iris.offset.x + this.pos.x) + "px").css("top", (mouse.y - this.iris.offset.y + this.pos.y) + "px");
} else {
this.iris.$iris.css("left", this.pos.x + newX + "px").css("top", this.pos.y + newY + "px");
}
};
Eye.prototype.setPosition = function(position) {
if (position.x !== undefined && position.y !== undefined) {
this.iris.$iris.css("left", this.pos.x - position.x + "px").css("top", this.pos.y - position.y + "px");
} else if (typeof position === "number") {
var deg = position * Math.PI / -180;
this.iris.$iris.css("left", this.pos.x + Math.cos(deg) * (this.width / 2 - this.iris.width / 2 - this.padding) + "px").css("top", this.pos.y + Math.sin(deg) * (this.height / 2 - this.iris.height / 2 - this.padding) + "px");
} else if (position === "center") {
this.iris.$iris.css("left", this.pos.x + "px").css("top", this.pos.y + "px");
} else if (positions[position] !== undefined) {
var deg2 = positions[position] * Math.PI / -180;
this.iris.$iris.css("left", this.pos.x + Math.cos(deg2) * (this.width / 2 - this.iris.width / 2 - this.padding) + "px").css("top", this.pos.y + Math.sin(deg2) * (this.height / 2 - this.iris.height / 2 - this.padding) + "px");
}
};
$.fn.xeyes = function(options) {
options = $.extend(defaultOptions, options);
var padding = parseInt(options.padding, 10);
var $trigger = $(options.trigger);
this.each(function(index, irisEl) {
var $iris = $(irisEl),
$eye = $iris.parent();
var eye = new Eye($eye, $iris);
var iris = eye.iris;
eye.padding = padding;
if (options.radius == 'circular' && eye.width > eye.height)
eye.width = eye.height;
else if (options.radius == 'circular')
eye.height = eye.width;
eye.setPosition(options.position);
$trigger.mousemove(function(e) {
eye.follow({ x: e.pageX, y: e.pageY });
});
if (options.reset) {
$trigger.mouseleave(function(e) {
eye.setPosition(options.position);
});
}
});
};
})(jQuery);