DEV Community

Eulier Gonzalez
Eulier Gonzalez

Posted on

3 3

Vue animation state with GSAP

Little of theory:
We need to understand first “tweening” and why we use state based animation.

Tween (which is something that it use GSAP behind the scene in it's version 3 ) is short for between, so if we want to transition smoothly from 1 to 10, we’ll need to consider all the values between them along with all of their decimals.

By using Gsap we can have this happen automatically.
So we’ll use this concepts to create a state based animation.

This examples comes directly from Vue-mastery, so i don't own anything and this is just for educational purposes.

<template>
  <div>
    <div :style="{ width: tweenedNumber.toFixed(0) + 'px' }" class="bar">
      <span>{{ tweenedNumber.toFixed(0) }}</span>
    </div>
  </div>
</template>

<script>
import { gsap } from 'gsap'

export default {
  data() {
    return {
      number: 0,
      tweenedNumber: 0
    }
  },
  watch: {
    number(newValue) {
      gsap.to(this.$data, { 
          duration: 1, 
      ease: 'circ.out',
          tweenedNumber: newValue 
      })
    }
  },
  methods: {
    randomNumber() {
      this.number = Math.random() * (800 - 0)
    }
  },
  created() {
    setInterval(this.randomNumber, 1500)
  }
}
</script>

<style scoped>
.bar {
  padding: 5px;
  background-color: #2c3e50;
  border: 1px #16c0b0 solid;
  min-width: 20px;
}
.bar span {
  color: white;
}
</style>
Enter fullscreen mode Exit fullscreen mode

You can look at the example here:
https://repl.it/@eulier1/Gsap

Key features:

  • Watcher, it allows us to watch a reactive value and do things when that value changes.

  • Gsap, on the first parameter we’re passing the data’s option object, to give access to our data and on the second parameter we’re indicating how to animate it.

  • Tween a short for between and allow us to smoothly transition from 1 to 10 along with all their decimals.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️