我正在尝试为我的讲座创建一个倒数计时器。除了多帧的第一帧之后的小幅延伸外,一切正常。这是 MWE:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{animate}
\def\R{1cm} % radius
\newcommand{\timer}[1]{% input = number of second
\begin{animateinline}[autoplay]{1}
\pgfmathtruncatemacro\Nseconds{#1}
\pgfmathtruncatemacro\Nframes{\Nseconds+1}
\multiframe{\Nframes}{i=0+1}{%
\pgfmathtruncatemacro\Nseconds{#1} % dont know why it is necessary to recalculate
\pgfmathtruncatemacro\Nframes{\Nseconds+1} % -//-
\pgfmathsetmacro\dT{360/\Nseconds} % one time step
\pgfmathsetmacro\x{90+(\i*\dT)} % angle of already spent time
\pgfmathtruncatemacro\y{\Nseconds-\i} % remaining time
\begin{tikzpicture}[line cap=rect]
% background color
\filldraw[fill=red, draw = none, line width = 3pt] (0,0) circle [radius=\R];
% white color of spent time
\filldraw[fill opacity=0.5,fill=white,draw = none] (0,0) -- (0, \R) arc (90:\x:\R) -- cycle;
% border circle
\draw[draw = black, line width = 3pt] (0,0) circle [radius=\R];
% small ticks
\foreach \angle in {60,30,...,-270}\draw[line width=1pt] (\angle:0.9*\R) -- (\angle:\R);
% large ticks
\foreach \angle in {0,90,180,270}\draw[line width=2pt] (\angle:0.8*\R) -- (\angle:\R);
% remaining time
\node[font=\Large] at (0, 0) {\y};%
\end{tikzpicture}%
}%
\end{animateinline}
}
\begin{document}
\timer{90}
\end{document}
我有三个问题:1)如何避免这种“跳跃”?2)为什么文档的大小不是正方形?3)是什么原因导致图片左右两侧出现空白?
答案1
- TikZ 根据其内容计算大小
tikzpicture
,每个帧的大小可能不同。因此,对所有帧强制使用一个公共边界框:\useasboundingbox ...
\pgfmath...
(&3) 由于包含结尾未得到正确注释的行,动画帧前后出现了大量虚假空白。
\documentclass{standalone}
\usepackage{tikz}
\usepackage{animate}
\def\R{1cm} % radius
\newcommand{\timer}[1]{% input = number of second
\begin{animateinline}[autoplay]{1}
\pgfmathtruncatemacro\Nseconds{#1}%
\pgfmathtruncatemacro\Nframes{\Nseconds+1}%
\multiframe{\Nframes}{i=0+1}{%
\pgfmathtruncatemacro\Nseconds{#1}% dont know why it is necessary to recalculate
\pgfmathtruncatemacro\Nframes{\Nseconds+1}% -//-
\pgfmathsetmacro\dT{360/\Nseconds}% one time step
\pgfmathsetmacro\x{90+(\i*\dT)}% angle of already spent time
\pgfmathtruncatemacro\y{\Nseconds-\i}% remaining time
\begin{tikzpicture}[line cap=rect]
\useasboundingbox [draw = black, line width = 3pt] (0,0) circle [radius=\R];
% background color
\filldraw[fill=red, draw = none, line width = 3pt] (0,0) circle [radius=\R];
% white color of spent time
\filldraw[fill opacity=0.5,fill=white,draw = none] (0,0) -- (0, \R) arc (90:\x:\R) -- cycle;
% border circle
\draw[draw = black, line width = 3pt] (0,0) circle [radius=\R];
% small ticks
\foreach \angle in {60,30,...,-270}\draw[line width=1pt] (\angle:0.9*\R) -- (\angle:\R);
% large ticks
\foreach \angle in {0,90,180,270}\draw[line width=2pt] (\angle:0.8*\R) -- (\angle:\R);
% remaining time
\node[font=\Large] at (0, 0) {\y};%
\end{tikzpicture}%
}%
\end{animateinline}% <== line terminated with `%'
}
\begin{document}
\timer{90}
\end{document}