我需要在图像上绘制各种形状的蒙版。例如,我制作了一个圆形蒙版,如下所示:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\definecolor{background}{HTML}{2C414C}
\definecolor{foreground}{HTML}{FFFFFF}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt, draw=foreground, fill=foreground]
\node (image) {\includegraphics{example-image}};
\fill [red, opacity=0.5,even odd rule]
(image.north west) rectangle (image.south east) % Cover up everything
(0,1) circle [radius=50pt]; % Punch a hole
\end{tikzpicture}
\end{document}
如何使用更复杂的形状代替圆形?具体来说,我需要一个心形。我尝试使用这个答案,但它要么导致错误,要么根本不出现:
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt, draw=foreground, fill=foreground]
\node (image) {\includegraphics{example-image}};
\fill [red, opacity=0.5,even odd rule]
(image.north west) rectangle (image.south east)
(0,1) draw .. controls (0,0.75) and (-1.5,1.00) .. (-1.5,2) arc (180:0:0.75) -- cycle;
(0,1) draw .. controls (0,0.75) and ( 1.5,1.00) .. ( 1.5,2) arc (0:180:0.75) -- cycle;
\end{tikzpicture}
错误:! Use of \@next doesn't match its definition.
答案1
仅用一条路径绘制心形:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\definecolor{background}{HTML}{2C414C}
\definecolor{foreground}{HTML}{FFFFFF}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt, draw=foreground, fill=foreground]
\node (image) {\includegraphics{example-image}};
\fill [red, opacity=0.5,even odd rule]
(image.north west) rectangle (image.south east)
(0,1) .. controls (0,1.75) and (-1.5,2.00) .. (-1.5,3) arc (180:0:0.75) arc (180:0:0.75)
.. controls ( 1.5,2.00) and (0,1.75) .. (0,1) --cycle;
\end{tikzpicture}
\end{document}
使用哈特定义中的相对坐标,可以更轻松地将其放置在图像上的任何位置:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\definecolor{background}{HTML}{2C414C}
\definecolor{foreground}{HTML}{FFFFFF}
\usetikzlibrary{positioning,fit}
\newcommand{\hart}{.. controls +(0,.75) and +(0,-1.00) .. ++(-1.5,2) arc (180:0:0.75) arc (180:0:0.75)
.. controls +(0,-1.00) and +(0,.75) .. cycle}
\begin{document}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt, draw=foreground, fill=foreground]
\node (image) {\includegraphics{example-image}};
\fill [red, opacity=0.5,even odd rule]
(image.north west) rectangle (image.south east)
{[rotate=90, scale=.5](image.center)\hart}
{[scale=1.5](image.center)\hart}
{[rotate=-50]([shift={(1,2)}]image.south west)\hart};
\end{tikzpicture}
\end{document}
答案2
看来,两次都draw
太多;
了。
\documentclass[border=10pt, tikz]{standalone}
\usepackage{tikz}
\definecolor{background}{HTML}{2C414C}
\definecolor{foreground}{HTML}{FFFFFF}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt, draw=foreground, fill=foreground]
\node (image) {\includegraphics{example-image}};
\fill [red, opacity=0.5,even odd rule]
(image.north west) rectangle (image.south east) % Cover up everything
(0,1) circle [radius=50pt]; % Punch a hole
\end{tikzpicture}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt, draw=foreground, fill=foreground]
\node (image) {\includegraphics{example-image}};
\fill [red, opacity=0.5,even odd rule,]
(image.north west) rectangle (image.south east)
(0,1) .. controls (0,0.75) and (-1.5,1.00) .. (-1.5,2) arc (180:0:0.75) -- cycle
(0,1) .. controls (0,0.75) and ( 1.5,1.00) .. ( 1.5,2) arc (0:180:0.75) -- cycle;
\end{tikzpicture}
\end{document}