-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh2o.js
162 lines (135 loc) · 4.26 KB
/
h2o.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
/* Javascript Document */
/* Created by redAtom Studios @ 2012 */
// Okay, now starting with the actual EagleEye Object...
function EagleEye(targetClass, loggingFlag) {
if(targetClass) {
this.targetClass = '.' + targetClass;
} else {
this.targetClass = '.eagleEye';
}
// State 0 = stopped, 1 = rendering
this.state = 0;
this.origin_x = 100;
this.origin_y = 100;
// Maximum shadow size in x or y direction
this.maxOffset = 30;
this.enableLogging = loggingFlag;
this.log('Logging enabled');
this.log(this.origin_x);
// Get a reference to this object, so as to not
// have issues with using 'this' in jQuery
var self = this;
}
EagleEye.prototype.log = function(message) {
if(this.enableLogging) {
// Check if console is available. If not, alert the user and disable
// logging to prevent a dialog storm due to future calls
if(console) {
var alertString = '[EagleEye' + this.targetClass + '] '
console.log(alertString + message + ';');
} else {
alert('Console is unavailable. Autodisabling logging functionality to prevent additional dialogs.');
this.enableLogging = false;
}
}
}
EagleEye.prototype.leftShadowOffset = function(thisLeft) {
var offset = -this.getOffset(thisLeft, 0, 0) / this.origin_z;
if(this.maxOffset > Math.abs(offset)) {
return offset;
} else {
return ( offset > 0 ? this.maxOffset : -this.maxOffset );
}
}
EagleEye.prototype.bottomShadowOffset = function(thisTop) {
var offset = -this.getOffset(0, thisTop, 1) / this.origin_z;
if(this.maxOffset > Math.abs(offset)) {
return offset;
} else {
return ( offset > 0 ? this.maxOffset : -this.maxOffset );
}
}
EagleEye.prototype.shadowIntensity = function(thisTop, thisLeft) {
var offset = this.getOffset(thisLeft, thisTop, 2);
var shadowFalloff = 300;
return (1 - (offset / shadowFalloff)) * 0.3;
}
EagleEye.prototype.shadowSpread = function(thisTop, thisLeft) {
var offset = this.getOffset(thisLeft, thisTop, 2) / 10;
if(this.maxOffset > offset) {
return offset + 1;
} else {
return this.maxOffset;
}
}
EagleEye.prototype.getOffset = function(thisLeft, thisTop, returnType) {
switch(returnType) {
case 0: var offset_x = this.origin_x - thisLeft;
return offset_x;
break;
case 1: var offset_y = this.origin_y - thisTop;
return offset_y;
break;
case 2: var offset_x = this.origin_x - thisLeft;
var offset_y = this.origin_y - thisTop;
offset_x *= offset_x;
offset_y *= offset_y;
return Math.sqrt(offset_x + offset_y);
break;
default: this.log('returnType not specified for offset.');
break;
}
}
EagleEye.prototype.renderCycle = function(selector) {
// Used to calculate shadow size along with camera origin
this.screenHeight = $(document).height();
this.screenWidth = $(document).width();
// Origin of the camera, shadow size is proportional to this
this.origin_z = 100;
//this.log(this.origin_x);
var self = this;
$(selector).each(function(){
var thisLeft = $(this).offset().left;
var thisTop = $(this).offset().top;
if(self.getOffset(thisLeft, thisTop, 2) < 300) {
var styleString = self.leftShadowOffset(thisLeft) + 'px ' + self.bottomShadowOffset(thisTop) + 'px ' + self.shadowSpread(thisTop, thisLeft) + 'px rgba(0, 0, 0, ' + self.shadowIntensity(thisTop, thisLeft) + ')'
$(this).css('box-shadow', styleString);
} else {
$(this).css('box-shadow', 'none');
}
});
}
EagleEye.prototype.startRender = function(timeout, objName) {
this.renderCycle(this.targetClass);
this.rendering = setInterval('' + objName + '.renderCycle(' + objName + '.targetClass)', timeout);
this.state = 1;
}
EagleEye.prototype.stopRenderer = function() {
clearInterval(this.rendering);
this.state = 0;
}
jQuery(document).ready(function($){
// Preparing document by adding nodes
elements = ($(document).height() / 100) * ($(document).width() / 100);
while(elements > 1) {
$('body').prepend('<div class="building small"></div>');
elements--;
}
Eagle = new EagleEye('building', false);
Eagle.startRender(1, 'Eagle');
//Eagle.stopRenderer();
$(document).mousemove(function(e){
Eagle.origin_x = e.pageX;
Eagle.origin_y = e.pageY;
});
$('#PauseBtn').click(function(){
if(Eagle.state){
Eagle.stopRenderer();
}
});
$('#StartBtn').click(function(){
if(!Eagle.state){
Eagle.startRender(1, 'Eagle');
}
});
});