forked from ANerdyElva/JS13K
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
63 lines (58 loc) · 1.57 KB
/
util.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
function getAjax( name ) {
var key = name;
data[key]='';
function get(key, name) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if(xmlhttp.readyState!=4) return;
data[key] = xmlhttp.responseText;
loadingCount -= 1;
}
xmlhttp.open('GET',name,true);
xmlhttp.send();
}
get(key,name);
loadingCount += 1;
return key;
}
function bindRecursive(s) {
ret = {}
for ( key in s ) {
if (typeof s[key] == 'function') {
ret[key]=s[key].bind(ret);
} else {
ret[key]=s[key];
}
}
return ret;
}
//http://jsperf.com/native-and-non-native-random-numbers/5
var lcg = (function() {
// Set to values from http://en.wikipedia.org/wiki/Numerical_Recipes
// m is basically chosen to be large (as it is the max period)
// and for its relationships to a and c
var m = 4294967296,
// a - 1 should be divisible by m's prime factors
a = 1664525,
// c and m should be co-prime
c = 1013904223,
seed, z;
return {
setSeed : function(val) {
z = seed = val || Math.round(Math.random() * m);
},
getSeed : function() {
return seed;
},
rand : function() {
// define the recurrence relationship
z = (a * z + c) % m;
// return a float in [0, 1)
// if z = m then z / m = 0 therefore (z % m) / m < 1 always
return z / m;
}
};
});
function clamp( min, max, val ) {
return ( val < min ) ? min : ( ( val > max ) ? max : val );
}