15

My question was inspired by Rotate fading in tikz where the inquiror is drawing laser beams that shall look somewhat blurred not as lines but as boxes with a fading. The problem with this approach is that the fading has to be turned according to the orientation via quite unhandy canvas transformations. Above that curved lines are not possible this way.

That made me think if there is way to draw fading or blurred lines in a more convenient manner, maybe by applying some kind of very closely repeated decoration template to a path? Any ideas?

1
  • I can imagine that in a curvy line those small steps would cause a lot of aliasing problems.
    – percusse
    Nov 2, 2012 at 21:21

2 Answers 2

21

Here is a proposition using recursive preaction (each of the ten preactions draws the path with draw opacity=.1 and line width increased by .2pt):

enter image description here

\documentclass{standalone}
\usepackage{ifthen}
\usepackage{tikz}
\tikzset{
  laser beam action/.style={
    line width=\pgflinewidth+.2pt,draw opacity=.1,draw=#1,
  },
  laser beam recurs/.code 2 args={%
    \pgfmathtruncatemacro{\level}{#1-1}%
    \ifthenelse{\equal{\level}{0}}%
    {\tikzset{preaction={laser beam action=#2}}}%
    {\tikzset{preaction={laser beam action=#2,laser beam recurs={\level}{#2}}}}
  },
  laser beam/.style={preaction={laser beam recurs={10}{#1}},draw opacity=1,draw=#1},
}
\begin{document}
\begin{tikzpicture}
  \fill[lime] circle(1cm);
  \path[preaction={fill=black},laser beam=yellow,] (1,2) circle(1cm and 5mm);
  \path[laser beam=red] (0,0) -- (2,2);
  \path[rounded corners=5mm,laser beam=blue] (0,0) -- (3,0) -- (0,-2);
  \path[line width=4pt,laser beam=orange] (0,0) -- (-1,2);
\end{tikzpicture}
\end{document}
1
8

Since the proposed solution seemed to be too complicated if only straight lines should be drawn, I gave it another shot and came up with the following solution that involves the shadings library and canvas transformation:

MWE:

\documentclass[tikz,convert={density=400,outext=.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadings} % libraries for this TikZ picture
\begin{document}
\begin{tikzpicture}
    \draw (0,0) rectangle (2,2);
    \begin{scope}[transform canvas={rotate=30}] % rotate by 30 degree
        \shade[shading=axis,bottom color=white,top color=white,middle color=black,shading angle=0] (1,0) rectangle (2,0.2);
    \end{scope}
\end{tikzpicture}
\end{document}

Output:

Line with fading, rotated by 30 degree

This answer is inspired by this answer of user rgallego.

2
  • 1
    But this solution does only work as long as lines (which you "simulate" by rectangles) are straight, right? What about curved lines? Mar 10, 2014 at 14:09
  • @BenediktBauer You are absolutely right. I updated the answer accordingly. Mar 10, 2014 at 14:17

You must log in to answer this question.

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