5

I was trying to create an animated TaiChi symbol with TikZ and the animate package, and I encountered the following problem:

If I don't add a bigger bounding box to the tikzpicture, then some frames in the resulting animation will "shrink".

The following MWE illustrates the problem:

\documentclass{article}
\usepackage{tikz}
\usepackage{animate}

\begin{document}
\centering

This is wrong.\\
\newcommand\TaiChiWrong[1]{
  \begin{tikzpicture}[scale=3]
    \fill[white](#1+180:1)arc(#1+180:#1:1)--cycle;  % black half-circle
    \fill(#1:1)arc(#1:#1-180:1)--cycle;             % white half-circle
    \fill(#1+180:.5)circle[radius=.5];              % black head
    \fill[white](#1:.5)circle[radius=.5];           % white head
    \fill(#1:.5)circle[radius=.1];                  % black eye
    \fill[white](#1+180:.5)circle[radius=.1];       % white eye
    \draw[gray](0,0)circle[radius=1cm];             % border
    \draw[red](-1,-1)rectangle(1,1);
    % \draw[blue](-1.3,-1.3)rectangle(1.3,1.3);
  \end{tikzpicture}
}
\begin{animateinline}[loop]{10}
  \multiframe{36}{i=90+-10}{\TaiChiWrong{\i}}
\end{animateinline}

\bigskip

This is correct.\\
\newcommand\TaiChi[1]{
  \begin{tikzpicture}[scale=3]
    \fill[white](#1+180:1)arc(#1+180:#1:1)--cycle;  % black half-circle
    \fill(#1:1)arc(#1:#1-180:1)--cycle;             % white half-circle
    \fill(#1+180:.5)circle[radius=.5];              % black head
    \fill[white](#1:.5)circle[radius=.5];           % white head
    \fill(#1:.5)circle[radius=.1];                  % black eye
    \fill[white](#1+180:.5)circle[radius=.1];       % white eye
    \draw[gray](0,0)circle[radius=1cm];             % border
    \draw[red](-1,-1)rectangle(1,1);
    \draw[blue](-1.3,-1.3)rectangle(1.3,1.3);
  \end{tikzpicture}
}
\begin{animateinline}[loop]{10}
  \multiframe{36}{i=90+-10}{\TaiChi{\i}}
\end{animateinline}

\end{document}

The only difference between the wrong and correct versions is that in the latter, a bigger bounding box (the blue one) is drawn. Moreover, the bigger bounding box has to somehow be "big enough": for example, if the blue box is \draw[blue](-1.1,-1.1)rectangle(1.1,1.1); the problem still exists.

I suspect that this problem is not related to animate because the problem still exists when I use \foreach to create a series of images.

My question is: Why does a bigger bounding box make a difference?

3

1 Answer 1

5

TikZ automatically creates a bounding box to fit the contents. To override, you can specify

\draw[use as bounding box](-1.3,-1.3)rectangle(1.3,1.3);

This is probably necessary due to the automatically-created bounding rectangle of your sequence of frames varying due to the varying placement of the elements in the picture.

You might also want to clip to this rectangle.

Edit in response to comments below: In the current instance, the problem is that the TikZ bounding box of an arc is not actually the true bounding box of the arc. Compare

\begin{tikzpicture}
    \draw (0,0) arc[start angle=0,end angle=180,radius=5cm];
    \draw[color=blue] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}

\begin{tikzpicture}
    \draw (0,0) arc[start angle=20,end angle=200,radius=5cm];
    \draw[color=blue] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}

The reason for this (at a guess) is that the calculated bounding box includes the Bezier control points for the arc, which actually lie off the path.

I am not 100% familiar with the animate package, but it looks like it takes the bounding box of the first image as the "canvas" and then scales all subsequent images to fit. Since the automatic bounding box of the images is wrong (as above) this creates the observed behaviour.

So following my earlier suggestion, something like this does work (I've checked)

\newcommand\TaiChiFixed[1]{
  \begin{tikzpicture}[scale=3]
    \path[clip,use as bounding box](-1,-1)rectangle(1,1);
    \fill[white](#1+180:1)arc(#1+180:#1:1)--cycle;  % black half-circle
    \fill(#1:1)arc(#1:#1-180:1)--cycle;             % white half-circle
    \fill(#1+180:.5)circle[radius=.5];              % black head
    \fill[white](#1:.5)circle[radius=.5];           % white head
    \fill(#1:.5)circle[radius=.1];                  % black eye
    \fill[white](#1+180:.5)circle[radius=.1];       % white eye
    \draw[gray](0,0)circle[radius=1cm];             % border
    \draw[red](-1,-1)rectangle(1,1);
  \end{tikzpicture}
}
5
  • But this doesn't answer my question: Why does a bigger bounding box solve the problem of some frames in the animation being smaller than others?
    – Herr K.
    Dec 2, 2013 at 3:33
  • OK I thought you just wanted a fix rather than a complete explanation. I'll update the answer to explain more completely. Dec 2, 2013 at 4:04
  • 2
    After all my edits, I have found that my guesses on the cause and correct solution are correct -- See page 8 of animate package documentation: ctan.math.utah.edu/ctan/tex-archive/macros/latex/contrib/… Dec 2, 2013 at 4:30
  • 1
    Thanks! "the calculated bounding box includes the Bezier control points for the arc, which actually lie off the path" is exactly what I'm looking for :)
    – Herr K.
    Dec 2, 2013 at 4:38
  • 1
    Someone needs to do this: floris.briolas.nl/floris/2009/10/bounding-box-of-cubic-bezier Dec 2, 2013 at 11:00

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.