Animation and Easing

Animation and Easing

This tutorial shows how to use different kinds of animation to change a property from one value to another in an interesting way. It progresses from simple lerp() to easing functions, then to animating multiple properties at once.

Use the ◀ ▶ buttons or arrow keys to step through the stages. Click inside the canvas to trigger animations. Drag any number in the code to adjust it live.

This tutorial shows how to use different kinds of animation, to change a property from one value to another in an interesting way.

It first shows a very simple way of adding animation, using just the p5.js lerp() function. Then it demonstrates how to set up a sketch so that it can use different functions that compute the intermediate value as a function of time. This allows you to use easing functions, such as the functions on easings.net.

Any property (position, size, color, rotation) can be animated, and you can animate several properties at once. For simplicity, the first part of this tutorial animates a single value: the horizontal (x) position of a line.

In this first step, there is no animation. Click the mouse to change the line’s horizontal position. It immediately jumps to the new position.

let currentX = 100;

function setup() {
  createCanvas(windowWidth, windowHeight);
}

function draw() {
  background(100);

  strokeWeight(2);
  line(currentX, 0, currentX, height);
}

function mousePressed() {
  currentX = mouseX;
}
Step 1 of 15

Try it on OpenProcessing: Animation and Easing