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.

I can get the corresponding lighting effect as Ivan Andrus' codes, but here I want to rotate or slant the box with lighting effect of the following type:

enter image description here

So I added the codes "xslant" and "yslant", but I obtained a failed type as the following:

enter image description here

Here is my MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shadings}
\begin{document}
\def\yslant{0.5}
\def\xslant{-0.6}
\def\shadowradius{3pt}
%
\newcommand\drawshadowbis[1]{
  \begin{pgfonlayer}{shadow}
  %
    \fill[inner color=blue,outer color=blue!10!black] ($(#1.south east)$) circle(\shadowradius);
    \fill[inner color=blue,outer color=blue!10!white] ($(#1.north west)$) circle (\shadowradius);
  %
  \begin{scope}
    \clip ($(#1.south west)$) circle (\shadowradius);
    \shade[upper left=blue!10!white,upper right=blue,lower left=blue!10,lower right=blue!10!black]
        ($(#1.south west)$) rectangle ++(-\shadowradius,-\shadowradius);
  \end{scope}

  \begin{scope}
    \clip ($(#1.north east)$) circle (\shadowradius);
    \shade[upper left=blue!10!white,upper right=blue!10,
         lower left=blue,         lower right=blue!10!black]
      ($(#1.north east)$) rectangle ++(\shadowradius,\shadowradius);
  \end{scope}

  \fill[ top color=blue, bottom color=blue!10!black] ($(#1.south west)+(0,-\shadowradius)$) rectangle ($(#1.south east)$);
  \fill[left color=blue,right color=blue!10!black] ($(#1.south east)$) rectangle ($(#1.north east)+(\shadowradius,0)$);
  \fill[bottom color=blue,top color=blue!10!white] ($(#1.north west)$) rectangle ($(#1.north east)+(0,\shadowradius)$);
  \fill[right color=blue,left color=blue!10!white] ($(#1.south west)$) rectangle ($(#1.north west)+(-\shadowradius,0)$);
  \end{pgfonlayer}
}
%
\pgfdeclarelayer{shadow}
\pgfsetlayers{shadow,main}
\begin{tikzpicture}[yslant=\yslant,xslant=\xslant,
   every node/.append style={yslant=\yslant,xslant=\xslant}]
  \node [fill=blue,rectangle,rounded corners=0pt,draw=blue, ultra thick, text=white] (box) {Test!!!};
\drawshadowbis{box}
\end{tikzpicture}
\end{document} 

Any help will be really appreciated!

share|improve this question
6  
Add \pgflowlevel{\pgftransformrotate{45}} both in tikzpicture environment and after the \begin{pgfonlayer}{shadow} in the macro definition and it should work. If you want to keep the rotation local, put the contents in a scope in the tikzpicture copy. No need for the other copy. –  percusse Jun 12 '12 at 1:35
    
@percusse why don't you turn your comment into an answer? –  Gonzalo Medina Jun 12 '12 at 2:06
    
@GonzaloMedina It's kind of a sledgehammer to kill a mosquito. So I'll wait a little before I do that unless someone else joins in :) –  percusse Jun 12 '12 at 2:08
    
@percusse I was thinking about a transform canvas={rotate=30} kind of solution... but I am not sure it will work with the shadow layer. –  Gonzalo Medina Jun 12 '12 at 2:11
1  
@GonzaloMedina I think the shading doesn't follow the current transformation matrix so that kind of upper level transformations might not be powerful enough to affect the shading coordinate calculations hence the low level proposition. –  percusse Jun 12 '12 at 2:21
show 1 more comment

1 Answer 1

up vote 5 down vote accepted

This is one of the cases where I do something and hope that it's picked up by the correct mechanism. The reason for the misaligned shading pattern is that the actual shader does not know you have rotated the picture. It's very much like the view out of the window regardless of the window shape. You just see the same stuff through. So we have to somehow pass the news to the shader.

As usual with shadings, it depends on the PDF viewer and to the contrary of the usual case, Adobe Reader renders it correctly but Sumatra fails to get the corners right. On a different machine it's the other way around. So you have been warned :P

We simply add low level canvas transformations to the required places via \pgflowlevel{} command. I use rotate here but can be other transformations too. As discussed in the comments you can keep the transformation local by surrounding it with \begin{scope}...\end{scope}

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shadings}
\begin{document}
\def\yslant{0.5}
\def\xslant{-0.6}
\def\shadowradius{3pt}
%
\newcommand\drawshadowbis[1]{
  \begin{pgfonlayer}{shadow}
    \pgflowlevel{\pgftransformrotate{45}}
  %
    \fill[inner color=blue,outer color=blue!10!black] ($(#1.south east)$) circle(\shadowradius);
    \fill[inner color=blue,outer color=blue!10!white] ($(#1.north west)$) circle (\shadowradius);
  %
  \begin{scope}
    \clip ($(#1.south west)$) circle (\shadowradius);
    \shade[upper left=blue!10!white,upper right=blue,lower left=blue!10,lower right=blue!10!black]
        ($(#1.south west)$) rectangle ++(-\shadowradius,-\shadowradius);
  \end{scope}

  \begin{scope}
    \clip ($(#1.north east)$) circle (\shadowradius);
    \shade[upper left=blue!10!white,upper right=blue!10,lower left=blue,lower right=blue!10!black] ($(#1.north east)$) rectangle ++(\shadowradius,\shadowradius);
  \end{scope}

  \fill[ top color=blue, bottom color=blue!10!black] ($(#1.south west)+(0,-\shadowradius)$) rectangle ($(#1.south east)$);
  \fill[left color=blue,right color=blue!10!black] ($(#1.south east)$) rectangle ($(#1.north east)+(\shadowradius,0)$);
  \fill[bottom color=blue,top color=blue!10!white] ($(#1.north west)$) rectangle ($(#1.north east)+(0,\shadowradius)$);
  \fill[right color=blue,left color=blue!10!white] ($(#1.south west)$) rectangle ($(#1.north west)+(-\shadowradius,0)$);
  \end{pgfonlayer}
}
%
\pgfdeclarelayer{shadow}
\pgfsetlayers{shadow,main}
\begin{tikzpicture}[transform shape]
    \pgflowlevel{\pgftransformrotate{45}}   
    \node [fill=blue,rectangle,rounded corners=0pt,draw=blue, ultra thick, text=white] (box) {Test!!!};
    \drawshadowbis{box}
\end{tikzpicture}
\end{document} 

enter image description here

share|improve this answer
    
Yeah! Thanks a lot. I understand. –  XIAO Lishun Jun 12 '12 at 12:53
    
@xiaodaxialiang My pleasure. –  percusse Jun 12 '12 at 13:57
add comment

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.