🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

tm-odometer

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tm-odometer

TmOdometer: Lightweight JavaScript library for animated numeric counters with smooth transitions and precise decimal handling.

2.0.0
latest
stable
Source
npm
Version published
Weekly downloads
550
-3.51%
Maintainers
1
Weekly downloads
 
Created
Source

TmOdometer npm version MIT license

TmOdometer is a lightweight JavaScript library for creating animated numeric counters with smooth transitions and precise decimal handling. It is ideal for projects requiring dynamic number animations.

Table of Contents

Project Overview

TmOdometer is inspired by and built on top of HubSpot's Odometer library. It enhances the original functionality by introducing modern TypeScript and ES Module support, along with precise decimal handling to ensure numbers with decimal places retain their accuracy during and after animations (e.g., 1200 with a precision of 2 will display as 1200.00). Designed for seamless integration into any JavaScript or TypeScript project, it works effortlessly with modern module systems and bundlers.

While TmOdometer is a fully standalone library, it is also used as the foundation for the Angular version, TmNgOdometer, which provides seamless integration with Angular applications.

Features

  • Lightweight: Minimal dependencies and optimized for performance.
  • TypeScript Support: Fully rewritten in TypeScript, the library provides type safety, an enhanced developer experience, and automatically generated type definitions.
  • Flexible Integration: Works natively with ES Modules, CommonJS, AMD, and UMD bundles.
  • Decimal Precision: Preserves decimal precision during animations.
  • Customizable Themes: Supports various themes and animation styles.
  • Dynamic Updates: Easily update values programmatically to trigger animations.

Screenshots

See the TmOdometer in action below:

Quick Preview 1

A quick look at the TmOdometer in action.

Full FPS Video

Full FPS Video 1

Full FPS Video 2

Click to watch the high-quality video with smooth animations and detailed UI/UX.

Environment Setup

Prerequisites

Setup Steps

  • Install the library via npm:

    npm install tm-odometer --save
    
  • Include one or more themes

    <link
      rel="stylesheet"
      href="node_modules/tm-odometer/themes/odometer-theme-default.css"
    />
    

Tip: Replace 'default' in the file path with any of the available themes: 'car', 'digital', 'minimal', 'plaza', 'slot-machine', 'train-station'.

  • Import the library using one of the following methods:
  • ES Modules:

    import TmOdometer from 'tm-odometer';
    

Tip: If using TypeScript, types are directly provided with the library bundle.

  • CommonJS:

    const TmOdometer = require('tm-odometer');
    
  • AMD:

    require.config({
      baseUrl: './src',
      paths: {
        'tm-odometer': 'node_modules/tm-odometer/umd/tm-odometer',
      },
    });
    define(['tm-odometer'], function (TmOdometer) {});
    
  • Direct Inclusion

    <script src="node_modules/tm-odometer/umd/tm-odometer.min.js"></script>
    

Usage

How to Use

Use the Library Programmatically

Once the library is imported, you can use it as follows:

const odometer = new TmOdometer({
  el: document.getElementById('odometer-element'),
  value: 1000,
  animation: 'count',
  duration: 2000,
  format: '(,ddd).dd',
  theme: 'default',
});

// Update the value programmatically
odometer.update(2000);

Tip: When included directly, the legacy Odometer constructor is also available for backward compatibility with older versions.

Add Odometer to an Element

Another way to use the library is to add the odometer class to any element in your HTML:

<div class="odometer">123</div>

Update the value programmatically:

document.querySelector('.odometer').innerHTML = 456;

// or

document.querySelector('.odometer').odometer.update(456);

Configuration

The library supports the following configuration options:

OptionTypeDefaultDescription
elHTMLElementnullThe DOM element where the odometer will be rendered.
valuenumber0The initial value of the odometer.
animationstring'slide'Animation effect type. Options: 'slide', 'count'.
durationnumber2000 (ms)Duration of the animation in milliseconds.
formatstring'(,ddd).dd'Number format. Examples: '(,ddd)'12,345, '(,ddd).dd'12,345.67, (.ddd),dd12.345,67.
themestring'default'Theme for the odometer. Options: 'default', 'car', 'digital', 'minimal', 'plaza', 'slot-machine', 'train-station'.

