1

I have a couple of problems regarding an animation in latex

1) The animation flicker while it runs

2) The animation is shown as a sliding window, I would like to keep the already played animation as it is being played (So the entire animation is visible at the end of the animation)

3) In the animation, deflections on the four lines being played is colored at specific moment with a specific color. I would like to color both points on each side of the deflection in the corresponding color. At the moment only the downward facing point is being colored.

Any help and improvements are more than welcome :)

\documentclass{standalone}
\usepackage{lmodern}
\usepackage{ifthen}
\usepackage{tikz,animate}
\usetikzlibrary{decorations,positioning,shapes}
\usepackage{pdfpages}
\usetikzlibrary{calc}
\usepackage{adjustbox}
\usepackage{xsavebox}

 \newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}   
 %used for the item box around

\begin{document}
\newcommand\neuronX{2.5}%
\newcommand\neuronY{9.5}%

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% static objects shared by all frames
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{xlrbox}{staticElems}%
\begin{tikzpicture}
\path[use as bounding box] (0,6) rectangle (14,11);

\draw[] (1.6,6.8) rectangle ++(0.1,3.5);
\draw[] (1.2,8.6) rectangle ++(0.1,1.7);
\draw[] (1.4,7.9) rectangle ++(0.1,2.4);
\draw[] (1,9.0) rectangle ++(0.1,1.3);

\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,fill = red] at (\neuronX,\neuronY) (N1) {};
\node [circle,draw,below right = 0.4cm and 0.4cm of N1,fill = green] () {};
\node [circle,draw,below right = 2.2cm and 0.2cm of N1,,fill = blue] () {};

% save bounding box coordinates
\coordinate (LowerLeft) at (current bounding box.south west);
\coordinate (UpperRight) at (current bounding box.north east);
\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}[autoplay]{8}%loop
\xusebox{staticElems} %first frame showing only the static objects
\newframe
\gdef\oldyOne{9.3}%
\gdef\oldyTwo{8.3}%
\gdef\oldyThree{7.3}%
\gdef\oldyFour{6.3}%
\multiframe{80}{rx=4.9+0.05}{%
    % set line colour, vertical graph displacement and which shaded
    % object to use
    %\def\whichShaded{shaded-2}%
    \def\linecolor{black}%
    \def\dy{9.3}%
    \def\dyTwo{8.3}%
    \def\dyThree{7.3}%
    \def\dyFour{6.3}%


    \def\nOne{0}
    \def\nTwo{0}
    \def\nThree{0}
    \def\nFour{0}

    \ifdim 5.3pt < \rx pt\relax%
    \ifdim  5.4pt > \rx pt\relax%
        \def\nOne{-0.4} 
        \def\linecolor{red}%    
    \fi%
    \fi%

    \ifdim 6.3pt < \rx pt\relax%
    \ifdim  6.4pt > \rx pt\relax%
        \def\nTwo{-0.4} 
            \def\linecolor{green}%

    \fi%
    \fi%

    \ifdim 6.8pt < \rx pt\relax%
    \ifdim  6.9pt > \rx pt\relax%
        \def\nThree{-0.4}   
        \def\linecolor{blue}%   
    \fi%
    \fi%

    \ifdim 7.1pt < \rx pt\relax%
    \ifdim  7.2pt > \rx pt\relax%
    \def\nOne{-0.4} 
    \def\linecolor{red}%    

    \fi%
    \fi%
    \ifdim 7.5pt < \rx pt\relax%
    \ifdim  7.6pt > \rx pt\relax%
    \def\nOne{-0.4} 
    \def\linecolor{red}%    
    \fi%
    \fi%

    % assemble the graph
    \pgfmathsetmacro{\oldx}{\rx-0.05}%
    \pgfmathsetmacro{\y}{0.1*rand}%
    \pgfmathsetmacro{\yTwo}{0.1*rand}%
    \pgfmathsetmacro{\yThree}{0.1*rand}%
    \pgfmathsetmacro{\yFour}{0.1*rand}%

    \pgfmathsetmacro{\chOne}{\dy+\nOne+\y}%
    \pgfmathsetmacro{\chTwo}{\dyTwo+\nOne + \nTwo+\yTwo}%
    \pgfmathsetmacro{\chThree}{\dyThree   + \nThree+ + \yThree}%
    \pgfmathsetmacro{\chFour}{\dyFour     + \nThree +  \yFour}%

    \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,draw] (LowerLeft) rectangle (UpperRight);

        \draw (\oldx,\oldyOne) -- (\rx,\chOne);         

        \draw (\oldx,\oldyTwo) -- (\rx,\chTwo);

        \draw (\oldx,\oldyThree) -- (\rx,\chThree);

        \draw (\oldx,\oldyFour) -- (\rx,\chFour);


        \end{tikzpicture}%
    \end{xlrbox}%

    \xdef\oldyOne{\chOne}%
    \xdef\oldyTwo{\chTwo}%
    \xdef\oldyThree{\chThree}%
    \xdef\oldyFour{\chFour}%

    % overlay the static objects
    \makebox[0pt][l]{\xusebox{staticElems}}%
    %
    % put graph in the foreground
    %\xusebox{mygraph}%
    \xusebox{mygraph}%

}
\end{animateinline}

\end{document}
  • Do you want the colour be kept for all line segments until the colour changes again? – AlexG Sep 23 '19 at 8:32
  • If you can make it so, that it is only the deflection there is colored I would like that. Otherwise, it is fine, if the 4 lines are colored in short segement by the same color – Jesper Hagelskjær Sep 23 '19 at 8:51
  • Currently, I can only solve issue (1). Colouring (3) is certainly depending on the dimension comparisons. (2) I don't quite understand. Do you want all segments beginning with the very first ones of the animation be kept until the animation's end? – AlexG Sep 23 '19 at 8:57
  • 2) That is correct... Let me clarify a little. After some time, in the animation it will start to erase itself. I dont want that. I want keep keep the lines segment which is being erased. I hoped that it helped – Jesper Hagelskjær Sep 23 '19 at 9:02
  • (2) There seems to be a limit of nested xsavebox levels (in Acrobat Reader). Using a timeline could solve this. – AlexG Sep 23 '19 at 9:32
1
  1. The bounding boxes with and without the enclosing rectangle differ slightly. This leads to re-scaling of the frames following the first one with the static elements. Excluding the rectangle from bbox calculation and putting it into the static frame (where it actually belongs) solves the flickering issue:

    \path[use as bounding box] (0,6) rectangle (14,11);
    \draw [thick] (0,6) rectangle (14,11);
    

    Also, there were some unprotected line endings which pile up to produce the spurious space in front of the big rectangle. Commenting out these line endings removes this space.

  2. There seems to be an upper limit of nested xsavebox levels (PDF XObjects) in Acrobat Reader. Putting the function line segments on the same level and assembling the animation frames using a timeline solves the issue of disappearing line segments.

  3. It is not quite clear how it should look like, but maybe like this:

enter image description here

\documentclass{standalone}
\usepackage{lmodern}
\usepackage{ifthen}
\usepackage{tikz,animate}
\usetikzlibrary{decorations,positioning,shapes}
\usepackage{pdfpages}
\usetikzlibrary{calc}

