// Tutorial //

Interpolating Values with Vue.js and Tween.js

Published on March 26, 2017
Default avatar
By Joshua Bemenderfer
Developer and author at DigitalOcean.
Interpolating Values with Vue.js and Tween.js

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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 @tweenjs/tween.js

# NPM
$ npm install @tweenjs/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.


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!