4

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}

psygo
  • 438
  • 3
    Have a look at the animate package – samcarter_is_at_topanswers.xyz Feb 03 '24 at 16:23
  • I'm examining that pacakge's documentation right now, but it doesn't seem to work on Foxit right now (doesn't work on Chrome either apparently), even though it says it should. Anyways, I thought TikZ might have some sort of frame-by-frame mechanism/environment, something similar to Beamer, even though I think Beamer just prints things on different slides. – psygo Feb 03 '24 at 16:56
  • TikZ also has animations, see section "26 Animations" of the manual (but carefully read the documentation about the limitations of the output formats) – samcarter_is_at_topanswers.xyz Feb 03 '24 at 17:00
  • hyperref lets you create interactive forms with buttons and such. However, I don't know if it lets you do this and I've had patchy results in viewers other than Adobe's. (But support in other viewers has improved, I think. Or the implementation in hyperref has improved. I don't know which, but things work better in Okular than they used to.) – cfr Feb 03 '24 at 17:18

2 Answers2

7

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 ++(\i13/\value{totalsteps}-\i5/\value{totalsteps} ,-0.3); \filldraw [blue] (5,-0.85) ++ (\i13/\value{totalsteps}-\i5/\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

Tom
  • 7,318
  • 4
  • 21
  • The issue is that animations are not necessarily interactive. What I wanted was to have buttons to go back and forth in the game. – psygo Feb 04 '24 at 11:56
  • 1
    Apparently, it is possible to add control buttons. – psygo Feb 04 '24 at 12:12
  • Wait a minut. Yours does have controls, you just didn't show it in your GIF. – psygo Feb 04 '24 at 13:53
  • Thank you for the answer this looks dope. Do you know if it's possible to add a progress bar to the controls (it doesn't need to be interactive)? – psygo Feb 04 '24 at 14:04
  • On Foxit Reader, if I click the arrows too fast, nothing happens actually. Is that a limitation of your viewer or of animate/tikz? – psygo Feb 04 '24 at 14:05
  • 1
    @PhilippeFanaro I don't know what is the reason cause that. I will update the answer to include a displayed progress bar. I think interactive bar is difficult, I don't know how to do that. Maybe there is way to do it, wait some one with more professional knowledges. – Tom Feb 04 '24 at 17:42
  • 1
    Thank you for the kind reference you made! And the progressbar looks awesome, @Tom! – AlexG Feb 04 '24 at 20:39
  • @PhilippeFanaro An interactive progressbar (slider) is close to impossible, as the PDF format does not define such a widget. Maybe, one could improvise something looking similar, using a row of buttons and some JavaScript, but I doubt it will fully provide the expected feel when interacted with using the mouse. – AlexG Feb 04 '24 at 20:58
  • The only interactive progress bar I was able to find is from, coincidentally, you (@AlexG)! It's in this answer, in which you use XML, and Flash? Anyways, that does look way more complicated than average, and, as you mentioned in that answer, there might still be limited editor support. But I wonder what changed in the 10+ years gap from that answer. – psygo Feb 05 '24 at 11:25
  • Yes, this one is part of the Flash file that is rendered by the Flash plugin in Acrobat Reader. Adobe discontinued Flash some years ago and its support in Acrobat Reader. So this kind of interaction is not available anymore. – AlexG Feb 05 '24 at 11:50
5

I used TikZ and animate package to make the following animation :

enter image description here

The classical approach is to make your frame by frame pictures in a .tex file, for mathematical or figures based on a sort of algorithms you can use loops it's perfect, but in this case I made a frame by frame file I named it Go-game.tex

\documentclass[tikz, border=30mm]{standalone}
\usetikzlibrary{fadings}
\definecolor{backcol}{HTML}{8b5a16}
\definecolor{wood}{HTML}{5d2f03}
\definecolor{gwhite}{HTML}{ecefe9}
\definecolor{gblack}{HTML}{221d1d}

\newcommand{\white}[1]{ \begin{scope}[shift={#1}] \shade[top color=gwhite, bottom color=white] (0,0)circle(0.95); \end{scope} }

\newcommand{\black}[1]{ \begin{scope}[shift={#1}] \shade[top color=gblack, bottom color=black] (0,0)circle(0.95); \end{scope} }

\begin{document} \pagecolor{backcol} \color{wood} \foreach\i in {0, 10, 20, ..., 100}{ \begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)}

\node[opacity=\i/100, scale=3, text width=4cm, align =center, preaction={fill, wood}] at (8,14) {\color{white}In order to take over the white goishi we need};

\end{tikzpicture} } \foreach\i in {90, 80,..., 0}{ \begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)}

\node[opacity=\i/100, scale=3, text width=4cm, align =center, preaction={fill, wood}] at (8,14) {\color{white}In order to take over the white goishi we need};

\end{tikzpicture} }

\foreach\i in {0, 10, 20, ..., 100}{ \begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)}

\node[opacity=\i/100, scale=3, text width=4cm, align =center, preaction={fill, wood}] at (8,14) {\color{white}To surround them using the black goishi}; \end{tikzpicture} } \foreach\i in {90, 80, 70, ..., 0}{ \begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)}

\node[opacity=\i/100, scale=3, text width=4cm, align =center, preaction={fill, wood}] at (8,14) {\color{white}To surround them using the black goishi}; \end{tikzpicture} }

\begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)} \black{(2,4)} \end{tikzpicture}

\begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)} \black{(2,4)} \black{(4,2)}

\end{tikzpicture}

\begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)} \black{(2,4)} \black{(4,2)} \black{(6,4)} \end{tikzpicture}

\begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)} \black{(2,4)} \black{(4,2)} \black{(6,4)} \black{(6,6)} \end{tikzpicture}

\begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)} \black{(2,4)} \black{(4,2)} \black{(6,4)} \black{(6,6)} \black{(4,8)} \end{tikzpicture}

\begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \white{(4,4)} \white{(4,6)} \black{(2,4)} \black{(4,2)} \black{(6,4)} \black{(6,6)} \black{(4,8)} \black{(2,6)} \end{tikzpicture}

\foreach\i in {90, 80, 70, ...,0}{ \begin{tikzpicture} \draw[line width=0.5mm, step=2] (0,0)grid(16,16); \begin{scope}[opacity=\i/100] \white{(4,4)} \white{(4,6)} \end{scope} \black{(2,4)} \black{(4,2)} \black{(6,4)} \black{(6,6)} \black{(4,8)} \black{(2,6)} \end{tikzpicture}

}

\end{document}

Compiling you file produce Go-game.pdf we'll use it to animate our frames :

\documentclass[tikz, border=40mm]{standalone}
\usepackage{animate}
\begin{document}
\animategraphics[controls=all, loop]{15}{Go-game}{}{}
\end{document}

And yeah you should be good to get your animation after that.

euler_med
  • 518
  • The problem is that I wanted each move to appear based on the click of a button. – psygo Feb 04 '24 at 12:01
  • But maybe this does have controls? I wasn't able to successfully compile so far. Could add a screenshot/GIF with the controls if there are some? – psygo Feb 04 '24 at 17:40
  • @PhilippeFanaro, with \animategraphics (used here) as with animateinline (Tom's answer), the same control buttons are available. It is just the inserted GIF animation that does not show them. – AlexG Feb 04 '24 at 20:45
  • Cool! If it were possible, I would have awarded the answer to this one as well! – psygo Feb 05 '24 at 10:58