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

              
                .dot
.container
  - for (var i = 0; i < 54; i++)
    div.box
              
            
!

CSS

              
                $color-yellow: #fdce00;
$color-dark: #1d1c1b;

body, html {
  width: 100%;
  height: 100%;
  background-color: $color-dark;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.container {
  width: 140vmin;
  height: 90vmin;
  margin: 5vmin;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

.dot {
  position: fixed;
  width: 6vmin;
  height: 6vmin;
  margin-top: -3vmin;
  margin-left: -3vmin;
  background-color: white;
  border-radius: 50%;
  z-index: 2;
  box-shadow: 0 0 5px white, 0 0 20px 10px rgba(white,0.3);
}

.box {
  background-color: $color-yellow;
  width: 10vmin;
  height: 10vmin;
  margin: 2.5vmin;
  box-shadow: 0 0 10px rgba($color-yellow, 0.5);
}
              
            
!

JS

              
                // Inspired by this great Dribbble by JT Grauke:
// https://dribbble.com/shots/5711542-kinetic-magnetic-dot

const boxes = [...document.querySelectorAll('.box')];
const dot = document.querySelector('.dot');
const container = document.querySelector('.container');
const rect = container.getBoundingClientRect();
const coords = {
  x: [0, Math.round(rect.width / 4), Math.round(3 * rect.width / 4), rect.width],
  y: [0, Math.round(rect.height / 2), rect.height]
};
const pos = {x: coords.x[0] + rect.left, y: coords.y[0] + rect.top};

// Move the dot to the given coordinates, rotating
// all the boxes so that they are facing the given coordinates
const moveDot = (x, y) => {
  // Position the dot
  dot.style.top = `${y}px`;
  dot.style.left = `${x}px`;
  pos.x = x;
  pos.y = y;
  
  // Rotate all the boxes
  boxes.forEach(box => {
    const {top, left, width, height} = box.getBoundingClientRect();
    const adjacent = x - left - width / 2;
    const opposite = y - top - height / 2;
    
    // arctan gives us the degree based on the opposite & adjacent edges
    // of the triangle created between the box' center and the given
    // coordinates
    const radians = Math.atan(opposite / adjacent); 
    box.style.transform = `rotate(${radians}rad)`;
  });
};

const generateArray = length => [...new Array(length)].map((_, i) => i);
const random = arr => arr[Math.floor(Math.random() * arr.length)];

let animation;
let coordinate = 'x';
const prev = {x: 0, y: 0};

// Recursively animate the dot the a random set of coordinates
const animate = () => {
  const pool = generateArray(coords[coordinate].length);
  pool.splice(prev[coordinate], 1);
  const next = random(pool);
  prev[coordinate] = next;
  
  animation = TweenMax.to(pos, 1, {
    [coordinate]: coords[coordinate][next] + rect[coordinate],
    ease: Power2.easeInOut, 
    onUpdate: function() {
      moveDot(pos.x, pos.y);
    },
    onComplete: animate,
  });
  // Toggle the animated coordinate (animate a differen axis every time)
  coordinate = coordinate === 'x' ? 'y' : 'x';
}
animate();
const dAnimate = _.debounce(animate, 500);

// Stop animation on mouse move and follow the cursor until 
// it rests for 500ms, at which point the animation should
// start again
window.addEventListener('mousemove', ({x, y}) => {
  animation.kill();
  dAnimate();
  window.requestAnimationFrame(() => {
    moveDot(x, y);
  });
});
              
            
!
999px

Console