asciiblaster- draw irc art in your web browser |
git clone git://git.acid.vegas/asciiblaster.git |
Log | Files | Refs | Archive | README |
util.js (6589B)
1 if (window.$) { 2 $.fn.int = function(){ return parseInt($(this).val(),10) } 3 $.fn.float = function(){ return parseFloat($(this).val()) } 4 $.fn.string = function(){ return trim($(this).val()) } 5 $.fn.enable = function() { return $(this).attr("disabled",null) } 6 $.fn.disable = function() { return $(this).attr("disabled","disabled") } 7 } 8 9 function noop(){} 10 function trim(s){ return s.replace(/^\s+/,"").replace(/\s+$/,"") } 11 12 var E = Math.E 13 var PI = Math.PI 14 var PHI = (1+Math.sqrt(5))/2 15 var TWO_PI = PI*2 16 var LN10 = Math.LN10 17 function clamp(n,a,b){ return n<a?a:n<b?n:b } 18 function norm(n,a,b){ return (n-a) / (b-a) } 19 function lerp(n,a,b){ return (b-a)*n+a } 20 function mix(n,a,b){ return a*(1-n)+b*n } 21 function ceil(n){ return Math.ceil(n) } 22 function floor(n){ return Math.floor(n) } 23 function round(n){ return Math.round(n) } 24 function max(a,b){ return Math.max(a,b) } 25 function min(a,b){ return Math.min(a,b) } 26 function abs(n){ return Math.abs(n) } 27 function sign(n){ return Math.abs(n)/n } 28 function pow(n,b) { return Math.pow(n,b) } 29 function exp(n) { return Math.exp(n) } 30 function log(n){ return Math.log(n) } 31 function ln(n){ return Math.log(n)/LN10 } 32 function sqrt(n) { return Math.sqrt(n) } 33 function cos(n){ return Math.cos(n) } 34 function sin(n){ return Math.sin(n) } 35 function tan(n){ return Math.tan(n) } 36 function acos(n){ return Math.cos(n) } 37 function asin(n){ return Math.sin(n) } 38 function atan(n){ return Math.atan(n) } 39 function atan2(a,b){ return Math.atan2(a,b) } 40 function sec(n){ return 1/cos(n) } 41 function csc(n){ return 1/sin(n) } 42 function cot(n){ return 1/tan(n) } 43 function cosp(n){ return (1+Math.cos(n))/2 } // cos^2 44 function sinp(n){ return (1+Math.sin(n))/2 } 45 function random(){ return Math.random() } 46 function rand(n){ return (Math.random()*n) } 47 function randint(n){ return rand(n)|0 } 48 function randrange(a,b){ return a + rand(b-a) } 49 function randsign(){ return random() >= 0.5 ? -1 : 1 } 50 function randnullsign(){ var r = random(); return r < 0.333 ? -1 : r < 0.666 ? 0 : 1 } 51 52 function xrandom(exp){ return Math.pow(Math.random(), exp) } 53 function xrand(exp,n){ return (xrandom(exp)*n) } 54 function xrandint(exp,n){ return rand(exp,n)|0 } 55 function xrandrange(exp,a,b){ return a + xrand(exp,b-a) } 56 57 function choice(a){ return a[randint(a.length)] } 58 function deg(n){ return n*180/PI } 59 function rad(n){ return n*PI/180 } 60 function xor(a,b){ a=!!a; b=!!b; return (a||b) && !(a&&b) } 61 function mod(n,m){ n = n % m; return n < 0 ? (m + n) : n } 62 function modi(n,m){ return floor(mod(n,m)) } 63 function dist(x0,y0,x1,y1){ return sqrt(pow(x1-x0,2)+pow(y1-y0,2)) } 64 function angle(x0,y0,x1,y1){ return atan2(y1-y0,x1-x0) } 65 function avg(m,n,a){ return (m*(a-1)+n)/a } 66 function quantize(a,b){ return floor(a/b)*b } 67 function quantile(a,b){ return floor(a/b) } 68 69 function pixel(x,y){ return 4*(mod(y,actual_h)*actual_w+mod(x,actual_w)) } 70 function rgbpixel(d,x,y){ 71 var p = pixel(~~x,~~y) 72 r = d[p] 73 g = d[p+1] 74 b = d[p+2] 75 a = d[p+3] 76 } 77 function fit(d,x,y){ rgbpixel(d,x*actual_w/w,y*actual_h/h) } 78 79 function step(a, b){ 80 return (b >= a) + 0 81 // ^^ bool -> int 82 } 83 84 function julestep (a,b,n) { 85 return clamp(norm(n,a,b), 0.0, 1.0); 86 } 87 88 // hermite curve apparently 89 function smoothstep(min,max,n){ 90 var t = clamp((n - min) / (max - min), 0.0, 1.0); 91 return t * t * (3.0 - 2.0 * t) 92 } 93 94 function toArray(a){ return Array.prototype.slice.call(a) } 95 function shuffle(a){ 96 for (var i = a.length; i > 0; i--){ 97 var r = randint(i) 98 var swap = a[i-1] 99 a[i-1] = a[r] 100 a[r] = swap 101 } 102 return a 103 } 104 function reverse(a){ 105 var reversed = [] 106 for (var i = 0, _len = a.length-1; i <= _len; i++){ 107 reversed[i] = a[_len-i] 108 } 109 return reversed 110 } 111 function deinterlace(a){ 112 var odd = [], even = [] 113 for (var i = 0, _len = a.length; i < _len; i++) { 114 if (i % 2) even.push(a[i]) 115 else odd.push(a[i]) 116 } 117 return [even, odd] 118 } 119 function weave(a){ 120 var aa = deinterlace(a) 121 var b = [] 122 aa[0].forEach(function(el){ b.push(el) }) 123 reverse(aa[1]).forEach(function(el){ b.push(el) }) 124 return b 125 } 126 function cssRule (selector, declaration) { 127 var x = document.styleSheets, y = x.length-1; 128 x[y].insertRule(selector+"{"+declaration+"}", x[y].cssRules.length); 129 } 130 131 // easing functions 132 function circular (t) { return Math.sqrt( 1 - ( --t * t ) ) } 133 function quadratic (t) { return t * ( 2 - t ) } 134 function back (t) { 135 var b = 4; 136 return ( t = t - 1 ) * t * ( ( b + 1 ) * t + b ) + 1; 137 } 138 function bounce (t) { 139 if (t >= 1) return 1; 140 if ( ( t /= 1 ) < ( 1 / 2.75 ) ) { 141 return 7.5625 * t * t; 142 } else if ( t < ( 2 / 2.75 ) ) { 143 return 7.5625 * ( t -= ( 1.5 / 2.75 ) ) * t + 0.75; 144 } else if ( t < ( 2.5 / 2.75 ) ) { 145 return 7.5625 * ( t -= ( 2.25 / 2.75 ) ) * t + 0.9375; 146 } else { 147 return 7.5625 * ( t -= ( 2.625 / 2.75 ) ) * t + 0.984375; 148 } 149 } 150 function elastic (t) { 151 var f = 0.22, 152 e = 0.4; 153 154 if ( t === 0 ) { return 0; } 155 if ( t == 1 ) { return 1; } 156 157 return ( e * Math.pow( 2, - 10 * t ) * Math.sin( ( t - f / 4 ) * ( 2 * Math.PI ) / f ) + 1 ); 158 } 159 160 Model=function a(b,c,d,e){function f(){var a=this,f={};a.on=function(a,b){(f[a]|| 161 (f[a]=[])).push(b)},a.trigger=function(a,b){for(var c=f[a],d=0;c&&d<c.length;)c 162 [d++](b)},a.off=function(a,b){for(d=f[a]||[];b&&(c=d.indexOf(b))>-1;)d.splice(c 163 ,1);f[a]=b?d:[]};for(c in b)d=b[c],a[c]=typeof d=="function"?function(){return( 164 d=this.apply(a,arguments))===e?a:d}.bind(d):d;a.init&&a.init.apply(a,arguments) 165 }return f.extend=function(f){d={};for(c in b)d[c]=b[c];for(c in f)d[c]=f[c],b[c 166 ]!==e&&(d["__"+c]=b[c]);return a(d)},f},typeof module=="object"&&(module.exports 167 =Model); // c-{{{-< 168 169 function defaults (dest, src) { 170 dest = dest || {} 171 for (var i in src) { 172 dest[i] = typeof dest[i] == 'undefined' ? src[i] : dest[i] 173 } 174 return dest 175 } 176 177 function setSelectionRange(input, selectionStart, selectionEnd) { 178 if (input.setSelectionRange) { 179 input.focus(); 180 input.setSelectionRange(selectionStart, selectionEnd); 181 } 182 else if (input.createTextRange) { 183 var range = input.createTextRange(); 184 range.collapse(true); 185 range.moveEnd('character', selectionEnd); 186 range.moveStart('character', selectionStart); 187 range.select(); 188 } 189 } 190 function setCaretToPos(input, pos) { 191 setSelectionRange(input, pos, pos); 192 } 193 194 // Naive useragent detection pattern 195 var is_iphone = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) 196 var is_ipad = (navigator.userAgent.match(/iPad/i)) 197 var is_android = (navigator.userAgent.match(/Android/i)) 198 var is_mobile = is_iphone || is_ipad || is_android 199 var is_desktop = ! is_mobile;