var element = document.querySelector("#box-1");
gsap.to("#box-1", {
duration: 2,
x: element.parentNode.offsetWidth - element.offsetWidth,
rotation: 360,
yoyo: true,
repeat: -1,
});
Creating an animation.
The method(s)
gsap.to() – This is the most common type of tween. A .to() tween will start at the element’s current state and animate “to” the values defined in the tween.
gsap.from() – Like a backwards .to() where it animates “from” the values defined in the tween and ends at the element’s current state.
gsap.fromTo() – You define both the starting and ending values.
gsap.set() – Immediately sets properties (no animation). It’s essentially a zero-duration .to() tween which can be reverted.
The target (or targets)
Next up we have to tell GSAP what we want to animate. Under the hood GSAP uses document.querySelectorAll(), so for HTML or SVG targets we can use selector text like ".class" and "#id". Or you can pass in a variable or even an Array.
// use a class or ID
gsap.to(".box", { x: 200 });
// a complex CSS selector
gsap.to("section > .box", { x: 200 });
// a variable
let box = document.querySelector(".box");
gsap.to(box, { x: 200 })
// or even an Array of elements
let square = document.querySelector(".square");
let circle = document.querySelector(".circle");
gsap.to([square, circle], { x: 200 })
The variables
The vars object contains all the information about the animation. This can be arbitrary properties you want to animate, or special properties that influence the behavior of the animation – like duration, onComplete or repeat.
gsap.to(target, {
// this is the vars object
// it contains properties to animate
x: 200,
rotation: 360,
// and special properties
duration: 2
})
Transform shorthand
| GSAP | CSS | Explanation |
|---|---|---|
| x: 100 | transform: translateX(100px) | Move horizontally (px or SVG units) |
| y: 100 | transform: translateY(100px) | Move vertically (px or SVG units) |
| xPercent: -50 | transform: translateX(-50%) | Move horizontally (percentage of element’s width) |
| yPercent: -50 | transform: translateY(-50%) | Move vertically (percentage of element’s height) |
| rotation: 360 | transform: rotate(360deg) | Rotate (degrees) |
| scale: 2 | transform: scale(2, 2) | Increase or decrease size |
| transformOrigin: “0% 100%” | transform-origin: 0% 100%; | The center of translation, this will rotate around the bottom left. |
| rotationX: 360 | transform: rotateX(360deg) |
| rotationY: 360 | transform: rotateY(360deg) |
| skewX: 45 | transform: skewX(45deg) |
| skewY: 45 | transform: skewY(45deg) |
| scaleX: 2 | transform: scaleX(2) |
| scaleY: 2 | transform: scaleY(2) |
x: 200, // use default of px x: "+=200" // relative values x: '40vw', // or pass in a string with a different unit for GSAP to parse x: () => window.innerWidth / 2, // you can even use functional values to do a calculation! rotation: 360 // use default of degrees rotation: "1.25rad" // use radians
What else can I animate?
CSS properties
Transforms, colors, padding, border radius, GSAP can animate it all! Just remember to camelCase the properties – e.g. background-color becomes backgroundColor.
gsap.to(".box", {
duration: 2,
backgroundColor: '#8d3dae',
yoyo: true,
repeat: -1
});
SVG attributes
Just like HTML elements, SVG elements can be animated with transform shorthands. Additionally you can animate SVG attributes like width, height, fill, stroke, cx, opacity and even the SVG viewBox itself using an attr object.
gsap.to(".svgBox", {
duration: 2,
x: 100, // use transform shorthand (this is now using SVG units not px, the SVG viewBox is 100 units wide)
xPercent: -100,
// or target SVG attributes
attr: {
fill: '#8d3dae',
rx: 50,
},
});
Any numeric value, color, or complex string containing numbers
GSAP doesn’t need DOM elements in order to animate properties. You can target literally any property of any object, even arbitrary ones you create like this:
let obj = { myNum: 10, myColor: "red" };
gsap.to(obj, {
myNum: 200,
myColor: "blue",
onUpdate: () => console.log(obj.myNum, obj.myColor)
});
In the demo below we have a box drawn with HTML canvas. We’re animating x and y values stored in a position object and then we update the canvas on each tick of the animation. GSAP is often used this way to animate in Three.js, HTML canvas and Pixi.js:
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#28a92b";
let position = { x: 0, y: 0 };
function draw() {
// erase the canvas
ctx.clearRect(0, 0, 300, 300);
// redraw the square at it's new position
ctx.fillRect(position.x, position.y, 100, 100);
}
//animate x and y of point
gsap.to(position, {
x: 200,
y: 200,
duration: 4,
// unlike DOM elements, canvas needs to be redrawn and cleared on every tick
onUpdate: draw
});
Special Properties
| Property | Description |
|---|---|
| duration | Duration of animation (seconds) Default: 0.5 |
| delay | Amount of delay before the animation should begin (seconds) |
| repeat | How many times the animation should repeat. |
| yoyo | If true, every other repeat the tween will run in the opposite direction. (like a yoyo) Default: false |
| stagger | Time (in seconds) between the start of each target’s animation (if multiple targets are provided) |
| ease | Controls the rate of change during the animation, like the motion’s “personality” or feel. Default: “power1.out” |
| onComplete | A function that runs when the animation completes |
Repeats and alternating repeats
repeat does exactly what you might think – it allows you to play an animation more than once. repeat is often paired with yoyo in order to reverse the direction each cycle. Change the code to yoyo:false in the demo below to see the difference.
gsap.to(".box", {
rotation: 360,
x: '100vw',
xPercent: -100,
// special properties
duration: 2, // how long the animation lasts
repeat: -1, // the number of repeats
yoyo: true, // this will alternate back and forth on each repeat. Like a yoyo
});
Delays
You can delay the start of an animation by a certain number of seconds. You can also use repeatDelay to add a delay to the start of any repeat iterations.
gsap.to(".green", {
rotation: 360,
duration: 1,
repeat: 1,
repeatDelay: 1,
});
gsap.to(".purple", {
rotation: 360,
duration: 1,
delay: 1 // delay the start of this animation
});
Easing
Easing is possibly the most important part of motion design. A well-chosen ease will add personality and breathe life into your animation.
Take a look at the difference between no ease and a bounce ease in the demo below! The green box with no ease spins around at a consistent speed, whereas the purple box with the ‘bounce’ ease revs up, races along and then bounces to a stop.
gsap.to(".green", { rotation: 360, duration: 2, ease: "none" }); gsap.to(".purple", { rotation: 360, duration: 2, ease: "bounce.out" });
gsap.to(".green", {
rotation: 360,
duration: 2,
repeat: -1,
repeatDelay: 2,
ease: 'none'
});
gsap.to(".purple", {
rotation: 360,
duration: 2,
repeat: -1,
repeatDelay: 2,
ease: 'bounce.out'
});
Staggers
This is one of our favourite tricks! If a tween has multiple targets, you can easily add some delightful stagger between the start of each animation:
gsap.from(".box", {
duration: 2,
scale: 0.5,
opacity: 0,
delay: 0.5,
stagger: 0.2,
ease: "elastic",
force3D: true
});
document.querySelectorAll(".box").forEach(function(box) {
box.addEventListener("click", function() {
gsap.to(".box", {
duration: 0.5,
opacity: 0,
y: -100,
stagger: 0.1,
ease: "back.in"
});
});
});
Sequencing animations
Just like we’ve just seen with staggers, It’s common to animate more than one thing. But what if we need more control over the order and timing of those animations. Delays give us rudimentary control:
gsap.to(".green", {
rotation: 360,
duration: 1,
});
gsap.to(".purple", {
rotation: 360,
duration: 1,
delay: 1,
});
gsap.to(".orange", {
rotation: 360,
duration: 1,
delay: 2,
});