demo.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { CountUp } from '../dist/countUp.js';
  2. window.onload = function () {
  3. var el = function (id) {
  4. return document.getElementById(id);
  5. };
  6. var code, stars, endVal, options;
  7. var demo = new CountUp('myTargetElement', 100);
  8. var codeVisualizer = el('codeVisualizer');
  9. var errorSection = el('errorSection');
  10. var startTime;
  11. el('version').innerHTML = demo.version;
  12. document.querySelectorAll('.updateCodeVis').forEach(elem => elem.onchange = updateCodeVisualizer);
  13. el('swapValues').onclick = function () {
  14. var oldStartVal = el('startVal').value;
  15. var oldEndVal = el('endVal').value;
  16. el('startVal').value = oldEndVal;
  17. el('endVal').value = oldStartVal;
  18. updateCodeVisualizer();
  19. };
  20. el('start').onclick = createCountUp;
  21. el('apply').onclick = createCountUp;
  22. el('pauseResume').onclick = function () {
  23. code += '<br>demo.pauseResume();';
  24. codeVisualizer.innerHTML = code;
  25. demo.pauseResume();
  26. };
  27. el('reset').onclick = function () {
  28. code += '<br>demo.reset();';
  29. codeVisualizer.innerHTML = code;
  30. demo.reset();
  31. };
  32. el('update').onclick = function () {
  33. var updateVal = el('updateVal').value;
  34. var num = updateVal ? updateVal : 0;
  35. code += "<br>demo.update(" + num + ");";
  36. codeVisualizer.innerHTML = code;
  37. demo.update(num);
  38. };
  39. el('updateVal').onchange = function () {
  40. var updateVal = el('updateVal').value;
  41. var num = updateVal ? updateVal : 0;
  42. code += '<br>demo.update(' + num + ');';
  43. codeVisualizer.innerHTML = code;
  44. };
  45. // OPTION VALUES
  46. var easingFunctions = {
  47. easeOutExpo: function (t, b, c, d) {
  48. return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
  49. },
  50. outQuintic: function (t, b, c, d) {
  51. var ts = (t /= d) * t;
  52. var tc = ts * t;
  53. return b + c * (tc * ts + -5 * ts * ts + 10 * tc + -10 * ts + 5 * t);
  54. },
  55. outCubic: function (t, b, c, d) {
  56. var ts = (t /= d) * t;
  57. var tc = ts * t;
  58. return b + c * (tc + -3 * ts + 3 * t);
  59. }
  60. };
  61. function getEasingFn() {
  62. var fn = el('easingFnsDropdown').value;
  63. if (fn === 'easeOutExpo') {
  64. return null;
  65. }
  66. if (typeof easingFunctions[fn] === 'undefined') {
  67. return undefined;
  68. }
  69. return easingFunctions[fn];
  70. }
  71. function getEasingFnBody(fn) {
  72. fn = typeof fn === 'undefined' ? getEasingFn() : fn;
  73. if (typeof fn === 'undefined') {
  74. return 'undefined function';
  75. }
  76. if (fn !== null) {
  77. return fn.toString().replace(/^ {8}/gm, '');
  78. }
  79. return '';
  80. }
  81. function getNumerals() {
  82. var numeralsCode = el('numeralsDropdown').value;
  83. // optionally provide alternates for 0-9
  84. switch (numeralsCode) {
  85. case 'ea': // Eastern Arabic
  86. return ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
  87. case 'fa': // Farsi
  88. return ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
  89. default:
  90. return null;
  91. }
  92. }
  93. var stringifyArray = function (arr) { return '[\'' + arr.join('\', \'') + '\']'; };
  94. // COUNTUP AND CODE VISUALIZER
  95. function createCountUp() {
  96. establishOptionsFromInputs();
  97. demo = new CountUp('myTargetElement', endVal, options);
  98. if (!demo.error) {
  99. errorSection.style.display = 'none';
  100. startTime = Date.now();
  101. demo.start();
  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 calculateAnimationTime() {
  111. const duration = Date.now() - startTime;
  112. console.log('actual animation duration (ms):', duration);
  113. alert('COMPLETE!');
  114. }
  115. function establishOptionsFromInputs() {
  116. endVal = Number(el('endVal').value);
  117. options = {
  118. startVal: el('startVal').value,
  119. decimalPlaces: el('decimalPlaces').value,
  120. duration: Number(el('duration').value),
  121. useEasing: el('useEasing').checked,
  122. useGrouping: el('useGrouping').checked,
  123. useIndianSeparators: el('useIndianSeparators').checked,
  124. easingFn: typeof getEasingFn() === 'undefined' ? null : getEasingFn(),
  125. separator: el('separator').value,
  126. decimal: el('decimal').value,
  127. prefix: el('prefix').value,
  128. suffix: el('suffix').value,
  129. numerals: getNumerals(),
  130. onCompleteCallback: el('useOnComplete').checked ? calculateAnimationTime : null
  131. };
  132. // unset null values so they don't overwrite defaults
  133. for (var key in options) {
  134. if (options.hasOwnProperty(key)) {
  135. if (options[key] === null) {
  136. delete options[key];
  137. }
  138. }
  139. }
  140. }
  141. function updateCodeVisualizer() {
  142. establishOptionsFromInputs();
  143. code = '';
  144. if (options.useEasing && options.easingFn) {
  145. code += 'const easingFn = ';
  146. var split = getEasingFnBody(options.easingFn).split('\n');
  147. for (var line in split) {
  148. if (split.hasOwnProperty(line)) {
  149. code += split[line].replace(' ', '&nbsp;') + '<br>';
  150. }
  151. }
  152. }
  153. function indentedLine(keyPair, singleLine) {
  154. if (singleLine === void 0) { singleLine = false; }
  155. var delimeter = (singleLine) ? ';' : ',';
  156. return "&emsp;&emsp;" + keyPair + delimeter + "<br>";
  157. }
  158. var opts = '';
  159. opts += (options.startVal !== '0') ? indentedLine("startVal: " + options.startVal) : '';
  160. opts += (options.decimalPlaces !== '0') ? indentedLine("decimalPlaces: " + options.decimalPlaces) : '';
  161. opts += (options.duration !== 2) ? indentedLine("duration: " + options.duration) : '';
  162. opts += (options.useEasing) ? '' : indentedLine("useEasing: " + options.useEasing);
  163. opts += (options.useEasing && options.easingFn) ? indentedLine("easingFn") : '';
  164. opts += (options.useGrouping) ? '' : indentedLine("useGrouping: " + options.useGrouping);
  165. opts += (options.useIndianSeparators) ? indentedLine("useIndianSeparators: " + options.useIndianSeparators) : '';
  166. opts += (options.separator !== ',') ? indentedLine("separator: '" + options.separator + "'") : '';
  167. opts += (options.decimal !== '.') ? indentedLine("decimal: '" + options.decimal + "'") : '';
  168. opts += (options.prefix.length) ? indentedLine("prefix: '" + options.prefix + "'") : '';
  169. opts += (options.suffix.length) ? indentedLine("suffix: '" + options.suffix + "'") : '';
  170. opts += (options.numerals && options.numerals.length) ?
  171. indentedLine("numerals: " + stringifyArray(options.numerals)) : '';
  172. opts += (options.onCompleteCallback) ? indentedLine("onCompleteCallback: methodToCallOnComplete") : '';
  173. if (opts.length) {
  174. code += "const options = {<br>" + opts + "};<br>";
  175. code += "let demo = new CountUp('myTargetElement', " + endVal + ", options);<br>";
  176. }
  177. else {
  178. code += "let demo = new CountUp('myTargetElement', " + endVal + ");<br>";
  179. }
  180. code += 'if (!demo.error) {<br>';
  181. code += indentedLine('demo.start()', true);
  182. code += '} else {<br>';
  183. code += indentedLine('console.error(demo.error)', true);
  184. code += '}';
  185. codeVisualizer.innerHTML = code;
  186. }
  187. // get current star count
  188. var repoInfoUrl = 'https://api.github.com/repos/inorganik/CountUp.js';
  189. var getStars = new XMLHttpRequest();
  190. getStars.open('GET', repoInfoUrl, true);
  191. getStars.timeout = 5000;
  192. getStars.onreadystatechange = function () {
  193. // 2: received headers, 3: loading, 4: done
  194. if (getStars.readyState === 4) {
  195. if (getStars.status === 200) {
  196. if (getStars.responseText !== 'undefined') {
  197. if (getStars.responseText.length > 0) {
  198. var data = JSON.parse(getStars.responseText);
  199. stars = data.stargazers_count;
  200. // change input values
  201. el('endVal').value = stars;
  202. createCountUp();
  203. }
  204. }
  205. }
  206. }
  207. };
  208. getStars.onerror = function () {
  209. console.error('error getting stars:', getStars.status);
  210. stars = getStars.status;
  211. demo.start();
  212. };
  213. getStars.send();
  214. }