6

Original question title - "Fade from specific point using fadings TikZ"

How can I fade out from a specific point on an object? I want to apply two fadings to this red square. Consider this MWE:

enter image description here

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
\fill [red] (0,0) rectangle (5,5);
\draw [black] (4,0)--(4,5);
\draw [black] (1,0)--(1,5);
\draw [->,line width=1mm] (4,-0.3)--(5,-0.3);
\draw [->,line width=1mm] (1,-0.3)--(0,-0.3);
\end{tikzpicture}
\end{document}

I want to fade out to 100% transparency, but only starting at the black lines and then out to left and right edges (as indicated by the arrows).

For this simple example, I can achieve the effect I'm looking for by re-drawing this diagram as three separate rectangles. Keeping the centre rectangle solid red, and applying path fading=west and path fading=east to the left and right rectangles respectively. But this doesn't seem very elegant. And is not practical for a more complicated shape I am working on.

What I want to achieve

enter image description here

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
\fill [red,path fading=west] (0,0) rectangle (1,5);
\fill [red] (1,0) rectangle (4,5);
\fill [red,path fading=east] (4,0) rectangle (5,5);
\draw [black] (4,0)--(4,5);
\draw [black] (1,0)--(1,5);
\draw [->,line width=1mm] (4,-0.3)--(5,-0.3);
\draw [->,line width=1mm] (1,-0.3)--(0,-0.3);
\end{tikzpicture}
\end{document}

Is there a better way of doing this?

UPDATE The reason the 'splitting up' method doesn't work for me is because I have more complicated elements of my tikzpicture. Consider having an image overlaid on the red square. How can the fading also apply to it?

enter image description here

Perhaps the fadings package is not what I'm after...?

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
\fill [red] (0,0) rectangle (5,5);
\node at (2.5,2.5) (Image)  {\includegraphics[width=5cm]{example-image.pdf}};
\draw [black] (4,0)--(4,5);
\draw [black] (1,0)--(1,5);
\draw [->,line width=1mm] (4,-0.3)--(5,-0.3);
\draw [->,line width=1mm] (1,-0.3)--(0,-0.3);
\end{tikzpicture}
\end{document}
1
  • 1
    The title of this question doesn't reflect its content very well, as far as I can tell. Either that or I, too, have misunderstood what you're asking.
    – cfr
    Commented May 23, 2018 at 3:27

2 Answers 2

2

One can get some partial answer using the shadows.blur package and modifying it a bit. I am using this because it already comes with the facilities to draw contours around a given shape in different steps. What one has to do is to adjust the shadow color. This has been asked here, but unfortunately I was unable to make the accepted answer work for this purpose as, according to what I find, it mocks up the steps (but I might well be doing something stupid). Yet it is not at all difficult to re-adjust the color in a TikZy way. I illustrate this by fading out a cloud because that is a fairly complicated shape, and I like the idea of making a cloud more cloudy, yet this trick can be used for general shapes.

\documentclass[tikz,border=10mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,calc,shadows.blur,shapes}
% \tikzset{ % from https://tex.stackexchange.com/a/328561/121799
%     render blur shadow/.prefix code={
%       \colorlet{black}{orange}
%     }
%   } % does not work, unfortunately
\makeatletter
\tikzset{ % based on https://tex.stackexchange.com/q/328433/121799
/tikz/render blur shadow/.code={
    \pgfbs@savebb
    \pgfsyssoftpath@getcurrentpath{\pgfbs@input@path}%
    \pgfbs@compute@shadow@bbox
    \pgfbs@process@rounding{\pgfbs@input@path}{\pgfbs@fadepath}%
    \pgfbs@apply@canvas@transform
    \colorlet{pstb@shadow@color}{white!\pgfbs@opacity!\my@shadow@color}%
    \pgfdeclarefading{shadowfading}{\pgfbs@paint@fading}%
    \pgfsetfillcolor{\my@shadow@color}%
    \pgfsetfading{shadowfading}%
       {\pgftransformshift{\pgfpoint{\pgfbs@midx}{\pgfbs@midy}}}%
    \pgfbs@usebbox{fill}%
    \pgfbs@restorebb
  },}
\tikzset{
  /tikz/shadow color/.store in=\my@shadow@color,
  /tikz/shadow color=gray,
}
\makeatother

\tikzset{
  cloudy/.style={cloud,cloud puffs=10,cloud puff arc=120, aspect=2}
}
\begin{document}
  \begin{tikzpicture}[every shadow/.style={shadow blur steps=20,
  shadow scale=1.2,shadow xshift=0pt,shadow yshift=0pt,shadow opacity=100}]
    \node[cloudy,fill=orange,blur shadow={shadow color=orange}] at (0,0) {I'm a cloud};

    \node[fill=red,blur shadow={shadow color=red,shadow scale=1.25},minimum width=4cm,minimum
    height=4cm] at (0,6){};
  \end{tikzpicture}
\end{document}

enter image description here

EDIT: Answer to edited question: Very simple, just add a white fading rectangle on top.

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
\fill [red] (0,0) rectangle (5,5);
\node at (2.5,2.5) (Image)  {\includegraphics[width=5cm]{example-image.pdf}};
\fill [white,path fading=east] (0,0) rectangle (1,5);
\fill [white,path fading=west] (4,0) rectangle (5,5);
\draw [black] (4,0)--(4,5);
\draw [black] (1,0)--(1,5);
\draw [->,line width=1mm] (4,-0.3)--(5,-0.3);
\draw [->,line width=1mm] (1,-0.3)--(0,-0.3);
\end{tikzpicture}
\end{document}

enter image description here

2
  • 1
    @Myles Well, you can always use clip, right? Just add \clip (-3,3.7) rectangle (3,8.3); before the red node.
    – user121799
    Commented May 23, 2018 at 0:39
  • I've added an update to my question. It was perhaps not clear what I was after. It was not exactly a more complex shape, but a diagram with more elements to it. I want the fade to apply to everything in the diagram.
    – Milo
    Commented May 23, 2018 at 1:02
5

You need to use a scope fading if you want to fade a node e.g. to include an external image. At least, so says the TikZ manual. Since your fading is not just one of the standard ones, but a bit more complex, you might want to use a picture as the basis for the fading and then apply the fading as a scope fading.

For example,

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}
\begin{tikzfadingfrompicture}[name=my fading]
  \fill [transparent!0] (-2,-2) rectangle (2,2);
  \shade [left color=transparent!100, right color=transparent!0] (-3,-2) rectangle (-2,2);
  \shade [left color=transparent!0, right color=transparent!100] (2,-2) rectangle (3,2);
\end{tikzfadingfrompicture}
\begin{document}
\begin{tikzpicture}
  \begin{scope}
    \path [scope fading=my fading, fit fading=false] (-3,-2) rectangle (3,2);
    \fill [color=blue] (-3,-2) rectangle (3,2);
    \node {\includegraphics{example-image}};
  \end{scope}
\end{tikzpicture}
\end{document}

faded node and stuff

2
  • Excellent this does work, I did not find in the manual that I have, what I want to know is if it must necessarily be centered at the origin, since in my code I had to do it because it did not work for me.
    – J Leon V.
    Commented May 23, 2018 at 4:42
  • @JLeonV. I'm not sure exactly what you mean. You can always shift the fading, just as you can rotate it. But, since you're doing it with a picture anyway, I guess you wouldn't need to do that. (Fadings from pictures never work quite as intuitively as the out-of-the-box ones - at least, not with my intuitions!)
    – cfr
    Commented May 23, 2018 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .