0

I'm trying to create some kind of flashlight effect. I have a div (fixed, radiant background, height and width 100%) masking the entire screen. On mouse move, I'd like to reveal what is behind the div in a flashlight way.

I don't find a way to make a portion of this div transparent (opacity: 0). See example attached:

enter image description here

The "Pré-inscription" button appears on mouse over, the gradiant remains around the mouse cursor (the circle is too small on the screenshot, I will make it bigger)

EDIT: for a better understanding, I created a Codepen. As you can see, the fixed radiant gradiant. The red circle should allow to see through the gradiant.

let magnifier = document.getElementById('magnifier')
let size = magnifier.offsetWidth

window.addEventListener('mousemove', (e) => {
  magnifier.style.top = e.clientY - (size / 2)+'px'
  magnifier.style.left = e.clientX - (size / 2)+'px'
  
})
#overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background: rgb(255,215,108);
  background: radial-gradient(circle, rgba(251, 92, 3, 1) 0%, rgba(255,215,108,1) 100%);
}

#magnifier {
  position: absolute;
  background: red;
  width: 7rem;
  height: 7rem;
  border-radius: 100%;
  top: -20rem;
  left: -20rem;
}
<h1 class="text-7xl">Coming soon</h1>

<div id="overlay">
  <div id="magnifier"></div>
</div>

Any idea? Thanks

Axel

Axel
  • 35
  • 3
  • Have huge div element that will follow mouse. Background image is whole non-transparent part + cutout – Justinas Jun 02 '23 at 06:43
  • Does this answers your question? [Mouse Tracking Spotlight](https://codepen.io/2123/pen/OKRoQg) – Justinas Jun 02 '23 at 06:45
  • Thanks Justinas, but no it doesn't. I know how to do it with a black background. But in my case, I need a radiant background as seen on the screen shot. If the background follows the mouse, the gradiant will move. – Axel Jun 02 '23 at 07:56
  • Does this answer your question? [Spotlight effect with css, html, javascript](https://stackoverflow.com/questions/73003714/spotlight-effect-with-css-html-javascript) – gre_gor Jun 02 '23 at 10:09
  • I have an idea, but haven't tested it. Use a clip path on the gradent overlay and then use another overlay over that to get the blur with a `filter: blur`. – Apodemus Jun 08 '23 at 07:35

2 Answers2

2

You could try something with an overlay with an inset shadow which tracks the mouse location.

document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
  var eventDoc, doc, body;

  eventDoc = (event.target && event.target.ownerDocument) || document;
  doc = eventDoc.documentElement;
  body = eventDoc.body;

  event.pageX = event.clientX +
    (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
    (doc && doc.clientLeft || body && body.clientLeft || 0);
  event.pageY = event.clientY +
    (doc && doc.scrollTop  || body && body.scrollTop  || 0) -
    (doc && doc.clientTop  || body && body.clientTop  || 0 );

  const spotlight = document.getElementById("spotlight");
  const boundingRect = spotlight.getBoundingClientRect();
  spotlight.style.left = `${event.pageX - boundingRect.width/2}px`
  spotlight.style.top = `${event.pageY - boundingRect.height/2}px`
}
html, body {
  margin: 0;
}

.bigoverlay {
  width: 100dvw;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

.spotlight {
  border-radius: 50%;
  width: 250vmax;
  height: 250vmax;
  box-shadow: 0 0 5vmax 110vmax inset black;
  position: absolute;
  z-index: -1;
  left: -75vmax;
  top: -75vmax;
}
<div class="bigoverlay" id="bigoverlay">
    <h1>Some hidden text</h1>
    <div class="spotlight" id="spotlight"></div>  
</div>
DarkBee
  • 16,592
  • 6
  • 46
  • 58
Apodemus
  • 579
  • 2
  • 19
  • 1
    I've added some dummy text to show the "flashlight" actually reveals "hidden" content – DarkBee Jun 02 '23 at 08:51
  • Thanks but on your example, the background is black. I have no issue creating a flashlight effect with a simple color (black or anything else). My issue is to create the same thing but with a fixed radial gradiant. The gradiant cannot move among the mouse, the gradiant center must remain at the center of the screen. – Axel Jun 02 '23 at 15:23
-2

To create a flashlight effect that reveals content behind a div on mouse move, you can use CSS and JavaScript. Here's an example

document.addEventListener('mousemove',
  function(event) {
    var flashlight =
      document.querySelector('.flashlight');
    var x = event.clientX;
    var y = event.clientY;
    var radius = 100;
    flashlight.style.backgroundPosition =
      x + 'px ' + y + 'px';
    flashlight.style.backgroundSize =
      radius + 'px ' + radius + 'px';
  });
.flashlight {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, rgba(0, 0, 0, 0.8), transparent);
  pointer-events: none;
  z-index: 9999;
}
<div class="flashlight"></div>

In this code, we have a fixed div with the class "flashlight" that covers the entire screen. It has a radial gradient background that starts with a dark color (rgba(0, 0, 0, 0.8)) and transitions to transparent, creating a flashlight effect.

We use JavaScript to listen for the mousemove event on the document. On each mouse move, we update the background position of the div to match the mouse coordinates (x, y). This makes the center of the flashlight follow the mouse cursor. We also set the size of the background to create the desired radius for the flashlight effect.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • 1
    Did you try to run your own code? It doesn't work properly for multiple reasons. – gre_gor Jun 02 '23 at 09:29
  • 1
    Welcome to StackOverflow! Please test your code before posting here. This code does not work as intended. – ethry Jun 02 '23 at 09:40
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 02 '23 at 09:41
  • The spotlight black in the center and not transparent. And the black isn't even opaque. Why is the background's size set in JS, if it's fixed? And why is it set at all? It just makes things worse. The spotlight doesn't even follow the cursor at the center. – gre_gor Jun 02 '23 at 10:06
  • Not working indeed like others said before. But thanks for trying – Axel Jun 02 '23 at 15:24