我怎样才能从这张图片制作一个动画,让点按照箭头移动?
图片中有两个格子。底部的格子是顶部格子按照箭头移动点后的结果。我想制作一个顶部格子的电影,展示点在站点之间移动。我知道我必须使用
\usepackage{animate}
但我不知道如何完成这个任务。如果有其他代码可以显示类似的任务,也非常感谢。
\begin{center}
\begin{tikzpicture}[
node distance = 0pt,
shorten <>/.style = {shorten <=#1, shorten >=#1},
start chain = going right,
box/.style = {shape=rectangle, draw, fill=#1,
minimum width=6mm, minimum height=9mm, outer sep=0pt,
node contents={},
on chain},
box/.default = none,
arrow/.style = {draw=blue!60!black, thick, shorten <>=1mm,
out=90, in=90, looseness=3,
-{Straight Barb[bend]}},
arbox/.style = {inner sep=0pt, minimum size=5pt},
crbox/.style = {inner sep=0pt,
node contents={\scriptsize\color{red}$\boldsymbol{\times}$}
},
label distance = -3pt,
sx/.style = {xshift=#1pt}
]
\node (n0) [box,dashed];
\foreach \i in {1,2,...,13}
\ifnum\i<1
\node (n\i) [box]
\else
\node (n\i) [box=gray!25]
\fi;
\node (n14) [box,dashed];
\draw[ultra thick,dotted,shorten <=1mm] (n0) -- + (-9mm,0mm);
\draw[ultra thick,dotted,shorten <=1mm] (n14) -- + (+9mm,0mm);
\fill[black!75] ($(n2)+(0,0.2)$) circle(1mm) circle (1mm) ($(n2)+(0,-0.2)$) circle(1mm) (n5) circle (1mm)($(n3)+(0,0)$) circle(1mm) ($(n5)+(0,0)$) circle(1mm)
($(n6)+(0,0)$) circle(1mm) (n8) circle (1mm) ($(n9)+(0.1,0)$) circle(1mm)circle (1mm) ($(n9)+(-0.1,0.2)$) circle(1mm) ($(n9)+(-0.1,-0.2)$) circle(1mm)(n13) circle (1mm) ($(n11)+(0,0.2)$) circle(1mm) ($(n11)+(0,-0.2)$) circle(1mm);
\draw[arrow] ([sx= 1] n0.north) to node[arbox] {} (n1.north);
\draw[arrow] ([sx= 1] n5.north) to node[arbox] {} (n6.north);
\draw[arrow] ([sx= 1] n9.north) to node[arbox] {} (n10.north);
\draw[arrow] ([sx= 1] n11.north) to node[arbox] {} (n12.north);
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}[
node distance = 0pt,
shorten <>/.style = {shorten <=#1, shorten >=#1},
start chain = going right,
box/.style = {shape=rectangle, draw, fill=#1,
minimum width=6mm, minimum height=9mm, outer sep=0pt,
node contents={},
on chain},
box/.default = none,
arrow/.style = {draw=blue!60!black, thick, shorten <>=1mm,
out=90, in=90, looseness=3,
-{Straight Barb[bend]}},
arbox/.style = {inner sep=0pt, minimum size=5pt},
label distance = -3pt,
sx/.style = {xshift=#1pt}
]
\node (n0) [box,dashed];
\foreach \i in {1,2,...,13}
\ifnum\i<1
\node (n\i) [box]
\else
\node (n\i) [box=gray!25]
\fi;
\node (n14) [box,dashed];
\draw[ultra thick,dotted,shorten <=1mm] (n0) -- + (-9mm,0mm);
\draw[ultra thick,dotted,shorten <=1mm] (n14) -- + (+9mm,0mm);
\fill[black!75] (n1) circle (1mm) ($(n2)+(0,-0.2)$) circle(1mm) circle (1mm) ($(n2)+(0,0.2)$) circle(1mm) ($(n3)+(0,0)$) circle(1mm) ($(n6)+(0,-0.2)$) circle(1mm) ($(n6)+(0,0.2)$) circle(1mm) (n8) circle (1mm) ($(n9)+(0,0.2)$) circle(1mm) ($(n9)+(0,-0.2)$) circle(1mm) ($(n10)+(0,0)$) circle(1mm)
(n13) circle (1mm) ($(n11)+(0,0)$) circle(1mm) circle(1mm)($(n12)+(0,0)$) circle(1mm);
\end{tikzpicture}
\captionof{figure}{}
\end{center}
答案1
\documentclass[11pt,a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{calc,chains,arrows.meta,bending}
\usepackage{animate}
\tikzset{mystyle/.style={node distance=0pt,start chain=going right,label distance=-3pt},
shorten <>/.style={shorten <=#1,shorten >=#1},
box/.style={shape=rectangle,draw,fill=#1,minimum width=6mm,minimum height=9mm,outer sep=0pt,node contents={},on chain},
box/.default=none,
arrow/.style={draw=blue!60!black,thick,shorten <>=1mm,out=90,in=90,looseness=3,-{Straight Barb[bend]}},
arbox/.style={inner sep=0pt,minimum size=5pt},
crbox/.style={inner sep=0pt,node contents={\scriptsize\color{red}$\boldsymbol{\times}$}},
sx/.style={xshift=#1pt},
shdot/.style={ultra thick,dotted,shorten <=1mm}}
\def\drawnewbox{\node (n0) [box,dashed];
\foreach \i in {1,2,...,13}{%
\ifnum \i<1
\node (n\i) [box];
\else
\node (n\i) [box=gray!25];
\fi
}
\node (n14) [box,dashed];
\draw[shdot] (n0) -- + (-9mm,0mm);
\draw[shdot] (n14) -- + (+9mm,0mm);}
\begin{document}
\begin{animateinline}[autoplay,loop,
begin={\begin{tikzpicture}[mystyle]
\useasboundingbox (-1,-1)rectangle(9.5,1.2);
},
end={ \end{tikzpicture}}]{3}
\drawnewbox
\foreach \d in {2,3,5,6,8,9,11,13}{%
\ifnum \d=2
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\ifnum \d=9
\fill[black!75] ($(n\d)+(0.1,0)$) circle(1mm) ($(n\d)+(-0.1,0.2)$) circle(1mm) ($(n\d)+(-0.1,-0.2)$) circle(1mm);
\else
\ifnum \d=11
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\fill[black!75] (n\d) circle (1mm);
\fi\fi\fi
}
\foreach \a/\b in {0/1,5/6,9/10,11/12}{%
\draw[arrow] ([sx= 1] n\a.north) to node[arbox] {} (n\b.north);
}
%+++++++
\newframe
%+++++++ 2
\drawnewbox
\foreach \d in {1,2,3,6,8,9,10,11,12,13}{%
\ifnum \d=2
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\ifnum \d=6
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\ifnum \d=9
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\fill[black!75] (n\d) circle (1mm);
\fi\fi\fi
}
%+++++++
\newframe
%+++++++ 3
\drawnewbox
\foreach \d in {1,2,3,6,8,9,10,11,12,13}{%
\ifnum \d=2
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\ifnum \d=6
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\ifnum \d=9
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\fill[black!75] (n\d) circle (1mm);
\fi\fi\fi
}
\foreach \a/\b in {2/3,6/7,11/12}{%
\draw[arrow] ([sx= 1] n\a.north) to node[arbox] {} (n\b.north);
}
%+++++++
\newframe
%+++++++ 4
\drawnewbox
\foreach \d in {1,2,3,6,7,8,9,10,12,13}{%
\ifnum \d=3
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\ifnum \d=9
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\ifnum \d=12
\fill[black!75] ($(n\d)+(0,0.2)$) circle(1mm) ($(n\d)+(0,-0.2)$) circle(1mm);
\else
\fill[black!75] (n\d) circle (1mm);
\fi\fi\fi
}
\end{animateinline}
\end{document}
输出: