Tutorial

Super-Easy Property Tweening in Vue.js

Published on November 20, 2017
author

Joshua Bemenderfer

Super-Easy Property Tweening in Vue.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.

So if you’ve read our previous article on tweening properites in Vue.js, you probably noticed that, well, there’s a lot of code in there. It’s not a super simple thing to rip out and scatter throughout your own projects. So what if there was a way to abstract it so you’d hardly have to write any more code than with CSS transitions? Well, Luke Chinworth was thinking the same thing, and came up with vue-mixin-tween. It uses tween.js and distills the complicated tweening code into a simple mixin which can be included in any component.

Installation

# Yarn
$ yarn vue-mixin-tween
# NPM
$ npm install vue-mixin-tween --save

Usage

Go ahead and throw it right into any old component with a numeric property somewhere. The mixin will create a new reactive property based on the property name you passed in. For example: myProp becomes myPropTween and will update whenever myProp updates.

<template>
  <div>
    <button @click="addAlligators">Add Some Alligators</button>
    <h2>Number of Alligators: {{ numberOfAlligators }}</h2>
    <!-- You may want to Math.floor() the value first as it is a float. -->
    <h2>Number of Alligators: (Tweened) {{ numberOfAlligatorsTween }}</h2>
  </div>
</template>

<script>
import VueMixinTween from 'vue-mixin-tween';

export default {
  data() {
    return {
      numberOfAlligators: 0
    }
  },

  mixins: [
    // The only required argument is the name of the property to tween.
    // The default duration is 500 milliseconds.
    // The default timing function is TWEEN.Easing.Quadratic.Out
    // We're using a "custom" linear timing function here.
    VueMixinTween('numberOfAlligators', 5000, (pos) => pos)
  ],

  methods: {
    addAlligators() {
      this.numberOfAlligators = 500;
    }
  }
}
</script>

And there you have it. It’s dead-simple to customize the duration or even the timing function with far less code-per-component than doing it manually. Awesome!

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

Learn more about our products


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!

Featured on Community

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