TikZでanimation
animate packageを使う
フレームごとにSVGが作られる
TikZでanimationする図を作ろうと検索したときに出てくる情報は全てこれ
animation (TikZ)を使う
svg animationのみ対応、PDF非対応
tikzjaxなどに使う分にはこれで十分
https://scrapbox.io/files/65b709d364c88e0025f56ae1.svg
code:animation.tikz(tex)
% from https://tikz.dev/tikz-animations#sec-26.1
\usetikzlibrary{animations}
\begin{document}
\begin{tikzpicture}[
animate/orbit/.style 2 args = {
myself:shift = {
along = {
(0,0) circle radius=#1
} sloped in #2s/10,
repeats }} ]
\nodefill,circle :color = {0s = "orange",
2s = "red",
4s = "orange",
repeats}
{Sun};
\begin{scope}animate={orbit={2.5cm}{365}}
\nodedraw,circle {Earth};
\node draw,circle,animate={orbit={1cm}{28}} {Moon};
\end{scope}
\useasboundingbox (-3.8,-3.8) (3.8,3.8);
\end{tikzpicture}
\end{document}
最近できた機能なせいか、ほとんど検索結果に引っかからない
構文
sec-14.20 | Syntax for Path Specifications - PGF/TikZ Manual
:<animation attribute>={<options>}
[animate={myself:<animation attribute>={<options>}}]の略記法
正式な記法
[animate=<animation specification>]
<animation specification>に色々いれる
[animate={myself:<animation attribute>={<options>}}]は[animate={object=myself,attribulte=<animation attribute>,<options>}]の略記
これらの構文の詳しい同値関係はsection-anim-syntax-animate | Animations - PGF/TikZ Manualを参照
公式サンプルをいろいろ動かしてみる
<animation attribute>にanimationしたいoption nameを入れ、<options>に時刻とoption valueを指定するというかんじか
0s = "red", 2s = "blue"と複数点でoptionを指定すると、よしなにsmoothingしてくれる
例
shakeは指定した値で反復するように点を定めている
code:sample.tikz(tex)
\usetikzlibrary{animations}
\begin{document}
\tikz \draw :xshift = {0s = "0cm", 1s = "-15mm", repeats} (0,0) circleradius=5mm;
\tikzevery node/.style={fill, text = white}
\node (A) animate = {myself:fill = {0s = "red", 2s = "blue", repeats }} {Click me}
node text = white,anchor=north :fill = {0s = "red", 2s = "blue", repeats } at (A.south){Click me};
\begin{tikzpicture}[
animate/shake/.style = {myself:xshift = {
repeats,
0s = "0mm", 50ms = "#1", 150ms = "-#1", 250ms = "#1", 300ms = "0mm"
}}
]
\begin{scope}every node/.style={circle,fill = blue!20, draw=blue,very thick}
\node (A) animate = {shake = 1mm} {Shake};
\node at (A.east) anchor=west,animate = {shake = 2mm} {SHAKE};
\end{scope}
\end{tikzpicture}
\begin{tikzpicture}[
shake/.style={animate={myself:xshift = {
repeats,
0s = "0mm", 50ms = "#1", 150ms = "-#1", 250ms = "#1", 300ms = "0mm"
}}}
]
\begin{scope}every node/.style={circle,fill = blue!20, draw=blue,very thick}
\node (A) shake = 1mm {Shake};
\node at (A.east) anchor=west,shake = 2mm {SHAKE};
\end{scope}
\end{tikzpicture}
\end{document}
飛び飛びの値にしたいときは、jumppgf./tikz/animate/options/jump | Animations - PGF/TikZ Manualを入れる
https://scrapbox.io/files/66023c97bd29550023c32457.svg
code:jump.tikz(tex)
\usetikzlibrary{animations}
\begin{document}
\tikz \drawline width=1mm :color = {
0s = "red", %なぜかこれを入れないと点滅しない。色はなんでもいい
1s = {jump, "red"},
2s = {jump, "green"},
3s = {jump, "blue"},
repeats
} (0,0) circleradius=5mm;
\end{document}
animationの起動方法section-anim-timeline | Animations - PGF/TikZ Manual
repeats:繰り返し
pgf./tikz/animate/options/repeats | Animations - PGF/TikZ Manual
begin on=click:クリックで開始
pgf./tikz/animate/options/begin:on | Animations - PGF/TikZ Manual
animate/<style name>/.style={myself:<animation attribute>={<options>}}でanimation styleを定義すると、animate = {<style name>}で使えるようになる
通常のstyle (TikZ)記法を使うと
<style name>/.style={animate={myself:<animation attribute>={<options>}}}で定義
<style name>で使用
となる
使用可能な<animation attribute>のリスト
section-anim-attrs | Animations - PGF/TikZ Manual
3D animation
Help Me to Create Animation from Still Tikz-3D Image with changing value of one variable - TeX - LaTeX Stack Exchange
断念
rotate around zは<animation attribute>非対応
animate packageを使わざるをえないか
code:3d.tikz(tex)
\usepackage{tikz-3dplot}
\usetikzlibrary{animations}
\tdplotsetmaincoords{0}{0}
\definecolor{ocre}{RGB}{243,102,25}
\colorlet{mild}{ocre!50}
\begin{document}
\begin{tikzpicture}y={(0,-0.385cm)},z={(0,1cm)},scale=1.25
\tikzset{
rotation/.style={animate={myself:rotate around z = {
repeats,
0s="0", 1s="180", 2s="360"
}}}
}
\fillrotate around z=10, mild (0,0) -- (0,0,2) -- (1,0,2) -- (1,0,0) -- cycle;
\draw (1,0) arc (0:180:1);
\drawdashed (-1,0) arc (180:360:1);
\draw (0,0,2) circle (1);
\drawrotate around z=10, dashed (1,0,0) -- (0,0) -- (0,0,2);
\drawrotate around z=10 (0,0,2) -- (1,0,2);
\drawrotate around z=10, \ifnum10>180 dashed\fi (1,0,2) -- (1,0,0);
\draw (-1,0,0) -- (-1,0,2);
\draw (1,0,0) -- (1,0,2);
\drawrotate around z=10, scale=0.8 (0,0,0.3) -- (0.3,0,0.3) -- (0.3,0,0);
\end{tikzpicture}
\end{document}
nodeの連動は不可
code:sin.tikz(tex)
% from https://tikz.dev/tikz-animations#sec-26.1
\usetikzlibrary{animations}
\begin{document}
\begin{tikzpicture}[
animate/orbit/.style 2 args = {
myself:shift = {
along = {
(0,0) circle radius=#1
} sloped in #2s/10,
repeats }} ]
\node (A) draw,circle,animate={orbit={1cm}{28}} {A};
\draw (0,0) circle radius=1.0cm;
\draw (A) -- ++(4,0);
\end{tikzpicture}
\end{document}
References
Animations - PGF/TikZ Manual
tikz pgf - Animations code doesn't work - TeX - LaTeX Stack Exchange
\usepackage{animate}と\usetikzlibrary{animations}との違いが説明されている
関連する質問へのリンクもある
#2024-03-26 12:11:04
#2024-02-03 20:05:11
#2024-02-02
#2024-01-29 10:30:07