demo.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. demo.js
  3. Only used for the countUp demo page, not a dependency
  4. */
  5. var demo, options, code, data, stars, easingFunctions,
  6. useOnComplete = false,
  7. useEasing = true,
  8. easingFn = null,
  9. useGrouping = true;
  10. window.onload = function() {
  11. var codeVisualizer = codeVisualizer,
  12. easingFnsDropdown = document.getElementById('easingFnsDropdown'),
  13. errorSection = document.getElementById('errorSection');
  14. // setup CountUp object - last 3 params (decimals, duration, options) are optional
  15. demo = new CountUp('myTargetElement', 0, 100, 0, 2.5, options);
  16. // you could do demo.start() right here but we are getting actual current star count from github api below
  17. // since it is an asynchronous call, we fire start() in the success fn of the XMLHttpRequest object
  18. getStars.send();
  19. // display version
  20. document.getElementById('version').innerHTML = 'v'+demo.version();
  21. };
  22. easingFunctions = {
  23. easeOutExpo: function(t, b, c, d) {
  24. return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
  25. },
  26. outQuintic: function(t, b, c, d) {
  27. var ts = (t /= d) * t;
  28. var tc = ts * t;
  29. return b + c * (tc * ts + -5 * ts * ts + 10 * tc + -10 * ts + 5 * t);
  30. },
  31. outCubic: function(t, b, c, d) {
  32. var ts = (t /= d) * t;
  33. var tc = ts * t;
  34. return b + c * (tc + -3 * ts + 3 * t);
  35. }
  36. };
  37. // for demo:
  38. function swapValues() {
  39. var oldStartVal = document.getElementById('startVal').value;
  40. var oldEndVal = document.getElementById('endVal').value;
  41. document.getElementById('startVal').value = oldEndVal;
  42. document.getElementById('endVal').value = oldStartVal;
  43. updateCodeVisualizer();
  44. }
  45. function getEasingFn() {
  46. var fn = easingFnsDropdown.value;
  47. if (fn === 'easeOutExpo') return null;
  48. if (typeof easingFunctions[fn] === 'undefined') return undefined;
  49. return easingFunctions[fn];
  50. }
  51. function getEasingFnBody(fn) {
  52. fn = typeof fn === 'undefined' ? getEasingFn() : fn;
  53. if (typeof fn === 'undefined') return 'undefined function';
  54. if (fn !== null) {
  55. return fn.toString().replace(/^ {8}/gm, '');
  56. }
  57. return '';
  58. }
  59. function getNumerals() {
  60. var numeralsCode = document.getElementById('numeralsDropdown').value;
  61. // optionally provide alternates for 0-9
  62. switch (numeralsCode) {
  63. case 'ea': // Eastern Arabic
  64. return ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
  65. case 'fa': // Farsi
  66. return ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
  67. default:
  68. return []
  69. }
  70. }
  71. function stringifyArray(arr) {
  72. return '[\'' + arr.join('\', \'') + '\']';
  73. }
  74. function createCountUp() {
  75. var startVal = document.getElementById('startVal').value;
  76. var endVal = document.getElementById('endVal').value;
  77. var decimals = document.getElementById('decimals').value,
  78. duration = document.getElementById('duration').value,
  79. prefix = document.getElementById('prefix').value,
  80. suffix = document.getElementById('suffix').value,
  81. easingFn = getEasingFn();
  82. options = {
  83. useEasing: useEasing,
  84. easingFn: typeof easingFn === 'undefined' ? null : easingFn,
  85. useGrouping: useGrouping,
  86. separator: document.getElementById('separator').value,
  87. decimal: document.getElementById('decimal').value,
  88. numerals: getNumerals()
  89. };
  90. if (prefix.length) options.prefix = prefix;
  91. if (suffix.length) options.suffix = suffix;
  92. // you don't have to create a new instance of CountUp every time you start an animation,
  93. // you can just change the properties individually. But I do here in case user changes values in demo.
  94. demo = new CountUp('myTargetElement', startVal, endVal, decimals, duration, options);
  95. if (!demo.error) {
  96. errorSection.style.display = 'none';
  97. if (useOnComplete) {
  98. demo.start(methodToCallOnComplete);
  99. } else {
  100. demo.start();
  101. }
  102. updateCodeVisualizer();
  103. }
  104. else {
  105. errorSection.style.display = 'block';
  106. document.getElementById('error').innerHTML = demo.error;
  107. console.error(demo.error);
  108. }
  109. }
  110. function showCodeAndPauseResume() {
  111. code = 'demo.pauseResume();';
  112. codeVisualizer.innerHTML = code;
  113. demo.pauseResume();
  114. }
  115. function showCodeAndReset() {
  116. code = 'demo.reset();';
  117. codeVisualizer.innerHTML = code;
  118. demo.reset();
  119. }
  120. function showCodeAndUpdate() {
  121. var updateVal = document.getElementById('updateVal').value;
  122. var num = updateVal ? updateVal : 0;
  123. code = 'demo.update('+num+');<br>';
  124. code += '// update method is useful for counting to large numbers. See README.';
  125. codeVisualizer.innerHTML = code;
  126. demo.update(num);
  127. }
  128. function toggleOnComplete(checkbox) {
  129. useOnComplete = checkbox.checked;
  130. updateCodeVisualizer();
  131. }
  132. function toggleEasing(checkbox) {
  133. useEasing = checkbox.checked
  134. easingFnsDropdown.disabled = !useEasing
  135. if (useEasing) {
  136. easingFnsDropdown.value = 'easeOutExpo';
  137. document.getElementById('easingFnPreview').value = "";
  138. }
  139. updateCodeVisualizer();
  140. }
  141. function toggleGrouping(checkbox) {
  142. useGrouping = checkbox.checked;
  143. updateCodeVisualizer();
  144. }
  145. function methodToCallOnComplete() {
  146. console.log('COMPLETE!');
  147. alert('COMPLETE!');
  148. }
  149. function updateCodeVisualizer() {
  150. var startVal = document.getElementById('startVal').value;
  151. startVal = Number(startVal.replace(',','').replace(' ',''));
  152. var endVal = document.getElementById('endVal').value;
  153. endVal = Number(endVal.replace(',','').replace(' ',''));
  154. var decimals = document.getElementById('decimals').value,
  155. duration = document.getElementById('duration').value,
  156. separator = document.getElementById('separator').value,
  157. decimal = document.getElementById('decimal').value,
  158. prefix = document.getElementById('prefix').value,
  159. suffix = document.getElementById('suffix').value,
  160. easingFn = getEasingFn(),
  161. easingFnBody = getEasingFnBody(easingFn),
  162. numerals = getNumerals(),
  163. code = '';
  164. if (useEasing && easingFn) {
  165. code += 'var easingFn = ';
  166. var split = easingFnBody.split('\n');
  167. for (var line in split) {
  168. code += split[line].replace(' ', '&nbsp;') + '<br>';
  169. }
  170. }
  171. code += 'var options = {<br>';
  172. code += (useEasing) ? '&emsp;&emsp;useEasing: true, <br>' : '&emsp;&emsp;useEasing: false, <br>';
  173. code += (easingFn && useEasing) ? '&emsp;&emsp;easingFn: easingFn, <br>' : '';
  174. code += (useGrouping) ? '&emsp;&emsp;useGrouping: true, <br>' : '&emsp;&emsp;useGrouping: false, <br>';
  175. code += '&emsp;&emsp;separator: \''+separator+'\', <br>';
  176. code += '&emsp;&emsp;decimal: \''+decimal+'\', <br>';
  177. if (prefix.length) code += '&emsp;&emsp;prefix: \''+prefix+'\', <br>';
  178. if (suffix.length) code += '&emsp;&emsp;suffix: \''+suffix+'\' <br>';
  179. if (numerals.length) code += '&emsp;&emsp;numerals: '+stringifyArray(numerals)+' <br>';
  180. code += '};<br>';
  181. code += 'var demo = new CountUp(\'myTargetElement\', '+startVal+', '+endVal+', '+decimals+', '+duration+', options);<br>';
  182. code += 'if (!demo.error) {<br>';
  183. if (useOnComplete) {
  184. code += '&emsp;&emsp;demo.start(methodToCallOnComplete);<br>';
  185. } else {
  186. code += '&emsp;&emsp;demo.start();<br>';
  187. }
  188. code += '} else {<br>';
  189. code += '&emsp;&emsp;console.error(demo.error);<br>}';
  190. codeVisualizer.innerHTML = code;
  191. }
  192. function updateCodeVisualizerForUpdate() {
  193. var updateVal = document.getElementById('updateVal').value;
  194. var num = updateVal ? updateVal : 0;
  195. code = 'demo.update(' + updateVal + ');';
  196. codeVisualizer.innerHTML = code;
  197. }
  198. // get current star count
  199. var repoInfoUrl = 'https://api.github.com/repos/inorganik/CountUp.js';
  200. var getStars = new XMLHttpRequest();
  201. getStars.open('GET', repoInfoUrl, true);
  202. getStars.timeout = 5000;
  203. getStars.onreadystatechange = function() {
  204. // 2: received headers, 3: loading, 4: done
  205. if (getStars.readyState == 4) {
  206. if (getStars.status == 200) {
  207. if (getStars.responseText !== 'undefined') {
  208. if (getStars.responseText.length > 0) {
  209. data = JSON.parse(getStars.responseText);
  210. stars = data.stargazers_count;
  211. // change input values
  212. document.getElementById('startVal').value = 0;
  213. document.getElementById('endVal').value = stars;
  214. document.getElementById('decimals').value = 0;
  215. createCountUp();
  216. }
  217. }
  218. }
  219. }
  220. };
  221. getStars.onerror = function() {
  222. console.error('error getting stars:', getStars.status);
  223. stars = getStars.status;
  224. demo.start();
  225. };