Guest User

pointer-pointer-improved

a guest
Oct 20th, 2025
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   // Kill their React app
  2.   document.getElementById('root').innerHTML = '';
  3.  
  4.   // Create our own simple container
  5.   const container = document.createElement('div');
  6.   container.style.cssText = `
  7.     position: fixed;
  8.     top: 0;
  9.     left: 0;
  10.     width: 100vw;
  11.     height: 100vh;
  12.     background: black;
  13.     overflow: hidden;
  14.   `;
  15.   document.body.appendChild(container);
  16.  
  17.   // Image element
  18.   const img = new Image();
  19.   img.style.cssText = `
  20.     position: absolute;
  21.     transform-origin: 0 0;
  22.     transition: opacity 0.15s;
  23.   `;
  24.   container.appendChild(img);
  25.  
  26.   let positions = [];
  27.   let lastIndex = -1;
  28.   let currentMouse = { x: 0, y: 0 };
  29.  
  30.   // Load positions
  31.   fetch('/new-positions.json')
  32.     .then(r => r.json())
  33.     .then(data => {
  34.       positions = data;
  35.       console.log('✅ Loaded', positions.length, 'positions');
  36.     });
  37.  
  38.   // Find nearest position
  39.   function findNearest(x, y) {
  40.     const nx = x / window.innerWidth;
  41.     const ny = y / window.innerHeight;
  42.  
  43.     let minDist = Infinity;
  44.     let bestIndex = 0;
  45.  
  46.     for (let i = 0; i < positions.length; i++) {
  47.       const [px, py] = positions[i];
  48.       const dist = (nx - px) ** 2 + (ny - py) ** 2;
  49.       if (dist < minDist) {
  50.         minDist = dist;
  51.         bestIndex = i;
  52.       }
  53.     }
  54.  
  55.     return bestIndex;
  56.   }
  57.  
  58.   // Position image so pointing location aligns with cursor
  59.   function positionImage() {
  60.     if (!img.complete || lastIndex === -1) return;
  61.  
  62.     const [px, py] = positions[lastIndex];
  63.     const vw = window.innerWidth;
  64.     const vh = window.innerHeight;
  65.     const iw = img.naturalWidth;
  66.     const ih = img.naturalHeight;
  67.  
  68.     // Scale to cover viewport
  69.     const scale = Math.max(vw / iw, vh / ih) * 1.2;
  70.  
  71.     // Where the pointing location ends up after scaling
  72.     const pointX = px * iw * scale;
  73.     const pointY = py * ih * scale;
  74.  
  75.     // Translate so pointing location moves to cursor
  76.     const tx = currentMouse.x - pointX;
  77.     const ty = currentMouse.y - pointY;
  78.  
  79.     img.style.transform = `translate(${tx}px, ${ty}px) scale(${scale})`;
  80.   }
  81.  
  82.   // Update image
  83.   function updateImage(index) {
  84.     if (index === lastIndex || !positions.length) return;
  85.     lastIndex = index;
  86.  
  87.     img.style.opacity = '0';
  88.     img.onload = () => {
  89.       positionImage();
  90.       img.style.opacity = '1';
  91.     };
  92.     img.src = `/images/${index}.jpg`;
  93.   }
  94.  
  95.   // Track mouse
  96.   document.addEventListener('mousemove', (e) => {
  97.     currentMouse = { x: e.clientX, y: e.clientY };
  98.     const index = findNearest(e.clientX, e.clientY);
  99.     updateImage(index);
  100.     positionImage(); // Re-position current image as mouse moves
  101.   });
  102.  
  103.   // Initial image at center
  104.   setTimeout(() => {
  105.     currentMouse = { x: window.innerWidth / 2, y: window.innerHeight / 2 };
  106.     const index = findNearest(currentMouse.x, currentMouse.y);
  107.     updateImage(index);
  108.   }, 100);
  109.  
  110.   console.log('🚀 Perfect pointer alignment! Move your mouse.');
  111.  
Advertisement
Add Comment
Please, Sign In to add comment