如何通过 tikz 淡化光线

如何通过 tikz 淡化光线

我想要绘制如下图所示的光线。 在此处输入图片描述

下面的 MWE 不太美观。请帮我改正。提前谢谢!

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{fadings}
\pgfdeclareradialshading{rayball}{\pgfpoint{-0.45cm}{0.35cm}}%
{%
  color(0cm)=(white!50!red);
  color(0.15cm)=(red!90!white);
  color(0.55cm)=(red!70!white);
  color(1cm)=(red!60!white)
}
\begin{document}
\pagecolor{red}
\begin{tikzpicture}
\def\n{8} %%number of ray lights
\def\r{0.15} %% radius of circle
\pgfmathsetmacro{\arcangle}{360/(4*\n)}
\def\lightray{(\arcangle:\r)
    \foreach \i in {1,3,...,2\n}{
      --({2*\i*\arcangle}:{30*\r})--({(2*\i+1)*\arcangle}:\r)
      --({2*(\i+1)*\arcangle}:{14*\r})--({(2*\i+3)*\arcangle}:\r)
    }
};
\tikzfading[name=fade out, inner color=transparent!0, outer
color=transparent!100]
\tikzfading[name=fade outTWO, inner color=transparent!0, outer
color=transparent!50]
%%%Ball color
\begin{scope}[opacity=0.85]
    \foreach \rt in {1,2,...,9}{
        \fill[path fading=fade outTWO,shading=rayball,color=red] circle(\rt*\r) ;
    }
\end{scope}
%%%Light rays
\begin{scope}
\pgfsetblendmode{lighten}
\fill[path fading=fade out,color=yellow!70!red] \lightray;
\foreach \i in {-0.25,0,0.25}{
    \fill[path fading=fade out,color=white,scale=1.75,opacity=0.25,rotate=\i] \lightray;
}
\end{scope}
\end{tikzpicture}
\end{document}

答案1

在此处输入图片描述

光线的构造基于@Alain Matthes回答;其中一种颜色(在锥形端)是背景颜色。之后,添加一些褪色圆盘(或多或少)来暗示亮点。所需的库是decorations.markingsfadings

代码

\documentclass[11pt, margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, math}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fadings}

\pgfkeys{/pgf/decoration/.cd,
  width factor/.store in =\wfactor,
  start color/.store in =\startcolor,
  end color/.store in =\endcolor
}
\makeatletter
\pgfdeclaredecoration{width and color change}{initial}{%
  \state{initial}[width=0pt, next state=line, persistent precomputation={%
    \pgfmathdivide{50}{\pgfdecoratedpathlength}%
    \let\increment=\pgfmathresult%
    \def\x{0}%
  }]{}
  \state{line}[width=.5pt, persistent postcomputation={%
    \pgfmathadd@{\x}{\increment}%
    \let\x=\pgfmathresult%
  }]{%
    \pgfsetlinewidth{\wfactor*\x/50*0.075pt+\pgflinewidth}%
    \pgfsetarrows{-}%
    \pgfpathmoveto{\pgfpointorigin}%
    \pgfpathlineto{\pgfqpoint{.75pt}{0pt}}%
    \pgfsetstrokecolor{\endcolor!\x!\startcolor}%
    \pgfusepath{stroke}%
  }
  \state{final}{%
    \pgfsetlinewidth{\pgflinewidth}%
    \pgfpathmoveto{\pgfpointorigin}%
    \color{\endcolor!\x!\startcolor}%
    \pgfusepath{stroke}% 
  }
}
\makeatother

\xdefinecolor{Y}{RGB}{238, 204, 17}
\xdefinecolor{R}{RGB}{238, 34, 34}
\xdefinecolor{LightY}{RGB}{247, 225, 34}

\begin{document}

\tikzfading[name=ffade out, inner color=transparent!64, outer color=transparent!100]
\tikzfading[name=fade out, inner color=transparent!0, outer color=transparent!100] 
\begin{tikzpicture}
  % background
  \fill[R] (-5, -5) rectangle (5, 5);

  % small fading rays
  \foreach \a in {0, 60, ..., 300}{%
    \draw[%line width=.4pt,
    decoration={
      width and color change,   
      width factor=.1,
      start color=R,
      end color=LightY!40!white
    }, decorate] (\a+30: 3.5) -- (0, 0);
  }

  % long rays
  \foreach \a in {0, 60, ..., 300}{%
    \draw[decoration={
      width and color change,   
      width factor=.3,
      start color=R,
      end color=Y
    }, decorate] (\a+5: 5) -- (0, 0);
  }
  \foreach \a in {0, 60, ..., 300}{%
    \draw[decoration={
      width and color change,   
      width factor=.1,
      start color=R,
      end color=LightY
    }, decorate] (\a+5: 5) -- (0, 0);
  }

  % higlights
  \fill[LightY!20!white, path fading=fade out] (0, 0) circle (.8);
  \foreach \a in {0, 60, 120}{%
    \fill[LightY!50!white, path fading=ffade out, rotate={\a+5}] 
    (0, 0) ellipse [x radius=1.3, y radius=1];
  }
\end{tikzpicture}

相关内容