With some experimentation I came up with the following answer. Although in the final use case the coordinates are almost a straight line, here I use three coordinates that form a large angle (for demonstration purposes).
Case a) repeating continuously
In the code I added a 3 second delay before each animation starts:
\usetikzlibrary {animations}
\begin{tikzpicture}
\useasboundingbox (0,0) rectangle (6,4);
\node at (0.5,1) :position = { 0s = "{(0.5,1)}", 3s = "{(0.5,1)}", 5s = "{(1,2.5)}" , 8s = "{(2,2)}", repeat }
[fill = blue!20, draw = blue, ultra thick, circle] (node1) {};
\node at (2,2) :position = { 0s = "{(2,2)}", 3s = "{(2,2)}", 5s = "{(4,3)}", 8s = "{(5,3.5)}", repeat }
[fill = red!20, draw = red, ultra thick, circle] (node2) {};
\draw [green] (node1) -- (node2) :path = { 0s = "{(node1) -- (node2)}", 3s = "{(node1) -- (node2)}",
5s = "{(1,2.5) -- (4,3)}", 8s = "{(2,2) -- (5,3.5)}", repeat };
\end{tikzpicture}
Here is the SVG (I hope this works):
Criticism: I do not like that the connecting line grows in length up to the center of both circles - I need to subtract the radii of the two ending circles.
Case b) with a start button
\usetikzlibrary {animations}
\begin{tikzpicture}
\draw (5,5) node[minimum height =0cm,draw,scale=1.5] (Go) {Go!};
\node at (0.5,1) :position = { 0s = "{(0.5,1)}", 2s = "{(1,2.5)}" , 5s = "{(2,2)}", begin on={click, of=Go} }
[fill = blue!20, draw = blue, ultra thick, circle] (node1) {};
\node at (2,2) :position = { 0s = "{(2,2)}", 2s = "{(4,3)}", 5s = "{(5,3.5)}", begin on={click, of=Go} }
[fill = red!20, draw = red, ultra thick, circle] (node2) {};
\draw [green] (node1) -- (node2) :path = { 0s = "{(node1) -- (node2)}", 2s = "{(1,2.5) -- (4,3)}",
5s = "{(2,2) -- (5,3.5)}", begin on={click, of=Go} };
\end{tikzpicture}
Here is the SVG (sorry, the button works in the created SVG though):
Criticism: if the node1 and node2 coordinates are extracted into a \begin{scope} [animate = { .... }] .... \end{scope}
section, within the :position
list of arguments begin on={click, of=Go}
is ignored, i.e. the animation never starts.
Case a2) repeating continuously
The gradually extending line that connects the nodes (hard to see in the examples above) can be removed using phantom nodes. See: Phantom nodes for TikZ animation
\usetikzlibrary {animations}
\begin{tikzpicture}
\useasboundingbox (0,0) rectangle (6,4);
\node at (0.5,1) :position = { 0s = "{(0.5,1)}", 3s = "{(0.5,1)}", 5s = "{(1,2.5)}" , 8s = "{(2.5,1.5)}", 10s = "{(2.5,1.5)}", repeat }
[fill = blue!05, draw = blue, thick, circle, radius = 0.6] (node1) {};
\node at (2,2) :position = { 0s = "{(2,2)}", 3s = "{(2,2)}", 5s = "{(4,3)}", 8s = "{(5,0.5)}", 10s = "{(5,0.5)}", repeat }
[fill = red!05, draw = red, thick, circle, radius = 0.6] (node2) {};
\begin{lrbox}{0}
\node at (1,2.5) [draw, circle, radius = 0.6, opacity = 0.2] (n1) {};
\node at (2.5,1.5) [draw, circle, radius = 0.6, opacity = 0.2] (n2) {};
\node at (4,3) [draw, circle, radius = 0.6, opacity = 0.2] (n3) {};
\node at (5,0.5) [draw, circle, radius = 0.6, opacity = 0.2] (n4) {};
\end{lrbox}
\draw [brown] (node1) -- (node2) :path = { 0s = "{(node1) -- (node2)}", 3s = "{(node1) -- (node2)}", 5s = "{(n1) -- (n3)}", 8s = "{(n2) -- (n4)}", 10s = "{(n2) -- (n4)}", repeat };
\end{tikzpicture}
The SVG is here:
If you comment out the beginning and end of the {lrbox}
, you see faint images of where the phantom nodes are placed:
This is a good solution, but cumbersome to code. I've since seen (on page 382 of the TikZ manual:
So, all in all, I'm satisfied with this solution, which will use Python to create the .tex
file anyway. If anyone can improve on it, I'd be pleased to see your version.