TikZ - 为圆柱体添加模糊阴影

TikZ - 为圆柱体添加模糊阴影

我正在使用该shapes.geometric库绘制圆柱体。我还使用它shadows.blur来实现模糊效果。问题是,将这两个库结合起来并不能产生预期/所需的输出。

输出

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric} % Cylinder
\usetikzlibrary{shadows.blur}
\begin{document}
  \begin{tikzpicture}[
    withoutShadow/.style={
      cylinder, minimum height=200pt, minimum width=25pt, 
      % fill=white, %will override the body fill and end fill 
      cylinder uses custom fill,
      cylinder body fill=yellow,
      cylinder end fill=red, 
      draw=red,
    },
    withShadow/.style={
      cylinder, minimum height=200pt, minimum width=25pt, 
      % fill=white, %will override the body fill and end fill 
      cylinder uses custom fill,
      cylinder body fill=yellow,
      cylinder end fill=red, 
      draw=red,
      blur shadow={
        shadow blur steps=10,
        shadow blur extra rounding=2pt, 
        shadow xshift=1pt
      }
    },
  ]
  \node[withoutShadow](c1) at (0, 0){No shadow};
  \node[withShadow, below of = c1, yshift = -1em](c2){With shadow};
  \node[withShadow, fill = white, below of = c2, yshift = -1em]
       {With shadow and \texttt{fill=white}};
  \end{tikzpicture}
\end{document}

有没有办法让选项cylinder uses custom fill按预期工作并在圆柱体周围产生有效的模糊?

答案1

你可以使用这个定义cylinder end fill,它除了解决您的问题之外,还允许您进行渐变填充。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric} % Cylinder
\usetikzlibrary{shadows.blur,positioning}
\tikzset{cylinder end fill/.style={path picture={
\pgftransformshift{\centerpoint}%
\pgftransformrotate{\rotate}%  
\pgfpathmoveto{\beforetop}%
\pgfpatharc{90}{-270}{\xradius and \yradius}%
\pgfpathclose
\pgfsetfillcolor{#1}%
\pgfusepath{fill}}
}}

\begin{document}
  \begin{tikzpicture}[font=\sffamily,
    withoutShadow/.style={
      cylinder, minimum height=200pt, minimum width=25pt, 
      % fill=white, %will override the body fill and end fill 
      cylinder uses custom fill,
      cylinder body fill=yellow,
      cylinder end fill=red, 
      draw=red,
    },
    withShadow/.style={
      cylinder, minimum height=200pt, minimum width=25pt,     
      cylinder end fill=red, 
      draw=red,
      blur shadow={
        shadow blur steps=10,
        shadow blur extra rounding=2pt, 
        shadow xshift=1pt
      }
    },
  ]
  \node[withoutShadow](c1) at (0, 0){No shadow};
  \node[withShadow, below=1em of c1,
  left color=yellow!40,right color=yellow, middle color=yellow!20,
      shading angle=0,](c2){With shadow and gradient fill};
  \node[withShadow, fill = yellow, below=1em of c2,]
       {With shadow and \texttt{fill=yellow}};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容