TikZ-PGF – How to Create Interactive Diagrams with TikZ

diagramsinteractiontikz-pgf

I'm trying to create an interactive diagram, much like the one used in the EPUBs sold by Go Books:

Go Books Diagram - Initial Position

Go Books Diagram - Move 1

What I mean by "interactive" is that the user controls when things are triggered. For example, animations are not what I would call interactivity because they happen regardless of what the user does. (Of course, in-between each move, we could have an animation.)

I quite frankly don't know if that's even possible with TikZ. Is it? Maybe there's a way of injecting HTML and JS into TikZ?

Here's a minimal example of a Go board with 3 moves, the idea would be to have TikZ add some sort of UI for going back and forth, and then showing each move accordingly — no need for the play button —:

\documentclass{article}

\usepackage{tikz}

\newlength{\step}

\begin{document}
  \begin{tikzpicture}
    \setlength{\step}{\dimexpr 10cm / 18 \relax}

    \draw[step=\step] (0, 0) grid (10, 10);

    \draw[draw = white, fill = black, line width = 0.1mm]
      (2 * 10cm / 18, 3 * 10cm / 18)
      circle [radius = 0.2575cm]
      node[color = white] {1};
    \draw[draw = black, fill = white, line width = 0.1mm]
      (3 * 10cm / 18, 3 * 10cm / 18)
      circle [radius = 0.2575cm]
      node[color = black] {2};
    \draw[draw = white, fill = black, line width = 0.1mm]
      (4 * 10cm / 18, 3 * 10cm / 18)
      circle [radius = 0.2575cm]
      node[color = white] {3};
  \end{tikzpicture}
\end{document}

Best Answer

Credits should also go to AlexG. Thanks for his awesome animate package. And the \playgo command is also come from his idea in this post (\uncover command).

Here is an example using animateinline from package animate. It is working in Adobe Reader, and Foxit Reader (it doesn't seem to work on VS Code's PDF Viewer):

\documentclass{article}

\usepackage{tikz}
\usepackage{animate}

\newlength{\step}
\setlength{\step}{\dimexpr 10cm / 18 \relax}

\newcommand\playgo[3]{\ifnum#1<#2\phantom{#3}\else#3\fi}

\begin{document}
  \begin{animateinline}[step,controls=step]{1}
    \multiframe{4}{i=0+1}{
      \begin{tikzpicture}[x=\step,y=\step]
        %create the board 
        \draw[step=1] (0, 0) grid (18, 18);

        %setup black
        \foreach \bloc in {{2,4},{3,4},{4,4},{5,4},{6,5},{7,6}}{
          \filldraw[line width = 0.1mm] (\bloc) circle [radius = 0.2575cm];
        }

        %setup white
        \foreach \wloc in {{2,6},{3,6},{4,6},{5,6},{6,6},{7,7}}{
          \filldraw[fill=white,line width = 0.1mm] (\wloc) circle [radius = 0.2575cm];
        }

        %play the go-game start from black
        \foreach \stepnum/\loc in {1/{8,7},2/{8,8},3/{9,8}}{
          \playgo{\i}{\stepnum}{\filldraw[fill={\ifodd\stepnum black\else white\fi},line width = 0.1mm] (\loc) circle [radius = 0.2575cm] node [color = \ifodd\stepnum white\else black\fi] {\stepnum};}
        }
      \end{tikzpicture}
    }
  \end{animateinline}
\end{document}

Step by Step Animations

And this animation is controlled by this:

Animation Controls

Updates: add a displayed progress bar and fancy color.

\documentclass{article}

\usepackage{tikz}
\usepackage{animate}

\newlength{\step}
\setlength{\step}{\dimexpr 10cm / 18 \relax}

\newcommand\playgo[3]{\ifnum#1<#2\phantom{#3}\else#3\fi}
\newcounter{totalsteps}
\setcounter{totalsteps}{3}

\begin{document}
{\centering
  \begin{animateinline}[step,controls=step]{1}
    \multiframe{\numexpr\value{totalsteps}+1}{i=0+1}{
      \begin{tikzpicture}[x=\step,y=\step]
        %create the board 
        \fill [brown!30] (-0.5,-0.5) rectangle (18.5,18.5);
        \draw[step=1] (0, 0) grid (18, 18);
        \draw [line width=2pt] (0,0) rectangle (18,18);
        \foreach \sloc in 
 {{3,3},{3,9},{3,15},{9,3},{9,9},{9,15},{15,3},{15,9},{15,15}}{\filldraw (\sloc) circle [radius=1.5pt];}
        %add progress bar
        \draw [rounded corners=3pt,blue!20] (5,-0.85) rectangle (13,-1.15);
        \fill [rounded corners=3pt,blue] (5,-0.85) rectangle ++(\i*13/\value{totalsteps}-\i*5/\value{totalsteps} ,-0.3);
        \filldraw [blue] (5,-0.85) ++ (\i*13/\value{totalsteps}-\i*5/\value{totalsteps} ,-0.15) circle [radius=5pt];
        %setup black
        \foreach \bloc in {{2,4},{3,4},{4,4},{5,4},{6,5},{7,6}}{
          \filldraw[line width = 0.1mm] (\bloc) circle [radius = 0.2575cm];
        }

        %setup white
        \foreach \wloc in {{2,6},{3,6},{4,6},{5,6},{6,6},{7,7}}{
          \filldraw[fill=white,line width = 0.1mm] (\wloc) circle [radius = 0.2575cm];
        }

        %play the go-game start from black
        \foreach \stepnum/\loc in {1/{8,7},2/{8,8},3/{9,8}}{
          \playgo{\i}{\stepnum}{\filldraw[fill={\ifodd\stepnum black\else white\fi},line width = 0.1mm] (\loc) circle [radius = 0.2575cm] node [color = \ifodd\stepnum white\else black\fi] {\stepnum};}
        }
      \end{tikzpicture}
    }
  \end{animateinline}\par}
\end{document}

enter image description here