countup.js
Advanced tools
Comparing version 2.6.0 to 2.6.1
module.exports = { | ||
root: true, | ||
ignorePatterns: ['*.js', '*.html', 'node_modules/*'], | ||
parser: '@typescript-eslint/parser', | ||
@@ -4,0 +5,0 @@ plugins: [ |
@@ -52,3 +52,3 @@ export interface CountUpOptions { | ||
* by disabling easing for the first part and enabling it on the second part. It is used if | ||
* usingEasing is true and the total animation amount exceeds the smartEasingThreshold. | ||
* useEasing is true and the total animation amount exceeds the smartEasingThreshold. | ||
*/ | ||
@@ -55,0 +55,0 @@ private determineDirectionAndSmartEasing; |
@@ -18,3 +18,3 @@ var __assign = (this && this.__assign) || function () { | ||
this.options = options; | ||
this.version = '2.6.0'; | ||
this.version = '2.6.1'; | ||
this.defaults = { | ||
@@ -181,3 +181,3 @@ startVal: 0, | ||
* by disabling easing for the first part and enabling it on the second part. It is used if | ||
* usingEasing is true and the total animation amount exceeds the smartEasingThreshold. | ||
* useEasing is true and the total animation amount exceeds the smartEasingThreshold. | ||
*/ | ||
@@ -184,0 +184,0 @@ CountUp.prototype.determineDirectionAndSmartEasing = function () { |
@@ -1,1 +0,1 @@ | ||
var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(t){for(var i,n=1,s=arguments.length;n<s;n++)for(var a in i=arguments[n])Object.prototype.hasOwnProperty.call(i,a)&&(t[a]=i[a]);return t}).apply(this,arguments)},CountUp=function(){function t(t,i,n){var s=this;this.endVal=i,this.options=n,this.version="2.6.0",this.defaults={startVal:0,decimalPlaces:0,duration:2,useEasing:!0,useGrouping:!0,useIndianSeparators:!1,smartEasingThreshold:999,smartEasingAmount:333,separator:",",decimal:".",prefix:"",suffix:"",enableScrollSpy:!1,scrollSpyDelay:200,scrollSpyOnce:!1},this.finalEndVal=null,this.useEasing=!0,this.countDown=!1,this.error="",this.startVal=0,this.paused=!0,this.once=!1,this.count=function(t){s.startTime||(s.startTime=t);var i=t-s.startTime;s.remaining=s.duration-i,s.useEasing?s.countDown?s.frameVal=s.startVal-s.easingFn(i,0,s.startVal-s.endVal,s.duration):s.frameVal=s.easingFn(i,s.startVal,s.endVal-s.startVal,s.duration):s.frameVal=s.startVal+(s.endVal-s.startVal)*(i/s.duration);var n=s.countDown?s.frameVal<s.endVal:s.frameVal>s.endVal;s.frameVal=n?s.endVal:s.frameVal,s.frameVal=Number(s.frameVal.toFixed(s.options.decimalPlaces)),s.printValue(s.frameVal),i<s.duration?s.rAF=requestAnimationFrame(s.count):null!==s.finalEndVal?s.update(s.finalEndVal):s.options.onCompleteCallback&&s.options.onCompleteCallback()},this.formatNumber=function(t){var i,n,a,e,o=t<0?"-":"";i=Math.abs(t).toFixed(s.options.decimalPlaces);var r=(i+="").split(".");if(n=r[0],a=r.length>1?s.options.decimal+r[1]:"",s.options.useGrouping){e="";for(var l=3,h=0,u=0,p=n.length;u<p;++u)s.options.useIndianSeparators&&4===u&&(l=2,h=1),0!==u&&h%l==0&&(e=s.options.separator+e),h++,e=n[p-u-1]+e;n=e}return s.options.numerals&&s.options.numerals.length&&(n=n.replace(/[0-9]/g,function(t){return s.options.numerals[+t]}),a=a.replace(/[0-9]/g,function(t){return s.options.numerals[+t]})),o+s.options.prefix+n+a+s.options.suffix},this.easeOutExpo=function(t,i,n,s){return n*(1-Math.pow(2,-10*t/s))*1024/1023+i},this.options=__assign(__assign({},this.defaults),n),this.formattingFn=this.options.formattingFn?this.options.formattingFn:this.formatNumber,this.easingFn=this.options.easingFn?this.options.easingFn:this.easeOutExpo,this.startVal=this.validateValue(this.options.startVal),this.frameVal=this.startVal,this.endVal=this.validateValue(i),this.options.decimalPlaces=Math.max(this.options.decimalPlaces),this.resetDuration(),this.options.separator=String(this.options.separator),this.useEasing=this.options.useEasing,""===this.options.separator&&(this.options.useGrouping=!1),this.el="string"==typeof t?document.getElementById(t):t,this.el?this.printValue(this.startVal):this.error="[CountUp] target is null or undefined","undefined"!=typeof window&&this.options.enableScrollSpy&&(this.error?console.error(this.error,t):(window.onScrollFns=window.onScrollFns||[],window.onScrollFns.push(function(){return s.handleScroll(s)}),window.onscroll=function(){window.onScrollFns.forEach(function(t){return t()})},this.handleScroll(this)))}return t.prototype.handleScroll=function(t){if(t&&window&&!t.once){var i=window.innerHeight+window.scrollY,n=t.el.getBoundingClientRect(),s=n.top+window.pageYOffset,a=n.top+n.height+window.pageYOffset;a<i&&a>window.scrollY&&t.paused?(t.paused=!1,setTimeout(function(){return t.start()},t.options.scrollSpyDelay),t.options.scrollSpyOnce&&(t.once=!0)):(window.scrollY>a||s>i)&&!t.paused&&t.reset()}},t.prototype.determineDirectionAndSmartEasing=function(){var t=this.finalEndVal?this.finalEndVal:this.endVal;this.countDown=this.startVal>t;var i=t-this.startVal;if(Math.abs(i)>this.options.smartEasingThreshold&&this.options.useEasing){this.finalEndVal=t;var n=this.countDown?1:-1;this.endVal=t+n*this.options.smartEasingAmount,this.duration=this.duration/2}else this.endVal=t,this.finalEndVal=null;null!==this.finalEndVal?this.useEasing=!1:this.useEasing=this.options.useEasing},t.prototype.start=function(t){this.error||(t&&(this.options.onCompleteCallback=t),this.duration>0?(this.determineDirectionAndSmartEasing(),this.paused=!1,this.rAF=requestAnimationFrame(this.count)):this.printValue(this.endVal))},t.prototype.pauseResume=function(){this.paused?(this.startTime=null,this.duration=this.remaining,this.startVal=this.frameVal,this.determineDirectionAndSmartEasing(),this.rAF=requestAnimationFrame(this.count)):cancelAnimationFrame(this.rAF),this.paused=!this.paused},t.prototype.reset=function(){cancelAnimationFrame(this.rAF),this.paused=!0,this.resetDuration(),this.startVal=this.validateValue(this.options.startVal),this.frameVal=this.startVal,this.printValue(this.startVal)},t.prototype.update=function(t){cancelAnimationFrame(this.rAF),this.startTime=null,this.endVal=this.validateValue(t),this.endVal!==this.frameVal&&(this.startVal=this.frameVal,null==this.finalEndVal&&this.resetDuration(),this.finalEndVal=null,this.determineDirectionAndSmartEasing(),this.rAF=requestAnimationFrame(this.count))},t.prototype.printValue=function(t){var i;if(this.el){var n=this.formattingFn(t);if(null!==(i=this.options.plugin)&&void 0!==i&&i.render)this.options.plugin.render(this.el,n);else if("INPUT"===this.el.tagName)this.el.value=n;else"text"===this.el.tagName||"tspan"===this.el.tagName?this.el.textContent=n:this.el.innerHTML=n}},t.prototype.ensureNumber=function(t){return"number"==typeof t&&!isNaN(t)},t.prototype.validateValue=function(t){var i=Number(t);return this.ensureNumber(i)?i:(this.error="[CountUp] invalid start or end value: ".concat(t),null)},t.prototype.resetDuration=function(){this.startTime=null,this.duration=1e3*Number(this.options.duration),this.remaining=this.duration},t}();export{CountUp}; | ||
var t=window&&window.__assign||function(){return t=Object.assign||function(t){for(var i,n=1,s=arguments.length;n<s;n++)for(var a in i=arguments[n])Object.prototype.hasOwnProperty.call(i,a)&&(t[a]=i[a]);return t},t.apply(this,arguments)},i=function(){function i(i,n,s){var a=this;this.endVal=n,this.options=s,this.version="2.6.1",this.defaults={startVal:0,decimalPlaces:0,duration:2,useEasing:!0,useGrouping:!0,useIndianSeparators:!1,smartEasingThreshold:999,smartEasingAmount:333,separator:",",decimal:".",prefix:"",suffix:"",enableScrollSpy:!1,scrollSpyDelay:200,scrollSpyOnce:!1},this.finalEndVal=null,this.useEasing=!0,this.countDown=!1,this.error="",this.startVal=0,this.paused=!0,this.once=!1,this.count=function(t){a.startTime||(a.startTime=t);var i=t-a.startTime;a.remaining=a.duration-i,a.useEasing?a.countDown?a.frameVal=a.startVal-a.easingFn(i,0,a.startVal-a.endVal,a.duration):a.frameVal=a.easingFn(i,a.startVal,a.endVal-a.startVal,a.duration):a.frameVal=a.startVal+(a.endVal-a.startVal)*(i/a.duration);var n=a.countDown?a.frameVal<a.endVal:a.frameVal>a.endVal;a.frameVal=n?a.endVal:a.frameVal,a.frameVal=Number(a.frameVal.toFixed(a.options.decimalPlaces)),a.printValue(a.frameVal),i<a.duration?a.rAF=requestAnimationFrame(a.count):null!==a.finalEndVal?a.update(a.finalEndVal):a.options.onCompleteCallback&&a.options.onCompleteCallback()},this.formatNumber=function(t){var i,n,s,e,o=t<0?"-":"";i=Math.abs(t).toFixed(a.options.decimalPlaces);var r=(i+="").split(".");if(n=r[0],s=r.length>1?a.options.decimal+r[1]:"",a.options.useGrouping){e="";for(var l=3,h=0,u=0,p=n.length;u<p;++u)a.options.useIndianSeparators&&4===u&&(l=2,h=1),0!==u&&h%l==0&&(e=a.options.separator+e),h++,e=n[p-u-1]+e;n=e}return a.options.numerals&&a.options.numerals.length&&(n=n.replace(/[0-9]/g,(function(t){return a.options.numerals[+t]})),s=s.replace(/[0-9]/g,(function(t){return a.options.numerals[+t]}))),o+a.options.prefix+n+s+a.options.suffix},this.easeOutExpo=function(t,i,n,s){return n*(1-Math.pow(2,-10*t/s))*1024/1023+i},this.options=t(t({},this.defaults),s),this.formattingFn=this.options.formattingFn?this.options.formattingFn:this.formatNumber,this.easingFn=this.options.easingFn?this.options.easingFn:this.easeOutExpo,this.startVal=this.validateValue(this.options.startVal),this.frameVal=this.startVal,this.endVal=this.validateValue(n),this.options.decimalPlaces=Math.max(this.options.decimalPlaces),this.resetDuration(),this.options.separator=String(this.options.separator),this.useEasing=this.options.useEasing,""===this.options.separator&&(this.options.useGrouping=!1),this.el="string"==typeof i?document.getElementById(i):i,this.el?this.printValue(this.startVal):this.error="[CountUp] target is null or undefined","undefined"!=typeof window&&this.options.enableScrollSpy&&(this.error?console.error(this.error,i):(window.onScrollFns=window.onScrollFns||[],window.onScrollFns.push((function(){return a.handleScroll(a)})),window.onscroll=function(){window.onScrollFns.forEach((function(t){return t()}))},this.handleScroll(this)))}return i.prototype.handleScroll=function(t){if(t&&window&&!t.once){var i=window.innerHeight+window.scrollY,n=t.el.getBoundingClientRect(),s=n.top+window.pageYOffset,a=n.top+n.height+window.pageYOffset;a<i&&a>window.scrollY&&t.paused?(t.paused=!1,setTimeout((function(){return t.start()}),t.options.scrollSpyDelay),t.options.scrollSpyOnce&&(t.once=!0)):(window.scrollY>a||s>i)&&!t.paused&&t.reset()}},i.prototype.determineDirectionAndSmartEasing=function(){var t=this.finalEndVal?this.finalEndVal:this.endVal;this.countDown=this.startVal>t;var i=t-this.startVal;if(Math.abs(i)>this.options.smartEasingThreshold&&this.options.useEasing){this.finalEndVal=t;var n=this.countDown?1:-1;this.endVal=t+n*this.options.smartEasingAmount,this.duration=this.duration/2}else this.endVal=t,this.finalEndVal=null;null!==this.finalEndVal?this.useEasing=!1:this.useEasing=this.options.useEasing},i.prototype.start=function(t){this.error||(t&&(this.options.onCompleteCallback=t),this.duration>0?(this.determineDirectionAndSmartEasing(),this.paused=!1,this.rAF=requestAnimationFrame(this.count)):this.printValue(this.endVal))},i.prototype.pauseResume=function(){this.paused?(this.startTime=null,this.duration=this.remaining,this.startVal=this.frameVal,this.determineDirectionAndSmartEasing(),this.rAF=requestAnimationFrame(this.count)):cancelAnimationFrame(this.rAF),this.paused=!this.paused},i.prototype.reset=function(){cancelAnimationFrame(this.rAF),this.paused=!0,this.resetDuration(),this.startVal=this.validateValue(this.options.startVal),this.frameVal=this.startVal,this.printValue(this.startVal)},i.prototype.update=function(t){cancelAnimationFrame(this.rAF),this.startTime=null,this.endVal=this.validateValue(t),this.endVal!==this.frameVal&&(this.startVal=this.frameVal,null==this.finalEndVal&&this.resetDuration(),this.finalEndVal=null,this.determineDirectionAndSmartEasing(),this.rAF=requestAnimationFrame(this.count))},i.prototype.printValue=function(t){var i;if(this.el){var n=this.formattingFn(t);if(null===(i=this.options.plugin)||void 0===i?void 0:i.render)this.options.plugin.render(this.el,n);else if("INPUT"===this.el.tagName)this.el.value=n;else"text"===this.el.tagName||"tspan"===this.el.tagName?this.el.textContent=n:this.el.innerHTML=n}},i.prototype.ensureNumber=function(t){return"number"==typeof t&&!isNaN(t)},i.prototype.validateValue=function(t){var i=Number(t);return this.ensureNumber(i)?i:(this.error="[CountUp] invalid start or end value: ".concat(t),null)},i.prototype.resetDuration=function(){this.startTime=null,this.duration=1e3*Number(this.options.duration),this.remaining=this.duration},i}();export{i as CountUp}; |
@@ -1,311 +0,1 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.countUp = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
var __assign = (undefined && undefined.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
// playground: stackblitz.com/edit/countup-typescript | ||
var CountUp = /** @class */ (function () { | ||
function CountUp(target, endVal, options) { | ||
var _this = this; | ||
this.endVal = endVal; | ||
this.options = options; | ||
this.version = '2.6.0'; | ||
this.defaults = { | ||
startVal: 0, | ||
decimalPlaces: 0, | ||
duration: 2, | ||
useEasing: true, | ||
useGrouping: true, | ||
useIndianSeparators: false, | ||
smartEasingThreshold: 999, | ||
smartEasingAmount: 333, | ||
separator: ',', | ||
decimal: '.', | ||
prefix: '', | ||
suffix: '', | ||
enableScrollSpy: false, | ||
scrollSpyDelay: 200, | ||
scrollSpyOnce: false, | ||
}; | ||
this.finalEndVal = null; // for smart easing | ||
this.useEasing = true; | ||
this.countDown = false; | ||
this.error = ''; | ||
this.startVal = 0; | ||
this.paused = true; | ||
this.once = false; | ||
this.count = function (timestamp) { | ||
if (!_this.startTime) { | ||
_this.startTime = timestamp; | ||
} | ||
var progress = timestamp - _this.startTime; | ||
_this.remaining = _this.duration - progress; | ||
// to ease or not to ease | ||
if (_this.useEasing) { | ||
if (_this.countDown) { | ||
_this.frameVal = _this.startVal - _this.easingFn(progress, 0, _this.startVal - _this.endVal, _this.duration); | ||
} | ||
else { | ||
_this.frameVal = _this.easingFn(progress, _this.startVal, _this.endVal - _this.startVal, _this.duration); | ||
} | ||
} | ||
else { | ||
_this.frameVal = _this.startVal + (_this.endVal - _this.startVal) * (progress / _this.duration); | ||
} | ||
// don't go past endVal since progress can exceed duration in the last frame | ||
var wentPast = _this.countDown ? _this.frameVal < _this.endVal : _this.frameVal > _this.endVal; | ||
_this.frameVal = wentPast ? _this.endVal : _this.frameVal; | ||
// decimal | ||
_this.frameVal = Number(_this.frameVal.toFixed(_this.options.decimalPlaces)); | ||
// format and print value | ||
_this.printValue(_this.frameVal); | ||
// whether to continue | ||
if (progress < _this.duration) { | ||
_this.rAF = requestAnimationFrame(_this.count); | ||
} | ||
else if (_this.finalEndVal !== null) { | ||
// smart easing | ||
_this.update(_this.finalEndVal); | ||
} | ||
else { | ||
if (_this.options.onCompleteCallback) { | ||
_this.options.onCompleteCallback(); | ||
} | ||
} | ||
}; | ||
// default format and easing functions | ||
this.formatNumber = function (num) { | ||
var neg = (num < 0) ? '-' : ''; | ||
var result, x1, x2, x3; | ||
result = Math.abs(num).toFixed(_this.options.decimalPlaces); | ||
result += ''; | ||
var x = result.split('.'); | ||
x1 = x[0]; | ||
x2 = x.length > 1 ? _this.options.decimal + x[1] : ''; | ||
if (_this.options.useGrouping) { | ||
x3 = ''; | ||
var factor = 3, j = 0; | ||
for (var i = 0, len = x1.length; i < len; ++i) { | ||
if (_this.options.useIndianSeparators && i === 4) { | ||
factor = 2; | ||
j = 1; | ||
} | ||
if (i !== 0 && (j % factor) === 0) { | ||
x3 = _this.options.separator + x3; | ||
} | ||
j++; | ||
x3 = x1[len - i - 1] + x3; | ||
} | ||
x1 = x3; | ||
} | ||
// optional numeral substitution | ||
if (_this.options.numerals && _this.options.numerals.length) { | ||
x1 = x1.replace(/[0-9]/g, function (w) { return _this.options.numerals[+w]; }); | ||
x2 = x2.replace(/[0-9]/g, function (w) { return _this.options.numerals[+w]; }); | ||
} | ||
return neg + _this.options.prefix + x1 + x2 + _this.options.suffix; | ||
}; | ||
// t: current time, b: beginning value, c: change in value, d: duration | ||
this.easeOutExpo = function (t, b, c, d) { | ||
return c * (-Math.pow(2, -10 * t / d) + 1) * 1024 / 1023 + b; | ||
}; | ||
this.options = __assign(__assign({}, this.defaults), options); | ||
this.formattingFn = (this.options.formattingFn) ? | ||
this.options.formattingFn : this.formatNumber; | ||
this.easingFn = (this.options.easingFn) ? | ||
this.options.easingFn : this.easeOutExpo; | ||
this.startVal = this.validateValue(this.options.startVal); | ||
this.frameVal = this.startVal; | ||
this.endVal = this.validateValue(endVal); | ||
this.options.decimalPlaces = Math.max(this.options.decimalPlaces); | ||
this.resetDuration(); | ||
this.options.separator = String(this.options.separator); | ||
this.useEasing = this.options.useEasing; | ||
if (this.options.separator === '') { | ||
this.options.useGrouping = false; | ||
} | ||
this.el = (typeof target === 'string') ? document.getElementById(target) : target; | ||
if (this.el) { | ||
this.printValue(this.startVal); | ||
} | ||
else { | ||
this.error = '[CountUp] target is null or undefined'; | ||
} | ||
// scroll spy | ||
if (typeof window !== 'undefined' && this.options.enableScrollSpy) { | ||
if (!this.error) { | ||
// set up global array of onscroll functions to handle multiple instances | ||
window['onScrollFns'] = window['onScrollFns'] || []; | ||
window['onScrollFns'].push(function () { return _this.handleScroll(_this); }); | ||
window.onscroll = function () { | ||
window['onScrollFns'].forEach(function (fn) { return fn(); }); | ||
}; | ||
this.handleScroll(this); | ||
} | ||
else { | ||
console.error(this.error, target); | ||
} | ||
} | ||
} | ||
CountUp.prototype.handleScroll = function (self) { | ||
if (!self || !window || self.once) | ||
return; | ||
var bottomOfScroll = window.innerHeight + window.scrollY; | ||
var rect = self.el.getBoundingClientRect(); | ||
var topOfEl = rect.top + window.pageYOffset; | ||
var bottomOfEl = rect.top + rect.height + window.pageYOffset; | ||
if (bottomOfEl < bottomOfScroll && bottomOfEl > window.scrollY && self.paused) { | ||
// in view | ||
self.paused = false; | ||
setTimeout(function () { return self.start(); }, self.options.scrollSpyDelay); | ||
if (self.options.scrollSpyOnce) | ||
self.once = true; | ||
} | ||
else if ((window.scrollY > bottomOfEl || topOfEl > bottomOfScroll) && | ||
!self.paused) { | ||
// out of view | ||
self.reset(); | ||
} | ||
}; | ||
/** | ||
* Smart easing works by breaking the animation into 2 parts, the second part being the | ||
* smartEasingAmount and first part being the total amount minus the smartEasingAmount. It works | ||
* by disabling easing for the first part and enabling it on the second part. It is used if | ||
* usingEasing is true and the total animation amount exceeds the smartEasingThreshold. | ||
*/ | ||
CountUp.prototype.determineDirectionAndSmartEasing = function () { | ||
var end = (this.finalEndVal) ? this.finalEndVal : this.endVal; | ||
this.countDown = (this.startVal > end); | ||
var animateAmount = end - this.startVal; | ||
if (Math.abs(animateAmount) > this.options.smartEasingThreshold && this.options.useEasing) { | ||
this.finalEndVal = end; | ||
var up = (this.countDown) ? 1 : -1; | ||
this.endVal = end + (up * this.options.smartEasingAmount); | ||
this.duration = this.duration / 2; | ||
} | ||
else { | ||
this.endVal = end; | ||
this.finalEndVal = null; | ||
} | ||
if (this.finalEndVal !== null) { | ||
// setting finalEndVal indicates smart easing | ||
this.useEasing = false; | ||
} | ||
else { | ||
this.useEasing = this.options.useEasing; | ||
} | ||
}; | ||
// start animation | ||
CountUp.prototype.start = function (callback) { | ||
if (this.error) { | ||
return; | ||
} | ||
if (callback) { | ||
this.options.onCompleteCallback = callback; | ||
} | ||
if (this.duration > 0) { | ||
this.determineDirectionAndSmartEasing(); | ||
this.paused = false; | ||
this.rAF = requestAnimationFrame(this.count); | ||
} | ||
else { | ||
this.printValue(this.endVal); | ||
} | ||
}; | ||
// pause/resume animation | ||
CountUp.prototype.pauseResume = function () { | ||
if (!this.paused) { | ||
cancelAnimationFrame(this.rAF); | ||
} | ||
else { | ||
this.startTime = null; | ||
this.duration = this.remaining; | ||
this.startVal = this.frameVal; | ||
this.determineDirectionAndSmartEasing(); | ||
this.rAF = requestAnimationFrame(this.count); | ||
} | ||
this.paused = !this.paused; | ||
}; | ||
// reset to startVal so animation can be run again | ||
CountUp.prototype.reset = function () { | ||
cancelAnimationFrame(this.rAF); | ||
this.paused = true; | ||
this.resetDuration(); | ||
this.startVal = this.validateValue(this.options.startVal); | ||
this.frameVal = this.startVal; | ||
this.printValue(this.startVal); | ||
}; | ||
// pass a new endVal and start animation | ||
CountUp.prototype.update = function (newEndVal) { | ||
cancelAnimationFrame(this.rAF); | ||
this.startTime = null; | ||
this.endVal = this.validateValue(newEndVal); | ||
if (this.endVal === this.frameVal) { | ||
return; | ||
} | ||
this.startVal = this.frameVal; | ||
if (this.finalEndVal == null) { | ||
this.resetDuration(); | ||
} | ||
this.finalEndVal = null; | ||
this.determineDirectionAndSmartEasing(); | ||
this.rAF = requestAnimationFrame(this.count); | ||
}; | ||
CountUp.prototype.printValue = function (val) { | ||
var _a; | ||
if (!this.el) | ||
return; | ||
var result = this.formattingFn(val); | ||
if ((_a = this.options.plugin) === null || _a === void 0 ? void 0 : _a.render) { | ||
this.options.plugin.render(this.el, result); | ||
return; | ||
} | ||
if (this.el.tagName === 'INPUT') { | ||
var input = this.el; | ||
input.value = result; | ||
} | ||
else if (this.el.tagName === 'text' || this.el.tagName === 'tspan') { | ||
this.el.textContent = result; | ||
} | ||
else { | ||
this.el.innerHTML = result; | ||
} | ||
}; | ||
CountUp.prototype.ensureNumber = function (n) { | ||
return (typeof n === 'number' && !isNaN(n)); | ||
}; | ||
CountUp.prototype.validateValue = function (value) { | ||
var newValue = Number(value); | ||
if (!this.ensureNumber(newValue)) { | ||
this.error = "[CountUp] invalid start or end value: ".concat(value); | ||
return null; | ||
} | ||
else { | ||
return newValue; | ||
} | ||
}; | ||
CountUp.prototype.resetDuration = function () { | ||
this.startTime = null; | ||
this.duration = Number(this.options.duration) * 1000; | ||
this.remaining = this.duration; | ||
}; | ||
return CountUp; | ||
}()); | ||
exports.CountUp = CountUp; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i((t="undefined"!=typeof globalThis?globalThis:t||self).countUp={})}(this,(function(t){"use strict";var i=window&&window.__assign||function(){return i=Object.assign||function(t){for(var i,n=1,s=arguments.length;n<s;n++)for(var e in i=arguments[n])Object.prototype.hasOwnProperty.call(i,e)&&(t[e]=i[e]);return t},i.apply(this,arguments)},n=function(){function t(t,n,s){var e=this;this.endVal=n,this.options=s,this.version="2.6.1",this.defaults={startVal:0,decimalPlaces:0,duration:2,useEasing:!0,useGrouping:!0,useIndianSeparators:!1,smartEasingThreshold:999,smartEasingAmount:333,separator:",",decimal:".",prefix:"",suffix:"",enableScrollSpy:!1,scrollSpyDelay:200,scrollSpyOnce:!1},this.finalEndVal=null,this.useEasing=!0,this.countDown=!1,this.error="",this.startVal=0,this.paused=!0,this.once=!1,this.count=function(t){e.startTime||(e.startTime=t);var i=t-e.startTime;e.remaining=e.duration-i,e.useEasing?e.countDown?e.frameVal=e.startVal-e.easingFn(i,0,e.startVal-e.endVal,e.duration):e.frameVal=e.easingFn(i,e.startVal,e.endVal-e.startVal,e.duration):e.frameVal=e.startVal+(e.endVal-e.startVal)*(i/e.duration);var n=e.countDown?e.frameVal<e.endVal:e.frameVal>e.endVal;e.frameVal=n?e.endVal:e.frameVal,e.frameVal=Number(e.frameVal.toFixed(e.options.decimalPlaces)),e.printValue(e.frameVal),i<e.duration?e.rAF=requestAnimationFrame(e.count):null!==e.finalEndVal?e.update(e.finalEndVal):e.options.onCompleteCallback&&e.options.onCompleteCallback()},this.formatNumber=function(t){var i,n,s,a,o=t<0?"-":"";i=Math.abs(t).toFixed(e.options.decimalPlaces);var r=(i+="").split(".");if(n=r[0],s=r.length>1?e.options.decimal+r[1]:"",e.options.useGrouping){a="";for(var l=3,u=0,h=0,p=n.length;h<p;++h)e.options.useIndianSeparators&&4===h&&(l=2,u=1),0!==h&&u%l==0&&(a=e.options.separator+a),u++,a=n[p-h-1]+a;n=a}return e.options.numerals&&e.options.numerals.length&&(n=n.replace(/[0-9]/g,(function(t){return e.options.numerals[+t]})),s=s.replace(/[0-9]/g,(function(t){return e.options.numerals[+t]}))),o+e.options.prefix+n+s+e.options.suffix},this.easeOutExpo=function(t,i,n,s){return n*(1-Math.pow(2,-10*t/s))*1024/1023+i},this.options=i(i({},this.defaults),s),this.formattingFn=this.options.formattingFn?this.options.formattingFn:this.formatNumber,this.easingFn=this.options.easingFn?this.options.easingFn:this.easeOutExpo,this.startVal=this.validateValue(this.options.startVal),this.frameVal=this.startVal,this.endVal=this.validateValue(n),this.options.decimalPlaces=Math.max(this.options.decimalPlaces),this.resetDuration(),this.options.separator=String(this.options.separator),this.useEasing=this.options.useEasing,""===this.options.separator&&(this.options.useGrouping=!1),this.el="string"==typeof t?document.getElementById(t):t,this.el?this.printValue(this.startVal):this.error="[CountUp] target is null or undefined","undefined"!=typeof window&&this.options.enableScrollSpy&&(this.error?console.error(this.error,t):(window.onScrollFns=window.onScrollFns||[],window.onScrollFns.push((function(){return e.handleScroll(e)})),window.onscroll=function(){window.onScrollFns.forEach((function(t){return t()}))},this.handleScroll(this)))}return t.prototype.handleScroll=function(t){if(t&&window&&!t.once){var i=window.innerHeight+window.scrollY,n=t.el.getBoundingClientRect(),s=n.top+window.pageYOffset,e=n.top+n.height+window.pageYOffset;e<i&&e>window.scrollY&&t.paused?(t.paused=!1,setTimeout((function(){return t.start()}),t.options.scrollSpyDelay),t.options.scrollSpyOnce&&(t.once=!0)):(window.scrollY>e||s>i)&&!t.paused&&t.reset()}},t.prototype.determineDirectionAndSmartEasing=function(){var t=this.finalEndVal?this.finalEndVal:this.endVal;this.countDown=this.startVal>t;var i=t-this.startVal;if(Math.abs(i)>this.options.smartEasingThreshold&&this.options.useEasing){this.finalEndVal=t;var n=this.countDown?1:-1;this.endVal=t+n*this.options.smartEasingAmount,this.duration=this.duration/2}else this.endVal=t,this.finalEndVal=null;null!==this.finalEndVal?this.useEasing=!1:this.useEasing=this.options.useEasing},t.prototype.start=function(t){this.error||(t&&(this.options.onCompleteCallback=t),this.duration>0?(this.determineDirectionAndSmartEasing(),this.paused=!1,this.rAF=requestAnimationFrame(this.count)):this.printValue(this.endVal))},t.prototype.pauseResume=function(){this.paused?(this.startTime=null,this.duration=this.remaining,this.startVal=this.frameVal,this.determineDirectionAndSmartEasing(),this.rAF=requestAnimationFrame(this.count)):cancelAnimationFrame(this.rAF),this.paused=!this.paused},t.prototype.reset=function(){cancelAnimationFrame(this.rAF),this.paused=!0,this.resetDuration(),this.startVal=this.validateValue(this.options.startVal),this.frameVal=this.startVal,this.printValue(this.startVal)},t.prototype.update=function(t){cancelAnimationFrame(this.rAF),this.startTime=null,this.endVal=this.validateValue(t),this.endVal!==this.frameVal&&(this.startVal=this.frameVal,null==this.finalEndVal&&this.resetDuration(),this.finalEndVal=null,this.determineDirectionAndSmartEasing(),this.rAF=requestAnimationFrame(this.count))},t.prototype.printValue=function(t){var i;if(this.el){var n=this.formattingFn(t);if(null===(i=this.options.plugin)||void 0===i?void 0:i.render)this.options.plugin.render(this.el,n);else if("INPUT"===this.el.tagName)this.el.value=n;else"text"===this.el.tagName||"tspan"===this.el.tagName?this.el.textContent=n:this.el.innerHTML=n}},t.prototype.ensureNumber=function(t){return"number"==typeof t&&!isNaN(t)},t.prototype.validateValue=function(t){var i=Number(t);return this.ensureNumber(i)?i:(this.error="[CountUp] invalid start or end value: ".concat(t),null)},t.prototype.resetDuration=function(){this.startTime=null,this.duration=1e3*Number(this.options.duration),this.remaining=this.duration},t}();t.CountUp=n,Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "countup.js", | ||
"description": "Animates a numerical value by counting to it", | ||
"version": "2.6.0", | ||
"version": "2.6.1", | ||
"license": "MIT", | ||
"main": "./dist/countUp.umd.js", | ||
"author": "Jamie Perkins", | ||
"main": "./dist/countUp.min.js", | ||
"module": "./dist/countUp.min.js", | ||
"author": "@inorganik", | ||
"types": "./dist/countUp.d.ts", | ||
"repository": { | ||
@@ -14,5 +15,4 @@ "type": "git", | ||
"scripts": { | ||
"build": "npm run clean && tsc && gulp && npm run build:umd", | ||
"build:umd": "rollup dist/countUp.js --format umd --file dist/countUp.umd.js --name countUp", | ||
"clean": "gulp clean", | ||
"build": "npm run clean && tsc && rollup -c rollup.config.js", | ||
"clean": "rimraf dist/countUp.*", | ||
"lint": "eslint -c .eslintrc.js --ext .ts ./src", | ||
@@ -29,18 +29,13 @@ "serve": "http-server -o -c-1 ./", | ||
"@typescript-eslint/parser": "^5.41.0", | ||
"browserify": "^17.0.0", | ||
"del": "^6.1.1", | ||
"eslint": "^8.26.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"gulp": "^4.0.2", | ||
"gulp-concat": "^2.6.1", | ||
"gulp-uglify": "^3.0.2", | ||
"http-server": "^14.1.1", | ||
"jest": "^28.1.3", | ||
"jest-environment-jsdom": "^28.1.3", | ||
"rimraf": "^5.0.0", | ||
"rollup": "^2.79.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"ts-jest": "^28.0.8", | ||
"typescript": "^4.8.4", | ||
"uglify-es": "^3.3.9" | ||
}, | ||
"types": "./dist/countUp.d.ts" | ||
"typescript": "^4.8.4" | ||
} | ||
} |
@@ -14,3 +14,2 @@ # CountUp.js | ||
- **[Features](#features)** | ||
- **[Usage](#usage)** | ||
@@ -21,24 +20,26 @@ - **[Including CountUp](#including-countup)** | ||
## CountUp for frameworks and plugins: | ||
- **[CountUp.js with Angular 2+](https://github.com/inorganik/ngx-countUp)** | ||
- **[CountUp.js with Angular 1.x](https://github.com/inorganik/countUp.js-angular1)** | ||
- **[CountUp.js with React](https://gist.github.com/inorganik/2cf776865a4c65c12857027870e9898e)** | ||
- **[CountUp.js with Svelte](https://gist.github.com/inorganik/85a66941ab88cc10c5fa5b26aead5f2a)** | ||
- **[CountUp.js Vue component wrapper](https://github.com/xlsdg/vue-countup-v2)** | ||
- **[CountUp.js WordPress Plugin](https://wordpress.org/plugins/countup-js/)** | ||
- **[CountUp.js with jQuery](https://gist.github.com/inorganik/b63dbe5b3810ff2c0175aee4670a4732)** | ||
## Features | ||
- **Animate when element scrolls into view** - new in v2.1.0. Use option `enableScrollSpy`. | ||
- **Animate when element scrolls into view.** Use option `enableScrollSpy`. | ||
- **Highly customizeable** with a large range of options, you can even substitute numerals. | ||
- **Smart easing**: CountUp intelligently defers easing until it gets close enough to the end value for easing to be visually noticeable. Configureable in the [options](#options). | ||
- **Separate bundles** for modern and legacy browsers, with and without the requestAnimationFrame polyfill. Choose `countUp.min.js` for modern browsers or `countUp.withPolyfill.min.js` for IE9 and older, and Opera mini. | ||
- **Plugins** allow for alternate animations like the [Odometer plugin](https://www.npmjs.com/package/odometer_countup) | ||
![Odomoeter plugin](./demo/images/odometer_plugin.gif) | ||
## Usage: | ||
**On npm** as: `countup.js`. You can import as a module or include the UMD script and access CountUp as a global. See [detailed instructions](#including-countup) below. | ||
**Use CountUp with:** | ||
- [Angular 2+](https://github.com/inorganik/ngx-countUp) | ||
- [Angular 1.x](https://github.com/inorganik/countUp.js-angular1) | ||
- [React](https://gist.github.com/inorganik/2cf776865a4c65c12857027870e9898e) | ||
- [Svelte](https://gist.github.com/inorganik/85a66941ab88cc10c5fa5b26aead5f2a) | ||
- [Vue](https://github.com/xlsdg/vue-countup-v2) | ||
- [WordPress](https://wordpress.org/plugins/countup-js/) | ||
- [jQuery](https://gist.github.com/inorganik/b63dbe5b3810ff2c0175aee4670a4732) | ||
**Use CountUp directly:** | ||
On npm as `countup.js`. You can import as a module, or include the UMD script and access CountUp as a global. See [detailed instructions](#including-countup) on including CountUp. | ||
**Params**: | ||
@@ -125,3 +126,4 @@ - `target: string | HTMLElement | HTMLInputElement` - id of html element, input, svg text element, or DOM element reference where counting occurs | ||
### Animate when the element is scrolled into view | ||
--- | ||
### **Animate when the element is scrolled into view** | ||
@@ -143,6 +145,20 @@ Use the scroll spy option to animate when the element is scrolled into view. When using scroll spy, just initialize CountUp but do not call start(); | ||
--- | ||
### **Alternate animations with plugins** | ||
Currently there's just one plugin, the **[Odometer Plugin](https://github.com/msoler75/odometer_countup.js)**. | ||
To use a plugin, you'll need to first install the plugin package. Then you can include it and use the plugin option. See each plugin's docs for more detailed info. | ||
```js | ||
const countUp = new CountUp('targetId', 5234, { | ||
plugin: new Odometer({ duration: 2.3, lastDigitDelay: 0 }), | ||
duration: 3.0 | ||
}); | ||
``` | ||
If you'd like to make your own plugin, see [the docs](#creating-animation-plugins) below! | ||
--- | ||
## Including CountUp | ||
CountUp is distributed as an ES6 module because it is the most standardized and most widely compatible module for browsers, though a UMD module is [also included](#umd-module). | ||
CountUp is distributed as an ES6 module because it is the most standardized and most widely compatible module for browsers, though a UMD module is [also included](#umd-module), along with a separate requestAnimationFrame polyfill (see below). | ||
@@ -197,2 +213,6 @@ For the examples below, first install CountUp. This will give you the latest: | ||
### requestAnimationFrame polyfill | ||
You can include `dist/requestAnimationFrame.polyfill.js` if you want to support IE9 and older, and Opera mini. | ||
--- | ||
@@ -244,1 +264,3 @@ | ||
``` | ||
If you make a plugin, be sure to create a PR to add it to this README! |
@@ -32,3 +32,3 @@ export interface CountUpOptions { // (default) | ||
version = '2.6.0'; | ||
version = '2.6.1'; | ||
private defaults: CountUpOptions = { | ||
@@ -139,3 +139,3 @@ startVal: 0, | ||
* by disabling easing for the first part and enabling it on the second part. It is used if | ||
* usingEasing is true and the total animation amount exceeds the smartEasingThreshold. | ||
* useEasing is true and the total animation amount exceeds the smartEasingThreshold. | ||
*/ | ||
@@ -142,0 +142,0 @@ private determineDirectionAndSmartEasing(): void { |
1235223
14
261
1517