I am using setInterval
, so transitions take place after a certain interval. Is it possible to get pausing and resuming to work with setInterval?
Any suggestions/pointers in the right direction will be really helpful.
I am using setInterval
, so transitions take place after a certain interval. Is it possible to get pausing and resuming to work with setInterval?
Any suggestions/pointers in the right direction will be really helpful.
This question was posted when D3 v3 was the latest version available. 5 years later D3 v5 has some new methods, like
selection.interrupt()
,transition.on("interrupt"...)
andlocal variables
, which can make the task more simple and less painful.So, let's suppose a simple
cx
transition on a circle:The idea is interrupting the transition when, for instance, a button is pressed:
And then, with a local variable, use the listener for
interrupt
to get the current position:Finally, when the button is pressed again, we use
local.get(this)
and a simple math to get the remainingduration
.It's also worth mentioning that this works for linear easing; if you have another easing, like the default
d3.easeCubic
, you'll need a way more complex code.And here is the demo: