Catch me if you can

In this example below you will see how to do a Catch me if you can with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by nilsvogt, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright nilsvogt ©
  • HTML
  • CSS
  • JavaScript
    <div class="container">
    <div>n</div>
    <div>i</div>
    <div>l</div>
    <div>s</div>
    <div>v</div>
    <div>o</div>
    <div>g</div>
    <div>t</div>
<div>

/*Downloaded from https://www.codeseek.co/nilsvogt/catch-me-if-you-can-lusjm */
    body { 
  background: #00b4f0; 
  color: #000; 
  font: 1em 'Helvetica neue', Arial, sans-serif 
}

.container { 
  width: 100px; 
  position: absolute; 
  top: 50%; 
  left: 50%; 
  margin: 0 0 0 -50px;
}

.container > div { 
  display: inline-block; 
}


/*Downloaded from https://www.codeseek.co/nilsvogt/catch-me-if-you-can-lusjm */
    $(function(){
    
    "use strict";
    
    var 
        $container = $('.container'),
        mouseX = $container.offset().left - 400,
        mouseY = 0,
        container_offset = $container.offset(),
        $items = $('> div', $container),
        mouseDidEnterStage = false;

    $(window).mousemove(function(e){
        mouseX = e.pageX;
        mouseY = e.pageY;
    });
  
    $(window).one('mouseenter', function(){
      mouseDidEnterStage = true;
    });
    
    var initializeItem = function( $item ){
        
        var position = $item.position();

        $item.css({
            left: position.left,
            top: position.top
        });

        var item_data = {x: position.left, y: position.top};

        item_data.currX = item_data.x;
        item_data.currY = item_data.y;
        item_data.originX = item_data.currX;
        item_data.originY = item_data.currY;
        item_data.limit = 250;

        $item.data('data', item_data);
    };

    var avoidCursor = function($item) {
        
        var item_data = $item.data('data'),
            containerMouseX = mouseX - container_offset.left,
            containerMouseY = mouseY - container_offset.top,
            itemMouseDist = Math.sqrt((containerMouseX - item_data.currX) * (containerMouseX - item_data.currX) + (containerMouseY - item_data.currY) * (containerMouseY - item_data.currY));
        
        item_data.currX = item_data.currX + (item_data.originX - item_data.currX) / 80;
        item_data.currY = item_data.currY + (item_data.originY - item_data.currY) / 80;
        
        if (itemMouseDist < item_data.limit) {
            item_data.currX = containerMouseX - item_data.limit * (containerMouseX - item_data.currX) / itemMouseDist;
            item_data.currY = containerMouseY - item_data.limit * (containerMouseY - item_data.currY) / itemMouseDist;
        }
        
        item_data.x = item_data.currX;
        item_data.y = item_data.currY;
        
        $item.css({
            top: item_data.y | 0,
            left: item_data.x | 0
        });

        $item.data('data', item_data);
    };

    $items.each(function(){
        initializeItem( $(this) );
    }).css('position', 'absolute');

    setInterval(function(){
      
        if(!mouseDidEnterStage){
            mouseX = (mouseX + 2)%$(window).width();
            mouseY = $container.offset().top+90;
        }
        $items.each(function(){
            avoidCursor( $(this) );
        });
    }, 1/60);
});


Comments