9

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}

\usetikzlibrary{fadings}

\tikzfading[name=middle,
            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);
\fill[rotate=20, path fading=middle, fading transform={rotate=20}, red] (0,0) rectangle (4,0.2);


\end{tikzpicture}

\end{document}

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

7

Apply a canvas transformation:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}

\tikzfading[name=middle,
            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}

enter image description here

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
  • @PaulGaborit yes; I have included a note about this in my answer. Thank you. – Gonzalo Medina Oct 30 '12 at 23:30
4

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.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fadings}

\tikzfading[name=middle,
            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}]
  \fill[path fading=middle,orange] (0,-2) rectangle ++(4,.2);
\end{scope}

\end{tikzpicture}
\end{document}

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.