如何在一个图层/页面中绘制 4 个不同的活塞,然后将 Tikz 中的动画保存为 GIF?

如何在一个图层/页面中绘制 4 个不同的活塞,然后将 Tikz 中的动画保存为 GIF?

我看到了这样一个话题:

改进 TikZ 中四冲程发动机的代码

问题:

  1. 如何将 4 个活塞(进气、压缩、燃烧、排气)放在同一行,作为 4 冲程发动机,并一起为它们设置动画?

  2. 我想将动画保存为 gif,我能够获得包含大量图片的 pdf。但不知道如何将其制作成 gif。在线转换器甚至无法使用。

这是我想要转换为 GIF 的:

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes}

\tikzset{
    relative to node/.style={
        shift={(#1.center)},
        x={(#1.east)},
        y={(#1.north)},
    },
    kolben/.pic={
        \node[fill=white,draw,minimum width=1.5cm,minimum height=1.3cm] (-a) at (0,0.2) {};
        \begin{scope}[relative to node=-a]
            \draw (-1,0.2) -- ++(2,0);
            \draw (-1,0.3) -- ++(2,0);
            \draw (-1,0.4) -- ++(2,0);
        \end{scope}
        \draw (0,0) circle(0.07);
    },
    valve/.pic={
        \draw[fill=white] (-0.175,0) -- (-0.125,0.05) -- (-0.05,0.05) -- (-0.05,0.15) -- (-0.075,0.15) -- (-0.075,0.2) -- (0.075,0.2) -- (0.075,0.15) -- (0.05,0.15) -- (0.05,0.05) -- (0.125,0.05) -- (0.175,0) -- cycle;
    },
    cylinder/.pic={
        \draw (-0.75,1.55) -- (-0.8,1.55) -- (-0.8,5.05) -- (-0.5,5.05) -- (-0.55,5) -- (-0.75,5) -- cycle;
        \draw (-0.25,5.05) -- (-0.2,5) -- (0.2,5) -- (0.25,5.05) -- cycle;
        \draw[xscale=-1] (-0.75,1.55) -- (-0.8,1.55) -- (-0.8,5.05) -- (-0.5,5.05) -- (-0.55,5) -- (-0.75,5) -- cycle;
    },
    crank/.pic={
        \draw[fill=white,rounded corners=1mm] (90:0.75) arc (90:270:0.75) -- (0,-0.1) [rounded corners=0mm] -- (1,-0.1) arc (-90:90:0.1) [rounded corners=1mm] -- (0,0.1) -- cycle (1,0) circle (0.05);
    }
}

\begin{document}
    \foreach \phase [
    evaluate=\phase as \x using {int(mod(\phase + 90,360))},
    evaluate=\phase as \case using {int(mod(\phase/180,4))},
    evaluate=\x as \d using {40*sin(\x)+50},
    ] in {0,5,...,719}{
        \begin{tikzpicture}
            \useasboundingbox (-2,-2) rectangle (2,6);
            
            \coordinate (pivot) at (0,{3+sin(\x)});
            \coordinate (piston-top) at (0,{3+sin(\x)+1.3/2+0.2});
            
            \ifcase\case
            \colorlet{gascolor}{blue!10}
            \pgfmathsetmacro\valveone{1}
            \pgfmathsetmacro\valvetwo{0}
            \or
            \colorlet{gascolor}{blue!\d}
            \pgfmathsetmacro\valveone{0}
            \pgfmathsetmacro\valvetwo{0}
            \or
            \colorlet{gascolor}{red!\d}
            \pgfmathsetmacro\valveone{0}
            \pgfmathsetmacro\valvetwo{0}
            \or
            \colorlet{gascolor}{gray!10}
            \pgfmathsetmacro\valveone{0}
            \pgfmathsetmacro\valvetwo{1}
            \fi
            
            \draw[line width=14.9mm,gascolor] (piston-top) -- (0,5);
            
            \pic (cylinder) at (0,0) {cylinder};
            
            \pic (valve1) at (-0.375,{5-0.1*\valveone}) {valve};
            \pic (valve2) at (0.375,{5-0.1*\valvetwo}) {valve};
            
            
            \draw[double distance=2mm-\pgflinewidth,line cap=round] (\x:1) -- (pivot);  
            
            \pic[rotate=\x] (crank) at (0,0) {crank};
            \pic (kolben) at (pivot) {kolben};
            
            \node[anchor=base] at (1.25,3) {$ x $};
            \ifnum\x=0
            \fill[red] (1,3) circle (1.5pt);
            \else
            \ifnum\x=180
            \fill[red] (1,3) circle (1.5pt);
            \else
            \draw[->,red,ultra thick] (1,3) -- +(0,{sin(\x)});
            \fi
            \fi
            
            \node[anchor=base] at (1.75,3) {$ \dot{x} $};
            \ifnum\x=90
            \fill[blue] (1.5,3) circle (1.5pt);
            \else
            \ifnum\x=270
            \fill[blue] (1.5,3) circle (1.5pt);
            \else
            \draw[->,blue,ultra thick] (1.5,3) -- +(0,{cos(\x)});
            \fi
            \fi
            
            \ifnum\phase=360
            \begin{scope}
                \clip (-7.5mm,5cm-0.5\pgflinewidth) rectangle (7.5mm,3cm);
                \node[starburst,starburst point height=3mm,inner color=yellow,outer color=red,draw=orange] at (0,5) {};
            \end{scope}
            \fi
            
        \end{tikzpicture}
    }
\end{document}

相关内容