TransWikia.com

How to animate an element that move along a path in Beamer?

TeX - LaTeX Asked by Ana Batista on April 23, 2021

I am new to Beamer/Tikz animation, I have been searching intensively and I have not found any answers to my specific case.

Description: enter image description here
I have a graph of a process similar to a flowchart with several paths that can be obtained. I would like to draw a ball or a dot that moves along the different options, to describe the possible path that can be taken. The most similar thread that I have found is ttps://tex.stackexchange.com/questions/475463/animate-an-airplane-in-beamer but in my case, the graph is fixed I only want to move an element through the graph.

I am attaching a sample flow chart but mine is less complicated. I will appreciate your help!

The following code is what I have at the moment. Instead of a line, I would like to draw a dot that moves around through different paths.

    documentclass{beamer}
usepackage{tikz}

pgfdeclarelayer{background}
pgfdeclarelayer{foreground}
pgfsetlayers{background,main,foreground}

    begin{document}
    
    begin{frame}
    
    begin{figure}
        begin{center}
            begin{tikzpicture}
         node[anchor=south west,inner sep=0] (image) at (0,0) 
          {includegraphics[width=0.9textwidth]{figures/Flow_3.png}};
        begin{scope}[x={(image.south east)},y={(image.north west)}]
          %draw path
            begin{pgfonlayer}{foreground}
               path<2->[draw,line width=2pt,-,red] (0.16,0.24) edge node {} (0.59,0.24);
            end{pgfonlayer}
           end{scope} 
           end{tikzpicture}
               end{center}  
     label{fig:1}           
    end{figure}    
    
    end{frame}
    end{document} 

EDIT***

Thanks to the @AlexG contribution I was able to write the following code. The problem now is that I need to include different paths "resetting" the frame each time. How can I do it?

documentclass{beamer}
    usepackage{tikz}
     userpackage{animate}
    
       tikzset{dot/.pic={fill[red] (0,0) circle [radius=2pt];}}

        begin{document}
        
        begin{frame}
    

begin{figure}
    begin{center}
%Animation
begin{animateinline}[controls={play,stop}]{12}%
  multiframe{11}{rPos=0+0.1}{%
    begin{tikzpicture}
      node [anchor=south west,inner sep=0] (image) at (0,0)  {includegraphics[width=0.9textwidth]{figures/Flow_3.png}};
      path (1.67,1.24) -| (2.3,1.24) pic [pos=rPos] {dot};
      
 %To draw the grid  for coordinates
begin{scope}[x={(image.south east)},y={(image.north west)}]
      %GRID
    draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
    foreach x in {0,1,...,9} { node [anchor=north] at (x/10,0) {0.x}; }
    foreach y in {0,1,...,9} { node [anchor=east] at (0,y/10) {0.y}; }
end{scope}
    end{tikzpicture}%
  }%  
  newframe* % pause here, click to continue
  multiframe{11}{rPos=0+0.1}{%
   begin{tikzpicture}
       node [anchor=south west,inner sep=0] (image) at (0,0){includegraphics[width=0.9textwidth]{figures/Flow_3.png}};
      path (2.3, 1.24) |- (6.59,1.24) pic [pos=rPos] {dot};
      begin{scope}[x={(image.south east)},y={(image.north west)}] end{scope}    
    end{tikzpicture}%
  }%  
  
end{animateinline}        
end{center}  
 
end{figure}  

I will appreciate your help!

One Answer

For the sake of beauty, the flow chart itself should be drawn with TikZ as well. This comes with the bonus that all node coordinates are readily available for positioning a dot on the connecting lines.

What follows is an animate(-package)-based solution. The flow chart with its static objects is saved in a box first and then re-used in the animation. (Using package xsavebox for size-effective storage.) Click for viewing it in action.

documentclass{standalone}

usepackage{tikz}
usetikzlibrary{shapes}
usepackage{animate}
usepackage{xsavebox}

tikzset{dot/.pic={fill[red] (0,0) circle [radius=2pt];}}

begin{document}

% save flow chart
begin{xlrbox}{Flow}
  begin{tikzpicture}[every node/.style=draw, -latex]
    useasboundingbox (-0.6,-1.5) rectangle (3,1.5);
    path (0,1) node [rounded rectangle] (start) {Start}
          (2,0) node [trapezium, trapezium left angle=60, trapezium right angle=-60] (hello) {Hello!}
          (0,-1) node [rounded rectangle] (end) {End};
    draw (start) -| (hello);
    draw (hello) |- (end);
  end{tikzpicture}
end{xlrbox}%
%
% dot moving along node connections
begin{animateinline}[controls={play,stop}]{12}%
  multiframe{11}{rPos=0+0.1}{%
    begin{tikzpicture}
      node [anchor=south west, inner sep=0pt] at (-0.6,-1.5) {theFlow};
      path (start) -| (hello) pic [pos=rPos] {dot};
    end{tikzpicture}%
  }%  
  newframe* % pause here, click to continue
  multiframe{11}{rPos=0+0.1}{%
    begin{tikzpicture}
      node [anchor=south west, inner sep=0pt] at (-0.6,-1.5) {theFlow};
      path (hello) |- (end) pic [pos=rPos] {dot};
    end{tikzpicture}%
  }%  
end{animateinline}

end{document}

If, however, an external graphic with a flow chart is to be used, the connection line's start and end coordinates must be manually determined and then inserted by means of TikZ's coordinate command. Putting a grid with help lines on top of the node with the embedded graphic might help in this process.

Here we make use of the callouts package to put coordinates and to annotate the external flow chart with the animated red dot. Helper lines are temporarily inserted with helpgrid[gray]. The number of frames of each animation section should be adjusted with respect to the individual lengths of the connection lines in order to achieve a well-balanced speed of the moving dot. Again, click to see the animation in action.

%documentclass[dvisvgm]{standalone} % dvilualatex example ; dvisvgm --exact --font-format=woff2 --zoom=-1 example 
documentclass{standalone}

usepackage{animate}
usepackage{callouts}
usepackage{xsavebox}

tikzset{dot/.pic={fill[red] (0,0) circle [radius=3pt];}}

begin{document}

% save flow chart
begin{xlrbox}{Flow}
  begin{annotate}{includegraphics{N5Zfy}}{1.0}
    helpgrid[gray]
    coordinate (plan route east) at (-6.85,-2.05);
    coordinate (route west) at (-6,-2.05);
    coordinate (route north) at (-4.7,-1.3);
    coordinate (don't know west) at (-2.75,3.9);
  end{annotate}
end{xlrbox}%
%
% dot moving along node connections
begin{animateinline}[controls={play,stop}]{12}%
  multiframe{6}{rPos=0+0.2}{%
    begin{annotate}{theFlow}{1.0}
      path (plan route east) -- (route west) pic [pos=rPos] {dot};
    end{annotate}
  }%  
  newframe* % pause here, click to continue
  multiframe{41}{rPos=0+0.025}{%
    begin{annotate}{theFlow}{1.0}
      path (route north) |- (don't know west) pic [pos=rPos] {dot};
    end{annotate}
  }%  
end{animateinline}

end{document}

Answered by AlexG on April 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP