怎样燃放烟花?

怎样燃放烟花?

很快就要庆祝新年了。我们如何使用 TikZ 绘制烟花?例如,这张由 Kurume-Shimin 拍摄的照片,可在维基百科

2011 年 8 月 5 日,日本福冈久留米市的烟花

点击图像搜索以获得进一步的印象。在维基百科页面上,您可以看到更多效果,只需单击图像即可。我想在我们的博客庆祝新年,并作为TikZ 示例库在这种场合。

答案1

以下是我的看法:

更新:更多“bling bling”,更多编译时间......(并尝试@Stefan 的绝妙想法)

更新 2感谢 Stefan,我发现之前的版本只能在 QtikZ 环境中使用,其他地方(我猜)都不能用。出于某些我无法理解的原因,淡入淡出效果在 Adob​​e Reader 中仍然无法正确显示,但至少在 Windows 上的 SumatraPDF 中(几乎)可以正常工作。抱歉给您带来不便,祝大家新年快乐!

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathmorphing}
\pgfdeclareradialshading{someshade}{\pgfpointorigin}{color(0mm)=(pgftransparent!40);color(4mm)=(pgftransparent!50);color(8mm)=(pgftransparent!70);color(2cm)=(pgftransparent!100)}
\pgfdeclareradialshading{somenodeshade}{\pgfpointorigin}{color(0mm)=(pgftransparent!0);color(2mm)=(pgftransparent!0);color(5mm)=(pgftransparent!99);color(20mm)=(pgftransparent!100)}
\pgfdeclareradialshading{invertshade}{\pgfpointorigin}{color(0mm)=(pgftransparent!100);color(10mm)=(pgftransparent!95);color(16mm)=(pgftransparent!60);color(2cm)=(pgftransparent!0)}

\pgfdeclarefading{fadeit}{\pgfuseshading{someshade}}
\pgfdeclarefading{fadein}{\pgfuseshading{invertshade}}

\begin{document}
\begin{tikzpicture}[projectile/.style={decorate,decoration={random steps,segment length=2pt,amplitude=0.5pt}}]
\fill[black] (-4,-3) rectangle (6,4);

\begin{scope}[xshift=0cm,yshift=-0.4cm,transparency group]
\pgfsetfading{fadein}{\pgftransformshift{\pgfpointorigin}}
    \foreach \x in {0,10,..., 360}{\draw[blue!80!white,projectile,line width=1.1pt] (0,0) to [in=90] (10*rand+\x:rand*1mm+2cm);};
\end{scope}

\begin{scope}[xshift=2cm,yshift=1cm]
   \foreach \x in {0,12,..., 360}{\draw [yellow!5,thick,projectile] (0.7,0) to  (3*rand+\x :1mm*rand+2.2cm)  node%
[circle,inner sep=1mm,shade,shading=somenodeshade,opacity=1,outer sep=0] {};}
{\pgfsetfading{fadeit}{\pgftransformshift{\pgfpoint{2.5cm}{1cm}}}};\fill[white] (-3,-3) rectangle (3,3);
\end{scope}

\begin{scope}[xshift=3cm,yshift=-1cm]
\foreach \x in {0,10,..., 360}{\def\r1{rand}\draw [yellow]  ($(0,0)!abs{\r1}!(\x :5mm)$) to [in=90] ($(0,0)!abs{\r1}+0.2!(\x :8mm)$);}
{\pgfsetfading{fadeit}{\pgftransformshift{\pgfpoint{3cm}{-1cm}}}};\fill[yellow,opacity=0.6] (-3,-3) rectangle (3,3);
\end{scope}

\begin{scope}[xshift=-1cm,yshift=1.5cm]
   \foreach \x in {0,12,..., 360}{\def\r2{rand}\draw [red,line width=0.5pt]  ($(0,0)!abs{\r2}!(\x :3mm)$) -- ($(0,0)!abs{\r2}+0.1!(\x :7mm)$);}
{\pgfsetfading{fadeit}{\pgftransformshift{\pgfpoint{-1cm}{1.5cm}}}};\fill[red,opacity=0.6] (-3,-3) rectangle (3,3);
\end{scope}
\end{tikzpicture}
\end{document}

更新后的代码如下(查看之前代码的历史记录)

在此处输入图片描述

答案2

更新 4:如果有更多的射线,效果会更好,我添加了一个装饰(Stephan 的想法),但结果是随机的。我更改了一些参数。在中心添加一些阴影(来自 percusse 和 bloodworks 的想法):

\documentclass{minimal} 
\usepackage[dvipsnames,svgnames]{xcolor} 
\usepackage{tikz}
 \usetikzlibrary{calc} 

 \usetikzlibrary{decorations,decorations.pathmorphing} 

\begin{document}

\begin{tikzpicture}[decoration={bent,aspect=.3}]
  \draw[fill=black] (-8,-3) rectangle (8,5); 
  \foreach \colA/\colB/\sx/\sy in {red/yellow/-4/2,blue/green/-2/1,
                                   yellow/red/0/2,green/blue/4/2,yellow/white/3/1.5} 
{% 
   \draw[shading=radial, inner color=white, outer color=black, opacity=.5]  (\sx,\sy) circle (1cm);   
  \pgfmathparse{rnd}   
  \let\ri\pgfmathresult  
  \begin{scope}[xshift=\sx]
      \foreach \i in {0,...,150}{%  
         \pgfmathparse{rnd}
         \global\let\x\pgfmathresult 
         \pgfmathparse{rnd}  
         \global\let\y\pgfmathresult 
         \pgfmathparse{rnd} 
         \let\c\pgfmathresult 
         \pgfmathsetmacro{\col}{50*\c+50} 
           \draw[decorate,\colA!\col!\colB] %
           ($(360*\x:2*\ri*\y+1) +(\sx,\sy)$) to    ($(360*\x:1*\y) + (\sx,\sy)$);} 
  \end{scope}
} 
\end{tikzpicture} 
\end{document} 

在此处输入图片描述

答案3

祝TEX.SX的所有朋友新年快乐。

此代码基于 Cmhughes 和 Bloodworks 的想法。我很失望 Latex 不允许我添加声音:

\documentclass{article}
\usepackage{filecontents}%
\begin{filecontents*}{beam.tex}
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,decorations.shapes,backgrounds}

\begin{document}

\foreach \radius in {1,2,...,20}
{
\begin{tikzpicture}
  % background rectangle
  \filldraw[black] (-3,-3) rectangle (5,3);
    \pgfmathparse{100-(\radius)};
   \foreach \x in {-2,-.5,2.3,4}{
   \pgfmathrandominteger{\y}{0}{2.9}
   \node[xshift=5*rnd,blue!\pgfmathresult!white,scale={.8*rnd}]at (\x,\y){$\star$};
   }
  \ifnum\radius<4
  \pgfmathparse{100-(\radius)*4};
  \draw[blue!\pgfmathresult!black,decorate, decoration={random steps,segment length=3pt,amplitude=.5pt},ultra thin] (0.5 , -2.9)--(0,0);
\fi
  \ifnum\radius<11
  \pgfmathparse{100-(\radius-1)*10};
  \fill[shading=radial, inner color=white!\pgfmathresult!black, outer color=black, opacity=.8](0,0) circle (\radius mm -2mm);   
   \foreach \angle in {0,10,...,360}
{
 \draw[red!\pgfmathresult!black,,decorate, decoration={random steps,segment length=1pt,amplitude=.3pt}] (0,0) -- (\angle:\radius mm)node[white!\pgfmathresult!blue, yshift=-.1mm,scale=.35*rnd]{*};
 }
\fi
%%%
  \ifnum\radius>5
  \ifnum\radius<10
  \pgfmathparse{100-(\radius)*4};
  \draw[xshift=1cm,yshift=1cm,red!\pgfmathresult!black,decorate, decoration={random steps,segment length=3pt,amplitude=.5pt},ultra thin] (1 , -3.9)--(0,0);
\fi\fi
\ifnum\radius>5
  \pgfmathparse{100-(\radius-6)*7};
 \fill[xshift=1cm,yshift=1cm,shading=radial, inner color=white!\pgfmathresult!black, outer color=black, opacity=.8](0,0) circle (\radius mm -9mm);
 \foreach \angle in {0,20,...,360}
{
 \draw[xshift=1cm,yshift=1cm,orange!\pgfmathresult!black,decorate, decoration={random steps,segment length=1pt,amplitude=.3pt}] (0,0) -- (\angle:\radius mm -4mm)node[white!\pgfmathresult!blue, yshift=-.1mm,scale=.5*rnd]{*};
 }
 \foreach \angle in {10,30,...,350}{
 \draw[xshift=1cm,yshift=1cm,red!\pgfmathresult!black,decorate, decoration={random steps,segment length=1pt,amplitude=.5pt}] (0,0) -- (\angle:\radius mm -5mm)node[red!\pgfmathresult!black, yshift=-.1mm,scale=.5*rnd]{*};
 }
    \foreach \angle in {15,35,...,355}{
 \draw[xshift=1cm,yshift=1cm,yellow!\pgfmathresult!black,decorate, decoration={random steps,segment length=1pt,amplitude=.5pt},opacity=.5] (0,0) -- (\angle:\radius mm -6mm)node[white!\pgfmathresult!black, yshift=-.1mm,scale=.5*rnd,opacity=1]{*};
 }
     \foreach \angle in {5,25,...,355}{
 \draw[xshift=1cm,yshift=1cm,green!\pgfmathresult!black,decorate, decoration={random steps,segment length=1pt,amplitude=.5pt},opacity=.5] (0,0) -- (\angle:\radius mm -3mm)node[orange!\pgfmathresult!black, yshift=-.1mm,scale=.5*rnd,opacity=1]{*};
 }
 \fi
 % yellow firework
  \ifnum\radius>10
  \ifnum\radius<14
  \pgfmathparse{100-(\radius)*4};
      \draw[xshift=3cm,yshift=1.5cm,blue!\pgfmathresult!black,decorate, decoration={random steps,segment length=3pt,amplitude=.5pt},ultra thin] (1 , -4.5)--(0,0);
\fi\fi
   \ifnum\radius>10
  \pgfmathparse{100-(\radius-1)*5};
   \fill[xshift=3cm,yshift=1.5cm,shading=radial, inner color=green!\pgfmathresult!black, outer color=black, opacity=.8](0,0) circle (\radius mm -10mm);
   \foreach \angle in {0,10,...,360}
{
 \draw[xshift=3cm,yshift=1.5cm,yellow!\pgfmathresult!black,decorate, decoration={random steps,segment length=2pt,amplitude=.5pt},opacity=.5] (0,0) -- (\angle:\radius mm -8mm)node[orange!\pgfmathresult!black, yshift=-.1mm,scale=.8*rnd,opacity=1]{*};
 }
\fi
   % blue green firework
  \ifnum\radius>10
  \ifnum\radius<14
  \pgfmathparse{100-(\radius)*4};
  \draw[xshift=-1.5cm,yshift=1.5cm,green!\pgfmathresult!black,decorate, decoration={random steps,segment length=3pt,amplitude=.5pt},ultra thin] (-1, -4.5)--(0,0);
\fi\fi
   \ifnum\radius>10
  \pgfmathparse{100-(\radius-1)*5};
       \fill[xshift=-1.5cm,yshift=1.5cm,shading=radial, inner color=yellow!\pgfmathresult!black, outer color=black, opacity=.8](0,0) circle (\radius mm -10mm);
   \foreach \angle in {10,20,...,360}
{
 \draw[xshift=-1.5cm,yshift=1.5cm,green!\pgfmathresult!black,decorate, decoration={random steps,segment length=2pt,amplitude=.5pt},opacity=.5] (0,0) -- (\angle:\radius mm -8mm)node[red!\pgfmathresult!black, yshift=-.1mm,scale=.8*rnd,opacity=1]{*};
 }
\fi
\end{tikzpicture}
}
\end{document}

