.duration()
The .duration()
method sets the time for which a transition lasts. This method is crucial for creating animated visualizations as it allows developers to control the length of an animation. It accepts a single argument representing the duration in milliseconds for the transition.
Syntax
transition().duration(t)
t
: It is the duration of the animation and the only argument taken by the.duration()
method. If not specified, the default duration value is set to 250ms for the transition.
Example
Below is an example of how to use the .duration()
method to create a transition effect (color change) on an SVG circle:
function changeColor() {// Select the circle element by its IDd3.select('#myCircle').transition() // Start a transition.duration(1000) // Set the duration to 1 second.attr('fill', 'steelblue'); // Change the fill color to steelblueconsole.log('Color changed successfully');}
In this example, the selection identified by myCircle
will smoothly transition its color to steelblue
. Once the transition is complete, a console message will confirm its completion.
The following gif shows the transition effect on an SVG circle when the function is called:
The output in the console is as follows:
"Color changed successfully"
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.