tikz 剪辑圆角

tikz 剪辑圆角

如何应用此 Tikz 解决方案(TikZ 节点仅一侧有圆角)对剪辑有同样的效果吗?

我想使用具有不同圆角的矩形来剪辑东西(图像)。

结果仅被剪裁为矩形。没有圆角。

非常感谢!

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\makeatletter

\pgfkeys{/pgf/.cd,
  rectangle corner radius north west/.initial=10pt,
  rectangle corner radius north east/.initial=10pt,
  rectangle corner radius south west/.initial=10pt,
  rectangle corner radius south east/.initial=10pt
}
\newif\ifpgf@rectanglewrc@donecorner@
\def\pgf@rectanglewithroundedcorners@docorner#1#2#3#4#5{%
  \edef\pgf@marshal{%
    \noexpand\pgfintersectionofpaths
      {%
        \noexpand\pgfpathmoveto{\noexpand\pgfpoint{\the\pgf@xa}{\the\pgf@ya}}%
        \noexpand\pgfpathlineto{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
      }%
      {%
        \noexpand\pgfpathmoveto{\noexpand\pgfpointadd
          {\noexpand\pgfpoint{\the\pgf@xc}{\the\pgf@yc}}%
          {\noexpand\pgfpoint{#1}{#2}}}%
        \noexpand\pgfpatharc{#3}{#4}{#5}%
      }%
    }%
  \pgf@process{\pgf@marshal\pgfpointintersectionsolution{1}}%
  \pgf@process{\pgftransforminvert\pgfpointtransformed{}}%
  \pgf@rectanglewrc@donecorner@true
}
\pgfdeclareshape{rectangle with rounded corners}
{
  \inheritsavedanchors[from=rectangle] % this is nearly a rectangle
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}

  \savedmacro\cornerradiusnw{%
    \edef\cornerradiusnw{\pgfkeysvalueof{/pgf/rectangle corner radius north west}}%
  }
  \savedmacro\cornerradiusne{%
    \edef\cornerradiusne{\pgfkeysvalueof{/pgf/rectangle corner radius north east}}%
  }
  \savedmacro\cornerradiussw{%
    \edef\cornerradiussw{\pgfkeysvalueof{/pgf/rectangle corner radius south west}}%
  }
  \savedmacro\cornerradiusse{%
    \edef\cornerradiusse{\pgfkeysvalueof{/pgf/rectangle corner radius south east}}%
  }

  \backgroundpath{%
    \northeast\advance\pgf@y-\cornerradiusne\relax
    \pgfpathmoveto{}%
    \pgfpatharc{0}{90}{\cornerradiusne}%
    \northeast\pgf@ya=\pgf@y\southwest\advance\pgf@x\cornerradiusnw\relax\pgf@y=\pgf@ya
    \pgfpathlineto{}%
    \pgfpatharc{90}{180}{\cornerradiusnw}%
    \southwest\advance\pgf@y\cornerradiussw\relax
    \pgfpathlineto{}%
    \pgfpatharc{180}{270}{\cornerradiussw}%
    \northeast\pgf@xa=\pgf@x\advance\pgf@xa-\cornerradiussw\southwest\pgf@x=\pgf@xa
    \pgfpathlineto{}%
    \pgfpatharc{270}{360}{\cornerradiusse}%
    \northeast\advance\pgf@y-\cornerradiusse\relax
    \pgfpathlineto{}%

  }

  \anchor{before north east}{\northeast\advance\pgf@y-\cornerradiusne}
  \anchor{after north east}{\northeast\advance\pgf@x-\cornerradiusne}
  \anchor{before north west}{\southwest\pgf@xa=\pgf@x\advance\pgf@xa\cornerradiusnw
    \northeast\pgf@x=\pgf@xa}
  \anchor{after north west}{\northeast\pgf@ya=\pgf@y\advance\pgf@ya-\cornerradiusnw
    \southwest\pgf@y=\pgf@ya}
  \anchor{before south west}{\southwest\advance\pgf@y\cornerradiussw}
  \anchor{after south west}{\southwest\advance\pgf@x\cornerradiussw}
  \anchor{before south east}{\northeast\pgf@xa=\pgf@x\advance\pgf@xa-\cornerradiusse
    \southwest\pgf@x=\pgf@xa}
  \anchor{after south east}{\southwest\pgf@ya=\pgf@y\advance\pgf@ya\cornerradiusse
    \northeast\pgf@y=\pgf@ya}

  \anchorborder{%
    \pgf@xb=\pgf@x% xb/yb is target
    \pgf@yb=\pgf@y%
    \southwest%
    \pgf@xa=\pgf@x% xa/ya is se
    \pgf@ya=\pgf@y%
    \northeast%
    \advance\pgf@x by-\pgf@xa%
    \advance\pgf@y by-\pgf@ya%
    \pgf@xc=.5\pgf@x% x/y is half width/height
    \pgf@yc=.5\pgf@y%
    \advance\pgf@xa by\pgf@xc% xa/ya becomes center
    \advance\pgf@ya by\pgf@yc%
    \edef\pgf@marshal{%
      \noexpand\pgfpointborderrectangle
      {\noexpand\pgfqpoint{\the\pgf@xb}{\the\pgf@yb}}
      {\noexpand\pgfqpoint{\the\pgf@xc}{\the\pgf@yc}}%
    }%
    \pgf@process{\pgf@marshal}%
    \advance\pgf@x by\pgf@xa% 
    \advance\pgf@y by\pgf@ya%
    \pgfextract@process\borderpoint{}%
    %
    \pgf@rectanglewrc@donecorner@false
    %
    % do southwest corner
    \southwest\pgf@xc=\pgf@x\pgf@yc=\pgf@y
    \advance\pgf@xc\cornerradiussw\relax\advance\pgf@yc\cornerradiussw\relax 
    \borderpoint
    \ifdim\pgf@x<\pgf@xc\relax\ifdim\pgf@y<\pgf@yc\relax
      \pgf@rectanglewithroundedcorners@docorner{-\cornerradiussw}{0pt}{180}{270}{\cornerradiussw}%
    \fi\fi
    %
    % do southeast corner
    \ifpgf@rectanglewrc@donecorner@\else
      \southwest\pgf@yc=\pgf@y\relax\northeast\pgf@xc=\pgf@x\relax
      \advance\pgf@xc-\cornerradiusse\relax\advance\pgf@yc\cornerradiusse\relax
      \borderpoint
      \ifdim\pgf@x>\pgf@xc\relax\ifdim\pgf@y<\pgf@yc\relax
       \pgf@rectanglewithroundedcorners@docorner{0pt}{-\cornerradiusse}{270}{360}{\cornerradiusse}%
      \fi\fi
    \fi
    %
    % do northeast corner
    \ifpgf@rectanglewrc@donecorner@\else
      \northeast\pgf@xc=\pgf@x\relax\pgf@yc=\pgf@y\relax
      \advance\pgf@xc-\cornerradiusne\relax\advance\pgf@yc-\cornerradiusne\relax
      \borderpoint
      \ifdim\pgf@x>\pgf@xc\relax\ifdim\pgf@y>\pgf@yc\relax
       \pgf@rectanglewithroundedcorners@docorner{\cornerradiusne}{0pt}{0}{90}{\cornerradiusne}%
      \fi\fi
    \fi
    %
    % do northwest corner
    \ifpgf@rectanglewrc@donecorner@\else
      \northeast\pgf@yc=\pgf@y\relax\southwest\pgf@xc=\pgf@x\relax
      \advance\pgf@xc\cornerradiusnw\relax\advance\pgf@yc-\cornerradiusnw\relax
      \borderpoint
      \ifdim\pgf@x<\pgf@xc\relax\ifdim\pgf@y>\pgf@yc\relax
       \pgf@rectanglewithroundedcorners@docorner{0pt}{\cornerradiusnw}{90}{180}{\cornerradiusnw}%
      \fi\fi
    \fi
  }
}

\makeatother

\begin{tikzpicture}
\clip [rectangle with rounded corners, rectangle corner radius south west=none,rectangle corner radius south east=none,rectangle corner radius north west=10,rectangle corner radius north east=10] (0,0) rectangle coordinate (clipname) (100,100);
\node [text width=100, inner sep=0pt] (mainpicnode) at (clipname)
{this i want to be clipped (originally its an image here)};
\end{tikzpicture}

\end{document}

答案1

我认为你的方法太复杂了。你可以简单地使用路径来剪辑 TikZ 中的内容。我将通过一些示例来展示它,但你也可以在答案的末尾找到完整的代码。

  1. 如果您只想使用图像的某个部分,则将图像包含在 tikzpicture 中要剪辑的节点中,并在其前面绘制剪辑路径。绘制剪辑路径时,您可以在任何坐标前使用选项来更改其角落。(您可以简单地通过使用而不是来rounded corners省略绘制线条。)pathdraw

    \begin{tikzpicture}
        \draw[clip] (2cm, 0.5cm)
            [rounded corners=5pt] -- (2cm, 2.5cm)
            [rounded corners=10pt] -- (4cm, 2.5cm)
            [rounded corners=0.5cm] -- (4cm, 0.5cm)
            [sharp corners] -- cycle;
    
        \node[anchor=south west,%
            inner sep=0,%
            outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
    \end{tikzpicture}
    
  2. 如果要从图像中剪切出一部分,首先必须指定 tikzpicture 的宽度和高度,并确保它与包含的图像的宽度和高度相同。然后,​​当您绘制剪切路径时,首先将整个图像包含在其中,然后继续使用所需的剪切路径。您可以rounded corners在“边界路径”和剪切中使用该选项。

    \begin{tikzpicture}[x=6cm, y=3cm]
        \path[clip] [rounded corners=10pt] (0, 0) -- (1, 0) -- (1, 1) -- (0, 1) -- cycle
        (2cm, 0.5cm) [rounded corners=5pt] -- (2cm, 2.5cm)
        [rounded corners=10pt] -- (4cm, 2.5cm)
        [rounded corners=0.5cm] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;
    
        \node[anchor=south west,%
            inner sep=0,%
            outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
    \end{tikzpicture}
    

您可以在下面的完整代码中找到所示的示例和更多示例。(您可以下载我在示例中使用的图像这里

\documentclass[a4paper, 11pt]{article}

\usepackage[a4paper,
    tmargin=2cm,%
    rmargin=2cm,%
    bmargin=2cm,%
    lmargin=2cm,
    vscale=1,%
    hscale=1]{geometry}

\usepackage{tikz}

% only used for the example
\newlength{\skiplength}
\setlength{\skiplength}{1cm}
\setlength{\parskip}{\skiplength}
\setlength{\parindent}{0pt}

\begin{document}

\includegraphics[width=6cm, height=3cm]{clip.png}

\begin{tikzpicture}
    \draw (2cm, 0.5cm)
        [rounded corners=5pt] -- (3cm, 2.5cm)
        [rounded corners=20pt] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;
\end{tikzpicture}
\hspace{\skiplength}
\begin{tikzpicture}
    \draw[clip] (2cm, 0.5cm)
        [rounded corners=5pt] -- (3cm, 2.5cm)
        [rounded corners=20pt] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}
\hspace{\skiplength}
\begin{tikzpicture}
    \path[clip] (2cm, 0.5cm)
        [rounded corners=5pt] -- (3cm, 2.5cm)
        [rounded corners=20pt] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}
\hspace{\skiplength}
\begin{tikzpicture}[x=6cm, y=3cm]
    \draw[clip] (0, 0) -- (1, 0) -- (1, 1) -- (0, 1) -- cycle
        (2cm, 0.5cm) [rounded corners=5pt] -- (3cm, 2.5cm)
        [rounded corners=20pt] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}

\begin{tikzpicture}[x=6cm, y=3cm]
    \path[clip] (0, 0) -- (1, 0) -- (1, 1) -- (0, 1) -- cycle
        (2cm, 0.5cm) [rounded corners=5pt] -- (3cm, 2.5cm)
        [rounded corners=20pt] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}
\hspace{\skiplength}
\begin{tikzpicture}[x=6cm, y=3cm]
    \path[clip] (0, 0) -- (1, 0) [rounded corners=0.5cm] -- (1, 1) [sharp corners] -- (0, 1) [rounded corners=2cm] -- cycle
        (2cm, 0.5cm) [rounded corners=5pt] -- (3cm, 2.5cm)
        [rounded corners=20pt] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}

\begin{tikzpicture}
    \draw (2cm, 0.5cm)
        [rounded corners=5pt] -- (2cm, 2.5cm)
        [rounded corners=10pt] -- (4cm, 2.5cm)
        [rounded corners=0.5cm] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;
\end{tikzpicture}
\hspace{\skiplength}
\begin{tikzpicture}
    \draw[clip] (2cm, 0.5cm)
        [rounded corners=5pt] -- (2cm, 2.5cm)
        [rounded corners=10pt] -- (4cm, 2.5cm)
        [rounded corners=0.5cm] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}
\hspace{\skiplength}
\begin{tikzpicture}
    \path[clip] (2cm, 0.5cm)
        [rounded corners=5pt] -- (2cm, 2.5cm)
        [rounded corners=10pt] -- (4cm, 2.5cm)
        [rounded corners=0.5cm] -- (4cm, 0.5cm)
        [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}
\hspace{\skiplength}
\begin{tikzpicture}[x=6cm, y=3cm]
    \draw[clip] (0, 0) -- (1, 0) -- (1, 1) -- (0, 1) -- cycle
    (2cm, 0.5cm) [rounded corners=5pt] -- (2cm, 2.5cm)
    [rounded corners=10pt] -- (4cm, 2.5cm)
    [rounded corners=0.5cm] -- (4cm, 0.5cm)
    [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}

\begin{tikzpicture}[x=6cm, y=3cm]
    \path[clip] (0, 0) -- (1, 0) -- (1, 1) -- (0, 1) -- cycle
    (2cm, 0.5cm) [rounded corners=5pt] -- (2cm, 2.5cm)
    [rounded corners=10pt] -- (4cm, 2.5cm)
    [rounded corners=0.5cm] -- (4cm, 0.5cm)
    [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}
\hspace{\skiplength}
\begin{tikzpicture}[x=6cm, y=3cm]
    \path[clip] [rounded corners=10pt] (0, 0) -- (1, 0) -- (1, 1) -- (0, 1) -- cycle
    (2cm, 0.5cm) [rounded corners=5pt] -- (2cm, 2.5cm)
    [rounded corners=10pt] -- (4cm, 2.5cm)
    [rounded corners=0.5cm] -- (4cm, 0.5cm)
    [sharp corners] -- cycle;

    \node[anchor=south west,%
        inner sep=0,%
        outer sep=0pt] (image) at (0, 0) {\includegraphics[width=6cm, height=3cm]{clip.png}};
\end{tikzpicture}

\end{document}

相关内容