![如何为 Beamer 中沿路径移动的元素制作动画?](https://linux22.com/image/423222/%E5%A6%82%E4%BD%95%E4%B8%BA%20Beamer%20%E4%B8%AD%E6%B2%BF%E8%B7%AF%E5%BE%84%E7%A7%BB%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E5%88%B6%E4%BD%9C%E5%8A%A8%E7%94%BB%EF%BC%9F.png)
我是 Beamer/Tikz 动画的新手,我一直在深入搜索,但没有找到针对我的具体情况的任何答案。
描述:
我有一个类似于流程图的过程图,其中有几条路径可以获取。我想画一个沿着不同选项移动的球或点,以描述可以采取的可能路径。我发现的最相似的线程是 ttps://tex.stackexchange.com/questions/475463/animate-an-airplane-in-beamer,但在我的例子中,图表是固定的,我只想在图表中移动一个元素。
我附上了一个示例流程图,但我的流程图没有那么复杂。非常感谢您的帮助!
以下是我目前拥有的代码。我不想画一条线,而是想画一个沿着不同路径移动的点。
\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.9\textwidth]{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}
编辑***
感谢 @AlexG 的贡献,我能够编写以下代码。现在的问题是我需要每次都包含不同的路径来“重置”框架。我该怎么做?
\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.9\textwidth]{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.9\textwidth]{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}
我将非常感谢您的帮助!
答案1
为了美观,流程图本身也应该用 TikZ 绘制。这样做的好处是,所有节点坐标都很容易获得,可以在连接线上定位一个点。
接下来是一个animate
基于(-package)的解决方案。流程图及其静态对象首先保存在一个框中,然后在动画中重复使用。(使用包xsavebox
进行有效存储。)单击以查看实际效果。
\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}
但是,如果要使用带有流程图的外部图形,则必须手动确定连接线的起始和终止坐标,然后通过 TikZ 的\coordinate
命令插入。将带有辅助线的网格放在带有嵌入图形的节点上方可能会有助于此过程。
这里我们利用callouts
包来放置坐标,并用动画红点注释外部流程图。辅助线暂时插入\helpgrid[gray]
。每个动画部分的帧数应根据连接线的各自长度进行调整,以实现移动点的均衡速度。再次单击以查看动画的运行情况。
%\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}