In tikz I want to draw a narrow long rectangle which is faded at the top and at the bottom. This is supposed to represent a laser beam.

I define a tikz fading and it works fine as long as the beam is horizontal. When I rotate the rectangle the fading does not produce the beam as before. I tried to use the fading transform option to also rotate the fading beam but it did not work. Since I want to draw several beam with different rotations in one picture I thought I should avoid a canvas transform.

Here is my example code:

\documentclass{minimal}
\usepackage{tikz}

\begin{document}

top color=transparent!100,
bottom color=transparent!100,
middle color=transparent!20]

\begin{tikzpicture}

\fill[path fading=middle, red] (0,0) rectangle (4,0.2);
\fill[rotate=-20, path fading=middle, red] (0,0) rectangle (4,0.2);

\end{tikzpicture}

\end{document}


How can I get the correct fading for the rotated beams?

-
Welcome to TeX.SE. Related question : tex.stackexchange.com/questions/59524/… – percusse Oct 30 '12 at 22:34
Your question inspired me to ask for a more generalized solution: tex.stackexchange.com/questions/80171/faded-or-blurred-lines and got a nice answer from @PaulGaborit – Benedikt Bauer Nov 3 '12 at 12:42

Apply a canvas transformation:

\documentclass{article}
\usepackage{tikz}

top color=transparent!100,
bottom color=transparent!100,
middle color=transparent!20]

\begin{document}

\begin{tikzpicture}
\fill[path fading=middle, red] (0,0) rectangle (4,0.2);
\foreach \angle in {20,40,...,340}
\fill[transform canvas={rotate=-\angle},path fading=middle, red] (0,0) rectangle (4,0.2);
\end{tikzpicture}

\end{document}


As Paul Gaborit mentions in a comment, a canvas transformation requires great care; the following quote is taken from the pgf manual:

Just as important, when you use canvas transformations pgf looses track of positions of nodes and of picture sizes since it does not take the effect of canvas transformations into account when it computes coordinates of nodes (you not, however, rely on this; it may change in the future).

-
Note: with canvas transform, you lose the automatic calculation of the bounding box. – Paul Gaborit Oct 30 '12 at 23:13
thanks a lot. works perfectly – Daniel Platz Nov 1 '12 at 0:45

The fading effect is applied to the bounding box of your path. With simple rotate, the bounding box is always aligned to the original canvas. With a canvas transform, you rotate your path and the canvas but you lose the automatic calculation of the bounding box. So you must use two operations:

1. a simple rotate to use your path without drawing to update the bounding box.

2. a canvas transform rotation to draw your path with rotated fading.

\documentclass{standalone}
\usepackage{tikz}

top color=transparent!100,
bottom color=transparent!100,
middle color=transparent!20]

\begin{document}
\begin{tikzpicture}
\fill[path fading=middle, red] (0,0) rectangle (4,0.2);

% update bounding box
\begin{scope}[rotate=45]
\path (0,-2) rectangle ++(4,.2);
\end{scope}
% draw with (canvas) rotated fading
\begin{scope}[transform canvas={rotate=45}]