countup.js vs odometer
JavaScript Animation Libraries Comparison
1 Year
countup.jsodometer
What's JavaScript Animation Libraries?

JavaScript animation libraries enhance web applications by providing smooth and visually appealing animations for numerical values. These libraries are particularly useful for displaying statistics, metrics, or any data that requires a dynamic representation. They help improve user engagement and provide a better understanding of data through animated transitions, making the information more digestible and visually appealing. Countup.js and Odometer are two popular libraries that serve this purpose, each with its own unique features and use cases.

Package Weekly Downloads Trend
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
countup.js457,0048,1051.24 MB1120 days agoMIT
odometer28,087---8 years agoMIT
Feature Comparison: countup.js vs odometer

Animation Style

  • countup.js:

    Countup.js provides a simple counting animation that smoothly transitions from a starting number to a target number. It allows for customizable duration and easing functions, making it easy to create a visually appealing count-up effect that feels natural and engaging.

  • odometer:

    Odometer features a unique mechanical-style animation that mimics the look of an old-fashioned odometer. The numbers flip and roll in a visually striking manner, making it highly engaging for users. This style is particularly effective for displaying metrics that change frequently.

Customization

  • countup.js:

    Countup.js offers various customization options, including the ability to set the duration of the animation, specify easing functions, and format the output number (e.g., adding commas or decimal places). This flexibility allows developers to tailor the animation to fit their specific design needs.

  • odometer:

    Odometer allows for customization of its appearance through CSS. Developers can change the font, size, and color of the numbers, as well as the overall style of the odometer. However, the core animation style is fixed, focusing on the mechanical effect.

Ease of Use

  • countup.js:

    Countup.js is straightforward to implement, requiring minimal setup. It has a simple API that allows developers to initiate the counting animation with just a few lines of code, making it accessible for quick integrations into projects.

  • odometer:

    Odometer is also easy to use but may require additional setup for styling and configuration. It involves creating an HTML element for the odometer and initializing it with JavaScript, which might be slightly more complex than Countup.js.

Performance

  • countup.js:

    Countup.js is lightweight and optimized for performance, ensuring smooth animations without significant overhead. It is designed to handle multiple instances efficiently, making it suitable for applications with numerous animated numbers.

  • odometer:

    Odometer, while visually appealing, may have a slightly higher performance cost due to its more complex animations. However, it is still optimized for performance and can handle multiple instances, though care should be taken with excessive use.

Use Cases

  • countup.js:

    Countup.js is ideal for scenarios where you want to display statistics, scores, or any numerical data that needs to count up to a target value. It is commonly used in dashboards, reports, and presentations where quick numerical representation is needed.

  • odometer:

    Odometer is best suited for applications that require a more dynamic and engaging display of changing numbers, such as real-time statistics, speedometers, or any context where a mechanical effect enhances the user experience.

How to Choose: countup.js vs odometer
  • countup.js:

    Choose Countup.js if you need a lightweight library specifically designed for counting up to a target number with customizable duration and easing functions. It is ideal for simple numeric animations and offers a straightforward API for quick implementation.

  • odometer:

    Choose Odometer if you want a more visually striking representation of numbers with a mechanical odometer effect. It is suitable for applications that require a more stylized and engaging way to display changing numbers, such as dashboards or statistics.

README for countup.js

CountUp.js

CountUp.js is a dependency-free, lightweight Javascript class that can be used to quickly create animations that display numerical data in a more interesting way.

Despite its name, CountUp can count in either direction, depending on the start and end values that you pass.

CountUp.js supports all browsers. MIT license.

Try the demo

Or tinker with CountUp in Stackblitz

Jump to:

Features

  • 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.
  • Plugins allow for alternate animations like the Odometer plugin

Odomoeter plugin

Usage:

Use CountUp with:

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 on including CountUp.

Params:

  • target: string | HTMLElement | HTMLInputElement - id of html element, input, svg text element, or DOM element reference where counting occurs
  • endVal: number - the value you want to arrive at
  • options?: CountUpOptions - optional configuration object for fine-grain control

Options (defaults in parentheses):

