[Tex/LaTex] How to animate a path or how to draw starting fraction of a complex but linear path

animatepgf-coretikz-pgf

I would like to reproduce something similar to Animating Protocols but with a compound path and not a straight one.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{animate}
\usetikzlibrary{intersections}

\begin{document}

\begin{animateinline}[loop]{10}

\multiframe{10}{rPos=0.1+0.1}{
\begin{tikzpicture}[every node/.style={circle,draw}]

\node(R0) at (0,0) {R0};
\node[draw,circle] (R1) at (1,3) {R1};
\node[draw,circle] (R2) at (2,1) {R2};
\node[draw,circle] (R3) at (3,2) {R3};

\path[name path=route] (R0)--(R1)--(R2)--(R3)
node[pos=\rPos,coordinate] (p) {};

%I would like to draw the already defined path from R0 to p
\draw[->] (R0) -- (p);

%\draw \route

\end{tikzpicture}
} 
\end{animateinline}
\end{document} 

I would like to declare the path and draw only the initial segment in each frame, but I don't know how to draw the "named path". With this solution, the path draw is a straight line between (R0) and (p) but I would like it to follow (R0)–(R1)–(R2)–(R3). Do you know how to do it?

pgf's manual only uses 'named path' to find intersections, so I don't know how to use them once defined.

Caramdir's answer to TikZ: draw only a certain central length of a given path
uses a decoration to draw only some part of any path. I've already adapted it to draw the starting segment of the path, but I would like to draw a certain percentatge of path length and not a fixed length (3 cm). I couldn't find in manual how to provide a parameter to a decoration.

And in Length, surface, and coordinates of a tikz-pgf path, Andrew Stacey proposes to use a new TEX-SX package still in development. It seems to be they way but I think there should be a easier solution.

Do you know it?

Best Answer

Here's the decoration Caramdir came up with, adapted to draw only the first fraction of an arbitrary path:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations}

% A simple empty decoration, that is used to ignore the last bit of the path
\pgfdeclaredecoration{ignore}{final}
{
\state{final}{}
}

% Declare the actual decoration.
\pgfdeclaremetadecoration{middle}{initial}{
    \state{initial}[
        width={0pt},
        next state=middle
    ]
    {\decoration{moveto}}

    \state{middle}[
        width={\pgfdecorationsegmentlength*\pgfmetadecoratedpathlength},
        next state=final
    ]
    {\decoration{curveto}}

    \state{final}
    {\decoration{ignore}}
}

% Create a key for easy access to the decoration
\tikzset{middle segment/.style={decoration={middle},decorate, segment length=#1}}

\begin{document}\noindent
\foreach \step in {0.2,0.4,...,1} {%
\begin{tikzpicture}
    \draw[middle segment=\step,ultra thick,red,->] (0,0) to[out=30,in=150] (3,0) -- (7,1) -- (8,0);
\end{tikzpicture}\\
}
\end{document}