<p>I recently redesigned my personal website to include a fun effect in the hero area, where the user’s cursor movement reveals alternative styling on the title and background, reminiscent of a spotlight. In this article we’ll walk through how the effect was created, using masks, CSS custom properties and much more. </p>
<p>Duplicating the content </p>
<p>We start with HTML to create two identical hero sections, with the title repeated. </p>
<p> Welcome to my website </p>
<p> Welcome to my website </p>
<p>Duplicating content isn’t a great experience for someone accessing the website using a screenreader. In order to prevent screenreaders announcing it twice, we can use aria-hidden=”true” on the second component. The second component is absolute-positioned with CSS to completely cover the first. </p>
<p>The two sections with the same content are layered one on top of the other </p>
<p>Using pointer-events: none on the second component ensures that the text of the first will be selectable by users. </p>
<p>Styling </p>
<p>Now we can add some CSS to style the two components. I deliberately chose a bright, rich gradient for the “revealed” background, in contrast to the dark monochrome of the initial view. </p>
<p>Somewhat counterintuitively, the component with the bright background is actually the one that will cover the other. In a moment we’ll add the mask, so that parts of it will be hidden — which is what gives the impression of it being underneath. </p>
<p>Text effects </p>
<p>There are a couple of different text effects at play in this component. The first applies to the bright text on the dark background. This uses -webkit-text-stroke, a non-standard CSS property that is nonetheless supported in all modern browsers. It allows us to outline our text, and works great with bold, chunky fonts like the one we’re using here. It requires a prefix in all browsers, and can be used as shorthand for -webkit-stroke-width and -webkit-stroke-color. </p>
<p>In order to get the “glow” effect, we can set the text color to a transparent value and use the CSS drop-shadow filter with the same color value. (We’re using a CSS custom property for the color in this example): </p>
<p>.heading {<br />
-webkit-text-stroke: 2px var(–primary);<br />
color: transparent;<br />
filter: drop-shadow(0 0 .35rem var(–primary));<br />
} </p>
<p>See the Pen Outlined text by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>The text on the colored panel has a different effect applied. The intention was, for it to feel a little like an x-ray revealing the skeleton underneath. The text fill has a dotted pattern, which is created using a repeated radial gradient. To get this effect on the text, we in fact apply it to the background of the element, and use background-clip: text, which also requires a prefix in most browsers (at the time of writing). Again, we need to set the text color to a transparent value in order to see the result of the background-clip property: </p>
<p>.hero–secondary .heading {<br />
background: radial-gradient(circle at center, white .11rem, transparent 0);<br />
background-size: .4rem .4rem;<br />
-webkit-background-clip: text;<br />
background-clip: text;<br />
color: transparent;<br />
} </p>
<p>See the Pen Dotted text by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>Creating the spotlight </p>
<p>We have two choices when it comes to creating the spotlight effect with CSS: clip-path and mask-image. These can produce very similar effects, but with some important differences. </p>
<p>Clipping </p>
<p>We can think of clipping a shape with clip-path as a bit like cutting it out with scissors. This is ideal for shapes with clean lines. In this case, we could create a circle shape for our spotlight, using the circle() function: </p>
<p>.hero–secondary {<br />
–clip: circle(20% at 70%);<br />
-webkit-clip-path: var(–clip);<br />
clip-path: var(–clip);<br />
} </p>
<p>(clip-path still needs to be prefixed in Safari, so I like to use a custom property for this.) </p>
<p>clip-path can also take an ellipse, polygon, SVG path or a URL with an SVG path ID. </p>
<p>See the Pen Hero with clip-path by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>Masking </p>
<p>Unlike clip-path the mask-image property is not limited to shapes with clean lines. We can use a PNGs, SVGs or even GIFs to create a mask. We can even use gradients: the blacker parts of the image (or gradient) act as the mask whereas the element will be hidden by the transparent parts. </p>
<p>We can use a radial gradient to create a mask very similar to the clip-path circle: </p>
<p>.hero–secondary {<br />
–mask: radial-gradient(circle at 70%, black 25%, transparent 0);<br />
-webkit-clip-path: var(–mask);<br />
clip-path: var(–mask);<br />
} </p>
<p>Another advantage is that there are additional mask properties than correspond to CSS background properties — so we can control the size and position of the mask, and whether or not it repeats in much the same way, with mask-size, mask-position and mask-repeat respectively. </p>
<p>See the Pen Hero with mask by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>There’s much more we could delve into with clipping and masking, but let’s leave that for another day! I chose to use a mask instead of a clip-path for this project — hopefully the reason will become clear a little later on. </p>
<p>Tracking the cursor </p>
<p>Now we have our mask, it’s a matter of tracking the position of the user’s cursor, for which we’ll need some Javascript. First we can set custom properties for the center co-ordinates of our gradient mask. We can use default values, to give the mask an initial position before the JS is executed. This will also ensure that non-mouse users see a static mask, rather than none at all. </p>
<p>.hero–secondary {<br />
–mask: radial-gradient(circle at var(–x, 70%) var(–y, 50%), black 25%, transparent 0);<br />
} </p>
<p>In our JS, we can listen for the mousemove event, then update the custom properties for the x and y percentage position of the circle in accordance with the cursor position: </p>
<p>const hero = document.querySelector(‘[data-hero]’) </p>
<p>window.addEventListener(‘mousemove’, (e) = > {<br />
const { clientX, clientY } = e<br />
const x = Math.round((clientX / window.innerWidth) * 100)<br />
const y = Math.round((clientY / window.innerHeight) * 100) </p>
<p> hero.style.setProperty(‘–x’, `${x}%`)<br />
hero.style.setProperty(‘–y’, `${y}%`)<br />
}) </p>
<p>See the Pen Hero with cursor tracking by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>(For better performance, we might want to throttle or debounce that function, or use requestAnimationFrame, to prevent it repeating too frequently. If you’re not sure which to use, this article has you covered.) </p>
<p>Adding animation </p>
<p>At the moment there is no easing on the movement of the spotlight — it immediately updates its position when the mouse it moved, so feels a bit rigid. We could remedy that with a bit of animation. </p>
<p>If we were using clip-path we could animate the path position with a transition: </p>
<p>.hero–secondary {<br />
–clip: circle(25% at 70%);<br />
-webkit-clip-path: var(–clip);<br />
clip-path: var(–clip);<br />
transition: clip-path 300ms 20ms;<br />
} </p>
<p>Animating a mask requires a different route. </p>
<p>Animating with CSS Houdini </p>
<p>In CSS we can transition or animate custom property values using Houdini – a set of low-level APIs that give developers access to the browser’s rendering engine. The upshot is we can animate properties (or, more accurately, values within properties, in this case) that aren’t traditionally animatable. </p>
<p>We first need to register the property, specifying the syntax, whether or not it inherits, and an initial value. The initial-value property is crucial, otherwise it will have no effect. </p>
<p>@property –x {<br />
syntax: ”;<br />
inherits: true;<br />
initial-value: 70%;<br />
} </p>
<p>Then we can transition or animate the custom property just like any regular animatable CSS property. For our spotlight, we can transition the –x and –y values, with a slight delay, to make them feel more natural: </p>
<p>.hero–secondary {<br />
transition: –x 300ms 20ms ease-out, –y 300ms 20ms ease-out;<br />
} </p>
<p>See the Pen Hero with cursor tracking (with Houdini animation) by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>Unfortunately, @property is only supported in Chromium browsers at the time of writing. If we want an improved animation in all browsers, we could instead reach for a JS library. </p>
<p>Animating with GSAP </p>
<p>In CSS we can transition or animate custom property values using Houdini –I love using the Greensock(GSAP) JS animation library. It has an intuitive API, and contains plenty of easing options, all of which makes animating UI elements easy and fun! As I was already using it for other parts of the project, it was a simple decision to use it here to bring some life to the spotlight. Instead of using setProperty we can let GSAP take care of setting our custom properties, and configure the easing using the built in options: </p>
<p>import gsap from ‘gsap’ </p>
<p>const hero = document.querySelector(‘[data-hero]’) </p>
<p>window.addEventListener(‘mousemove’, (e) = > {<br />
const { clientX, clientY } = e<br />
const x = Math.round((clientX / window.innerWidth) * 100)<br />
const y = Math.round((clientY / window.innerHeight) * 100) </p>
<p> gsap.to(hero, {<br />
‘–x’: `${x}%`,<br />
‘–y’: `${y}%`,<br />
duration: 0.3,<br />
ease: ‘sine.out’<br />
})<br />
}) </p>
<p>See the Pen Hero with cursor tracking (GSAP) by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>Animating the mask with a timeline </p>
<p>The mask on my website’s hero section is slightly more elaborate than a simple spotlight. We start with a single circle, then suddenly another circle “pops” out of the first, surrounding it. To get an effect like this, we can once again turn to custom properties, and animate them on a GSAP timeline. </p>
<p>Our radial gradient mask becomes a little more complex: We’re creating a gradient of two concentric circles, but setting the initial values of the gradient stops to 0% (via the default values in our custom properties), so that their size can be animated with JS: </p>
<p>.hero {<br />
–mask: radial-gradient(<br />
circle at var(–x, 50%) var(–y, 50%),<br />
black var(–maskSize1, 0%),<br />
transparent 0,<br />
transparent var(–maskSize2, 0%),<br />
black var(–maskSize2, 0%),<br />
black var(–maskSize3, 0%),<br />
transparent 0);<br />
} </p>
<p>Our mask will be invisible at this point, as the circle created with the gradient has a size of 0%. Now we can create a timeline with GSAP, so the central spot will spring to life, followed by the second circle. We’re also adding a delay of one second before the timeline starts to play. </p>
<p>const tl = gsap.timeline({ delay: 1 }) </p>
<p>tl<br />
.to(hero, {<br />
‘–maskSize1’: ‘20%’,<br />
duration: 0.5,<br />
ease: ‘back.out(2)’<br />
})<br />
.to(hero, {<br />
‘–maskSize2’: ‘28%’,<br />
‘–maskSize3’: ‘calc(28% + 0.1rem)’,<br />
duration: 0.5,<br />
delay: 0.5,<br />
ease: ‘back.out(2)’<br />
}) </p>
<p>See the Pen Hero with cursor tracking (GSAP) by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>Using a timeline, our animations will execute one after the other. GSAP offers plenty of options for orchestrating the timing of animations with timelines, and I urge you to explore the documentation to get a taste of the possibilities. You won’t be disappointed! </p>
<p>Smoothing the gradient </p>
<p>For some screen resolutions, a gradient with hard color stops can result in jagged edges. To avoid this we can add some additional color stops with fractional percentage values: </p>
<p>.hero {<br />
–mask: radial-gradient(<br />
circle at var(–x, 50%) var(–y, 50%),<br />
black var(–maskSize1, 0%) 0,<br />
rgba(0, 0, 0, 0.1) calc(var(–maskSize1, 0%) + 0.1%),<br />
transparent 0,<br />
transparent var(–maskSize2, 0%),<br />
rgba(0, 0, 0, 0.1) calc(var(–maskSize2, 0%) + 0.1%),<br />
black var(–maskSize2, 0%),<br />
rgba(0, 0, 0, 0.1) calc(var(–maskSize3, 0%) – 0.1%),<br />
black var(–maskSize3, 0%),<br />
rgba(0, 0, 0, 0.1) calc(var(–maskSize3, 0%) + 0.1%),<br />
transparent 0<br />
);<br />
} </p>
<p>This optional step results in a smoother-edged gradient. You can read more about this approach in this article by Mandy Michael. </p>
<p>A note on default values </p>
<p>While testing this approach, I initially used a default value of 0 for the custom properties. When creating the smoother gradient, it turned out that the browser didn’t compute those zero values with calc, so the mask wouldn’t be applied at all until the values were updated with JS. For this reason, I’m setting the defaults as 0% instead, which works just fine. </p>
<p>Creating the menu animation </p>
<p>There’s one more finishing touch to the hero section, which is a bit of visual trickery: When the user clicks on the menu button, the spotlight expands to reveal the full-screen menu, seemingly underneath it. To create this effect, we need to give the menu an identical background to the one on our masked element. </p>
<p>:root {<br />
–gradientBg: linear-gradient(45deg, turquoise, darkorchid, deeppink, orange);<br />
} </p>
<p>.hero–secondary {<br />
background: var(–gradientBg);<br />
} </p>
<p>.menu {<br />
background: var(–gradientBg);<br />
} </p>
<p>The menu is absolute-positioned, the same as the masked hero element, so that it completely overlays the hero section. </p>
<p>Then we can use clip-path to clip the element to a circle 0% wide. The clip path is positioned to align with the menu button, at the top right of the viewport. We also need to add a transition, for when the menu is opened. </p>
<p>.menu {<br />
background: var(–gradientBg);<br />
clip-path: circle(0% at calc(100% – 2rem) 2rem);<br />
transition: clip-path 500ms;<br />
} </p>
<p>When a user clicks the menu button, we’ll use JS to apply a class of .is-open to the menu. </p>
<p>const menuButton = document.querySelector(‘[data-btn=”menu”]’)<br />
const menu = document.querySelector(‘[data-menu]’) </p>
<p>menuButton.addEventListener(‘click’, () = > {<br />
menu.classList.toggle(‘is-open’)<br />
}) </p>
<p>(In a real project there’s much more we would need to do to make our menu fully accessible, but that’s beyond the scope of this article.) </p>
<p>Then we need to add a little more CSS to expand our clip-path so that it reveals the menu in its entirety: </p>
<p>.menu.is-open {<br />
clip-path: circle(200% at calc(100% – 2rem) 2rem);<br />
} </p>
<p>See the Pen Hero with cursor tracking and menu by Michelle Barker (@michellebarker) on CodePen.dark </p>
<p>Text animation </p>
<p>In the final demo, we’re also implementing a staggered animation on the heading, before animating the spotlight into view. This uses Splitting.js to split the text into elements. As it assigns each character a custom property, it’s great for CSS animations. The GSAP timeline however, is a more convenient way to implement the staggered effect in this case, as it means we can let the timeline handle when to start the next animation after the text finishes animating. We’ll add that to the beginning of our timeline: </p>
<p>// Set initial text styles (before animation)<br />
gsap.set(“.hero–primary .char”, {<br />
opacity: 0,<br />
y: 25,<br />
}); </p>
<p>/* Timeline */<br />
const tl = gsap.timeline({ delay: 1 }); </p>
<p>tl<br />
.to(“.hero–primary .char”, {<br />
opacity: 1,<br />
y: 0,<br />
duration: 0.75,<br />
stagger: 0.1,<br />
})<br />
.to(hero, {<br />
“–maskSize1”: “20%”,<br />
duration: 0.5,<br />
ease: “back.out(2)”,<br />
})<br />
.to(hero, {<br />
“–maskSize2”: “28%”,<br />
“–maskSize3”: “calc(28% + 0.1rem)”,<br />
duration: 0.5,<br />
delay: 0.3,<br />
ease: “back.out(2)”,<br />
}); </p>
<p>I hope this inspires you to play around with CSS masks and the fun effects that can be created! </p>
<p>The full demo<br />
The post Dynamic CSS Masks with Custom Properties and GSAP appeared first on Codrops.</p>