interface CountUpOptions {
  startVal?: number; // number to start at (0)
  decimalPlaces?: number; // number of decimal places (0)
  duration?: number; // animation duration in seconds (2)
  useGrouping?: boolean; // example: 1,000 vs 1000 (true)
  useIndianSeparators?: boolean; // example: 1,00,000 vs 100,000 (false)
  useEasing?: boolean; // ease animation (true)
  smartEasingThreshold?: number; // smooth easing for large numbers above this if useEasing (999)
  smartEasingAmount?: number; // amount to be eased for numbers above threshold (333)
  separator?: string; // grouping separator (',')
  decimal?: string; // decimal ('.')
  // easingFn: easing function for animation (easeOutExpo)
  easingFn?: (t: number, b: number, c: number, d: number) => number;
  formattingFn?: (n: number) => string; // this function formats result
  prefix?: string; // text prepended to result
  suffix?: string; // text appended to result
  numerals?: string[]; // numeral glyph substitution
  enableScrollSpy?: boolean; // start animation when target is in view
  scrollSpyDelay?: number; // delay (ms) after target comes into view
  scrollSpyOnce?: boolean; // run only once
  onCompleteCallback?: () => any; // gets called when animation completes
  onStartCallback?: () => any; // gets called when animation starts
  plugin?: CountUpPlugin; // for alternate animations
}

Example usage:

const countUp = new CountUp('targetId', 5234);
if (!countUp.error) {
  countUp.start();
} else {
  console.error(countUp.error);
}

Pass options:

const countUp = new CountUp('targetId', 5234, options);

with optional complete callback:

const countUp = new CountUp('targetId', 5234, { onCompleteCallback: someMethod });

// or (passing fn to start will override options.onCompleteCallback)
countUp.start(someMethod);

// or
countUp.start(() => console.log('Complete!'));

Other methods:

Toggle pause/resume:

countUp.pauseResume();

Reset the animation:

countUp.reset();

Update the end value and animate:

countUp.update(989);

Animate when the element is scrolled into view

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();

const countUp = new CountUp('targetId', 989, { enableScrollSpy: true });

Troubleshooting scroll spy

CountUp checks the scroll position as soon as it's initialized. So if you initialize it before the DOM renders and your target element is in view before any scrolling, you'll need to re-check the scroll position after the page renders:

// after DOM has rendered
countUp.handleScroll();

Alternate animations with plugins

Currently there's just one plugin, the Odometer Plugin.

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.

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 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, along with a separate requestAnimationFrame polyfill (see below).

For the examples below, first install CountUp. This will give you the latest:

npm i countup.js

Example with vanilla js

This is what I used in the demo. Checkout index.html and demo.js.

main.js:

import { CountUp } from './js/countUp.min.js';

window.onload = function() {
  var countUp = new CountUp('target', 2000);
  countUp.start();
}

Include in your html. Notice the type attribute:

<script src="./main.js" type="module"></script>

To support IE and legacy browsers, use the nomodule script tag to include separate scripts that don't use the module syntax:

<script nomodule src="js/countUp.umd.js"></script>
<script nomodule src="js/main-for-legacy.js"></script>

To run module-enabled scripts locally, you'll need a simple local server setup like this (test the demo locally by running npm run serve) because otherwise you may see a CORS error when your browser tries to load the script as a module.

For Webpack and other build systems

Import from the package, instead of the file location:

import { CountUp } from 'countup.js';

UMD module

CountUp is also wrapped as a UMD module in ./dist/countUp.umd.js and it exposes CountUp as a global variable on the window scope. To use it, include countUp.umd.js in a script tag, and invoke it like so:

var numAnim = new countUp.CountUp('myTarget', 2000);
numAnim.start()

requestAnimationFrame polyfill

You can include dist/requestAnimationFrame.polyfill.js if you want to support IE9 and older, and Opera mini.


Contributing

Before you make a pull request, please be sure to follow these instructions:

  1. Do your work on src/countUp.ts
  2. Lint: npm run lint
  3. Run tests: npm t
  4. Build and serve the demo by running npm start then check the demo to make sure it counts.

Creating Animation Plugins

CountUp supports plugins as of v2.6.0. Plugins implement their own render method to display each frame's formatted value. A class instance or object can be passed to the plugin property of CountUpOptions, and the plugin's render method will be called instead of CountUp's.

export declare interface CountUpPlugin {
  render(elem: HTMLElement, formatted: string): void;
}

An example of a plugin:

export class SomePlugin implements CountUpPlugin {
  // ...some properties here

  constructor(options: SomePluginOptions) {
    // ...setup code here if you need it
  }

  render(elem: HTMLElement, formatted: string): void {
    // render DOM here
  }
}

If you make a plugin, be sure to create a PR to add it to this README!