Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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

2 Answers 2

up vote 6 down vote accepted

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).

share|improve this answer
    
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
    
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.

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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