Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="eyes">
  <div class="eye"><div class="pupil"></div></div>
  <div class="eye"><div class="pupil"></div></div>
</div>
              
            
!

CSS

              
                body{
  color: white;
  height: 100vh;
  background: black;
  display: flex;
  align-items: center;
  justify-content: center;
}

/*Container that contains both eyes*/
.eyes{
  display: grid;
  grid-template-columns: 250px 250px;
  column-gap: 50px;
  row-gap: 15px;
  transition: 200ms;
}

/*Styling for an eye*/
.eye{
  width: 200px;
  height: 200px;
  background: white;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  overflow: hidden;
}

.pupil{
  width: 100px;
  height: 100px;
  background: red;
  margin-left: 0;
  margin-top: 0;
  border-radius: 50%;
}
              
            
!

JS

              
                //Define vars
var pupilLeft;
var pupilTop;

//Get the eyewidth (parent div of the pupils)
var eyeWidth = document.getElementsByClassName("eye")[0].offsetWidth;
var eyeHeight = document.getElementsByClassName("eye")[0].offsetHeight;

//Get the pupils
var pupil1 = document.getElementsByClassName("pupil")[0];
var pupil2 = document.getElementsByClassName("pupil")[1];
var pupils = [pupil1, pupil2];
//Get the pupils width and height
var pupilWidth = document.getElementsByClassName("pupil")[0].offsetWidth;
var pupilHeight = document.getElementsByClassName("pupil")[0].offsetHeight;

//On mousemove execute MoveEyes
document.addEventListener("mousemove", MoveEyes);

//Make pupils follow the cursor
function MoveEyes(event){
  //Get mouse positions
  var mouseX = (event.clientX / window.innerWidth) * 2 - 1;
  var mouseY = (event.clientY / window.innerHeight) * 2 - 1;
   
  //The standard offset (margins) for the pupils and keep the pupil in the eye
  var horizontalOffset = (eyeWidth - pupilWidth) - (pupilWidth/4); 
  var verticalOffset = (eyeHeight - pupilHeight) - (pupilHeight/4);
  
  //Calculate min and max that eye can move 
  pupilLeft = clamp(eyeWidth * mouseX, -horizontalOffset, horizontalOffset);
  pupilLeft = Math.floor(pupilLeft);
  pupilTop = clamp(eyeHeight * mouseY, -verticalOffset, verticalOffset);
  pupilTop = Math.floor(pupilTop);
  
  //Get all the pupils and change the margins
  pupils.forEach(pupil => {
    pupil.style.marginLeft = pupilLeft+"px";
    pupil.style.marginTop = pupilTop+"px";
    //Make sure width and height stay the same
    pupil.style.width = pupilWidth+"px";
    pupil.style.height = pupilHeight+"px";
  });
}

//Clamp between a min and max
function clamp(x, a, b) {
  if (x < a) return a;
  if (x > b) return b;
  return x;
}

              
            
!
999px

Console