3

I'm interested in making an effect of a totally dark webpage (and in dark I mean dark as night without lights at all) and give the mouse cursor a light effect to light the surrounding.
What should I use to achieve that kind of effect? I've tried looking for the answer in CSS and generally on the web but haven't found anything similar.
The only thing I found is this plugin for WordPress but it's fixed and can't be customized or used.

Gidil
  • 4,137
  • 2
  • 34
  • 50
  • I don't think it can be done in pure CSS...I think you'd need to write it in JavaScript, either in the DOM or using canvas. I haven't seen anything that replicates this out of the box either. – bronzehedwick Jan 06 '14 at 21:20

1 Answers1

5

I know this is an old thread, but I was interested in making the effect myself, and whipped up something on jsfiddle that I think accomplishes the task.

The code is at jsfiddle, and copied here with explanations. Everything is pretty simple.

HTML

I created a div with the id light, then I used a wrapper div, called content, containing lorum ibsum.

<div id="light"></div>

<div class="content">  
    <h1>Flashlight test</h1>   
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
    <ul>
        <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
        <li>Aliquam tincidunt mauris eu risus.</li>
        <li>Vestibulum auctor dapibus neque.</li>
    </ul>
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
    
</div>

CSS

I styled the html element, which is the top level element on the page, to have black text and a black background, so everything would be hidden by default.

Next I styled the div with the light id. I made mine 100px high and 100px wide, made the background yellow, and gave it a border-radius of 50px, half the width/height, making a circle. I also made the div position: relative, so it's taken out of the normal page flow, and can lay on top of other elements, which is important to the layering we need to create the effect. I decided to throw in a default position for the flashlight, before the user moves the mouse (more on that in the JS section), so I set the top and left properties to 50%, which centers it on the page.

Finally, I added a rules to my content wrapper div, setting position: relative and the z-index to 10. position: relative or position: absolute are needed to get z-index to work; z-index controls which elements stack on top of which. So setting the content div, namely the text, to a higher z-index, makes it appear on top of the light div, which you can now see because of the lighter background.

html { 
    color: #000; 
    background-color: #000; 
}

.content {
    position: relative;
    z-index: 10;
}

#light {
    top: 50%;
    left: 50%;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    position: absolute;
    background-color: rgb(231, 221, 122);
}

JavaScript

The JS is only 6 lines, using jQuery. All it does is set the offset - the position - of the element using the mousemove() event function. That function has a parameter which holds coordinates for where the mouse is. subtract 50 pixels from that number to center the light, and you have your effect.

$(document).mousemove(function(event) {
    $('#light').offset({
        top: event.pageY - 50,
        left: event.pageX - 50
    });
});

Edit: Modern Update

Following the same principles as above, here's how to leverage more modern, vanilla coding practices using features that were not available when I originally wrote the answer.

Full demo here: https://codepen.io/bronzehedwick/pen/oNdRwYN

HTML

<div id="light" style="--light-position-y: 0; --light-position-x: 0"></div>
<div class="content">
  <h1>Flashlight test</h1>
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
  <ul>
    <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
    <li>Aliquam tincidunt mauris eu risus.</li>
    <li>Vestibulum auctor dapibus neque.</li>
  </ul>
  <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</div>

CSS

body {
  background-color: black;
}

.content {
  position: relative;
  z-index: 10;
}

#light {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  position: absolute;
  transform: translate(var(--light-position-x, 0px), var(--light-position-y, 0px));
  background-color: rgb(231, 221, 122);
}

JavaScript

var light = document.getElementById('light');

document
  .documentElement
  .addEventListener('mousemove', function handleMouseMove(event) {
    light.style.setProperty('--light-position-y', (event.clientY - 50) + 'px');
    light.style.setProperty('--light-position-x', (event.clientX - 50) + 'px');
  });
bronzehedwick
  • 2,862
  • 2
  • 22
  • 28