tcolorbox 阴影形状

tcolorbox 阴影形状

我创建了一个newtcolorbox具有由路径定义的独特形状,然后按照下面的代码所示进行填充:

\documentclass[20pt, a4paper]{article}

\usepackage{tcolorbox}
\usepackage{lipsum}

\tcbuselibrary{skins}
\usetikzlibrary{calc}
\usetikzlibrary{shadows}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\definecolor{BaseColor}{HTML}{8533FF}

\newlength{\boxCornerOfset}
\setlength{\boxCornerOfset}{12pt}
\newlength{\boxShadowOfset}
\setlength{\boxShadowOfset}{2mm}

\newtcolorbox{mybox}{
    left=\boxCornerOfset,
    top=\boxCornerOfset,
    bottom=\boxCornerOfset,
    right=\boxCornerOfset,
    skin=enhanced jigsaw,
    sharpish corners,
    drop fuzzy shadow southeast={BaseColor!20!white},
    %frame style={drop shadow},
    %fuzzy shadow={\boxShadowOfset}{-\boxShadowOfset}{0mm}{BaseColor!30!white},
    frame code={%
        \begin{pgfonlayer}{main}
            \draw[line width=4pt,color=BaseColor,fill=BaseColor]($(frame.north west) + (\boxCornerOfset, 0pt)$)
                -- (frame.north east)
                -- ($(frame.south east) + (0pt, \boxCornerOfset)$)
                -- ($(frame.south east) + (-\boxCornerOfset, 0pt)$)
                -- (frame.south west)
                -- ($(frame.north west) + (0pt, -\boxCornerOfset)$)
                -- cycle;

        \end{pgfonlayer}
    },
    interior code={%
        \draw[line width=4pt,color=BaseColor,fill=BaseColor]($(frame.north west) + (\boxCornerOfset, -0pt)$)
                -- ($(frame.north east) + (-0pt, -0pt)$)
                -- ($(frame.south east) + (-0pt, \boxCornerOfset)$)
                -- ($(frame.south east) + (-\boxCornerOfset, 0pt)$)
                -- ($(frame.south west) + (0pt, 0pt)$)
                -- ($(frame.north west) + (0pt, -\boxCornerOfset)$)
                -- cycle;
    }

}

\begin{document}

\begin{mybox}
\begin{center}
{\sffamily\huge\color{white} Testing textbox, bippity!}
\end{center}
\end{mybox}

\end{document}

我的问题是,当我尝试应用阴影时,它并不遵循我创建的路径,我想知道是否有办法正确地布置阴影以遵循创建的形状。

答案1

基于这个答案来自@JLDiaz,您可以在同一路径上添加阴影并使用和控制其shadow xshift=\boxShadowOfset位置shadow yshift=-1.2\boxShadowOfset

\documentclass[20pt, a4paper]{article}

\usepackage{tcolorbox}
\usepackage{lipsum}

\tcbuselibrary{skins}
\usetikzlibrary{calc}
\usetikzlibrary{shadows,shadows.blur}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\definecolor{BaseColor}{HTML}{8533FF}

\newlength{\boxCornerOfset}
\setlength{\boxCornerOfset}{12pt}
\newlength{\boxShadowOfset}
\setlength{\boxShadowOfset}{2mm}

\def\framepath[#1]{\path[#1]($(frame.north west) + (\boxCornerOfset, 0pt)$)
                -- (frame.north east)
                -- ($(frame.south east) + (0pt, \boxCornerOfset)$)
                -- ($(frame.south east) + (-\boxCornerOfset, 0pt)$)
                -- (frame.south west)
                -- ($(frame.north west) + (0pt, -\boxCornerOfset)$)
                -- cycle;}

\newtcolorbox{mybox}{enhanced,
    left=\boxCornerOfset,
    top=\boxCornerOfset,
    bottom=\boxCornerOfset,
    right=\boxCornerOfset,
    skin=enhanced jigsaw,
    sharpish corners,
    frame code={%
        \begin{pgfonlayer}{main}
         \framepath[fill=BaseColor,blur shadow={shadow xshift=\boxShadowOfset, shadow yshift=-1.2\boxShadowOfset, shadow blur steps=6}]
         \end{pgfonlayer}
        },
    interior code={%
        \draw[line width=4pt,color=BaseColor,fill=BaseColor]($(frame.north west) + (\boxCornerOfset, -0pt)$)
                -- ($(frame.north east) + (-0pt, -0pt)$)
                -- ($(frame.south east) + (-0pt, \boxCornerOfset)$)
                -- ($(frame.south east) + (-\boxCornerOfset, 0pt)$)
                -- ($(frame.south west) + (0pt, 0pt)$)
                -- ($(frame.north west) + (0pt, -\boxCornerOfset)$)
                -- cycle;
    },
  }

\begin{document}

\begin{mybox}
\begin{center}
{\sffamily\huge\color{white} Testing textbox, bippity!}
\end{center}
\end{mybox}

\end{document}

结果

在此处输入图片描述

如果你想修改阴影的颜色,你可以render blur shadow像这样调整定义的代码

代码

\documentclass[20pt, a4paper]{article}

\usepackage{tcolorbox}
\usepackage{lipsum}

\tcbuselibrary{skins}
\usetikzlibrary{calc}
\usetikzlibrary{shadows,shadows.blur}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\definecolor{BaseColor}{HTML}{8533FF}

\newlength{\boxCornerOfset}
\setlength{\boxCornerOfset}{12pt}
\newlength{\boxShadowOfset}
\setlength{\boxShadowOfset}{2mm}


% change color of shadow 
\makeatletter
\tikzset{
render blur shadow/.code=
{\pgfbs@savebb
\pgfsyssoftpath@getcurrentpath{\pgfbs@input@path}%
\pgfbs@compute@shadow@bbox
\pgfbs@process@rounding{\pgfbs@input@path}{\pgfbs@fadepath}%
\pgfbs@apply@canvas@transform
\colorlet{pstb@shadow@color}{white!\pgfbs@opacity!black}%
\pgfdeclarefading{shadowfading}{\pgfbs@paint@fading}%
\pgfsetfillcolor{BaseColor}%
\pgfsetfading{shadowfading}%
{\pgftransformshift{\pgfpoint{\pgfbs@midx}{\pgfbs@midy}}}%
\pgfbs@usebbox{fill}%
\pgfbs@restorebb
},}
\makeatother

\def\framepath[#1]{\path[#1]($(frame.north west) + (\boxCornerOfset, 0pt)$)
                -- (frame.north east)
                -- ($(frame.south east) + (0pt, \boxCornerOfset)$)
                -- ($(frame.south east) + (-\boxCornerOfset, 0pt)$)
                -- (frame.south west)
                -- ($(frame.north west) + (0pt, -\boxCornerOfset)$)
                -- cycle;}

\newtcolorbox{mybox}{enhanced,
    left=\boxCornerOfset,
    top=\boxCornerOfset,
    bottom=\boxCornerOfset,
    right=\boxCornerOfset,
    skin=enhanced jigsaw,
    sharpish corners,
    frame code={%
        \begin{pgfonlayer}{main}
            \framepath[draw=BaseColor,blur shadow={shadow xshift=\boxShadowOfset, shadow yshift=-1.2\boxShadowOfset, shadow blur steps=6,fill=red}]
         \end{pgfonlayer}
                },
    interior code={%
            \framepath[fill=BaseColor]
                  },
     }

\begin{document}

\begin{mybox}
\begin{center}
{\sffamily\huge\color{white} Testing textbox, bippity!}
\end{center}
\end{mybox}

\end{document}

在此处输入图片描述

相关内容