2

I cannot connect the red line with the two black lines in the code (see figure). Another problem is the animation slows down as in plays. This is one of the first time I use animation so any help will be appreciated.

   \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}

enter image description here

1 Answer 1

4

The first issue, connecting the three graph sections can be solved by properly defining the old y-value \oldy:

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

where

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

Furthermore, a single savebox (mygraph) is used in order to incrementally build the animation frames. As you put everything into it, including many static graphical elements, and since the new line segments of the graph are added by recursively referencing the savebox within itself, a growing lot of redundancy is introduced with every new animation frame. Later, in the PDF viewer, the same graphical objects are rendered a growing number of times for every new frame, which slows down the animation while it plays.

This redundancy can be avoided by rearranging the code, storing different parts of the animation frames into individual saveboxes before putting the animation together:

\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}
3
  • Thank you. However, is on purpose there is a height difference between the red and black lines, hence the value 7.3 and 7.6
    – Jesper H
    Oct 20, 2017 at 6:47
  • Ah, I see. I misunderstood your question. This can be solved somehow. I will think about it.
    – AlexG
    Oct 20, 2017 at 7:07
  • @JesperH That was easy. See edit.
    – AlexG
    Oct 20, 2017 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.