4

I was trying to create an animation to illustrate a simple 2D convolution using tikz and the animate package. I tried the code below but I am getting the following errors:

  • Package animate Error: Contents of first frame must not have zero width.
  • Package animate Error: Contents of first frame must not have zero height.
  • pdfTeX error (ext1): \pdfxform cannot be used with a void box.

I checked the package manual and StackExchange and some other on-line sites but could not find and explanation why there are these errors. Has it to do with the \foreach loops? I managed to create the animation with \multiframe but I cannot control the 2 variables independently and the animation follows a diagonal direction instead of the convolution sequence required. From my previous experience, I suspect of some dumbness in my approach, and I count on this excellent community to help me! Thanks in advance for all help.

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

\begin{document}

\begin{animateinline}[poster=first, controls] {20}
\foreach \Ry in {9,8,...,0}
{
    \foreach \Rx in {0,1,...,9}
    {
    \begin{tikzpicture} [scale=1]
        \useasboundingbox (0,0) rectangle (10,10);
        \fill [gray!60] (0,0) rectangle (10,10);
        \fill [red!50] (\Rx,\Ry) rectangle ++(1,1);
    \end{tikzpicture}
    \newframe%
    }
}
\end{animateinline}

\end{document}
AlexG
  • 54,894
vsantos
  • 306

1 Answers1

6

enter image description here

This makes use of TikZ's mod and floor operators:

%\documentclass[export]{standalone} % for multipage PDF/animated GIF
\documentclass{article}

\usepackage{tikz}
\usepackage{animate}

\begin{document}

\begin{animateinline}[poster=first, controls] {20}
\multiframe{100}{ii=0+1,rj=9.9+-0.1}{% ii=0,1,...99 ; rj=9.9,9.8,...,0.0
  %
  \pgfmathsetmacro{\Rx}{mod(\ii,10)}%  Rx = [0,1,...,9,] x 10
  \pgfmathsetmacro{\Ry}{floor(\rj)}%   Ry = 9 x 10 , 8 x 10 , ... , 0 x 10
  %
  \begin{tikzpicture} [scale=1]
      \useasboundingbox (0,0) rectangle (10,10);
      \fill [gray!60] (0,0) rectangle (10,10);
      \fill [red!50] (\Rx,\Ry) rectangle ++(1,1);
  \end{tikzpicture}
}
\end{animateinline}

\end{document}
AlexG
  • 54,894