指定 tikz 中著色時的傾斜量

指定 tikz 中著色時的傾斜量

考慮以下範例:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,calc,shapes,arrows,shapes.geometric,patterns,shadows,arrows.meta,fadings}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage[T1]{fontenc}
\usepackage{calc}
\definecolor{sea}{HTML}{80A0C0}

\makeatletter
\tikzset{vertical custom shading/.code={%
 \pgfmathsetmacro\tikz@vcs@middle{#1}
 \pgfmathsetmacro\tikz@vcs@bottom{\tikz@vcs@middle/2}
 \pgfmathsetmacro\tikz@vcs@top{(100-\tikz@vcs@middle)/2+\tikz@vcs@middle}
\pgfdeclareverticalshading[tikz@axis@top,tikz@axis@middle,tikz@axis@bottom]{newaxis}{100bp}{%
  color(0bp)=(tikz@axis@bottom);
  color(\tikz@vcs@bottom bp)=(tikz@axis@bottom);
  color(\tikz@vcs@middle bp)=(tikz@axis@middle);
  color(\tikz@vcs@top bp)=(tikz@axis@top);
  color(100bp)=(tikz@axis@top)}
  \pgfkeysalso{/tikz/shading=newaxis}
  }
}
\makeatother  

\begin{document}
\begin{tikzpicture}

\begin{scope}
% region to clip
\coordinate(clipping area) at (8, 15);
\clip (2,5) rectangle (clipping area);
% draw water
\fill [decoration={snake, segment length=1cm, amplitude=0.125cm}, decorate, top color = red, bottom color = sea, middle color = sea, vertical custom shading = 65] (0,0) rectangle (10,10);
\end{scope}

\begin{scope}[xshift = 200]
% region to clip
\coordinate(clipping area) at (8, 15);
\clip (2,5) rectangle (clipping area);
% draw water
\fill [decoration={snake, segment length=1cm, amplitude=0.125cm}, decorate, top color = red, bottom color = sea, middle color = sea, vertical custom shading = 65] (0,0) rectangle (10,10);
\end{scope}

\end{tikzpicture}
\end{document}

結果是:

在此輸入影像描述

我想更改第二個面板中陰影的位置,以便藍色區域延伸到一側的頂部,而等量的紅色區域在另一側向相反方向移動。我試著透過註釋圖表來說明這一點:

在此輸入影像描述

即紅色區域在一側上升,在另一側下降相同的量。我怎樣才能達到這種效果?

答案1

我會以不同的方式處理這個問題:我不會剪切一個大的陰影矩形,而是使用單個路徑並僅裝飾該路徑的一段,就像tikz 在兩行之間套用填充。這樣,您可以使用“正常”淡入淡出,而不是範例中的自訂淡入淡出。

為了獲得一端沒有直線段的漂亮波形圖案,我使用TikZ 更好的波浪線

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,calc,shapes,arrows,shapes.geometric,patterns,shadows,arrows.meta,fadings}
\definecolor{sea}{HTML}{80A0C0}

\pgfdeclaredecoration{complete sines}{initial}
{
    \state{initial}[
        width=+0pt,
        next state=sine,
        persistent precomputation={\pgfmathsetmacro\matchinglength{
            \pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
            \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
        }] {}
    \state{sine}[width=\pgfdecorationsegmentlength]{
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
    \state{final}{}
} 

\begin{document}
\begin{tikzpicture}

\begin{scope}
\fill [decoration={complete sines, segment length=1cm, amplitude=0.125cm}, top color = red, bottom color = sea, middle color = sea] decorate {(0,0) -- (10,0)} |- (0,-2) -- cycle;
\end{scope}

\begin{scope}[xshift=11cm]
\fill [decoration={complete sines, segment length=1cm, amplitude=0.125cm}, top color = red, bottom color = sea, middle color = sea, shading angle=-20] decorate {(0,0) -- (10,0)} |- (0,-2) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}

相關內容