TikZ 图片旋转动画

TikZ 图片旋转动画

我制作了一个简单的 TikZ 图片,如下所示:

在此处输入图片描述

我将制作一个动画,使整个图像绕其 z_world 轴旋转。

我做了这样的尝试:

\documentclass[10pt]{article}
\usepackage[italian]{babel}
\usepackage{geometry}
\geometry{a4paper}

\usepackage{media9}
\usepackage{animate}

\newcounter{angle}
\setcounter{angle}{0}

\usepackage{tikz}
\usepackage{tikz-3dplot}

\begin{document}

\begin{figure}[ht]
\centering

\begin{animateinline}[loop, poster = first, controls]{30}  
\whiledo{\theangle<359}{

\tdplotsetmaincoords{120}{\theangle}
\begin{tikzpicture}[scale=2,tdplot_main_coords]

\def\laserPointX{1.5}
\def\laserPointY{0}
\def\laserPointZ{2.5}
\def\moduloQuadro{\laserPointX*\laserPointX+\laserPointY*\laserPointY+\laserPointZ*\laserPointZ}
\def\moduloLaser{sqrt{(\moduloQuadro)}}

% laser plane
\filldraw[red!50, opacity = 0.2] (\laserPointX,\laserPointY,\laserPointZ) -- (0,-0.8,0) -- (0,0.8,0) -- cycle;
\shade [ball color=red] (\laserPointX,\laserPointY,\laserPointZ) circle [radius=0.05cm];

% axis laser
\draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (1.5*\laserPointZ/\moduloLaser,0,-1.5*\laserPointX/\moduloLaser) node[anchor=west]{$y_{laser}$};
\draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (0,1.5,0) node[anchor=east]{$x_{laser}$};
\draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (\laserPointX/\moduloLaser*1.5,\laserPointY/\moduloLaser*1.5,\laserPointZ/\moduloLaser*1.5) node[anchor=south]{$z_{laser}$};

% axis world
\draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (1.5,0,0) node[anchor=north west]{$y_{world}$};
\draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (0,1.5,0) node[anchor=north]{$x_{world}$};
\draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (0,0,1.5) node[anchor=south]{$z_{world}$};

\end{tikzpicture}

\stepcounter{angle}
\ifthenelse{\theangle<359}{
        \newframe
}{
        \end{animateinline}
}
}
\end{figure}
\end{document}

但是我得到了一个由旋转和平移组成的丑陋效果(我认为是由于 TikZ 图片每帧都会调整大小)。

有人知道如何实现正确旋转吗?

答案1

所有帧必须共享相同的、手动插入的边界框(在 2D 屏幕坐标中),以便覆盖自动的、TikZ 计算的边界框(可能因帧而异)。

对于环境中的参数化图片animateinline,该\multiframe命令优先于\whiledo

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}

\usepackage{animate}

\begin{document}

\begin{animateinline}[loop, poster = first, controls]{24}
  \multiframe{360}{iAngle=0+1}{
    \tdplotsetmaincoords{120}{\iAngle}
    \begin{tikzpicture}[tdplot_main_coords]

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \useasboundingbox[tdplot_screen_coords] (-3,-1.5) rectangle (3,3);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    \def\laserPointX{1.5}
    \def\laserPointY{0}
    \def\laserPointZ{2.5}
    \def\moduloQuadro{\laserPointX*\laserPointX+\laserPointY*\laserPointY+\laserPointZ*\laserPointZ}
    \def\moduloLaser{sqrt{(\moduloQuadro)}}

    % laser plane
    \filldraw[red!50, opacity = 0.2] (\laserPointX,\laserPointY,\laserPointZ) -- (0,-0.8,0) -- (0,0.8,0) -- cycle;
    \shade [ball color=red] (\laserPointX,\laserPointY,\laserPointZ) circle [radius=0.05cm];

    % axis laser
    \draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (1.5*\laserPointZ/\moduloLaser,0,-1.5*\laserPointX/\moduloLaser) node[anchor=west]{$y_{laser}$};
    \draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (0,1.5,0) node[anchor=east]{$x_{laser}$};
    \draw[-latex,line width=3pt,green!70!black,line cap=round] (0,0,0) -- (\laserPointX/\moduloLaser*1.5,\laserPointY/\moduloLaser*1.5,\laserPointZ/\moduloLaser*1.5) node[anchor=south]{$z_{laser}$};

    % axis world
    \draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (1.5,0,0) node[anchor=north west]{$y_{world}$};
    \draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (0,1.5,0) node[anchor=north]{$x_{world}$};
    \draw[-latex,line width=1pt,blue,line cap=round] (0,0,0) -- (0,0,1.5) node[anchor=south]{$z_{world}$};

    \end{tikzpicture}
  }
\end{animateinline}

\end{document}

相关内容