我想画一条(非常粗的)线,一端逐渐褪色。我的想法是画一条线,将其延伸,然后path fading
仅应用于延伸的部分。对于较短的延伸,褪色似乎不完整(参见 MWE 中的红色和蓝色框)。对于较长的延伸,似乎覆盖了整个渐变范围。
有没有办法可以覆盖短路径的完整梯度范围?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}[ultra thick, black]
\draw (0,0) coordinate (ori) -- ++(1cm,0) coordinate (GradStart);
\draw[path fading=east] (GradStart) -- ++(0.3cm,0) coordinate (GradEnd);
\draw[red,very thin] (GradStart) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
\draw[blue,very thin] (GradEnd) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
\foreach \length in {0.5,1,3,5}{
\draw (ori) ++(0,-0.33) coordinate (ori) -- ++(1cm,0) coordinate (GradStart);
\draw[path fading=east] (GradStart) -- ++(\length,0) coordinate (GradEnd);
\draw[red,very thin] (GradStart) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
\draw[blue,very thin] (GradEnd) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
}
\end{tikzpicture}
\end{document}
答案1
比较前两行(一行带有line cap=butt
,另一行带有line cap=rect
)。两个边界框相同(边界框是 所用的区域path fading
)。
使用line cap=rect
和shorten >
不是shorten <
一个解决方案(第三行)。
一种可能的解决方案是使用明确的简化(第四行 = 绿色)。
\documentclass[tikz]{standalone}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
\draw[orange,line width=.1pt] (0,0) -- (2mm,0);
\draw[red,line width=1mm,path fading=east,line cap=butt] (0,0) -- (2mm,0);
\draw[red,line width=1mm,path fading=east,line cap=rect] (0,1mm) -- (2mm,1mm);
\draw[red,line width=1mm,path fading=east,line cap=rect,shorten <=.5mm,shorten >=.5mm]
(0,2mm) -- (2mm,2mm);
\draw[green,path fading=east,line width=1mm,line cap=rect]
(0+.5\pgflinewidth,3mm) -- (2mm-.5\pgflinewidth,3mm);
\end{tikzpicture}
\end{document}
答案2
根据 John Kormylo 的提示,我更改了代码,将线条变成了矩形:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}[ultra thick, black]
\fill[black] (0,0) coordinate (ori) ++(0,-0.5\pgflinewidth) rectangle ++(1cm,\pgflinewidth) ++(0,-0.5\pgflinewidth) coordinate (GradStart);
\fill[black,path fading=east] (GradStart) ++(0,-0.5\pgflinewidth) rectangle ++(0.3cm,\pgflinewidth) ++(0,-0.5\pgflinewidth) coordinate (GradEnd);
\draw[red,very thin] (GradStart) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
\draw[blue,very thin] (GradEnd) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
\foreach \length in {0.5,1,3,5}{
\fill[black] (ori) ++(0,-0.33) coordinate (ori) ++(0,-0.5\pgflinewidth) rectangle ++(1cm,\pgflinewidth) ++(0,-0.5\pgflinewidth) coordinate (GradStart);
\fill[black,path fading=east] (GradStart) ++(0,-0.5\pgflinewidth) rectangle ++(\length,\pgflinewidth) ++(0,-0.5\pgflinewidth) coordinate (GradEnd);
\draw[red,very thin] (GradStart) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
\draw[blue,very thin] (GradEnd) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
}
\end{tikzpicture}
\end{document}
使用此代码,path fading
确实也适用于短“行”:
显然,使用线条会更直接。因此,我仍然想知道为什么path fading
短线无法正常工作以及是否可以使其正常工作。
有任何想法吗?