Creative Coding with SVGs
zuubaDigital

Tweening

codepen practice page

Tween Methods

A tween is simply a method used to animate a property (or multiple properies) of an object over time. GSAP actually has three very descriptively named tween methods:

gsap.to(): Tweens an object from it's current state to a different state.

gsap.from(): Tweens an object from a state defined by the tween to it's current state

gsap.fromTo(): Tweens an object from a state defined in the tween to a different state.

gsap.to()

As stated above, the gsap.to() method allows you to animate an object from its current state to a new state over a specified duration. Let's start with an example. In the HTML section of our practice codepen page you'll see three circles, each with a different id and a class of ball.

1 2 3 <circle id="red" class="ball" cx="30" cy="150" r="30" fill="red" /> <circle id="green" class="ball" cx="30" cy="250" r="25" fill="green" /> <circle id="blue" class="ball" cx="30" cy="350" r="20" fill="blue" />

In the JS section let's create variables for the three circles:

1 2 3 const red = document.querySelector("#red"); const green = document.querySelector("#green"); const blue = document.querySelector("#blue");

In order to animate an element you'll need to change one or more of it's presentation attributes using the following syntax:

gsap.to(<object>, {
    attr: {
      <property>: <value>,
      <property>: <value>,
      ...
    },
    // other animation properties
  });

The attr object can be used to animte valid svg presentation attribue. Let's say we want to move the red ball horizontally across the screen. To do so we'll need to tween the cx value like so:

1 2 3 4 5 gsap.to(red, { attr: { cx: 470 } });

We can animate all of the circles if we pass an array of objects instead of a single objects:

1 2 3 4 5 gsap.to([red, green, blue], { attr: { cx: 470 } });

We can also animate using id and class names. Here's an example animating by id:

1 2 3 4 5 gsap.to("#red", { attr: { cx: 470 } });

And even more powerfully, animating by class. The following tween will animate all of the circles, similar to the array example above:

1 2 3 4 5 gsap.to(".ball", { attr: { cx: 470 } });

The default duration of any tween is 0.5 seconds, but we can specify our own using the duration property. Let's set it to two seconds:

1 2 3 4 5 6 gsap.to(".ball", { attr: { cx: 470 }, duration: 2 });

Right now the animation runs just one time, but we can change that using the repeat property and assigning a value. To get the animation to repeat infinitely, assign a value of -1.

1 2 3 4 5 6 7 gsap.to(".ball", { attr: { cx: 470 }, duration: 2, repeat: -1 });

We're not just limited to animating geometric properties. In the animation below we'll tween the fill , opacity and radius of each circle as well.

1 2 3 4 5 6 7 8 9 10 gsap.to(".ball", { attr: { cx: 470, r: 100, fill: "purple", opacity: 0.5, }, duration: 2, repeat: -1 });

Tween properties

We can make our animation more interesting by adding special properties to our animation object. I've already shown you two - duration and repeat. Here are a few more that you'll probably use from time to time when using gsap.

duration: The duration of the animation from beginning to end

repeat: The number of times the animation repeats. A value of -1 makes the animation run indefinitely.

yoyo: This property is similar to the alternate CSS animation property, and it makes the animation run in the opposite direction every other time it repeats.

ease: This is basically the gsap version of the css timing function. You can find a number of interesting pre-defined easing functions and a visualizer for each function here.

stagger: When defining an animation for multiple objects, this property staggers the start time for each object by the designated amount.

1 2 3 4 5 6 7 8 9 10 gsap.to([red, green, blue], { attr: { cx: 470, }, duration: 2, repeat: -1, yoyo: true, ease: "back.inOut", stagger: 1, });

stagger object: You'll notice that the repeat only occurs after all three of the staggered animations have completed. What if you want each ball to repeat/yoyo right away, instead of waiting for thee others? You'll need to use the stagger object, which gives you more fine-tuned control over the animation.

By using the stagger object and and defining the repeat and yoyo inside that object, you get a different result. The each property sets the delay in the start time for each sub-tween.

1 2 3 4 5 6 7 8 9 10 11 12 13 gsap.to([red, green, blue], { attr: { cx: 470, }, duration: 2, ease: "back.inOut", // Repeats immediately, not waiting for the other staggered animations to finish stagger: { each: 1, repeat: -1, yoyo: true, }, });

gsap.from()

The from method is used to animate an element from the designated state to it's current state. In the example below we're going to animate the balls from the same point. (470, 250).

1 2 3 4 5 6 7 8 9 10 gsap.to([red, green, blue], { attr: { cx: 470, cy: 250, }, duration: 2, ease: "back.inOut", repeat: -1, yoyo: true, });

gsap.fromTo()

The fromTo method is used to animate elements from one state to another.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 gsap.fromTo( [red, green, blue], // from state { attr: { cx: 250, cy: 50, fill: "red", }, }, // to state { attr: { cx: 250, cy: 450, fill: "blue", }, duration: 2, stagger: { each: 0.5, repeat: -1, yoyo: true, }, ease: "back.inOut", } );

I've created a codepen containing all the methods we just discussed. Go ahead and modify the code, and see the results!