7

In this example, the included PNG (the large rectangle in the middle) should fade from left an right due to the scope fading on a previous path. Why doesn't it?

Edit: I genuinely need a fade as I am trying to simulate a crossfade by layering a partially transparent image onto another image. Using \pgfdeclaremask doesn't really work as the source textures are being cropped to the left and right (and the exact dimensions involved may change), so the mask size can't easily be determined. How can I obtain a crossfade?

enter image description here

MWE:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{shadows}

\tikzset{path image/.style 2 args={
path picture={
\node at (path picture bounding box.center) {
\pgfimage[height=#2]{#1}
};}}}

\begin{document}
\begin{tikzpicture}
    \draw[path image={texture_red.png}{90mm}] (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
    \draw[black,scope fading=east] (0,0) rectangle (63mm,88mm); 
    \draw[path image={texture_black.png}{90mm}] (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
\end{tikzpicture}
\end{document}

texture_black.png

[]

texture_red.png

[http://i.stack.imgur.com/MidAg.png]

  • I don't really understand the question. Why do you think that the included image should be affected? – cfr Jun 15 '14 at 23:15
  • @cfr: I guess a misunderstanding as to how tikz works. A better question might then be how to achieve the effect with the image fade. – Mohan Jun 15 '14 at 23:19
  • 2
    OK. I think you may want to use \pgfdeclareimage... etc. instead of \includegraphics... so that you can use masking. This is around 1066 of the manual. Maybe there's another way but I thought that opacity etc. could only be applied normally to stuff tikz renders rather than including. (You can put something over the image but it doesn't affect the content of the included image.) So this maybe is a case where the pgf image inclusion is needed rather than ordinary graphics inclusion. – cfr Jun 15 '14 at 23:23
  • @cfr: Unfortunately switching to pgfdeclareimage doesn't seem to fix it. Shall I post a revised MWE? – Mohan Jun 15 '14 at 23:45
  • FWIW, 87.4 says: >You can only mask images the are in a “pixel format.” These are .jpg and .png.< So it should be possible. But I don't understand the pgf internals... – Mohan Jun 15 '14 at 23:54
10
+350

There are a couple of of problems here.

In the MWE the scope fading in the second \draw command will have no effect on nodes in any subsequent path. To have an effect the scope fading should be applied to a scope or the node itself (as the examples in the manual show).

However, it seems that it is necessary to "flatten" the texture_black.png in order to get fadings to work with the image. This can easily be done using gimp and will remove any existing alpha channel (which is what I presume is causing the problem).

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{fadings}
\begin{document}
\begin{tikzpicture}
\node [left,  scope fading=east, label=90:Original] 
  {\pgfimage[height=2in]{texture_black.png}};
\node [right, scope fading=east, label=90:Flattened] 
  {\pgfimage[height=2in]{texture_black_flattened.png}};
\end{tikzpicture}
\end{document}

enter image description here

So, returning to the OP's problem, the scope fading needs to be applied to the node containing the image and the image needs to be flattened.

\documentclass[tikz, border=5]{standalone}
\usepackage{tikz}

\usetikzlibrary{fadings}

\tikzset{path image/.style args={#1#2#3}{
  path picture={
    \node [#3]at (path picture bounding box.center) {
     \pgfimage[height=#2]{#1}
   };
  }
}}

\begin{document}
\begin{tikzpicture}
  \draw [path image={texture_red.png}{90mm}{}] 
    (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
  \draw [black] (0,0) rectangle (63mm,88mm); 
  \draw [path image={texture_black_flattened.png}{90mm}{scope fading=east}] 
    (3mm,3mm) rectangle (63mm-3mm,88mm-3mm);
\end{tikzpicture}
\end{document}

enter image description here

To compile the above examples, the images from the original post are required, and the following image, saved as texture_black_flattened.png:

enter image description here

  • I didn't understand your first point; the manual says "You add the scope fading option to a path in a scope – typically the first one – and then all subsequent drawings in the scope are faded." – Mohan Jun 18 '14 at 12:45
  • 1
    @Mohan the manual also says "Scope fadings are also needed if you wish to fade a node", specifically the contents of a node (the fade will apply to the fill of the node, but not the contents or drawn path). – Mark Wibrow Jun 18 '14 at 15:41

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.