我正在尝试修改此处展示的动画示例:正弦和余弦函数动画包括phase
,所以旋转每次都会从不同的角度开始,phase =[0, 45 ,-45]
因此我使用了 foreach,如下所示,原始动画显示在示例:正弦和余弦函数动画,但代码不起作用(请取消注释与动画相关的命令以检查我收到的错误)。
\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
%\newcounter{theangle}
%\setcounter{theangle}{0}
\begin{tikzpicture}
\def\angle{45};
\fill[fill=red] (0,0) -- plot [domain=0:pi/180*\angle] (\x,{sin(\x r)}) -- (pi/180*\angle,0) -- cycle;
\end{tikzpicture}
%\begin{animateinline}[loop, poster = first,controls]{40}
%
%\foreach \angle in {0,45,-45}
%\whiledo{\angle<359}{
\foreach \angle in {0,10,...,360}
{
\begin{tikzpicture}
%\fill[blue!50] (-1,0) arc (0:\angle:1) -- (-2,0) -- cycle;
%\fill[blue!50] plot[smooth,domain=0:\angle] (pi/180*\x,{sin(\x)}) |- (0,0);
\def\radangle{pi/180*\angle};
\fill[blue!50] (-1,0) arc (0:\angle:1) -- (-2,0) -- cycle;
%\fill[fill=red] (0,0) -- plot [domain=0:\radangle] (\x,{sin(\x r)}) -- (\radangle,0) -- cycle;
\draw (-2,0) +(\angle:1) circle (2pt) -- (pi/180*\angle,{sin(\angle)}) circle (2pt);
\draw (-3.5,0) -- (7,0);
\foreach \deg in {90, 180, 270, 360}
\draw (pi/180*\deg,2pt) -- (pi/180*\deg,-2pt) node[below] {$\deg^\circ$};
\draw (0,-1.2) -- (0,1.2);
\foreach \y in {-1,-0.5,0.5,1}
\draw (2pt,\y) -- (-2pt,\y) node[left] {$\y$};
\draw plot[smooth,domain=0:360] (pi/180*\x,{sin(\x)});
\draw (-2,0) circle (1);
\end{tikzpicture}
%
% \stepcounter{theangle}
% \ifthenelse{\theangle<359}{\newframe}{
% \end{animateinline}}
}
\end{document}
答案1
该示例利用\clist_map_inline:nn
LaTeX3 中的 而不是 TikZ 中的\foreach
来迭代以逗号分隔的相移列表。出于某种原因,\foreach
在 内不起作用animateinline
。
一些暂停(\newframe*
)被放置在合适的动画点;单击播放按钮或动画小部件以继续:
(建议使用基于 Chromium 的浏览器查看 SVG 版本,因为 Firefox 有点慢。)
\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{tikz,animate}
\ExplSyntaxOn
\let\clistMap\clist_map_inline:nn
\ExplSyntaxOff
\newcommand{\drawit}[2]{% #1 angle (0<=#1<=360)
\begin{tikzpicture} % #2 phase shift
\pgfmathsetmacro\angleRad{pi/180*(#1+#2)}
\pgfmathsetmacro\angleDeg{#1+#2}
\pgfmathsetmacro\unshiftedRad{pi/180*(#1)}
\fill[blue!50] (-2,0) -- ++(#2:1) arc (#2:\angleDeg:1) -- cycle;
\draw (-2,0) +(\angleDeg:1) circle (2pt) -- (\unshiftedRad,{sin(\angleDeg)}) circle (2pt);
\draw (-3.5,0) -- (7,0);
\foreach \deg in {90, 180, 270, 360}
\draw (pi/180*\deg,2pt) -- (pi/180*\deg,-2pt) node[below] {$\deg^\circ$};
\draw (0,-1.2) -- (0,1.2);
\foreach \y in {-1,-0.5,0.5,1}
\draw (2pt,\y) -- (-2pt,\y) node[left] {$\y$};
\draw plot[smooth,domain=0:360] (pi/180*\x,{sin(\x+#2)});
\draw (-2,0) circle (1);
\node [anchor=north east] at (current bounding box.north east) {$\varphi=#2^\circ$};
\end{tikzpicture}%
}
\begin{document}
\begin{animateinline}[controls]{30}
\clistMap{0,45,-45}{%
\drawit{0}{#1}
\newframe*
\multiframe{360}{i=1+1}{\drawit{\i}{#1}}%
\ifthenelse{#1=-45}{}{\newframe*}%
}
\end{animateinline}
\end{document}