123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- /*
- demo.js
- Only used for the countUp demo page, not a dependency
- */
- var demo, options, code, data, stars, easingFunctions,
- useOnComplete = false,
- useEasing = true,
- easingFn = null,
- useGrouping = true;
- window.onload = function() {
- var codeVisualizer = codeVisualizer,
- easingFnsDropdown = document.getElementById('easingFnsDropdown'),
- errorSection = document.getElementById('errorSection');
- // setup CountUp object - last 3 params (decimals, duration, options) are optional
- demo = new CountUp('myTargetElement', 0, 100, 0, 2.5, options);
- // you could do demo.start() right here but we are getting actual current star count from github api below
- // since it is an asynchronous call, we fire start() in the success fn of the XMLHttpRequest object
- getStars.send();
- // display version
- document.getElementById('version').innerHTML = 'v'+demo.version();
- };
- easingFunctions = {
- easeOutExpo: function(t, b, c, d) {
- return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b;
- },
- outQuintic: function(t, b, c, d) {
- var ts = (t /= d) * t;
- var tc = ts * t;
- return b + c * (tc * ts + -5 * ts * ts + 10 * tc + -10 * ts + 5 * t);
- },
- outCubic: function(t, b, c, d) {
- var ts = (t /= d) * t;
- var tc = ts * t;
- return b + c * (tc + -3 * ts + 3 * t);
- }
- };
- // for demo:
- function swapValues() {
- var oldStartVal = document.getElementById('startVal').value;
- var oldEndVal = document.getElementById('endVal').value;
- document.getElementById('startVal').value = oldEndVal;
- document.getElementById('endVal').value = oldStartVal;
- updateCodeVisualizer();
- }
- function getEasingFn() {
- var fn = easingFnsDropdown.value;
- if (fn === 'easeOutExpo') return null;
- if (typeof easingFunctions[fn] === 'undefined') return undefined;
- return easingFunctions[fn];
- }
- function getEasingFnBody(fn) {
- fn = typeof fn === 'undefined' ? getEasingFn() : fn;
- if (typeof fn === 'undefined') return 'undefined function';
- if (fn !== null) {
- return fn.toString().replace(/^ {8}/gm, '');
- }
- return '';
- }
- function getNumerals() {
- var numeralsCode = document.getElementById('numeralsDropdown').value;
- // optionally provide alternates for 0-9
- switch (numeralsCode) {
- case 'ea': // Eastern Arabic
- return ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
- case 'fa': // Farsi
- return ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
- default:
- return []
- }
- }
- function stringifyArray(arr) {
- return '[\'' + arr.join('\', \'') + '\']';
- }
- function createCountUp() {
- var startVal = document.getElementById('startVal').value;
- var endVal = document.getElementById('endVal').value;
- var decimals = document.getElementById('decimals').value,
- duration = document.getElementById('duration').value,
- prefix = document.getElementById('prefix').value,
- suffix = document.getElementById('suffix').value,
- easingFn = getEasingFn();
- options = {
- useEasing: useEasing,
- easingFn: typeof easingFn === 'undefined' ? null : easingFn,
- useGrouping: useGrouping,
- separator: document.getElementById('separator').value,
- decimal: document.getElementById('decimal').value,
- numerals: getNumerals()
- };
- if (prefix.length) options.prefix = prefix;
- if (suffix.length) options.suffix = suffix;
- // you don't have to create a new instance of CountUp every time you start an animation,
- // you can just change the properties individually. But I do here in case user changes values in demo.
- demo = new CountUp('myTargetElement', startVal, endVal, decimals, duration, options);
- if (!demo.error) {
- errorSection.style.display = 'none';
- if (useOnComplete) {
- demo.start(methodToCallOnComplete);
- } else {
- demo.start();
- }
- updateCodeVisualizer();
- }
- else {
- errorSection.style.display = 'block';
- document.getElementById('error').innerHTML = demo.error;
- console.error(demo.error);
- }
- }
- function showCodeAndPauseResume() {
- code = 'demo.pauseResume();';
- codeVisualizer.innerHTML = code;
- demo.pauseResume();
- }
- function showCodeAndReset() {
- code = 'demo.reset();';
- codeVisualizer.innerHTML = code;
- demo.reset();
- }
- function showCodeAndUpdate() {
- var updateVal = document.getElementById('updateVal').value;
- var num = updateVal ? updateVal : 0;
- code = 'demo.update('+num+');<br>';
- code += '// update method is useful for counting to large numbers. See README.';
- codeVisualizer.innerHTML = code;
- demo.update(num);
- }
- function toggleOnComplete(checkbox) {
- useOnComplete = checkbox.checked;
- updateCodeVisualizer();
- }
- function toggleEasing(checkbox) {
- useEasing = checkbox.checked
- easingFnsDropdown.disabled = !useEasing
- if (useEasing) {
- easingFnsDropdown.value = 'easeOutExpo';
- document.getElementById('easingFnPreview').value = "";
- }
- updateCodeVisualizer();
- }
- function toggleGrouping(checkbox) {
- useGrouping = checkbox.checked;
- updateCodeVisualizer();
- }
- function methodToCallOnComplete() {
- console.log('COMPLETE!');
- alert('COMPLETE!');
- }
- function updateCodeVisualizer() {
- var startVal = document.getElementById('startVal').value;
- startVal = Number(startVal.replace(',','').replace(' ',''));
- var endVal = document.getElementById('endVal').value;
- endVal = Number(endVal.replace(',','').replace(' ',''));
- var decimals = document.getElementById('decimals').value,
- duration = document.getElementById('duration').value,
- separator = document.getElementById('separator').value,
- decimal = document.getElementById('decimal').value,
- prefix = document.getElementById('prefix').value,
- suffix = document.getElementById('suffix').value,
- easingFn = getEasingFn(),
- easingFnBody = getEasingFnBody(easingFn),
- numerals = getNumerals(),
- code = '';
- if (useEasing && easingFn) {
- code += 'var easingFn = ';
- var split = easingFnBody.split('\n');
- for (var line in split) {
- code += split[line].replace(' ', ' ') + '<br>';
- }
- }
- code += 'var options = {<br>';
- code += (useEasing) ? '  useEasing: true, <br>' : '  useEasing: false, <br>';
- code += (easingFn && useEasing) ? '  easingFn: easingFn, <br>' : '';
- code += (useGrouping) ? '  useGrouping: true, <br>' : '  useGrouping: false, <br>';
- code += '  separator: \''+separator+'\', <br>';
- code += '  decimal: \''+decimal+'\', <br>';
- if (prefix.length) code += '  prefix: \''+prefix+'\', <br>';
- if (suffix.length) code += '  suffix: \''+suffix+'\' <br>';
- if (numerals.length) code += '  numerals: '+stringifyArray(numerals)+' <br>';
- code += '};<br>';
- code += 'var demo = new CountUp(\'myTargetElement\', '+startVal+', '+endVal+', '+decimals+', '+duration+', options);<br>';
- code += 'if (!demo.error) {<br>';
- if (useOnComplete) {
- code += '  demo.start(methodToCallOnComplete);<br>';
- } else {
- code += '  demo.start();<br>';
- }
- code += '} else {<br>';
- code += '  console.error(demo.error);<br>}';
- codeVisualizer.innerHTML = code;
- }
- function updateCodeVisualizerForUpdate() {
- var updateVal = document.getElementById('updateVal').value;
- var num = updateVal ? updateVal : 0;
- code = 'demo.update(' + updateVal + ');';
- codeVisualizer.innerHTML = code;
- }
- // get current star count
- var repoInfoUrl = 'https://api.github.com/repos/inorganik/CountUp.js';
- var getStars = new XMLHttpRequest();
- getStars.open('GET', repoInfoUrl, true);
- getStars.timeout = 5000;
- getStars.onreadystatechange = function() {
- // 2: received headers, 3: loading, 4: done
- if (getStars.readyState == 4) {
- if (getStars.status == 200) {
- if (getStars.responseText !== 'undefined') {
- if (getStars.responseText.length > 0) {
- data = JSON.parse(getStars.responseText);
- stars = data.stargazers_count;
- // change input values
- document.getElementById('startVal').value = 0;
- document.getElementById('endVal').value = stars;
- document.getElementById('decimals').value = 0;
-
- createCountUp();
- }
- }
- }
- }
- };
- getStars.onerror = function() {
- console.error('error getting stars:', getStars.status);
- stars = getStars.status;
- demo.start();
- };
|