Magnifying a lens

tikz-pgfzooming

I'm trying to draw a lens with camera in the middle similar to the ones in the iPhone and then I want to zoom it. This is what I could do so far.

Whenever I try to minimize the lens to fit the pipe size, I get an error that the dimension is too large.

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.markings,decorations.pathreplacing,calc}
\makeatletter
\usetikzlibrary{spy, backgrounds}
\usetikzlibrary{calc,fadings,decorations.text}
\tikzfading[name=fade inside, inner color=transparent!90, outer color=transparent!40]

\tikzset{every pipe/.code={%
  \pgfmathloop
  \ifnum\pgfmathcounter>20
  \else
    \pgfmathsetmacro\i{\pgfmathcounter}%
    \pgfmathsetmacro\j{80-\pgfmathcounter*3}%
    \tikzset{preaction/.expanded={line width=(\pipewidth)*(1-\i*\i/500),
        draw=black!50!white!\j!white}}%
  \repeatpgfmathloop
},
every flange/.style 2 args={insert path={
  let \p1=(#1), \p2=(#2),\n1={atan2(\y2-\y1, \x2-\x1)} in
    [shift={(\p1)}, rotate=\n1, pipe width={(\pipewidth)*4/3}, every pipe] 
      (0.01,0) -- (0.2,0)
}},
flanges/.style={postaction={%
  /utils/exec={
    \@for\pos:=#1\do{%
      \tikzset{decoration/.expanded={markings,
        mark=at position \pos\space with {
          \noexpand\path [pipe width=\pipewidth*4/3, every pipe] (0.01,0) -- (0.2,0);
    }}}}}, 
  decorate}},
pipe width/.code=\pgfmathsetlengthmacro\pipewidth{#1},
pipes/.style={
  every pipe,
  postaction={decoration={show path construction,
    lineto code={
      \path [every flange={\tikzinputsegmentfirst}{\tikzinputsegmentlast}];
      \path [every flange={\tikzinputsegmentlast}{\tikzinputsegmentfirst}];
    },
    curveto code={
      \path [every flange={\tikzinputsegmentfirst}{\tikzinputsegmentsupporta}];
      \path [every flange={\tikzinputsegmentlast}{\tikzinputsegmentsupportb}];
    }
}, decorate}},
pipe/.style={
  every pipe,
  flanges={0, 1}
}}
\begin{document}
\begin{tikzpicture}[line join=round,spy using outlines={circle, magnification=15, size=1.95cm, connect spies, transform shape}]
\path [pipe width=0.5cm, pipes] (-2,0) -- (-2,-5) arc (180:270:2)--(3,-7);
\path [pipe width=0.06cm, pipes] (-1.9,0) -- (-1.9,-5) arc (180:270:1.9)--(3,-6.9);
\coordinate (zoomLoc) at (1,-1.5);
\spy [black] on (-1.9,-3) in node [left] at (zoomLoc);

  \coordinate (baseSupportLunette) at (0,-4);
  \coordinate (coinSupportCameras) at  -- ($(baseSupportLunette)+(-3,1.75)$);
  \coordinate (moteurTilt) at (-1.75,0);

 \coordinate (baseSupportLunette) at (0,-4);
  \coordinate (coinSupportCameras) at  -- ($(baseSupportLunette)+(-3,1.75)$);
  \coordinate (moteurTilt) at (-1.75,0);
\coordinate (O) at  (-1.9,-3);
\def\x{2}

  % lens
%  \filldraw[black!85] (0,0) circle (1.75cm);
  \draw [darkgray] (O) circle ({0.01/\x});
  \shade [ball color=white,path fading=fade inside] (O) circle ({0.00975/\x});
  \draw [gray] (O) circle (0.025/\x);
  
 % \draw[] (O) circle (0.06);
 % \draw [darkgray] (O) circle (0.075);
  \shade [ball color=white,path fading=fade inside] (O) circle ({0.06225/\x});
  %\draw [darkgray,thick] (O) circle (0.125);
 % \draw [thick] (O) circle (0.135);
  \begin{scope}
    \clip (O) circle ({0.175/\x});
    \foreach \i in {0.8,...,1} \shade [ball color=white!95!blue, blend mode=hard light, opacity=1] (135:{0.13*(0.8-\i}) circle ({0.13*\i/\x});
    \shade [ball color=white!95!blue, shading angle=0, blend mode=hard light, opacity=1] (O) circle (0.13/\x);
    \draw [solid,black] (O) circle (0.01/\x);
  \end{scope}
\end{tikzpicture}
\end{document}

Best Answer

Updated answer:

How's this?

I don't know what \x is for, so I just ignored it. The shade balls are now no longer wider than the small pipe. Is that on the right track?

hc

MWE

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.markings,decorations.pathreplacing,calc}
\makeatletter
\usetikzlibrary{spy, backgrounds}
\usetikzlibrary{calc,fadings,decorations.text}
\tikzfading[name=fade inside, inner color=transparent!90, outer color=transparent!40]

\tikzset{every pipe/.code={%
  \pgfmathloop
  \ifnum\pgfmathcounter>20
  \else
    \pgfmathsetmacro\i{\pgfmathcounter}%
    \pgfmathsetmacro\j{80-\pgfmathcounter*3}%
    \tikzset{preaction/.expanded={line width=(\pipewidth)*(1-\i*\i/500),
        draw=black!50!white!\j!white}}%
  \repeatpgfmathloop
},
every flange/.style 2 args={insert path={
  let \p1=(#1), \p2=(#2),\n1={atan2(\y2-\y1, \x2-\x1)} in
    [shift={(\p1)}, rotate=\n1, pipe width={(\pipewidth)*4/3}, every pipe] 
      (0.01,0) -- (0.2,0)
}},
flanges/.style={postaction={%
  /utils/exec={
    \@for\pos:=#1\do{%
      \tikzset{decoration/.expanded={markings,
        mark=at position \pos\space with {
          \noexpand\path [pipe width=\pipewidth*4/3, every pipe] (0.01,0) -- (0.2,0);
    }}}}}, 
  decorate}},
pipe width/.code=\pgfmathsetlengthmacro\pipewidth{#1},
pipes/.style={
  every pipe,
  postaction={decoration={show path construction,
    lineto code={
      \path [every flange={\tikzinputsegmentfirst}{\tikzinputsegmentlast}];
      \path [every flange={\tikzinputsegmentlast}{\tikzinputsegmentfirst}];
    },
    curveto code={
      \path [every flange={\tikzinputsegmentfirst}{\tikzinputsegmentsupporta}];
      \path [every flange={\tikzinputsegmentlast}{\tikzinputsegmentsupportb}];
    }
}, decorate}},
pipe/.style={
  every pipe,
  flanges={0, 1}
}}
\begin{document}
\begin{tikzpicture}[line join=round,spy using outlines={circle, magnification=3.5, size=1.95cm, connect spies, transform shape}]
\path [pipe width=0.5cm, pipes] (-2,0) -- (-2,-5) arc (180:270:2)--(3,-7);
\path [pipe width=0.06cm, pipes] (-1.9,0) -- (-1.9,-5) arc (180:270:1.9)--(3,-6.9);
\coordinate (zoomLoc) at (1.5,-1.5);
\spy [black] on (-2,-3) in node [left] at (zoomLoc);

  \coordinate (baseSupportLunette) at (0,-4);
  \coordinate (coinSupportCameras) at  -- ($(baseSupportLunette)+(-3,1.75)$);
  \coordinate (moteurTilt) at (-1.75,0);

 \coordinate (baseSupportLunette) at (0,-4);
  \coordinate (coinSupportCameras) at  -- ($(baseSupportLunette)+(-3,1.75)$);
  \coordinate (moteurTilt) at (-1.75,0);
\coordinate (O) at  (-1.9,-3);
\def\x{1}

  % lens
%  \filldraw[black!85] (0,0) circle (1.75cm);
  \draw [violet] (O) circle ({0.01/\x});
  \shade [ball color=green,path fading=fade inside] (O) circle ({0.012/\x});
  \draw [blue] (O) circle (0.013/\x);
  
 % \draw[] (O) circle (0.06);
 % \draw [darkblue] (O) circle (0.075);
  \shade [ball color=red,path fading=fade inside] (O) circle ({0.014/\x});
  %\draw [darkblue,thick] (O) circle (0.125);
 % \draw [thick] (O) circle (0.135);
  \begin{scope}
    \clip (O) circle ({0.175/\x});
    \foreach \i in {0.8,...,1} \shade [ball color=white!95!blue, blend mode=hard light, opacity=1] (135:{0.13*(0.8-\i}) circle ({0.13*\i/\x});
    \shade [ball color=white!95!blue, shading angle=0, blend mode=hard light, opacity=1] (O) circle (0.03/\x);
    \draw [solid,black] (O) circle (0.01/\x);
  \end{scope}
\end{tikzpicture}
\end{document}

Original answer:

Here's a picture, where I changed magnification down to magnification=3.5, shifted the spy on slightly left, \spy [black] on (-2,-3)... and set x to 1, \def\x{1}. The pipe fits in both circles now.

What is the function or purpose of \x? What is it intended to control?

zoom

MWE

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.markings,decorations.pathreplacing,calc}
\makeatletter
\usetikzlibrary{spy, backgrounds}
\usetikzlibrary{calc,fadings,decorations.text}
\tikzfading[name=fade inside, inner color=transparent!90, outer color=transparent!40]

\tikzset{every pipe/.code={%
  \pgfmathloop
  \ifnum\pgfmathcounter>20
  \else
    \pgfmathsetmacro\i{\pgfmathcounter}%
    \pgfmathsetmacro\j{80-\pgfmathcounter*3}%
    \tikzset{preaction/.expanded={line width=(\pipewidth)*(1-\i*\i/500),
        draw=black!50!white!\j!white}}%
  \repeatpgfmathloop
},
every flange/.style 2 args={insert path={
  let \p1=(#1), \p2=(#2),\n1={atan2(\y2-\y1, \x2-\x1)} in
    [shift={(\p1)}, rotate=\n1, pipe width={(\pipewidth)*4/3}, every pipe] 
      (0.01,0) -- (0.2,0)
}},
flanges/.style={postaction={%
  /utils/exec={
    \@for\pos:=#1\do{%
      \tikzset{decoration/.expanded={markings,
        mark=at position \pos\space with {
          \noexpand\path [pipe width=\pipewidth*4/3, every pipe] (0.01,0) -- (0.2,0);
    }}}}}, 
  decorate}},
pipe width/.code=\pgfmathsetlengthmacro\pipewidth{#1},
pipes/.style={
  every pipe,
  postaction={decoration={show path construction,
    lineto code={
      \path [every flange={\tikzinputsegmentfirst}{\tikzinputsegmentlast}];
      \path [every flange={\tikzinputsegmentlast}{\tikzinputsegmentfirst}];
    },
    curveto code={
      \path [every flange={\tikzinputsegmentfirst}{\tikzinputsegmentsupporta}];
      \path [every flange={\tikzinputsegmentlast}{\tikzinputsegmentsupportb}];
    }
}, decorate}},
pipe/.style={
  every pipe,
  flanges={0, 1}
}}
\begin{document}
\begin{tikzpicture}[line join=round,spy using outlines={circle, magnification=3.5, size=1.95cm, connect spies, transform shape}]
\path [pipe width=0.5cm, pipes] (-2,0) -- (-2,-5) arc (180:270:2)--(3,-7);
\path [pipe width=0.06cm, pipes] (-1.9,0) -- (-1.9,-5) arc (180:270:1.9)--(3,-6.9);
\coordinate (zoomLoc) at (1.5,-1.5);
\spy [black] on (-2,-3) in node [left] at (zoomLoc);

  \coordinate (baseSupportLunette) at (0,-4);
  \coordinate (coinSupportCameras) at  -- ($(baseSupportLunette)+(-3,1.75)$);
  \coordinate (moteurTilt) at (-1.75,0);

 \coordinate (baseSupportLunette) at (0,-4);
  \coordinate (coinSupportCameras) at  -- ($(baseSupportLunette)+(-3,1.75)$);
  \coordinate (moteurTilt) at (-1.75,0);
\coordinate (O) at  (-1.9,-3);
\def\x{1}

  % lens
%  \filldraw[black!85] (0,0) circle (1.75cm);
  \draw [violet] (O) circle ({0.01/\x});
  \shade [ball color=white,path fading=fade inside] (O) circle ({0.00975/\x});
  \draw [blue] (O) circle (0.025/\x);
  
 % \draw[] (O) circle (0.06);
 % \draw [darkblue] (O) circle (0.075);
  \shade [ball color=white,path fading=fade inside] (O) circle ({0.06225/\x});
  %\draw [darkblue,thick] (O) circle (0.125);
 % \draw [thick] (O) circle (0.135);
  \begin{scope}
    \clip (O) circle ({0.175/\x});
    \foreach \i in {0.8,...,1} \shade [ball color=white!95!blue, blend mode=hard light, opacity=1] (135:{0.13*(0.8-\i}) circle ({0.13*\i/\x});
    \shade [ball color=white!95!blue, shading angle=0, blend mode=hard light, opacity=1] (O) circle (0.13/\x);
    \draw [solid,black] (O) circle (0.01/\x);
  \end{scope}
\end{tikzpicture}
\end{document}