Interpolating Values with Vue.js and Tween.js

Joshua Bemenderfer

Ever seen those fancy counter things on websites? You know, those numbers that increase over time and count all the little numbers in between? Well, if you’re looking to do that yourself, you’ve come to the right place. With Vue and Tween.js, we can implement the same sort of thing in a few minutes.

We’ll assume that you have a basic Vue project set up already. If not, it’s pretty easy.

Installation

First let’s install tween.js using Yarn or NPM.

# Yarn
$ yarn add tween.js

# NPM
$ npm install tween.js

Usage

Then let’s create a quick wrapper component to hold all the animating logic. It doesn’t really need to be styled.

AnimatedCounter.vue

<template>
  <span class="tweened-number">{{ tweeningValue }}</span>
</template>

<script>
export default {
  props: {
    // The value that we'll be tweening to.
    value: {
      type: Number,
      required: true
    },

    // How long the tween should take. (In milliseconds.)
    tweenDuration: {
      type: Number,
      default: 500
    }
  },

  watch: {
    // Whenever `props.value` changes, update the tween.
    value(newVal, oldVal) {
      this.tween(oldVal, newVal)
    }
  },

  // This holds the temporary state of the tweened value.
  data() {
    return {
      tweeningValue: 0
    }
  },

  mounted() {
    this.tween(0, this.value)
  },

  methods: {
    // This is our main logic block. It handles tweening from a start value to an end value.
    tween(start, end) {
      let frameHandler

      // Handles updating the tween on each frame.
      const animate = function (currentTime) {
        TWEEN.update(currentTime)
        frameHandler = requestAnimationFrame(animate)
      }

      const myTween = new TWEEN.Tween({ tweeningValue: start })
      .to({ tweeningValue: end }, this.tweenDuration)
      // Be careful to not to do too much here! It will slow down the app.
      .onUpdate(() => {
        this.tweeningValue = myTween.tweeningValue.toFixed(0)
      })
      .onComplete(() => {
        // Make sure to clean up after ourselves.
        cancelAnimationFrame(frameHandler)
      })
      // This actually starts the tween.
      .start()

      frameHandler = requestAnimationFrame(animate)
    }
  }
}
</script>

We can now use this component like so:

ExampleComponent.vue

<template>
  <div>
    <p>This number does move-y things. <animated-counter :value="myValue"></animated-counter></p>
    <p>You can change the tweened number here: <input type="text" v-model="myValue"/></p>
    <!-- It transitions like magic! Magic with a bunch of code behind it, that is. -->
  </div>
</template>
<script>
import AnimatedCounter from './AnimatedCounter.vue';

export default {
  components: {
    AnimatedCounter
  },

  data() {
    return {
      myValue: 100
    }
  }
}
</script>

That’s all there is to it! With the help of a few other libraries, you can tween other values too, like strings, arrays, or colors.

For more detailed information, consult the Tween.js and Vue.js docs.

  Tweet It
✖ Clear

🕵 Search Results

🔎 Searching...