# pgflayer: background and foreground and tikz: pattern for water

I am attempting to draw a barrel floating in water. I am trying to use pgfonlayer so that when the water line intersects the barrel, the line will be removed.

Also, I looked through small section in the manual on patterns, but I couldn't find a suitable pattern for water.

Here is the image I am trying to re-create:

Here is the code I have and the image:

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,foreground}

\begin{document}
\begin{tikzpicture}
\begin{pgfonlayer}{foreground}
% draw a barrel
coordinate (B) at (-1.5cm, 0) coordinate (A) at (1.5cm, 0);
start angle = 180, end angle = 360];
start angle = 180, end angle = 360];
start angle = 180, end angle = 360];
start angle = 180, end angle = 360];
\draw (-1.5cm, -3cm) coordinate (C) arc[x radius = 1.5cm, y radius = 0.5cm,
start angle = 180, end angle = 360] coordinate (D);
\draw (A) -- (D);
\draw (B) -- (C);

% draw the direction of the W = mg
\fill[black] (0, -1.85cm) coordinate (W) circle[radius = 0.025cm];
\draw[-stealth] (W) -- ++(0, -0.35cm) node[right, font = \scriptsize] {$mg$};

% draw the barrel dimensions
\begin{scope}[>=stealth]
\draw[|<->|] (-1.5cm, 1cm) -- ++(3cm, 0) node[pos = 0.5,
fill = white, font = \scriptsize, inner sep = 0.05cm] {$d$};
\draw[|<->|] (-2cm, 0cm) -- ++(0, -3.5cm) node[pos = 0.5,
fill = white, font = \scriptsize, inner sep = 0.05cm] {$\ell$};
\end{scope}
\end{pgfonlayer}

% draw the water
\begin{pgfonlayer}{background}
\draw (-2.5cm, -1.5cm) -- ++(7cm, 0);
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


I have re-drawn the cylinder and filled it with white so that whatever is drawn on background layer, is not visible. Further, by manipulating the dash pattern and using \foreach loop with some xshift, the desired effect can be achieved.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,foreground}

\begin{document}
\begin{tikzpicture}
\begin{pgfonlayer}{foreground}
% draw a barrel
\draw[fill=white] (-1.5cm, 0) -- (-1.5cm, -3cm) coordinate (C) arc[x radius = 1.5cm, y radius = 0.5cm,
start angle = 180, end angle = 360] coordinate (D) -- (1.5cm, 0) --(-1.5cm, 0) -- cycle;
%    \draw[red] (A) -- (D);
%    \draw[red] (B) -- (C);
coordinate (B) at (-1.5cm, 0) coordinate (A) at (1.5cm, 0);
start angle = 180, end angle = 360];
start angle = 180, end angle = 360];
start angle = 180, end angle = 360];
start angle = 180, end angle = 360];

% draw the direction of the W = mg
\fill[black] (0, -1.85cm) coordinate (W) circle[radius = 0.025cm];
\draw[-stealth] (W) -- ++(0, -0.35cm) node[right, font = \scriptsize] {$mg$};

% draw the barrel dimensions
\begin{scope}[>=stealth]
\draw[|<->|] (-1.5cm, 1cm) -- ++(3cm, 0) node[pos = 0.5,
fill = white, font = \scriptsize, inner sep = 0.05cm] {$d$};
\draw[|<->|] (-2cm, 0cm) -- ++(0, -3.5cm) node[pos = 0.5,
fill = white, font = \scriptsize, inner sep = 0.05cm] {$\ell$};
\end{scope}
\end{pgfonlayer}

% draw the water
\begin{pgfonlayer}{background}
\draw[dash pattern=on 20pt off 10pt] (-2.5cm, -1.5cm) -- ++(6cm, 0);
\foreach \x in {-1.7,-1.9,...,-4.1}{
\draw[gray!30,dash pattern=on 4pt off 4pt] ([xshift=4pt]-2.5cm, \x cm) -- ++([xshift=-4pt]6cm, 0);
}
\foreach \x in {-1.6,-1.8,...,-4}{
\draw[gray!30,dash pattern=on 4pt off 4pt] (-2.5cm, \x cm) -- ++(6cm, 0);
}
\end{pgfonlayer}
\end{tikzpicture}
\end{document}