\end{filecontents*}
%create the beam.pdf.
\immediate\write18{pdflatex beam}
% convert to GIF animation
\immediate\write18{convert -delay 15 -loop 0 -density 200 -alpha remove beam.pdf beam.gif}

\begin{document}
 Look for the beam.gif file in the same directory as this file.
\end{document}

用它编译pdflatex,您应该已经安装了imagemagick用于生成.gif文件。

在此处输入图片描述

答案4

不如上面两个那么聪明,但这就是我所拥有的。我(几乎)没有使用以 开头的命令pgf

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{decorations.pathmorphing} 

\begin{document}

\begin{tikzpicture}
 \draw[fill=black] (-5,-15) rectangle (25,15);
 \foreach \mya in {1,10,20}{
  \pgfmathrandominteger{\myb}{1}{10}
  \draw[shading=radial, inner color=white, outer color=black, opacity=.5](\mya , \myb) circle (5cm);
  \foreach \x in {-3.1,-2,-1.1,0.1,1.2,2.3,3.1}{%
  \foreach \y in {-3.2,-2.1,-1.2,0,1.1,2,3.2}{%
  \draw[red, thick, decorate, decoration={random steps,segment length=3pt,amplitude=1pt}, rotate around={10:(\mya , \myb)}, opacity=0.7](\mya , \myb)--(\mya+\x, \myb+\y)node[red, yshift=-.1mm]{*};%
  \draw[blue,decorate, decoration={random steps,segment length=3pt,amplitude=1pt}, rotate around={20:(\mya , \myb)}](\mya , \myb)--(\mya+\x, \myb+\y)node[red, yshift=-.1mm]{*};%
  \draw[red!50!yellow, decorate, decoration={random steps,segment length=3pt,amplitude=1pt}, rotate around={50:(\mya , \myb)}](\mya , \myb)--(\mya+\x,  \myb+\y);
 }
 }
 \draw[decorate, decoration=snake, red, thick] (0 , -13)--(\mya , \myb);
}


\end{tikzpicture}
\end{document}

在此处输入图片描述

编译需要一段时间!

相关内容