How can I run a transition continuously in D3.js?
For example, say I want to change the body
colour from red to blue and back again, continuously (I don't, that would be horrendous, but go with it).
This is how I would do it once:
d3.select("body").transition().duration(1000).style("background-color", "red");
How would I do it continuously?
The closest examples I have seen use d3.timer
, but I am not sure if there is a better way to do it.
For D3 version 4 you have to use .on instead of .each (based on Lars answer):
You can use
transition.each()
and the "end" event. The code would look something like the following.You might want to check out this blog post (2 parts) on D3 transitions - http://blog.safaribooksonline.com/2013/07/11/reusable-d3-js-using-attrtween-transitions-and-mv/
I'm just beginning to work with D3 and this was helpful.
I faced the same problem and I used css animations to solve the problem. With simple html and css would be like this:
With d3 and javascript :