demo.js 7.5 KB

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