我尝试学习
然后在这个 MWE 中创建我自己的衰落方案:
\documentclass[tikz]{standalone}
\usetikzlibrary{fadings}
\pgfdeclareradialshading{test}{\pgfpoint{0cm}{0cm}}%
{color(0cm)=(transparent!0);
color(0.7cm)=(transparent!0);
color(0.74cm)=(transparent!50);
color(0.82cm)=(transparent!70);
color(0.9cm)=(transparent!100)}
\begin{document}
\begin{tikzpicture}
\tikzfading[name=fade original,inner color=transparent!0,outer color=transparent!100]
\tikzfading[name=test]
\fill[blue] (0,0) rectangle (3,3);
\node[rectangle,fill=white,path fading=fade original,align=center,rounded corners] at (1.5,2) {\small this is text};
\node[rectangle,fill=white,path fading=test,align=center,rounded corners] at (1.5,1) {\small this is text};
\end{tikzpicture}
\end{document}
您可以在上半部分看到节点的标准径向渐变。下半部分有某种阴影,但肯定不是我想要实现的渐变。我正在寻找矩形版本的径向渐变。与在径向情况下仅在“is”字符后面使用中心纯白色不同,我试图在大多数文本后面实现纯白色,然后在节点边缘附近快速淡出为透明。在径向情况下,这种渐变以圆形/椭圆形图案出现(正如您对径向一词的期望)。我更希望渐变以矩形图案出现,例如path fading
北、南、东和西的组合transparent!0
,中间是纯白色()。希望这能在很大程度上保持我的矩形节点的形状,并且它会在边缘周围淡入背景。
如果我无法实现这一点,我就可以使用径向淡入淡出,如果我可以从文本外部开始淡入淡出并且淡入transparent!100
得更突然,但矩形版本是理想的。
答案1
这里有四种可能。没有一种是完美的。前两种方法类似,用于tikzfadingfrompicture
绘制矩形,其大小随着不透明度的增加而减小。唯一的区别是矩形不再完全不透明的点。在第一种情况下,中心不透明度区域比第二种情况大。
\begin{tikzfadingfrompicture}[name=rectangle fade]
\foreach \i [evaluate=\i as \k using {1.5-\i/100} ] in {0,1,...,100} \path [fill=white!\i!black] (-\k,-\k) rectangle (\k,\k);
\end{tikzfadingfrompicture}
\begin{tikzfadingfrompicture}[name=rectangle fading]
\foreach \i [evaluate=\i as \k using {1.25-\i/100} ] in {0,1,...,100} \path [fill=white!\i!black] (-\k,-\k) rectangle (\k,\k);
\end{tikzfadingfrompicture}
后两个使用\pgfdeclareshading
。我认为这没有什么实际区别,tikzfadingfrompicture
据我所知,您可以使用相同的效果,但我碰巧是这样做的。在这种情况下,想法是在定义阴影时对在正方形中心相交的四个三角形进行阴影处理。因此,顶部三角形在底部顶点不透明,顶部底边透明;底部三角形在顶部顶点不透明,底部底边透明;左边三角形在右边顶点不透明,左边底边透明;右边三角形在左边顶点不透明,右边底边透明。
据我所知,这样做效率更高,因为它只需要着色 4 个三角形,而不是填充 101 条路径。但是,渲染结果时会出现伪影。这对于第一个示例尤其成问题,但在两个示例中都可见。这可能取决于 PDF 查看器。
两者的区别在于,第二种方法将中间颜色指定为不透明,顶点的颜色也是如此,因此在开始着色之前有一个中心不透明区域。这也意味着可以在对三角形进行着色后,通过明确使用不透明颜色绘制中心区域的对角线来部分补偿伪影。然而,伪影仍然可见于该中心区域之外的对角线上。
\pgfdeclarefading{rectangular fading}{
\tikz {
\shade [bottom color=pgftransparent!100, top color=pgftransparent!0] (0,0) -- (1,-1) -- (-1,-1) -- cycle;
\shade [bottom color=pgftransparent!0, top color=pgftransparent!100] (0,0) -- (-1,1) -- (1,1) -- cycle;
\shade [right color=pgftransparent!0, left color=pgftransparent!100] (0,0) -- (-1,1) -- (-1,-1) -- cycle;
\shade [right color=pgftransparent!100, left color=pgftransparent!0] (0,0) -- (1,1) -- (1,-1) -- cycle;
}
}
\pgfdeclarefading{rectangular fade}{
\tikz {
\shade [bottom color=pgftransparent!100, top color=pgftransparent!0, middle color=pgftransparent!0] (0,0) -- (1,-1) -- (-1,-1) -- cycle;
\shade [bottom color=pgftransparent!0, top color=pgftransparent!100, middle color=pgftransparent!0] (0,0) -- (-1,1) -- (1,1) -- cycle;
\shade [right color=pgftransparent!0, left color=pgftransparent!100, middle color=pgftransparent!0] (0,0) -- (-1,1) -- (-1,-1) -- cycle;
\shade [right color=pgftransparent!100, left color=pgftransparent!0, middle color=pgftransparent!0] (0,0) -- (1,1) -- (1,-1) -- cycle;
\draw [pgftransparent!0] (-.5,.5) -- (.5,-.5) (-.5,-.5) -- (.5,.5);
}
}
以下是 4 种淡入淡出的演示:
完整代码:
\documentclass[tikz,border=5pt,multi]{standalone}
\usetikzlibrary{fadings}
\begin{tikzfadingfrompicture}[name=rectangle fade]
\foreach \i [evaluate=\i as \k using {1.5-\i/100} ] in {0,1,...,100} \path [fill=white!\i!black] (-\k,-\k) rectangle (\k,\k);
\end{tikzfadingfrompicture}
\begin{tikzfadingfrompicture}[name=rectangle fading]
\foreach \i [evaluate=\i as \k using {1.25-\i/100} ] in {0,1,...,100} \path [fill=white!\i!black] (-\k,-\k) rectangle (\k,\k);
\end{tikzfadingfrompicture}
\pgfdeclarefading{rectangular fading}{
\tikz {
\shade [bottom color=pgftransparent!100, top color=pgftransparent!0] (0,0) -- (1,-1) -- (-1,-1) -- cycle;
\shade [bottom color=pgftransparent!0, top color=pgftransparent!100] (0,0) -- (-1,1) -- (1,1) -- cycle;
\shade [right color=pgftransparent!0, left color=pgftransparent!100] (0,0) -- (-1,1) -- (-1,-1) -- cycle;
\shade [right color=pgftransparent!100, left color=pgftransparent!0] (0,0) -- (1,1) -- (1,-1) -- cycle;
}
}
\pgfdeclarefading{rectangular fade}{
\tikz {
\shade [bottom color=pgftransparent!100, top color=pgftransparent!0, middle color=pgftransparent!0] (0,0) -- (1,-1) -- (-1,-1) -- cycle;
\shade [bottom color=pgftransparent!0, top color=pgftransparent!100, middle color=pgftransparent!0] (0,0) -- (-1,1) -- (1,1) -- cycle;
\shade [right color=pgftransparent!0, left color=pgftransparent!100, middle color=pgftransparent!0] (0,0) -- (-1,1) -- (-1,-1) -- cycle;
\shade [right color=pgftransparent!100, left color=pgftransparent!0, middle color=pgftransparent!0] (0,0) -- (1,1) -- (1,-1) -- cycle;
\draw [pgftransparent!0] (-.5,.5) -- (.5,-.5) (-.5,-.5) -- (.5,.5);
}
}
\begin{document}
\begin{tikzpicture}
\fill[blue] (-1.5,-1.5) rectangle (1.5,1.5);
\node [align=center, rounded corners, fill=white, path fading=rectangle fade] at (0,1) {\small this is text};
\node [align=center, rounded corners, fill=white, path fading=rectangle fading] at (0,.33) {\small this is text};
\node [align=center, rounded corners, fill=white, path fading=rectangular fading] at (0,-.33) {\small this is text};
\node [align=center, rounded corners, fill=white, path fading=rectangular fade] at (0,-1) {\small this is text};
\end{tikzpicture}
\end{document}
答案2
抱歉,我知道在回答问题之前我真的应该通读指南 - 尤其是这么老的问题,但我自己也遇到过同样的问题,最后让你的代码按照我认为你想要的方式工作(嗯,你的第二偏好,而不是你的第一偏好)。
看起来你只是漏掉了 percusse 的回答中的一行TikZ:径向阴影您提到的戒指问题:
\pgfdeclarefading{test}{\pgfuseshading{test}}
这样,你的初始 MWE 就变成:
\documentclass[tikz]{standalone}
\usetikzlibrary{fadings}
\pgfdeclareradialshading{test}{\pgfpoint{0cm}{0cm}}%
{color(0cm)=(transparent!0);
color(0.7cm)=(transparent!0);
color(0.74cm)=(transparent!50);
color(0.82cm)=(transparent!70);
color(0.9cm)=(transparent!100)}
\pgfdeclarefading{test}{\pgfuseshading{test}}
\begin{document}
\begin{tikzpicture}
\tikzfading[name=fade original,inner color=transparent!0,outer color=transparent!100]
% \tikzfading[name=test]
\fill[blue] (0,0) rectangle (3,3);
\node[rectangle,fill=white,path fading=fade original,align=center,rounded corners] at (1.5,2) {\small this is text};
\node[rectangle,fill=white,path fading=test,align=center,rounded corners] at (1.5,1) {\small this is text};
\end{tikzpicture}
\end{document}
编辑: 对于圆边变化来说,这绝对不是完美的,但基于 cfr 使用三角形的想法,我尝试使用从中心向外构建的四个矩形,这些矩形具有如使用径向渐变填充正方形问题。
已经到了这个地步:
\documentclass[tikz]{standalone}
\usetikzlibrary{fadings}
\newcommand{\fillradialgradient}{color(0cm)=(transparent!0);%
color(0.7cm)=(transparent!0);%
color(0.74cm)=(transparent!50);%
color(0.82cm)=(transparent!70);%
color(0.9cm)=(transparent!100)}%
\pgfdeclareradialshading{test}{\pgfpoint{0cm}{0cm}}{\fillradialgradient}
\pgfdeclarefading{test-radial}{\pgfuseshading{test}}
\newcommand{\fillroundedgradient}{color(0bp)=(transparent!0);%
color(30bp)=(transparent!0);%
color(36bp)=(transparent!100);
color(50bp)=(transparent!100)}%
\pgfdeclareradialshading{sw-radial}{\pgfqpoint{-50bp}{-50bp}}{\fillroundedgradient}
\pgfdeclareradialshading{se-radial}{\pgfqpoint{50bp}{-50bp}}{\fillroundedgradient}
\pgfdeclareradialshading{nw-radial}{\pgfqpoint{-50bp}{50bp}}{\fillroundedgradient}
\pgfdeclareradialshading{ne-radial}{\pgfqpoint{50bp}{50bp}}{\fillroundedgradient}
\pgfdeclarefading{test-rounded}{
\tikz {
\begin{scope}[transparency group]
\shade [inner color=transparent!0,outer color=transparent!100,shading=sw-radial,even odd rule] (0,1) -- (1,1) -- (1,0) -- (0,0) -- cycle;
\shade [inner color=transparent!0,outer color=transparent!100,shading=se-radial,even odd rule] (-1,1) -- (0,1) -- (0,0) -- (-1,0) -- cycle;
\shade [inner color=transparent!0,outer color=transparent!100,shading=nw-radial,even odd rule] (0,-1) -- (1,-1) -- (1,0) -- (0,0) -- cycle;
\shade [inner color=transparent!0,outer color=transparent!100,shading=ne-radial,even odd rule] (-1,-1) -- (0,-1) -- (0,0) -- (-1,0) -- cycle;
\end{scope}
}
}
\begin{document}
\begin{tikzpicture}
\tikzfading[name=fade original,inner color=transparent!0,outer color=transparent!100]
\fill[blue] (0,0) rectangle (3,3);
\node[rectangle,fill=white,path fading=test-radial,align=center] at (1.5,2) {\small this is text};
\node[rectangle,fill=white,path fading=test-rounded,align=center,rounded corners] at (1.5,1) {\small this is text};
\end{tikzpicture}
\end{document}
它仍然有瑕疵,但是有所不同,最明显的是在水平边缘而不是对角线处;)。
如果您使用 \fillroundedgradient 颜色定义,您会发现它仍然遵循圆角边缘(将第一个 transparent!100 颜色设置为大约 45bp,您会再次看到那些边缘),但我个人更喜欢能够很好地融入背景的东西。