vendor: 7,333 bytes, lines 1-195
1 2/* 3 countUp.js 4 by @inorganik 5*/ 6 7// target = id of html element or var of previously selected html element where counting occurs 8// startVal = the value you want to begin at 9// endVal = the value you want to arrive at 10// decimals = number of decimal places, default 0 11// duration = duration of animation in seconds, default 2 12// options = optional object of options (see below) 13 14var CountUp = function(target, startVal, endVal, decimals, duration, options) { 15 16 // make sure requestAnimationFrame and cancelAnimationFrame are defined 17 // polyfill for browsers without native support 18 // by Opera engineer Erik Möller 19 var lastTime = 0; 20 var vendors = ['webkit', 'moz', 'ms', 'o']; 21 for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { 22 window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; 23 window.cancelAnimationFrame = 24 window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; 25 } 26 if (!window.requestAnimationFrame) { 27 window.requestAnimationFrame = function(callback, element) { 28 var currTime = new Date().getTime(); 29 var timeToCall = Math.max(0, 16 - (currTime - lastTime)); 30 var id = window.setTimeout(function() { callback(currTime + timeToCall); }, 31 timeToCall); 32 lastTime = currTime + timeToCall; 33 return id; 34 }; 35 } 36 if (!window.cancelAnimationFrame) { 37 window.cancelAnimationFrame = function(id) { 38 clearTimeout(id); 39 }; 40 } 41 42 var self = this; 43 44 // default options 45 self.options = { 46 useEasing : true, // toggle easing 47 useGrouping : true, // 1,000,000 vs 1000000 48 separator : ',', // character to use as a separator 49 decimal : '.', // character to use as a decimal 50 easingFn: null, // optional custom easing closure function, default is Robert Penner's easeOutExpo 51 formattingFn: null // optional custom formatting function, default is self.formatNumber below 52 }; 53 // extend default options with passed options object 54 for (var key in options) { 55 if (options.hasOwnProperty(key)) { 56 self.options[key] = options[key]; 57 } 58 } 59 if (self.options.separator === '') { self.options.useGrouping = false; } 60 if (!self.options.prefix) self.options.prefix = ''; 61 if (!self.options.suffix) self.options.suffix = ''; 62 63 self.d = (typeof target === 'string') ? document.getElementById(target) : target; 64 self.startVal = Number(startVal); 65 self.endVal = Number(endVal); 66 self.countDown = (self.startVal > self.endVal); 67 self.frameVal = self.startVal; 68 self.decimals = Math.max(0, decimals || 0); 69 self.dec = Math.pow(10, self.decimals); 70 self.duration = Number(duration) * 1000 || 2000; 71 72 self.formatNumber = function(nStr) { 73 nStr = nStr.toFixed(self.decimals); 74 nStr += ''; 75 var x, x1, x2, rgx; 76 x = nStr.split('.'); 77 x1 = x[0]; 78 x2 = x.length > 1 ? self.options.decimal + x[1] : ''; 79 rgx = /(\d+)(\d{3})/; 80 if (self.options.useGrouping) { 81 while (rgx.test(x1)) { 82 x1 = x1.replace(rgx, '$1' + self.options.separator + '$2'); 83 } 84 } 85 return self.options.prefix + x1 + x2 + self.options.suffix; 86 }; 87 // Robert Penner's easeOutExpo 88 self.easeOutExpo = function(t, b, c, d) { 89 return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b; 90 }; 91 92 self.easingFn = self.options.easingFn ? self.options.easingFn : self.easeOutExpo; 93 self.formattingFn = self.options.formattingFn ? self.options.formattingFn : self.formatNumber; 94 95 self.version = function () { return '1.7.1'; }; 96 97 // Print value to target 98 self.printValue = function(value) { 99 var result = self.formattingFn(value); 100 101 if (self.d.tagName === 'INPUT') { 102 this.d.value = result; 103 } 104 else if (self.d.tagName === 'text' || self.d.tagName === 'tspan') { 105 this.d.textContent = result; 106 } 107 else { 108 this.d.innerHTML = result; 109 } 110 }; 111 112 self.count = function(timestamp) { 113 114 if (!self.startTime) { self.startTime = timestamp; } 115 116 self.timestamp = timestamp; 117 var progress = timestamp - self.startTime; 118 self.remaining = self.duration - progress; 119 120 // to ease or not to ease 121 if (self.options.useEasing) { 122 if (self.countDown) { 123 self.frameVal = self.startVal - self.easingFn(progress, 0, self.startVal - self.endVal, self.duration); 124 } else { 125 self.frameVal = self.easingFn(progress, self.startVal, self.endVal - self.startVal, self.duration); 126 } 127 } else { 128 if (self.countDown) { 129 self.frameVal = self.startVal - ((self.startVal - self.endVal) * (progress / self.duration)); 130 } else { 131 self.frameVal = self.startVal + (self.endVal - self.startVal) * (progress / self.duration); 132 } 133 } 134 135 // don't go past endVal since progress can exceed duration in the last frame 136 if (self.countDown) { 137 self.frameVal = (self.frameVal < self.endVal) ? self.endVal : self.frameVal; 138 } else { 139 self.frameVal = (self.frameVal > self.endVal) ? self.endVal : self.frameVal; 140 } 141 142 // decimal 143 self.frameVal = Math.round(self.frameVal*self.dec)/self.dec; 144 145 // format and print value 146 self.printValue(self.frameVal); 147 148 // whether to continue 149 if (progress < self.duration) { 150 self.rAF = requestAnimationFrame(self.count); 151 } else { 152 if (self.callback) { self.callback(); } 153 } 154 }; 155 // start your animation 156 self.start = function(callback) { 157 self.callback = callback; 158 self.rAF = requestAnimationFrame(self.count); 159 return false; 160 }; 161 // toggles pause/resume animation 162 self.pauseResume = function() { 163 if (!self.paused) { 164 self.paused = true; 165 cancelAnimationFrame(self.rAF); 166 } else { 167 self.paused = false; 168 delete self.startTime; 169 self.duration = self.remaining; 170 self.startVal = self.frameVal; 171 requestAnimationFrame(self.count); 172 } 173 }; 174 // reset to startVal so animation can be run again 175 self.reset = function() { 176 self.paused = false; 177 delete self.startTime; 178 self.startVal = startVal; 179 cancelAnimationFrame(self.rAF); 180 self.printValue(self.startVal); 181 }; 182 // pass a new endVal and start animation 183 self.update = function (newEndVal) { 184 cancelAnimationFrame(self.rAF); 185 self.paused = false; 186 delete self.startTime; 187 self.startVal = self.frameVal; 188 self.endVal = Number(newEndVal); 189 self.countDown = (self.startVal > self.endVal); 190 self.rAF = requestAnimationFrame(self.count); 191 }; 192 193 // format startVal on initialization 194 self.printValue(self.startVal); 195};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.