6

I would like to draw a (very thick) line that is faded on one end. My idea was to draw the line, extend it, and apply path fading only to the extended part. For short extensions the fading seems not to be complete (see red and blue boxes in the MWE). For longer extensions the complete gradient range seems to be covered.

Is there a way to cover the complete gradient range also for short paths?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{document}

    \begin{tikzpicture}[ultra thick, black]

            \draw (0,0) coordinate (ori) -- ++(1cm,0) coordinate (GradStart);
            \draw[path fading=east] (GradStart) -- ++(0.3cm,0) coordinate (GradEnd);
            \draw[red,very thin] (GradStart) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
            \draw[blue,very thin] (GradEnd) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);

            \foreach \length in {0.5,1,3,5}{                
                \draw (ori) ++(0,-0.33) coordinate (ori) -- ++(1cm,0) coordinate (GradStart);
                \draw[path fading=east] (GradStart) -- ++(\length,0) coordinate (GradEnd);
                \draw[red,very thin] (GradStart) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
                \draw[blue,very thin] (GradEnd) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
            }

    \end{tikzpicture}

\end{document}

enter image description here

  • did you try fit fading? – percusse Nov 4 '14 at 15:35
  • @ percusse: The pgfmanual says about fit fading: "When set to true [default], the fading is shifted and resized (in exactly the same way as a shading) so that it covers the current path." (version 3.00, page 346). In my example it seems that the resizing does not work properly for short paths. Setting fit fading=false makes the faded paths disappear in my MWE. – Robert Nov 4 '14 at 15:57
  • My mistake, I remembered wrongly the default value – percusse Nov 4 '14 at 18:15
  • 2
    You could turn your line into a rectangle. – John Kormylo Nov 5 '14 at 13:51
  • 1
    The bounding box of a line (with default line cap=butt) exceeds .5\pgflinewidth the real line at each extremity. – Paul Gaborit Nov 5 '14 at 15:48
3

Compare the first two lines (one with line cap=butt and other with line cap=rect). The two bounding boxes are the same (the bounding box is the area used by path fading).

Using line cap=rect with shorten > and shorten < is not a solution (third line).

A possible solution uses an explicit reduction (fourth line = green).

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
  \draw[orange,line width=.1pt] (0,0) -- (2mm,0);

  \draw[red,line width=1mm,path fading=east,line cap=butt] (0,0) -- (2mm,0);
  \draw[red,line width=1mm,path fading=east,line cap=rect] (0,1mm) -- (2mm,1mm);

  \draw[red,line width=1mm,path fading=east,line cap=rect,shorten <=.5mm,shorten >=.5mm]
  (0,2mm) -- (2mm,2mm);

  \draw[green,path fading=east,line width=1mm,line cap=rect]
  (0+.5\pgflinewidth,3mm) -- (2mm-.5\pgflinewidth,3mm);
\end{tikzpicture}
2

Following up on John Kormylo's hint I changed the code turning the lines into rectangles:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{document}

    \begin{tikzpicture}[ultra thick, black]

            \fill[black] (0,0) coordinate (ori) ++(0,-0.5\pgflinewidth) rectangle ++(1cm,\pgflinewidth) ++(0,-0.5\pgflinewidth) coordinate (GradStart);
            \fill[black,path fading=east] (GradStart) ++(0,-0.5\pgflinewidth) rectangle ++(0.3cm,\pgflinewidth) ++(0,-0.5\pgflinewidth) coordinate (GradEnd);
            \draw[red,very thin] (GradStart) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
            \draw[blue,very thin] (GradEnd) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);

            \foreach \length in {0.5,1,3,5}{                
                \fill[black] (ori) ++(0,-0.33) coordinate (ori) ++(0,-0.5\pgflinewidth) rectangle ++(1cm,\pgflinewidth) ++(0,-0.5\pgflinewidth) coordinate (GradStart);
                \fill[black,path fading=east] (GradStart) ++(0,-0.5\pgflinewidth) rectangle ++(\length,\pgflinewidth) ++(0,-0.5\pgflinewidth) coordinate (GradEnd);
                \draw[red,very thin] (GradStart) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
                \draw[blue,very thin] (GradEnd) ++(-0.1cm,-0.1cm) rectangle ++(0.2cm,0.2cm);
            }

    \end{tikzpicture}

\end{document}

With this code, path fading indeed works also for short "lines":

enter image description here

Obviously, using lines would be much more straight-forward. Therefore, I would still like to know why path fading does not work correctly for short lines and whether one can get this working.

Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.