# Can we animate in LaTeX without precompiling every frame, i.e. simulate?

The animate package is great for simple animations, but it has the drawback that every frame of the animation has to be created during compilation. For animations with a simple dynamic but a long loop time (i.e. many different frames), this causes long compilation times and large PDF files.

As an example, consider the following MWE.

\documentclass{article}

\usepackage{tikz}
\usepackage{animate}

\def\bouncerow#1#2{%
\begin{tikzpicture}[baseline=0pt]
\def\bounceball##1##2{%
+(0,{abs(cos( (##2) / (##1) * 180 ))}) circle[radius=.5ex]
}
\draw [line cap=round]
(0,0) \bounceball{1}{#2}
foreach \n in {2, ..., #1} {
coordinate (previous)
({(\n-1)*1em},0pt) \bounceball{\n}{#2}
coordinate (this)
(previous) -- (this)
};
% Make the bounding box fixed.
\path (-1ex,-1ex) rectangle ({(#1 - 1)*1em + 1ex},1cm+1ex);
\end{tikzpicture}%
}

\begin{document}

Here are some bouncing circles:
\begin{animateinline}[loop, autoplay]{24}%
\multiframe{144}{r=0+0.0417}{% We need 6*24 = 144 frames here.
\bouncerow 3\r
}%
\end{animateinline}

Here are lots of those circles:
\begin{animateinline}[loop, autoplay]{24}%
\multiframe{216}{r=0+0.0417}{% We would need 2520*24 = 60480 frames for this!
\bouncerow 9\r
}%
\end{animateinline}

\end{document}


\bouncerow{n} draws a row of balls connected at their centers that bounce with different frequencies. The first one bounces once every second, the second every other second, and so on. The bounces in the first row (with periods 1 s, 2 s and 3 s) thus repeat every 6 seconds. We can easily animate that.

For the second row, with 9 balls, however, we would have to prepare 42 minutes of animation! This is obviously not feasible.

I expect it to be theoretically possible to do the necessary calculations for the animation while viewing it using JavaScript, which would remove the compilation overhead and reduce the file size of the resulting PDF. However, I cannot find any documentation (I can understand) on how to do this. Is it possible with a reasonable amount of work? I know my way around TeX and had some experience with JavaScript in the past, so it is not a problem if the solution is rather technical.

What I found so far:

• animate has a programming interface, but that seems to be restricted to controlling precompiled frames.
• TikZ has an animations library, but according to the documentation it only supports the output format SVG at the moment.
• This answer does something interactive with animations with JavaScript, but it apparently creates tens of thousands of OCGs while doing so, which isn't any better than having loads of frames. (I have to admit that I did not take the time to really understand the answer because of this.)
• This answer also does something interactive with JavaScript, but nothing is animated.

As noticed already, pkg animate-made animations consist of static animation frames which are played in rapid succession. Unlike SMIL-based SVG animations, single graphic objects cannot be animated individually.
Thus, pkg animate is suitable for producing relatively short animation sequences.
• Are you saying that it can't be done using animate (as mentioned in the question) or that it can't be done at all? – schtandard Mar 11 '20 at 14:50
• Not with animate. The PDF standard just doesn't provide this. animate's SVG export using dvisvgm is also based on static frames. You will have to use SMIL-based SVG animation, that TikZ can produce. – AlexG Mar 11 '20 at 16:03