Tween basics
Tween intro
A tween changes properties of a single object over time. This can be HTML elements form CSS Selectors, and also normal javascript objects.
You can create a tween of anything that has quantifiable properties as numbers.
Settings defaults
Some default properties, like duration
, is only 0.5 seconds. You can change this by setting the defaults.
These properties will then be applied across every tween by default.
gsap.defaults({
duration: 2,
ease: "power2.inOut",
});
Animation methods
You can pass essentially anything into a tweening method. This can be an HTML element, a CSS selector, or a javascript object.
All these methods return a Tween
object that also has special properties you can control.
gsap.set(selector, animOptions)
: sets the default starting values for the animation. Useful for preventing having to abide by what you put in your CSS.gsap.from(selector, animOptions)
: animates starting from the animation specifications you give, and then animates to the default properties set on the elements.- Think of it like reverse.
gsap.to(selector, animOptions)
: animates starting from the default properties, and then animates to the animation specs you give.gsap.fromTo(selector, fromAnimOptions, toAnimOptions)
: animates starting from the starting specs, and then animates to the ending specs.
animation options
The animation options you can pass in vary depending on what you selected, whether it was a CSS selector or a JavaScript object. But there are some special properties on every animation config that defines how the animation will behave:
properties
duration
: the animation duration in seconds, 0.5 seconds by defaultdelay
: the animation delay in secondsease
: the easing function. See eases for a list of details.repeat
: how many times the animation should repeat. Default is 0, which means it will only play once.repeat: -1
: plays the animation infinitelyrepeat: 1
: repeats the animation once.
paused
: A boolean determining whether the animation should start paused or not. If true, the animation will start out as paused.reversed
: if true, the animation will start out as reversedstagger
: the stagger time in seconds for choreographing elementsyoyo
: a boolean value where if true, setsanimation-direction: alternate
.yoyoEase
: the easing function of the reverse animation.startAt
: an object of animation options, to define the starting values. For example,startAt: {x: -100, opacity: 0}
callbacks
onStart
: a function that runs when the animation startsonUpdate
: a function that runs on every frame of the animationonComplete
: a function that runs when the animation completesonRepeat
: a function that runs when the animation repeats
Tween class
Tweens are just an animation, and are what are returned from the gsap.set() , gsap.to(), gsap.from() , and gsap.fromTo() methods. You can then control the play state of the animation.
const tween = gsap.from(".header", { yPercent: -100, ease: "bounce" });
tween.pause();
tween.seek(2);
tween.progress(0.5);
tween.play();
tween.pause()
: pause the animationtween.resume()
: resume the animationtween.reverse()
: reverse the animation, and play ittween.play()
: play the animationtween.play(n)
: play the animation from n seconds in
tween.restart()
: restart the animationtween.duration()
: gets the duration of the tweentween.duration(seconds)
: sets the duration of the tween in secondstween.delay()
: gets the delay of the tweentween.delay(seconds)
: sets the delay of the tween in secondstween.endTime()
: Returns the time at which the animation will finish according to the parent timeline's local time, in seconds.tween.isActive()
: returns a boolean of whether or not the animation is currently playing.tween.kill()
: immediately stop and kills the animationtween.progress(percent)
: seeks to a position in the animation, being passed in apercent
, which is a number between 0-1, where 0 is the start of the animation, and 1 is the end of the animation.
Eases
WIth each tween, you can define an animation timing functions, which are combinations of timings and easing functions.
Eases are dfined using the ease
property in the animation config object, which accepts a string corresponding to the easing function and the timing function.
gsap.to(".box", { ease: "elastic" });
Timing functions
Here are the three types of timing functions:
- in : the animation starts slow and speeds up. Good for exits
- out : the animation starts fast and slows down Good for entrances
- inOut : the animation starts slow, speeds up, and then slows down again
By default, all eases use an out timing. You can specify the timing function for the ease using this syntax:
{
ease: "easeName.timingFunction";
}
So a elastic.inOut
would be an elastic ease with an inOut timing.
Easing functions
power0
: complete linearpower1
: x^2power2
: x^3power3
: x^4back
: overshoots the target value.- You can also configure the amount to overshoot by using it like a function, like
"back(6)"
, overshooting it 6 pixels.
- You can also configure the amount to overshoot by using it like a function, like
elastic
: rubber band effectbounce
: bounce effect
Stagger
Staggering is a way to choreograph animations. You can stagger animations by using the stagger
property in the animation config object.
When choreographing animations, stagger defines the amount of time to wait before starting the animation for each element in succession.
gsap.to("img", {
y: -100,
stagger: 0.2
duration: 3,
});
Advancing staggering
We can change the configuration of our staggering, like defining the direction to stagger from, and the total staggering amount.
gsap.to("img", {
y: -100,
stagger: {
from: "edges",
each: 0.2,
},
duration: 3,
});
from
: which elements to start the stagger from."start"
is default."end"
: start from end, the last element"center"
: start from center elements and propagate outwards"edges"
: start from the edges, the beginning and end, and go to center.
each
: the stagger duration. You need this if not specifying amount.amount
: the total amount of time for how long all the elements get to start their animation.- If
amount = 2
, then the last element will begin its tweening motion 2 seconds into the animation.
- If
transform origin
We can define transform origin either with keywords, or with two values representing the x and y coordinates of the origin. We can use standard CSS units to represent those values.
transformOrigin: "center center";
transformOrigin: "50% 50%";
transformOrigin: "10px 40px";
The code below sets the transform origin to where you clicked on the element and then rotates around that point.
const tween = gsap.to("#main-fred", {
rotate: 360,
duration: 1,
paused: true,
});
document.querySelector("#main-fred").addEventListener("click", (e) => {
const rect = e.target.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
gsap.set("#main-fred", { transformOrigin: `${x}px ${y}px` });
tween.restart();
});
- Create a tween, but make sure it starts paused so we can programatically start it later.
- Listen to a click event, and we can get the coordinates of where they clicked on for transformOrigin by using the code below:
const rect = e.target.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
- Set the transform origin to the coordinates we got from the click event. Also restart the tween.
gsap.set("#main-fred", { transformOrigin: `${x}px ${y}px` });
tween.restart();