增强动画

增强动画

我从一个简单的动画开始,然后在第二步尝试改进它。

在第二帧中我添加了一个长水平矩形,并用白色方块填充(foreach 循环)。

有没有办法通过排除动画之外的“静态”部分(例如黑色方块或带有白色方块的水平矩形)来节省资源?

此外,我想知道是否有更好的方法可以将控件和动画精确地放置在框架中我想要的位置。

在最后一步中,我想让红色垂直线沿时间轴移动,与闪烁同步。实现此目的的最佳方法是什么?

代码如下:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{animate}
\usetikzlibrary{shapes, arrows.meta, positioning}

\begin{document}
    \begin{frame}
        \frametitle{Simple Flash Example}
        \begin{columns}
            \begin{column}{0.50\textwidth}
                Text for signal description and other explanations.
                
                Here a simple blink animation is expected, as long as the pseudo notes are copied to each new picture it works fine.
            \end{column}
            \begin{column}{0.50\textwidth}
                \begin{animateinline}[controls,loop,scale=.66]{2}
                    \begin{tikzpicture}
                        \node at (-6,0) {}; %playing with pseudo notes feels ineffctive, is there a simpler approach for correct placing of animatation and/or controls (general approach / within a column)?
                        \node at (4,0) {};
                        \node[draw=black, %black rectangle (white circle will appear)
                        fill=black,
                        minimum width=1cm,
                        minimum height=1cm] at (-1,1) {};
                    \end{tikzpicture}
                    \newframe
                    \begin{tikzpicture}
                        \node at (-6,0) {};
                        \node at (4,0) {};
                        \node[draw=black,
                        fill=black,
                        minimum width=1cm,
                        minimum height=1cm] at (-1,1) {};
                        \node[circle,draw,fill=white] (c) at (-1,1){}; %white Circle
                    \end{tikzpicture}
                \end{animateinline}
            \end{column}
        \end{columns}
    \end{frame} 
    \begin{frame}
        \frametitle{Complex Flash Example}
        \begin{columns}
            \begin{column}{0.50\textwidth}
                Text for signal description and other explanations. 
                
                The red vertikal line now should move sychronized with the blinking, what is the best way to realise this? And how could i add a timeline below the lower rectangle with e.g. seconds?
            \end{column}
            \begin{column}{0.50\textwidth}
                \begin{animateinline}[controls,loop,scale=.66]{2}
                    \begin{tikzpicture}
                        \node[draw=black, %black rectangle (white circle will appear)
                        fill=black,
                        minimum width=1cm,
                        minimum height=1cm] at (-1,1) {};
                        
                        \node[draw=black, %long rectangle
                        fill=black,
                        minimum width=6.0cm,
                        minimum height=0.5cm] at (-1,-1) {};
                        
                        \foreach \x in {-3.9,-3.4,...,1.9}
                        {
                            \node[draw=black,  
                            fill=white,
                            minimum width=0.5,
                            minimum height=0.5cm] at (\x,-1.) {}; %white rectangles
                        }
                            
                        \draw [line width=0.4mm,  red] (-4.05,-0.75) -- (-4.05,-1.25) node [right]{}; %red vertical line
                        
                        \foreach \x in {-4,-3.5,...,2} %timeline
                        {
                            \draw[shift={(\x,-1.5)},color=black] (0pt,3pt) -- (0pt,-3pt);
                        }
                        \foreach \x in {0,0.5,...,6} %timeline numbers
                        {
                        \pgfmathsetmacro\y{int(\x * 2)};
                        \draw[shift={({\x - 4},-1.5)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] 
                        {$\y$};
                        }
                        
                    \end{tikzpicture}
                    \newframe
                    \begin{tikzpicture}
                        \node[draw=black,
                        fill=black,
                        minimum width=1cm,
                        minimum height=1cm] at (-1,1) {}; %black square
                        
                        \node[draw=black, 
                        fill=black,
                        minimum width=6.0cm,
                        minimum height=0.5cm] at (-1,-1) {}; %long rectangle
                        
                        \foreach \x in {-3.9,-3.4,...,1.9}
                            {
                            \node[draw=black,  
                            fill=white,
                            minimum width=0.5,
                            minimum height=0.5cm] at (\x,-1.) {}; %white rectangles
                            }
                        
                        \node[circle,draw,fill=white] (c) at (-1,1){}; %white Circle 
                            
                        \draw [line width=0.4mm,  red] (-4.05,-0.75) -- (-4.05,-1.25) node [right]{}; %red vertical line
                        
                        \foreach \x in {-4,-3.5,...,2} %timeline
                        {
                            \draw[shift={(\x,-1.5)},color=black] (0pt,3pt) -- (0pt,-3pt);
                        }
                        \foreach \x in {0,0.5,...,6} %timeline numbers
                        {
                            \pgfmathsetmacro\y{int(\x * 2)}; %I want only full seconds
                            \draw[shift={({\x - 4},-1.5)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] 
                            {$\y$};
                        }
                    \end{tikzpicture}
                \end{animateinline}
            \end{column}
        \end{columns}
    \end{frame}
\end{document}

为了更好地解释,我添加了第二帧的输出: 2.第二帧,时间轴问题同时解决

答案1

静态部分确实可以排版一次并在多个框架中引用;请参阅选项timelineanimate但是,这会增加一些开销,并且从最终 PDF 文件大小的角度来看可能不值得。(因此,我们在这里不这样做。)

有几个选项可用,允许您调整控制按钮的大小、颜色、不透明度和对齐方式,以及仅选择按钮的子集。有关animate详细信息,请参阅手册。整个动画使用 位于右列的中心\centering。动画占用的空间仅由动画小部件决定。动画的基线是小部件的下边缘。控件可能会与相邻的文本重叠。

TikZ 节点的主要用途是将文本标签放置在图形中,而不是绘制几何形状。节点形状只是修饰文本标签的一种手段。为了精确起见,我使用了 TikZ 提供的专用绘图命令。

红色时间指示器的平滑同步移动需要更高的帧速率和更多的动画帧。为了切换白点的可见性,我正在评估“帧数\i”模数“16”。16 帧为一秒,即一个关闭/开启周期。

(点击图片可观看动画)

\documentclass{beamer}
\usepackage{tikz}
\usepackage{animate}

\begin{document}
  \begin{frame}[fragile]
    \frametitle{Simple Flash Example}
    \begin{columns}
      \begin{column}{0.50\textwidth}
        Text for signal description and other explanations.
        
        Here is simple blink animation, well centred (\verb+\centering+) within the right column.
      \end{column}
      \begin{column}{0.50\textwidth}\centering
        \begin{animateinline}[controls={play,stop,step},loop]{2}
          \begin{tikzpicture}[scale=0.66]
            \fill (0,0) rectangle (1,1);
          \end{tikzpicture}
          \newframe
          \begin{tikzpicture}[scale=0.66]
            \fill (0,0) rectangle (1,1);
            \fill[white] (0.5,0.5) circle [radius=0.2];
          \end{tikzpicture}
        \end{animateinline}
      \end{column}
    \end{columns}
  \end{frame} 
  \begin{frame}
    \frametitle{Complex Flash Example}
    \begin{columns}
      \begin{column}{0.50\textwidth}
        Text for signal description and other explanations. 
        
        The red vertical line now moves synchronously with the blinking.
      \end{column}
      \begin{column}{0.50\textwidth}\centering
        \begin{animateinline}[controls={play,stop,step},loop]{16}
          \multiframe{192}{i=0+1,rline=-3+0.03125}{
            \begin{tikzpicture}[scale=0.66]
              \fill (-0.5,0) rectangle (0.5,1);

              \pgfmathparse{int(mod(\i,16))} % modulo
              \ifnum\pgfmathresult<8\else
                \fill[white] (0,0.5) circle [radius=0.2];
              \fi

              \filldraw (-3,-1) rectangle (3,-0.5);
              \foreach \x in {-2.75,-2.25,...,2.75} {
                \fill[white] (\x,-1) rectangle ++(0.25,0.5);
              }  
              % time indicator
              \draw [thick, red] (\rline,-1) -- ++(0,0.5);

              \foreach \x in {-3,-2.5,...,3} {
                \pgfmathsetmacro\mylabel{int(6+2*\x)}
                \node [anchor=north, at={(\x,-1)}] {\tiny\mylabel};
              }  
            \end{tikzpicture}
          }
        \end{animateinline}
      \end{column}
    \end{columns}
  \end{frame}
\end{document}

可以通过将动画分成多个部分(使用多个连续的块)来构建非等时信号示例(如评论中所要求的那样)\multiframe。循环次数和参数的初始值i需要针对每个块rline进行调整。\multiframe

此扩展示例没有使用 pkganimatetimeline功能,而是使用了 packagexsavebox以避免代码重复,从而减少编译时间并获得较小的 PDF 输出文件。静态背景图像排版并保存一次xlrbox,然后使用 TikZ 节点插入到动画的每一帧中:

\documentclass{standalone}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{tikz}
%\usepackage[dvisvgm,buttonsize=1ex]{animate}
%\usepackage[dvisvgm]{xsavebox}
\usepackage[buttonsize=1ex]{animate}
\usepackage{xsavebox}

\begin{document}\sffamily
  % background graphics
  \begin{xlrbox}{BgImg}
    \begin{tikzpicture}[scale=0.66]
      \useasboundingbox (-3.2,-1.4) rectangle (3.2,1.1);

      \fill (-0.5,0) rectangle (0.5,1);

      \filldraw (-3,-1) rectangle (3,-0.5);
      \foreach \x in {-2.5625,-2.0625,-1.5625} {
        \fill[white] (\x,-1) rectangle ++(0.0625,0.5);
      }  
      \fill[white] (-1.0,-1) rectangle ++(1.0,0.5);

      \foreach \x in {-3,-2.5,...,3} {
        \pgfmathsetmacro\mylabel{int(6+2*\x)}
        \node [anchor=north, at={(\x,-1)}] {\tiny\mylabel};
      }  
    \end{tikzpicture}
  \end{xlrbox}%
  % animation
  \begin{animateinline}[controls={play,stop,step},loop]{16}
    % 3 short flashes of 1/8 s length
    \multiframe{48}{i=0+1,rline=-3+0.03125}{
      \begin{tikzpicture}[scale=0.66]
        % insert BG image
        \node[outer sep=0pt,inner sep=0pt,anchor=south west, at={(-3.2,-1.4)}] {\theBgImg};

        \pgfmathparse{int(mod(\i,16))} % modulo
        \ifnum\pgfmathresult<14\else
          \fill[white] (0,0.5) circle [radius=0.2]; % flash
        \fi

        \draw [thick, red] (\rline,-1) -- ++(0,0.5); % time indicator
      \end{tikzpicture}
    }
    \newframe
    % 1 s of rest
    \multiframe{16}{i=48+1,rline=-1.5+0.03125}{
      \begin{tikzpicture}[scale=0.66]
        \node[outer sep=0pt,inner sep=0pt,anchor=south west, at={(-3.2,-1.4)}] {\theBgImg};
        \draw [thick, red] (\rline,-1) -- ++(0,0.5);
      \end{tikzpicture}
    }
    \newframe
    % 2 s of light "on"
    \multiframe{32}{i=64+1,rline=-1+0.03125}{
      \begin{tikzpicture}[scale=0.66]
        \node[outer sep=0pt,inner sep=0pt,anchor=south west, at={(-3.2,-1.4)}] {\theBgImg};

        \fill[white] (0,0.5) circle [radius=0.2];
        \draw [thick, red] (\rline,-1) -- ++(0,0.5);
      \end{tikzpicture}
    }
    \newframe
    % 6 s of rest
    \multiframe{96}{i=96+1,rline=0.0+0.03125}{
      \begin{tikzpicture}[scale=0.66]
        \node[outer sep=0pt,inner sep=0pt,anchor=south west, at={(-3.2,-1.4)}] {\theBgImg};
        \draw [thick, red] (\rline,-1) -- ++(0,0.5);
      \end{tikzpicture}
    }
  \end{animateinline}
\end{document}

为了构建 SVG 版本,dvisvgm需要添加animate和选项xsavebox

\usepackage[dvisvgm,buttonsize=1ex]{animate}
\usepackage[dvisvgm]{xsavebox}

然后使用

dvilualatex example
dvilualatex example
dvisvgm --exact --zoom=-1 --font-format=woff2 example

为了获得最佳性能,应在基于 Chromium 的浏览器中打开动画 SVG。

相关内容