D3.js - Transition
D3.js - Transitions
Section titled “D3.js - Transitions”A transition in D3.js is the process of smoothly changing an element’s attributes or styles from one state to another over a specified duration. D3.js provides the transition() method to animate changes to HTML or SVG elements. This chapter explores how to use transitions effectively.
The transition() Method
Section titled “The transition() Method”The transition() method can be called on any D3 selection and initiates the transition process for the selected elements. It supports most selection methods for changing attributes (e.g., attr()) and styles (e.g., style()). However, methods that alter the DOM structure, like append() or data(), should typically be called before transition(). Transitions also have their own specific configuration methods like duration() and ease().
A simple transition can be defined by chaining it after a selection:
d3.select("body") .transition() .style("background-color", "lightblue");You can also create a transition object and apply it to a selection, though chaining is more common for straightforward transitions. If you create a transition object using d3.transition(), you can configure it and then pass it to a selection’s transition() method. This can be useful for synchronizing multiple transitions or reusing transition parameters.
const t = d3.transition() .duration(2000) // Animate over 2 seconds .ease(d3.easeLinear); // Use a linear easing function
d3.select("body") .transition(t) // Apply the pre-configured transition .style("background-color", "lightblue");Note: While the above method of passing a transition object t works, it’s often more idiomatic in modern D3 to chain configuration methods directly after selection.transition() unless you have specific reasons for reusing a transition object.
A Minimal Example
Section titled “A Minimal Example”Let’s create a basic example to observe how a transition works. This example will animate the background color of the page.
Create a new HTML file, for example transition_example.html, with the following code:
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title>D3 Transition Example</title> <script src="https://d3js.org/d3.v7.min.js"></script> </head> <body> <h3>Simple Transitions</h3> <script> // Select the body element d3.select("body") .transition() // Start a transition .duration(1500) // Set duration to 1.5 seconds .style("background-color", "lightblue"); // Target style </script> </body></html>In this example, we select the <body> element. Then, transition() initiates a transition. We then specify that the transition should last 1500 milliseconds (1.5 seconds) using duration(1500). Finally, .style("background-color", "lightblue") defines the target state: the background color should change to light blue. The browser will smoothly animate this change from its current color (likely white) to light blue.
If you wanted to chain another transition, for instance, to change the background color from light blue to gray after the first transition, you could do it like this:
d3.select("body") .transition() .duration(1500) .style("background-color", "lightblue") .transition() // Chain another transition .duration(1500) .delay(500) // Optional: wait 0.5 seconds before starting this one .style("background-color", "gray");This demonstrates how transitions can be sequenced. Each subsequent transition() call on an existing transition will wait for the previous one to finish before starting (unless a specific delay is also applied relative to the previous transition’s end).