Global Configuration

You can configure all odometer instances globally by defining the window.odometerOptions object before including or importing the tm-odometer library:

<script>
  window.odometerOptions = {
    animation: 'count', // Animation type ('slide' or 'count')
    duration: 3000, // Animation duration in milliseconds
    format: '(,ddd).dd', // Number format
    theme: 'car', // Theme for all instances
  };
</script>
<script src="tm-odometer/umd/tm-odometer.min.js"></script>

Per-Instance Configuration

If you need to configure a single odometer instance differently from the global configuration, you can initialize it programmatically using the TmOdometer constructor:

<div id="custom-odometer"></div>

<script>
  const customOdometer = new TmOdometer({
    el: document.getElementById('custom-odometer'),
    value: 123,
    animation: 'count', // Animation type ('slide' or 'count')
    duration: 3000, // Animation duration in milliseconds
    format: '(,ddd).dd', // Number format
    theme: 'minimal', // Theme for this instance
  });

  // Update the value programmatically
  setTimeout(() => {
    customOdometer.update(4567.89);
  }, 1000);
</script>

Demos

To help you get started, TmOdometer includes a comprehensive set of demos showcasing its usage with different module formats (CJS, ESM, UMD, AMD, and legacy). Each demo is a self-contained project that you can run locally.

Available Demos

DemoDescription
ESMDemonstrates usage with native ES Modules in modern browsers.
CJSDemonstrates usage with CommonJS in bundlers such as Webpack or ESBuild.
UMD BrowserDemonstrates usage with UMD bundles directly in the browser via <script>.
UMD AMDDemonstrates usage with AMD Modules in environments like RequireJS.
LegacyDemonstrates usage with the legacy version for older browsers.

How to Run the Demos

  • Clone the Repository:

    git clone https://github.com/mtmarco87/tm-odometer.git
    cd tm-odometer/demo
    
  • Run a Specific Demo:

    Each demo is a mini npm project. Navigate to the desired demo folder and follow the instructions below:

    • For ESM, CJS, and UMD AMD Demos:

      cd esm # Replace 'esm' with the desired demo folder (e.g., cjs, umd-amd)
      npm install
      npm start
      
    • For UMD Browser and Legacy Demos: These demos do not require npm. Simply open the index.html file in your browser:

      cd umd-browser # Replace 'umd-browser' with 'legacy' for the legacy demo
      open index.html
      
  • Explore the Code: Once the demo is running, explore the provided example to see how TmOdometer works with the selected module format. Feel free to modify the code to test different configurations, features, or integration scenarios.

Development

Setup for Development

  • Prerequisites:

  • Clone the Repository:

    git clone https://github.com/mtmarco87/tm-odometer.git
    cd tm-odometer
    
  • Install Dependencies:

    npm install
    gem install compass
    
  • Build the Library:

    npm run build
    
  • Watch for Changes:

    npm run dev
    
  • Build Package:

    npm run pack
    

Acknowledgments

  • HubSpot's Odometer: Original library by Adam Schwartz and Zack Bloom (GitHub).
  • Special thanks to contributors and the open-source community for their invaluable support and inspiration.

Extras

Angular Version

If you are looking for an Angular version of this library, check out TmNgOdometer, which builds on this project and provides seamless integration with Angular applications.

Support

If you find this library useful, consider supporting its development:

  • ⭐ Star the repository on GitHub.
  • 💬 Share feedback or suggestions by opening an issue.
  • Buy me a coffee to support future updates.
  • 🔵 BTC Address: bc1qzy6e99pkeq00rsx8jptx93jv56s9ak2lz32e2d
  • 🟣 ETH Address: 0x38cf74ED056fF994342941372F8ffC5C45E6cF21

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Keywords

tm-odometer

FAQs

Package last updated on 30 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts