Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="grid"></div>

<svg id="base-eye" class="eye" viewbox="0 0 1000 1000" xmlns="http://www.w3.org/2000/svg" fill="black">
  <clipPath id="lids">
    <path id="lids-path"
      stroke-linejoin="round" stroke-linecap="round"
      d="M 50 500 Q 500 0 950 500 Q 500 850 50 500"
    />
  </clipPath>
  <g clip-path="url(#lids)">
    <rect class="whites" width="1000" height="1000" fill="#fff"/>
    <g class="pupil-group">
      <circle class="pupil" cx="500" cy="500" r="150" fill="#000"/>
      <circle class="glint" cx="450" cy="450" r="20" fill="#fff"/>
    </g>
  </g>
  <use href="#lids-path" class="lids" stroke="#000" stroke-width="20" />
</svg>
            
          
!
            
              .eye {
  --pupil-x: 0;
  --pupil-y: 0;
  --color-whites: var(--fg, #fff);
  --color-lid: var(--bg, #fff);
  --color-pupil: var(--bg, #000);
  --color-glint: var(--fg, #fff);
  --scale: 0.75;
  max-width: 100%;
  max-height: 100vh;
  fill: none;
  transform: scale(var(--scale));
}
.lids {
  stroke: var(--color-lid);
  stroke-width: 5%;
}
.whites {
  fill: var(--color-whites);
}
.pupil {
  fill: var(--color-pupil);
  cx: 500px;
  cy: 500px;
}
.glint {
  fill: var(--color-glint);
}
.pupil-group {
  transform: translate(calc(var(--pupil-x) * 1px), calc(var(--pupil-y) * 1px));
}
#base-eye {
  display: none;
}
#grid {
  --num-columns: 5;
  --num-rows: 5;
  width: 100%;
  height: 100vh;
  display: grid;
  grid-template-columns: repeat(var(--num-columns), 1fr);
  grid-template-rows: repeat(var(--num-rows), 1fr);
}
body {
  --bg: #111;
  --fg: #fff;
  min-height: 100vh;
  background: var(--bg);
  overflow: hidden;
}

            
          
!
            
              const baseObject = document.getElementById("base-eye");
const grid = document.getElementById("grid");
let eyes, eyeCenters;
let eyeDensity = 5;
let numEyesX, numEyesY;
const center = new THREE.Vector2();
const mousePos = new THREE.Vector2();
const PI = Math.PI;
let maxDist;
const maxEyeTravelX = 275;
const maxEyeTravelY = 100;

init();

function init() {
  window.addEventListener("resize", throttled(handleResize));
  window.addEventListener("mousemove", throttled(handleMouseMove));
  handleResize();
  console.log("init");
}

function handleMouseMove(event) {
  mousePos.set(event.clientX, event.clientY);
  eyes.forEach((eye, i) => {
    const vecToMouse = new THREE.Vector2().subVectors(mousePos, eyeCenters[i]);
    const angle = vecToMouse.angle();
    const dist = mousePos.distanceTo(eyeCenters[i]);
    const distPercent = map(dist, 0, maxDist, 0, 1);
    const clampedMouseX = clamp(vecToMouse.x, maxEyeTravelX * -1, maxEyeTravelX);
    const clampedMouseY = clamp(vecToMouse.y, maxEyeTravelY * -1, maxEyeTravelY);
    const pupilX = map(clampedMouseX, 0, maxEyeTravelX, 0, maxEyeTravelX);
    const pupilY = map(clampedMouseY, 0, maxEyeTravelY, 0, maxEyeTravelY);
    const scale = map(dist, 0, maxDist, 0.5, 1.25);
    
    eye.style.setProperty("--pupil-x", pupilX);
    eye.style.setProperty("--pupil-y", pupilY);
    eye.style.setProperty("--scale", scale);
  });
}

function handleResize() {
  
  // recreate the grid and elements 
  const largeSide = Math.max(innerWidth, innerHeight);
  const size = Math.round(largeSide / eyeDensity);
  numEyesX = Math.ceil(innerWidth / size);
  numEyesY = Math.ceil(innerHeight / size);
  grid.style.setProperty("--num-columns", numEyesX);
  grid.style.setProperty("--num-rows", numEyesY);
  grid.innerHTML = "";
  generateArrowGrid();
  
  center.set(innerWidth * 0.5, innerHeight * 0.5);
  maxDist = center.length() * 2;
  
  // send a fake mouse event to trigger the initial point
  handleMouseMove({clientX: center.x, clientY: center.y});
}

function generateArrowGrid() {
  eyes = [];  
  eyeCenters = [];
  for (let i = 0; i < numEyesX * numEyesY; i += 1) {
    
    // add the eye to the grid
    const newArrow = baseObject.cloneNode(true);
    newArrow.id = `eye-${i}`;
    newArrow.setAttribute("class", "eye");
    grid.appendChild(newArrow);
    eyes.push(newArrow);
    
    // save its center point for use later
    const eyeRect = newArrow.getBoundingClientRect();
    const eyeCenter = new THREE.Vector2(
      eyeRect.left + (newArrow.clientWidth * 0.5),
      eyeRect.top + (newArrow.clientHeight * 0.5),
    );
    eyeCenters.push(eyeCenter);
  }
}

// USEFUL FUNCTIONS
function throttled(fn) {
  let didRequest = false;
  return param => {
    if (!didRequest) {
      requestAnimationFrame(() => {
        fn(param);
        didRequest = false;
      });
      didRequest = true;
    }
  };
}
function map(value, min1, max1, min2, max2) {
  return (value - min1) * (max2 - min2) / (max1 - min1) + min2;
}
function clamp (value, min = 0, max = 1) {
  return value <= min ? min : value >= max ? max : value;
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console