此代码:
\documentclass[tikz,border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[scale=4]
\begin{scope}[on background layer]
\draw[fill, top color=black, bottom color=white, shading=axis, shading angle=-33.02]
(1,1) -- (-1,1) -- (-1,0.8) -- (1,-0.5) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}
产生具有与对角线正交的颜色渐变的以下多边形。
我有两个问题:
1) 渐变应始终与对角线正交。如何自动计算直角?还有其他方法可以定义渐变/阴影的方向吗?
2) 阴影实际上并非从白色变为黑色。我希望阴影在对角线处为白色,在右上角为黑色。正确的做法是什么?
答案1
仅用于比较。运行xelatex
:
\documentclass[pstricks,border=1mm]{standalone}
\usepackage{pst-grad}
\begin{document}
\psset{unit=4}
\begin{pspicture}(-1,-0.5)(1,1)
\psclip{\pspolygon[linestyle=none](1,1)(-1,1)(-1,0.8)(1,-0.5)}
\pspolygon[linestyle=none,fillstyle=gradient,gradbegin=black,
gradangle=-33,gradend=white,gradmidpoint=0.47](-1.5,1)(1,1)(1,-0.5)
\endpsclip
\pspolygon(1,1)(-1,1)(-1,0.8)(1,-0.5)
\end{pspicture}
\end{document}
答案2
您绝对不必对阴影角度进行硬编码。此答案带有一种可以用作的样式
shading angle from={line from (-1,0.8) to (1,-0.5)}
然后插入适当的阴影角度
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{backgrounds,calc}
\begin{document}
\begin{tikzpicture}[scale=4,shading angle from/.style args={line from #1 to #2}{
insert path={let \p1=($#2-#1$),\n1={atan2(\y1,\x1)} in},
shading angle=\n1}]
\begin{scope}[on background layer]
\draw[fill, top color=black, bottom color=white,
shading angle from={line from (-1,0.8) to (1,-0.5)}]
(1,1) -- (-1,1) -- (-1,0.8) -- (1,-0.5) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}