TikZ로 대화형 다이어그램을 만드는 방법

TikZ로 대화형 다이어그램을 만드는 방법

나는인터렉티브에서 판매하는 EPUB에 사용되는 것과 매우 유사한 다이어그램책으로 이동:

Go Books 다이어그램 - 초기 위치

Go Books 다이어그램 - 이동 1

"대화형"이라는 말은 사용자가 상황이 트리거되는 시점을 제어한다는 것입니다. 예를 들어 애니메이션은 사용자가 무엇을 하든 관계없이 발생하기 때문에 상호작용이라고 부르지 않습니다. (물론 각 동작 사이에 애니메이션을 적용할 수도 있습니다.)

솔직히 그것이 TikZ에서도 가능한지 모르겠습니다. 그렇습니까? TikZ에 HTML과 JS를 삽입하는 방법이 있을까요?

다음은 3개의 동작이 있는 바둑판의 최소 예입니다. 아이디어는 TikZ가 앞뒤로 이동하기 위한 일종의 UI를 추가한 다음 그에 따라 각 동작을 표시하도록 하는 것입니다(재생 버튼이 필요 없음).

\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}

답변1

크레딧도 다음으로 이동해야 합니다.알렉스G. 그의 멋진 animate패키지에 감사드립니다. 그리고 그 \playgo명령 역시 그의 생각에서 나온 것이다.우편( \uncover명령).

animateinline다음은 from package 를 사용한 예입니다 animate. Adobe Reader 및 Foxit Reader에서 작동합니다(VS Code의 PDF 뷰어에서는 작동하지 않는 것 같습니다).

\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}

단계별 애니메이션

이 애니메이션은 다음과 같이 제어됩니다.

애니메이션 컨트롤

업데이트: 표시된 진행률 표시줄과 화려한 색상을 추가합니다.

\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}

여기에 이미지 설명을 입력하세요

답변2

TikZ와 animate 패키지를 사용하여 다음 애니메이션을 만들었습니다.

여기에 이미지 설명을 입력하세요

고전적인 접근 방식은 파일의 프레임별 그림을 만드는 것입니다 .tex. 수학적 또는 일종의 알고리즘을 기반으로 하는 수치의 경우 루프를 사용할 수 있으므로 완벽하지만 이 경우에는 프레임별 파일을 만들어 이름을 지정했습니다.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}

파일 생성을 컴파일하면 Go-game.pdf이를 사용하여 프레임에 애니메이션을 적용할 것입니다.

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

그리고 네, 그 후에 애니메이션을 얻는 것이 좋을 것입니다.

관련 정보