%%%%%%%%%%%%%%%%%%%%%%%%
% writing timeline file
%%%%%%%%%%%%%%%%%%%%%%%%
\newwrite\TimeLineFile
\immediate\openout\TimeLineFile=neuron.txt
\foreach \i in {0,1,...,80} {
  % appending `x0' to transparency number --> keep all them all visible
  \immediate\write\TimeLineFile{::\i x0}
}
\immediate\closeout\TimeLineFile
%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\newcommand\neuronX{2.5}%
\newcommand\neuronY{9.5}%

%staring points of curves
\def\dy{9.3}%
\def\dyTwo{8.3}%
\def\dyThree{7.3}%
\def\dyFour{6.3}%

\def\oldyOne{9.3}%
\def\oldyTwo{8.3}%
\def\oldyThree{7.3}%
\def\oldyFour{6.3}%

% putting the animation together
\begin{animateinline}[autoplay,timeline=neuron.txt]{8}%loop
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % static objects shared by all frames
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \begin{tikzpicture}
  \path[use as bounding box] (0,6) rectangle (14,11);
  \draw [thick] (0,6) rectangle (14,11);

  \draw[] (1.6,6.8) rectangle ++(0.1,3.5);
  \draw[] (1.2,8.6) rectangle ++(0.1,1.7);
  \draw[] (1.4,7.9) rectangle ++(0.1,2.4);
  \draw[] (1,9.0) rectangle ++(0.1,1.3);

  \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,fill = red] at (\neuronX,\neuronY) (N1) {};
  \node [circle,draw,below right = 0.4cm and 0.4cm of N1,fill = green] () {};
  \node [circle,draw,below right = 2.2cm and 0.2cm of N1,,fill = blue] () {};

  % save bounding box coordinates
  \coordinate (LowerLeft) at (current bounding box.south west);
  \coordinate (UpperRight) at (current bounding box.north east);
  \end{tikzpicture}%
\newframe
\multiframe{80}{rx=4.9+0.05,dimx=4.9pt+0.05pt}{%
  %%%%%%%%%%%%%%%%
  % line segments
  %%%%%%%%%%%%%%%%
  % set line colour, vertical graph displacement and which shaded
  % object to use
  %\def\whichShaded{shaded-2}%
  \def\linecolor{black}%
%
  \def\nOne{0}%
  \def\nTwo{0}%
  \def\nThree{0}%
  \def\nFour{0}%
%
  \ifdim 5.3pt < \dimx\relax%
  \def\linecolor{red}%    
  \ifdim  5.4pt > \dimx\relax%
      \def\nOne{-0.4}%
  \fi%
  \fi%
%
  \ifdim 6.3pt < \dimx\relax%
  \def\linecolor{green}%
  \ifdim  6.4pt > \dimx\relax%
    \def\nTwo{-0.4}% 
  \fi%
  \fi%
%
  \ifdim 6.8pt < \dimx\relax%
  \def\linecolor{blue}%   
  \ifdim  6.9pt > \dimx\relax%
    \def\nThree{-0.4}%   
  \fi%
  \fi%
%
  \ifdim 7.1pt < \dimx\relax%
  \def\linecolor{red}%    
  \ifdim  7.2pt > \dimx\relax%
    \def\nOne{-0.4}% 
  \fi%
  \fi%
%  
  \ifdim 7.5pt < \dimx\relax%
  \def\linecolor{red}%    
  \ifdim  7.6pt > \dimx\relax%
    \def\nOne{-0.4}%
  \fi%
  \fi%
%
  % assemble the graph
  \pgfmathsetmacro{\oldx}{\rx-0.05}%
  \pgfmathsetmacro{\y}{0.1*rand}%
  \pgfmathsetmacro{\yTwo}{0.1*rand}%
  \pgfmathsetmacro{\yThree}{0.1*rand}%
  \pgfmathsetmacro{\yFour}{0.1*rand}%
%
  \pgfmathsetmacro{\chOne}{\dy+\nOne+\y}%
  \pgfmathsetmacro{\chTwo}{\dyTwo+\nOne + \nTwo+\yTwo}%
  \pgfmathsetmacro{\chThree}{\dyThree   + \nThree+ + \yThree}%
  \pgfmathsetmacro{\chFour}{\dyFour     + \nThree +  \yFour}%
%
  %new line segments
  \begin{tikzpicture}[line cap=round, draw=\linecolor]
    \path[use as bounding box] (LowerLeft) rectangle (UpperRight);

    \draw (\oldx,\oldyOne) -- (\rx,\chOne);         

    \draw (\oldx,\oldyTwo) -- (\rx,\chTwo);

    \draw (\oldx,\oldyThree) -- (\rx,\chThree);

    \draw (\oldx,\oldyFour) -- (\rx,\chFour);
  \end{tikzpicture}%
%
  \xdef\oldyOne{\chOne}%
  \xdef\oldyTwo{\chTwo}%
  \xdef\oldyThree{\chThree}%
  \xdef\oldyFour{\chFour}%
}
\end{animateinline}

\end{document}
  • I got these error : ! Undefined control sequence. l.8 \xsb@newkey – AndréC Sep 23 '19 at 15:11
  • 1
    @AndréC remove the *.aux file. – AlexG Sep 23 '19 at 15:29
  • Hi AlexG.... Thank you very much for your help!!!... I have posted a new question under the title "Animation where only specific part, at specific intervals is shown" which is a question based of modifying your code (just given your a head-up) – Jesper Hagelskjær Sep 24 '19 at 6:50

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.