-
Notifications
You must be signed in to change notification settings - Fork 76
/
jquery.foggy.js
149 lines (131 loc) · 4.04 KB
/
jquery.foggy.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
// Foggy, v1.1.1
//
// Description: jQuery plugin for blurring page elements
// Homepage: http://nbartlomiej.github.com/foggy
// Author: [email protected]
(function( $ ){
$.fn.foggy = function( options ) {
var defaultOptions = {
opacity: 0.8,
blurRadius: 2,
quality: 16,
cssFilterSupport: true
};
var noBlurOptions = {
opacity: 1,
blurRadius: 0
};
var settings;
if (options == false) {
settings = $.extend( defaultOptions, noBlurOptions );
} else {
settings = $.extend( defaultOptions, options);
}
var BlurPass = function(content, position, offset, opacity){
this.content = content;
this.position = position;
this.offset = offset;
this.opacity = opacity;
};
BlurPass.prototype.render = function(target){
$('<div/>', {
html: this.content, 'class': 'foggy-pass-'+this.position
}).css({
position: this.position,
opacity: this.opacity,
top: this.offset[0],
left: this.offset[1]
}).appendTo(target);
};
var Circle = function(radius){
this.radius = radius;
};
Circle.prototype.includes = function(x,y){
if (Math.pow(x,2) + Math.pow(y,2) <= Math.pow(this.radius, 2)){
return true;
} else {
return false;
}
};
Circle.prototype.points = function(){
var results = [];
for (var x = -this.radius; x<=this.radius; x++){
for (var y = -this.radius; y<=this.radius; y++){
if (this.includes(x,y)){
results.push([x,y]);
}
}
}
return results;
};
var ManualFog = function(element, settings){
this.element = element;
this.settings = settings;
};
ManualFog.prototype.calculateOffsets = function(radius, quality){
var all_offsets = $.grep(
new Circle(radius).points(),
function(element){ return (element[0] != 0) || (element[1] != 0) }
);
var offsets;
if (all_offsets.length <= quality){
offsets = all_offsets;
} else {
var overhead = all_offsets.length - quality;
var targets = [];
for (var i = 0; i < overhead; i++){
targets.push(Math.round(i * (all_offsets.length / overhead)));
}
offsets = $.grep( all_offsets, function(element, index){
if ($.inArray(index, targets) >= 0){
return false;
} else {
return true;
}
});
}
return offsets;
};
ManualFog.prototype.getContent = function(){
var candidate = $(this.element).find('.foggy-pass-relative')[0];
if (candidate){
return $(candidate).html();
} else {
return $(this.element).html();
}
};
ManualFog.prototype.render = function(){
var content = this.getContent();
$(this.element).empty();
var wrapper = $('<div/>').css({ position: 'relative' });
var offsets = this.calculateOffsets(
this.settings.blurRadius*2, this.settings.quality
);
var opacity = (this.settings.opacity * 1.2) / (offsets.length + 1);
new BlurPass(content, 'relative', [0,0], opacity).render(wrapper);
$(offsets).each(function(index, offset){
new BlurPass(content, 'absolute', offset, opacity).render(wrapper);
});
wrapper.appendTo(this.element);
};
var FilterFog = function(element, settings){
this.element = element;
this.settings = settings;
}
FilterFog.prototype.render = function(){
var opacityPercent = (''+settings.opacity).slice(2,4);
var filterBlurRadius = this.settings.blurRadius;
$(this.element).css({
'-webkit-filter': 'blur('+filterBlurRadius+'px)',
opacity: settings.opacity
});
}
return this.each(function(index, element) {
if (settings.cssFilterSupport && '-webkit-filter' in document.body.style){
new FilterFog(element, settings).render();
} else {
new ManualFog(element, settings).render();
}
});
};
})( jQuery );