cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <div class="tileContainer">

</div>
            
          
!
            
              body{background-color:#1A1919;text-align:center;}

.tileContainer{
  border:10px solid black;
  display:inline-block;
  position:relative;
  width:1024px;
  height:768px;
  overflow:hidden;
  box-sizing:border-box;
}


.tile{
  position:relative;
  vertical-align:top;
  display:inline-block;
  border-right:1px solid rgba(0, 0, 0, 0.5);
  border-bottom:1px solid rgba(0, 0, 0, 0.5);  
  box-sizing:border-box;
}

.tile:after{
  content:"";
  background-color:#cc1c32;
  width:6px;
  height:6px;
  position:absolute;
  top:100%;
  right:0px;
  transform:translate(50%, -50%);
  z-index:2;
  line-height:1;
}

/*-- no grid --*/

.noGrid .tile{
  border-right:0px solid rgba(0, 0, 0, 0.5);
  border-bottom:0px solid rgba(0, 0, 0, 0.5);
}

.noGrid .tile:after{
  content:none;
}
            
          
!
            
              // options
var options = {
  imgSrc : "https://unsplash.it/g/1024/768?image=887",
  containerName : "tileContainer",
  grid : false,
  tileWidth : 80,
  tileHeight : 80,
  mouseTrail:true
}

// ----------------------------------------------------------
var tileWidth, tileHeight, numTiles, tileHolder, tileContainer;
var directionX, directionY;
var imgOriginalWidth, imgOriginalHeight;
var imgCoverWidth, imgCoverHeight;
var imageLoaded = false;

numTiles=0;
tileWidth = options.tileWidth;
tileHeight = options.tileHeight;

tileContainer = document.getElementsByClassName(options.containerName)[0];

function init(){
  if(options.grid == false)tileContainer.className += " noGrid";
  
  //preload image and get original image size, then create tiles
  var image = new Image();
  image.src = options.imgSrc;
  image.onload = function(e){
    imageLoaded = true;
    imgOriginalWidth = e.currentTarget.width;
    imgOriginalHeight = e.currentTarget.height;
    
    createTileHolder();
    checkTileNumber();
    positionImage();
    addListeners();
  };  
}

function resizeHandler()
{
  if(imageLoaded == false)return;
  
  //not working yet
  
  checkTileNumber();
  positionImage();
}

function createTileHolder(){
  tileHolder = document.createElement('div');
  tileHolder.className = "tileHolder";
  tileHolder.style.position = "absolute";
  tileHolder.style.top = "50%";
  tileHolder.style.left = "50%";
  tileHolder.style.transform = "translate(-50%, -50%)";
  tileContainer.appendChild(tileHolder);
}

function checkTileNumber()
{
  tileHolder.style.width = Math.ceil(tileContainer.offsetWidth / tileWidth) * tileWidth + "px";
  tileHolder.style.height = Math.ceil(tileContainer.offsetHeight / tileHeight) * tileHeight + "px";
  
  var tilesFitInWindow = Math.ceil(tileContainer.offsetWidth / tileWidth) * Math.ceil(tileContainer.offsetHeight / tileHeight);
  if(numTiles < tilesFitInWindow){
    for (var i=0, l=tilesFitInWindow-numTiles; i < l; i++){
      addTiles();
    }
  }else if(numTiles > tilesFitInWindow){
      for (var i=0, l=numTiles-tilesFitInWindow; i < l; i++){
        removeTiles();
      }
  }  
}


function addTiles()
{
  var tile = document.createElement('div');
  tile.className = "tile";  
  
  //maintain aspect ratio
  imgCoverWidth = tileContainer.offsetWidth;
  imgCoverHeight = tileContainer.offsetHeight;

  if(imgOriginalWidth > imgOriginalHeight){
        imgCoverHeight = imgOriginalHeight / imgOriginalWidth * imgCoverWidth;
    }else{
        imgCoverWidth = imgOriginalWidth / imgOriginalHeight * imgCoverHeight;     
    } 
  
  
  tile.style.background = 'url("'+options.imgSrc+'") no-repeat';
  tile.style.backgroundSize  = imgCoverWidth + "px " +  imgCoverHeight + "px";
  tile.style.width = tileWidth + "px";
  tile.style.height = tileHeight + "px";
  document.querySelectorAll(".tileHolder")[0].appendChild(tile);
  
  tile.addEventListener("mouseover", moveImage);  
  
  numTiles++;
}

function removeTiles()
{
  var tileToRemove = document.querySelectorAll(".tile")[0];
  tileToRemove.removeEventListener("mouseover", moveImage); 
  
  TweenMax.killTweensOf(tileToRemove);
  tileToRemove.parentNode.removeChild(tileToRemove);
  
  numTiles--;
}

function addListeners(){
 if(options.mouseTrail){
    document.addEventListener('mousemove', function (event) {
      directionX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
      directionY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
    });
  }
}

function positionImage(){
  for(var t=0, l=numTiles; t < l; t++)
  {
    var nowTile = document.querySelectorAll(".tile")[t];
   
    var left = (-nowTile.offsetLeft - (tileHolder.offsetLeft - (tileHolder.offsetWidth/2)));
    var top = (-nowTile.offsetTop - (tileHolder.offsetTop - (tileHolder.offsetHeight/2)));
    
    nowTile.style.backgroundPosition = left + "px " + top + "px";
  }
}

function resetImage(nowTile){    
  var left = (-nowTile.offsetLeft - (tileHolder.offsetLeft - (tileHolder.offsetWidth/2)));
  var top = (-nowTile.offsetTop - (tileHolder.offsetTop - (tileHolder.offsetHeight/2)));
  
  
  TweenMax.to(nowTile, 1, {backgroundPosition:left + "px " + top + "px", ease:Power1.easeInOut});
}


function moveImage(e){
  var nowTile = e.currentTarget
  var minWidth = -tileContainer.offsetWidth+nowTile.offsetWidth;
  var minHeight = -tileContainer.offsetHeight+nowTile.offsetHeight;
  var nowLeftPos = (-nowTile.offsetLeft - (tileHolder.offsetLeft - (tileHolder.offsetWidth/2)));
  var nowTopPos = (-nowTile.offsetTop - (tileHolder.offsetTop - (tileHolder.offsetHeight/2)))
  var offset = 60;
  var left = nowLeftPos;
  var top = nowTopPos;
    
  if(options.mouseTrail){
    //direction-aware movement
    if(directionX > 0){
      left = nowLeftPos + offset;
    }else if(directionX < 0){
      left = nowLeftPos - offset;
    }
    
    if(directionY > 0){
      top = nowTopPos + offset;
    }else if(directionY < 0){
      top = nowTopPos - offset;
    }
  }else{
    //random movement
    left = getRandomInt(nowLeftPos - offset , nowLeftPos + offset);
    top = getRandomInt(nowTopPos - offset, nowTopPos + offset);
  }
    
  // bounds
  if(left < minWidth)left=minWidth;
  if(left > 0)left=0;
  if(top < minHeight)top=minHeight;
  if(top > 0)top=0;
  
  //tween
  TweenMax.to(nowTile, 1.5, {backgroundPosition:left + "px " + top + "px", ease:Power1.easeOut, onComplete:resetImage, onCompleteParams:[nowTile]});
}

///////////////////////////////////////////////////////////////////

init();
// handle event
//window.addEventListener("optimizedResize", resizeHandler);

////////////////////////UTILS//////////////////////////////////////
//////////////////////////////////////////////////////////////////

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

(function() {
    var throttle = function(type, name, obj) {
        obj = obj || window;
        var running = false;
        var func = function() {
            if (running) { return; }
            running = true;
             requestAnimationFrame(function() {
                obj.dispatchEvent(new CustomEvent(name));
                running = false;
            });
        };
        obj.addEventListener(type, func);
    };

    /* init - you can init any event */
    throttle("resize", "optimizedResize");
})();

            
          
!
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.
Loading ..................

Console