Animations and Transitions

We talk about the different ways we can animate changes in our charts, and rule out SVG <animate>.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Fullstack D3 Masterclass course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Fullstack D3 Masterclass, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Fullstack D3 Masterclass

Alright, so this lesson is going to be really fun. We're going to talk about transitions and animations. So when your chart goes from one state to the next, it's really nice to preserve the context of the first state to show how it relates to the second state. So imagine you went to a track race and you blink and all of a sudden the runners are at the finish line. Now first of all, it's not very fun. And second of all, you have no information, you have no idea who won or how they got there, assuming they ran. So we're going to talk about the different ways to animate our charts and not only is this visually really appealing, but it gives your user that context of how the two states relate to each other. So there are three ways we can animate changes in our chart. We're going to talk about each of them in depth, but really briefly they are SVG animate tag, CSS transitions and D3 transitions. So let's take a quick look at these SVG animate tags. So we can see here we have an SVG element and a rectangle. And then within our rectangle element, we have these two animate elements. Now they don't render anything, but they describe how our square should be moving around. So this first one is animating the X attribute. It's going between 0, 20 and 0. It's going to take about two seconds and it's going to go indefinitely. And the second one is animating the fill in a similar way, but it has a different duration. Now this is all we're going to talk about with SVG animates, animate tag. And that is because when the current browser support is a little bit spotty, and to the future of this animate tag is a little bit uncertain. So generally you kind of want to stay away from this. It's not really worth learning at this point, but I wanted to show you all just in case you came across it when you were looking for examples online. The other thing here is these are static animations. So these animate tags are defined within our rectangle, but we might only want a one-time transition between one state and the next. And we don't know those values beforehand and we're editing this in JavaScript. So these really aren't ideal for what we're going to be doing with our charts. So again, these are SVG animate tags. Really don't use them. And they're not really worth learning, but I wanted to go over them anyway. And up next we're going to talk about the two ways which are actually really powerful, which are CSS transitions and D3 transition.