-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.js
More file actions
165 lines (143 loc) · 4.75 KB
/
util.js
File metadata and controls
165 lines (143 loc) · 4.75 KB
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
$(function () {
var zeroPad_vs, getRotation_vec, r, g, b;
var directionTexts = [
["east", 5.890486225480862],
["northeast", 5.10508806208],
["north", 4.31968989869],
["northwest", 3.53429173529],
["west", 2.74889357189],
["southwest", 1.96349540849],
["south", 1.1780972451],
["southeast", 0.392699081699]
];
var Util = {
/**
* Return true if this page has a fullscreen element.
*
* @return {boolean}
*/
isFullscreen: function () {
return (
document.fullscreenElement ||
document.webkitFullscreenElement ||
document.mozFullScreenElement ||
document.msFullscreenElement
);
},
getDirectionText: function (radians) {
var di;
for (var i=0; i<directionTexts.length;i++) {
di = directionTexts[i];
if (radians > di[1]) {
return di[0];
}
}
return directionTexts[0][0];
},
/**
* Make an element fullscreen
*
* @param {Object} el - the element to become fullscreen
*/
makeFullscreen: function (el) {
if (el.jquery) {
el = el.get(0);
}
if (el.requestFullscreen) {
el.requestFullscreen();
} else if (el.webkitRequestFullscreen) {
el.webkitRequestFullscreen();
} else if (el.mozRequestFullScreen) {
el.mozRequestFullScreen();
} else if (el.msRequestFullscreen) {
el.msRequestFullscreen();
}
},
/**
* un-fullscreen
*/
cancelFullscreen: function () {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
},
/**
* Pad a number with zeroes.
*
* @param {*} v - the number or string to pad
* @param {Number} l - the desired length;
* @return {String}
*/
zeroPad: function (v, l) {
zeroPad_vs = v.toString();
while (zeroPad_vs.length < l) {
zeroPad_vs = "0" + zeroPad_vs;
}
return zeroPad_vs;
},
/**
* Convert a string to Title Case
* @param {string} str - the string to titlecase
* @returns {string}
*/
titleCase: function (str) {
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
},
/**
* Convert a possibly negative angle in radians to one
* which is guaranteed positive.
*
* @param {Number} r - angle in radians
* @returns {Number}
*/
truncRadians: function (r) {
while (r < 0) {
r += Math.PI * 2;
}
return r;
},
/**
* Get the angle in radians from one point to another.
*
* @param {Object} p1 - the 'from' point
* @param {Object} p1 - the 'to' point
* @returns {*|Number}
*/
getRotation: function (p1, p2) {
getRotation_vec = {x: p2.x - p1.x, y: p2.y - p1.y};
return this.truncRadians(Math.atan2(getRotation_vec.y, getRotation_vec.x)); // - (Math.PI / 2));
},
/**
* Converts a color from an integer to a css-friendly hex string.
*
* @param {Number} num - the integer color value
* @returns {string}
*/
toCSSColor: function (num) {
num >>>= 0;
b = num & 0xFF;
g = (num & 0xFF00) >>> 8;
r = (num & 0xFF0000) >>> 16;
return '#' + this.zeroPad(r.toString(16), 2) + "" + this.zeroPad(g.toString(16), 2) + "" + this.zeroPad(b.toString(16), 2);
},
/**
* Returns a color somewhere between green and red based on
* a float value between 0 and 1
*
* @param {Number} value - must be between 0.0 and 1.0
* @returns {Number}
*/
getHealthColor: function (value) {
var g = Math.floor(Math.min(255, value * 510));
var r = Math.floor(Math.min(255, 510 - (value * 510)));
return 256 * 256 * r + 256 * g;
}
};
window.Util = Util;
});