forked from Indamix/real-shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realshadow.js
126 lines (108 loc) · 2.37 KB
/
realshadow.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
/*!
* Real Shadow v1.0.1
* https://github.com/Indamix/real-shadow
*
* Copyright 2012, Ivan Indamix
* Licensed under the MIT license
* https://raw.github.com/Indamix/real-shadow/master/license.txt
*/
(function($, window, undefined){
// TODO add fn(height) to pass shape form
var settings = {
followMouse: true,
},
pi = Math.PI,
els = [];
$.fn.realshadow = function(options){
$.extend(settings, options);
if (!els.length && settings.followMouse) $(document.body).mousemove(frame);
$(window).resize(updatePositions);
add(this);
frame({
pageX: settings.pageX !== undefined ? settings.pageX : $(window).width() >> 1,
pageY: settings.pageY !== undefined ? settings.pageY : 0
});
};
// $.fn.realshadow.frame=frame; //TODO
function add($els){
$.each($els, function(i, el){
var $el = $(el),
offset = $el.offset(),
c = $el.attr('rel'),
p = {
dom: el,
x: offset.left + ($el.outerWidth () >> 1),
y: offset.top + ($el.outerHeight() >> 1)
};
if (c)
p.c = {
r: c.indexOf('r') !== -1,
g: c.indexOf('g') !== -1,
b: c.indexOf('b') !== -1
}
else
if (settings.c) p.c = settings.c;
els.push(p);
});
}
function updatePositions(){
var i = els.length,
offset,
el;
while (i--) {
el = els[i];
offset = $(el.dom).offset();
el.x = offset.left;
el.y = offset.top;
}
}
function castShadows(el, angle, n, height){
height = height || 7;
// n = n || 2;
var shadows = [],
cos = Math.cos(angle),
sin = Math.sin(angle),
r;
for (var i = 0; i < height; ++i) {
r = Math.pow(i, n);
// TODO add ---^ + shadow distance
shadows.push(
( r * sin >> 0 ) + 'px ' +
( r * cos >> 0 ) + 'px ' +
( Math.pow(i, 1.7) >> 0 ) +
'px rgba(' +
(el.c ?
(el.c.r ? 100 : 0) + ',' +
(el.c.g ? 100 : 0) + ',' +
(el.c.b ? 100 : 0) + ','
:
'0,0,0,'
) +
'.05)'
);
}
el.dom.style.boxShadow = shadows.join(',');
}
var params = {
nMax: 2.3,
pow: .8,
div: 1500
}
function frame(e){
var i = els.length,
el;
while (i--) {
el = els[i];
var x = e.pageX - els[i].x,
y = e.pageY - els[i].y,
n = Math.pow(x * x + y * y, params.pow)
n = n / params.div + 1; // TODO n = f(obj.size, distance)
if (n > params.nMax) n = params.nMax;
castShadows(
el,
Math.atan2(x, y) - pi,
n
);
}
}
})(jQuery, this);