Tutorial

Interpolating Values with Vue.js and Tween.js

Published on March 26, 2017
Default avatar

By Joshua Bemenderfer

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Joshua Bemenderfer

author

Still looking for an answer?

Ask a questionSearch for more help

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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel