连接动画中的线条,动画播放时速度会变慢

连接动画中的线条,动画播放时速度会变慢

我无法将红线与代码中的两条黑线连接起来(见图)。另一个问题是动画在播放时会变慢。这是我第一次使用动画,所以任何帮助都将不胜感激。

   \documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{animate}
\usepackage{xsavebox}
\usepackage{pgf}

\begin{document}
\newcommand\XA{1}
\newcommand\YA{10}
\newcommand\LengthProbe{3.5}
\newcommand\widthProbe{0.5}
\newcommand\contactPoint{0.1}
\newcommand\neuronX{3}
\newcommand\neuronY{8.5}
\newcommand\neuronYTwo{3.5} 
\gdef\oldy{0}


    %empty first frame:
    \xsbox{mygraph}{\tikz \path[use as bounding box] (0,0) rectangle (11.5,12);}%
    \makebox[0pt][l]{\xusebox{mygraph}}%

    \xsbox{mygraph}{%   
    \begin{tikzpicture}

    \path[use as bounding box] (0,0) rectangle (11.5,12);
    \draw (\XA,\YA) -- (\XA,\YA-\LengthProbe) -- (\XA+\widthProbe,\YA-\LengthProbe -\widthProbe) --(\XA+\widthProbe,\YA) -- (\XA,\YA); %linear probes

    \foreach \gx in {0}{                %contact point on the first linear probe
        \foreach \ix in {1,3}
        \draw (\XA+0.1,\YA-\ix+\gx) rectangle (\XA+\contactPoint+0.1,\YA-\ix-\contactPoint+\gx);
        \foreach \ix in {0,2}
        \draw (\XA+0.3,\YA-\ix-0.2+\gx) rectangle (\XA+\contactPoint+0.3,\YA-\ix-\contactPoint-0.2+\gx);
    }

    \node[] at (10.5,9.5) {Channel 1};
    \node[] at (10.5,8.5) {Channel 2};
    \node[] at (10.5,7.5) {Channel 3};
    \node[] at (10.5,6.5) {Channel 4};

    \node [circle,draw] at (\neuronX,\neuronY) (N1) {}; 
    \node [circle,draw,below right = 0.2cm and 0.2cm of N1] () {};
    \node [circle,draw,below right = 0.15cm and -0.2cm of N1] () {};
    \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
    \node [circle,draw,above right = 0.05cm and 0.2cm of N1] () {};
    \node [circle,draw,below right = 0.8cm and 0.3cm of N1] () {};
    \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
    \node [circle,draw,below right = 1.2cm and 0.2cm of N1] () {};
    \node [circle,draw,below left = 1.4cm and 0.0cm of N1] () {};

    \node[yshift = 10.5cm,xshift = 3.5cm] () {Before Optimisation};

       \coordinate (A1) at (2.5,10);
       \coordinate (B1) at (2,6);
       \coordinate (A2) at (4 , 10);
       \coordinate (B2) at (4.5 , 6);


    \end{tikzpicture}

    %repeat previous graph
    \makebox[0pt][l]{\xusebox{mygraph}}%
}

    \begin{animateinline}[controls]{8}

        \xusebox{mygraph}
        \newframe
        \multiframe{30}{rx=6.9+0.05}{%
            \pgfmathsetmacro{\y}{0.1*rand}%
            \xsbox{mygraph}{%
                %repeat previous graph

                \makebox[0pt][l]{\xusebox{mygraph}}%
                %append new line segment
                \begin{tikzpicture}

                \foreach \x in {-1,0,1,2}{
                \path[use as bounding box] (0,0) rectangle (11.5,12);
                \pgfmathsetmacro{\oldx}{\rx-0.05}
                \ifdim 7.3pt < \rx pt \relax
                    \ifdim  7.8pt > \rx pt\relax
                       \draw [red,line cap=round] (\oldx,\oldy+7.6+\x) -- (\rx,\y+7.6+\x);
                       \node[yshift = 10.5cm,xshift = 7.5cm] () {Light Pulse};
                       \shade[top color=orange,bottom color=orange,middle color = red,fill opacity=0.4] (A2) to [bend right=10] (B2) to [] (B1) to [bend right=10] (A1) to (A2);
                       \node [circle,draw] at (\neuronX,\neuronY) (N1) {}; 
                       \node [circle,draw,below right = 0.2cm and 0.2cm of N1] () {};
                        \node [circle,draw,below right = 0.15cm and -0.2cm of N1] () {};
                        \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
                        \node [circle,draw,above right = 0.05cm and 0.2cm of N1] () {};
                        \node [circle,draw,below right = 0.8cm and 0.3cm of N1] () {};
                        \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
                        \node [circle,draw,below right = 1.2cm and 0.2cm of N1] () {};
                        \node [circle,draw,below left = 1.4cm and 0.0cm of N1] () {};
                    \else
                    \draw [line cap=round] (\oldx,\oldy+7.3+\x) -- (\rx,\y+7.3+\x);
                    \shade[top color=white,bottom color=white,middle color = white] (A2) to [bend right=10] (B2) to [] (B1) to [bend right=10] (A1) to (A2);
                    \node [circle,draw] at (\neuronX,\neuronY) (N1) {}; 
                    \node [circle,draw,below right = 0.2cm and 0.2cm of N1] () {};
                    \node [circle,draw,below right = 0.15cm and -0.2cm of N1] () {};
                    \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
                    \node [circle,draw,above right = 0.05cm and 0.2cm of N1] () {};
                    \node [circle,draw,below right = 0.8cm and 0.3cm of N1] () {};
                    \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
                    \node [circle,draw,below right = 1.2cm and 0.2cm of N1] () {};
                    \node [circle,draw,below left = 1.4cm and 0.0cm of N1] () {};
                    \fi 
                \else
                \draw [line cap=round] (\oldx,\oldy+7.3+\x) -- (\rx,\y+7.3+\x); 
                \shade[top color=white,bottom color=white,middle color = white] 
               (A2) to [bend right=10] (B2) to [] (B1) to [bend right=10] (A1) to 
               (A2);
                \node [circle,draw] at (\neuronX,\neuronY) (N1) {}; 
                \node [circle,draw,below right = 0.2cm and 0.2cm of N1] () {};
                \node [circle,draw,below right = 0.15cm and -0.2cm of N1] () {};
                \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
                \node [circle,draw,above right = 0.05cm and 0.2cm of N1] () {};
                \node [circle,draw,below right = 0.8cm and 0.3cm of N1] () {};
                \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
                \node [circle,draw,below right = 1.2cm and 0.2cm of N1] () {};
                \node [circle,draw,below left = 1.4cm and 0.0cm of N1] () {};
                \fi
                }


                \end{tikzpicture}%
            }%
            \xusebox{mygraph}%
            \xdef\oldy{\y}%
        }

    \end{animateinline}

\end{document}

在此处输入图片描述

答案1

第一个问题,连接三个图部分可以通过正确定义旧的-价值\oldy

\gdef\oldy{7.3}% initial value
...
% updated like this after appending a new line segment
\pgfmathsetmacro{\tmpy}{\y+\dy}%
\xdef\oldy{\tmpy}%

在哪里

\def\dy{7.3}% for 1st and 3rd graph sections
\def\dy{7.6}% for 2nd graph section

此外,使用单个保存框 ( mygraph) 来逐步构建动画帧。当您将所有内容(包括许多静态图形元素)放入其中时,由于图形的新线段是通过递归引用其自身的保存框来添加的,因此每个新的动画帧都会引入越来越多的冗余。稍后,在 PDF 查看器中,每个新帧都会渲染越来越多的相同图形对象,这会减慢动画播放的速度。

可以通过重新排列代码、将动画帧的不同部分存储到单独的保存箱中来避免这种冗余将动画放在一起:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{animate}
\usepackage{xsavebox} %\xsbox, \xsavebox, \xusebox, `xlrbox' environment

\begin{document}
\newcommand\XA{1}%
\newcommand\YA{10}%
\newcommand\LengthProbe{3.5}%
\newcommand\widthProbe{0.5}%
\newcommand\contactPoint{0.1}%
\newcommand\neuronX{3}%
\newcommand\neuronY{8.5}%
\newcommand\neuronYTwo{3.5}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% static objects shared by all frames
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{xlrbox}{staticElems}%
  \begin{tikzpicture}
    \draw (\XA,\YA) -- (\XA,\YA-\LengthProbe) -- (\XA+\widthProbe,\YA-\LengthProbe -\widthProbe) --(\XA+\widthProbe,\YA) -- (\XA,\YA); %linear probes

    \foreach \gx in {0}{ %contact point on the first linear probe
        \foreach \ix in {1,3}
        \draw (\XA+0.1,\YA-\ix+\gx) rectangle (\XA+\contactPoint+0.1,\YA-\ix-\contactPoint+\gx);
        \foreach \ix in {0,2}
        \draw (\XA+0.3,\YA-\ix-0.2+\gx) rectangle (\XA+\contactPoint+0.3,\YA-\ix-\contactPoint-0.2+\gx);
    }

    \node[] at (10.5,9.5) {Channel 1};
    \node[] at (10.5,8.5) {Channel 2};
    \node[] at (10.5,7.5) {Channel 3};
    \node[] at (10.5,6.5) {Channel 4};

    \node [circle,draw] at (\neuronX,\neuronY) (N1) {};
    \node [circle,draw,below right = 0.2cm and 0.2cm of N1] () {};
    \node [circle,draw,below right = 0.15cm and -0.2cm of N1] () {};
    \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
    \node [circle,draw,above right = 0.05cm and 0.2cm of N1] () {};
    \node [circle,draw,below right = 0.8cm and 0.3cm of N1] () {};
    \node [circle,draw,below left = 0.6cm and -0.2cm of N1] () {};
    \node [circle,draw,below right = 1.2cm and 0.2cm of N1] () {};
    \node [circle,draw,below left = 1.4cm and 0.0cm of N1] () {};
    \node[yshift = 10.5cm,xshift = 3.5cm] () {Before Optimisation};

    \coordinate (A1) at (2.5,10);
    \coordinate (B1) at (2,6);
    \coordinate (A2) at (4 , 10);
    \coordinate (B2) at (4.5 , 6);

    % save bounding box coordinates
    \coordinate (LowerLeft) at (current bounding box.south west);
    \coordinate (UpperRight) at (current bounding box.north east);
  \end{tikzpicture}%
\end{xlrbox}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% two shaded objects
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{xlrbox}{shaded-1}%
  \begin{tikzpicture}
    \path[use as bounding box] (LowerLeft) rectangle (UpperRight);
    \shade[top color=orange,bottom color=orange,middle color = red,fill opacity=0.4] (A2) to [bend right=10] (B2) to [] (B1) to [bend right=10] (A1) to (A2);
    \node[yshift = 10.5cm,xshift = 7.5cm] () {Light Pulse};
  \end{tikzpicture}%
\end{xlrbox}%
%
\begin{xlrbox}{shaded-2}%
  \begin{tikzpicture}
    \path[use as bounding box] (LowerLeft) rectangle (UpperRight);
    \shade[top color=white,bottom color=white,middle color = white] (A2) to [bend right=10] (B2) to [] (B1) to [bend right=10] (A1) to (A2);
  \end{tikzpicture}%
\end{xlrbox}%
%
% the empty graph to start with
\begin{xlrbox}{mygraph}%
  \begin{tikzpicture}
    \path[use as bounding box] (LowerLeft) rectangle (UpperRight);
  \end{tikzpicture}%
\end{xlrbox}%
%
% putting the animation together
\begin{animateinline}[controls]{8}
  \xusebox{staticElems} %first frame showing only the static objects
\newframe
  \gdef\oldy{7.3}%
  \multiframe{30}{rx=6.9+0.05}{%
    % set line colour, vertical graph displacement and which shaded
    % object to use
    \def\whichShaded{shaded-2}%
    \def\linecolor{black}%
    \def\dy{7.3}%
    \ifdim 7.3pt < \rx pt\relax%
      \ifdim  7.8pt > \rx pt\relax%
        \def\whichShaded{shaded-1}%
        \def\linecolor{red}%
        \def\dy{7.6}%
      \fi%
    \fi%
    % assemble the graph
    \pgfmathsetmacro{\oldx}{\rx-0.05}%
    \pgfmathsetmacro{\y}{0.1*rand}%
    \begin{xlrbox}{mygraph}%
      %repeat previous graph
      \makebox[0pt][l]{\xusebox{mygraph}}%
      %
      %append new line segments
      \begin{tikzpicture}[line cap=round, draw=\linecolor]
        \path[use as bounding box] (LowerLeft) rectangle (UpperRight);

        \foreach \x in {-1,0,1,2}{
          \draw (\oldx,\oldy+\x) -- (\rx,\y+\dy+\x);
        }
      \end{tikzpicture}%
    \end{xlrbox}%
    \pgfmathsetmacro{\tmpy}{\y+\dy}%
    \xdef\oldy{\tmpy}%
    %
    % shaded object in the background
    \makebox[0pt][l]{\xusebox{\whichShaded}}%
    %
    % overlay the static objects
    \makebox[0pt][l]{\xusebox{staticElems}}%
    %
    % put graph in the foreground
    \xusebox{mygraph}%
  }
\end{animateinline}

\end{document}

相关内容