\documentclass{article}
\usepackage[]{ocgx2}
\usepackage{tikz}
\usepackage{hyperref}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% OCG latex packages which these examples are based from include:
% ocgx2 - https://github.com/agrahn/ocgx2 (C) 2015--\today Alexander Grahn under LPPL 1.3c
% ocgx - https://www.ctan.org/pkg/ocgx (C) 2012 by Paul Gaborit under LPPL 1.3c
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\title{Layers via OCG: Examples}
For the layers in this document, Overleaf's renderer does not support the layers and hence renders all layers in the preview. The layers' features can be accessed through the PDF and layer visibility options in your PDF viewer. If your PDF viewer does not support optional content groups (layers), then layers set to \{0\} or \{off\} are not displayed. Most of these examples are taken (and modified slightly) from the documentation for the ocg package and the variants thereof. The version used here is ocgx2. As far as I can tell, OCGtools is not a supported package within Overleaf (yet).
\vspace{2cm}
\begin{ocg}{First layer}{oc1}{on}
The first Layer is visible at start.
\end{ocg}
\begin{ocg}{Second layer}{oc2}{off}
The second layer is not visible at start.
\end{ocg}
\begin{ocg}{Third layer}{oc3}{1}
The third layer is visible at start.
\end{ocg}
This text is not inside of a layer and always visible.
\vspace*{2cm}
\begin{tikzpicture}[node distance=3cm, state/.style={fill=green!20},auto]
\begin{ocg}{grid}{ocgridid}{1}
\draw[black!20] (-1,-1) grid (4,2);
\end{ocg}
\begin{ocg}{states}{ocstatesid}{1}
\node[state] (q_a) {$q_a$};
\node[state] (q_b) [right of=q_a] {$q_b$};
\end{ocg}
\begin{ocg}{edges}{ocedgesid}{1}
\path[->]
(q_a) edge node {0} (q_b)
edge [loop above] node {0} ()
(q_b) edge [loop above] node {1} ();
\end{ocg}
\end{tikzpicture}
\vspace*{2cm}
\newlength{\textlength}
\settowidth{\textlength}{This text is written in blue.}
The following text can be toggled:
\switchocg{ocblueid ocredid}{%
\begin{ocg}{red text}{ocredid}{1}
\textcolor{red}{\ This text is written in red.}
\end{ocg}%
\begin{ocg}{blue text}{ocblueid}{0}
\hspace{-\the\textlength}\textcolor{blue}{This text is written in blue.}
\end{ocg}%
}
And now the text continues.
\vspace{2cm}
\settowidth{\textlength}{\includegraphics[width=2cm]{facebook.png} www.facebook.com}
We also have that images can be toggled as well.
\begin{center}
\switchocg{in fb}{%
\begin{ocg}{facebook}{fb}{on}
\includegraphics[width=2cm]{facebook.png} www.facebook.com
\end{ocg}%
\begin{ocg}{linked in}{in}{off}
\hspace{-\the\textlength}\includegraphics[width=2cm]{linkedin.png} www.linkedin.com
\end{ocg}%
}
\end{center}
\end{document}