Clearly all too naïvely, I would expect the following code
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{fadings,backgrounds}
\pgfmathsetlengthmacro\hyd{sqrt(5)*.5*(145mm+2ex)}
\begin{tikzfadingfrompicture}[name=heulog]
\foreach \i [remember=\i as \j (initially 180), count=\k from 0] in {160,140,...,0}
\path [top color=transparent!100, bottom color=transparent!0, draw=transparent!100, line width=2.5pt, shading angle={-20*\k+80}] (0,0) -- ++(\j:\hyd) arc (\j:\i:\hyd) -- cycle;
\end{tikzfadingfrompicture}
\begin{document}
\begin{tikzpicture}
\path [ball color=magenta] (-.125*\hyd,0) -- (0,.25*\hyd) -- (.125*\hyd,0) -- (0,-.75*\hyd) -- cycle;
\begin{scope}[on background layer]
\coordinate [yshift=-1ex] (h) at (0,-.75*\hyd);
\begin{scope}[shift=(h)]
\path [scope fading=heulog, fit fading=false] (0,0);
\foreach \i [remember=\i as \j (initially 180)] in {160,140,...,0}
\path [fill=blue!50!cyan] (0,0) -- ++(\j:\hyd) arc (\j:\i:\hyd) -- cycle;
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
to produce
In fact, however, the result is
In my real document, I cannot control the foreground drawing's absolute coordinates at all easily. For purposes of this question, therefore, assume that I cannot change
\path [ball color=magenta] (-.125*\hyd,0) -- (0,.25*\hyd) -- (.125*\hyd,0) -- (0,-.75*\hyd) -- cycle;
How can I modify the code on the background layer and/or the definition of heulog to produce the expected output, without changing the code for the content on the main layer and without changing the order in which things are drawn?
I apologise for the slight complexity of the MWE. I had trouble breaking things satisfactorily with minimal code.
EDIT 1
As pointed out in comments by Zarko, it is possible to adjust the fading so that it is correctly centred by scaling the tikzfadingfrompicture by 1.5. This would be problematic in practice because the right scalar will not always be easy to calculate. However, the main problem with scaling the fading in this way is similar to the problem with letting TikZ auto-scale it: the fading is severely diluted and does not give the effect of fading from full opacity to full transparency. That is, the output is
which is great, if that's what you want, but unfortunately not what I need here.
EDIT 1a
It does matter that transparency is involved because it must work on a coloured background.
EDIT 2
The following works, though I'm not sure why.
\begin{tikzpicture}
\path [ball color=magenta] (-.125*\hyd,0) -- (0,.25*\hyd) -- (.125*\hyd,0) -- (0,-.75*\hyd) -- cycle;
\begin{scope}[on background layer]
\path (0,-.75*\hyd) ++(0,-1ex) coordinate (h) (0,.25*\hyd) ++(0,1ex) coordinate (o);
\path (h);
\pgfgetlastxy{\hx}{\hy}
\path (o);
\pgfgetlastxy{\ox}{\oy}
\pgfmathsetmacro\hydy{(\hy+\oy)/2}
\pgfsetfading{heulog}{\pgftransformshift{\pgfpoint{0}{\hydy pt}}}
\begin{scope}[shift=(h)]
\foreach \i [remember=\i as \j (initially 180)] in {160,140,...,0}
\path [fill=blue!50!cyan] (0,0) -- ++(\j:\hyd) arc (\j:\i:\hyd) -- cycle;
\end{scope}
\end{scope}
\end{tikzpicture}
My theory was to adjust for the difference in distance between the top of the picture and the origin, on the one hand, and the bottom of the picture and the origin, on the other.
However, this fails in my real code. There, using
\pgfmathsetlengthmacro\hydy{(\hy+\oy)+1ex}%
seems to approximate the expected result. I can't give the full code for this as there's too much of it, but the immediate context is as follows, where staff is a local bounding box containing the stuff drawn in the foreground.
...
\path (staff.south) ++(0,-1ex) coordinate (h) (staff.north) ++(0,1ex) coordinate (o);
\path (h);
\pgfgetlastxy{\hx}{\hy}%
\path (o);
\pgfgetlastxy{\ox}{\oy}%
\pgfmathsetlengthmacro\hydy{(\hy+\oy)+1ex}%
\pgfsetfading{heulog}{\pgftransformshift{\pgfpoint{0}{\hydy}}}
\begin{scope}[shift=(h)]
% commenting the \pgfsetfading above and uncommenting this also works
%\path [scope fading=heulog, fading transform={shift={(0,\hydy)}}, fit fading=false] (0,0);
\foreach \i [remember=\i as \j (initially 180), count=\k from 0, evaluate=\k as \ll using \k*100/8] in {160,140,...,0}
\path [fill=Green4!\ll!Red3] (0,0) -- ++(\j:\hyd) arc (\j:\i:\hyd) -- cycle;
\end{scope}
...
but this makes no sense whatsoever. (At least, I'm assuming it makes sense to the brain of TikZ, but it makes none to the puny one with which nature equipped me!)




1exadjustment matters for the example. But scaling the fading is more problematic as it messes up the effect. – cfr Sep 09 '16 at 23:51fit fadingis set to false, so the size and scale will matter. But I'd expect to be able to centre the scope fading at an arbitrary point or shift it in a predictable manner. Yet I can't figure out how to do it. – cfr Sep 10 '16 at 00:40fading transform={shift={(current bounding box.center)}}? – Paul Gaborit Sep 10 '16 at 06:43(0,\hydy)in place of(current bounding box.center)does work infading transform, though. – cfr Sep 10 '16 at 13:26