Pen Settings

HTML

CSS

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

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

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.

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

              
                <div class="grid">
  <div class="grid-view"></div>
  <div class="viewport">
    <div class="drag-proxy"></div>
  </div>
</div>

<div class="controls">  
  <button id="zoom">Zoom</button>
  <button id="up">Up</button>
  <button id="down">Down</button>
  <button id="left">Left</button>
  <button id="right">Right</button>
</div>

              
            
!

CSS

              
                html, body {
  height: 100%;
}

body {  
  overflow: hidden;
}

* {
  box-sizing: border-box;
}

section {
  position: relative;
}

button {
  display: block;
  margin: 10px;
  width: 80px;
}

.controls {
  width: 100px;
  position: absolute;
  left: 0;
  bottom: 0;
  border: 1px solid #aaa;
  background: #ccc;
}

.grid {
  position: absolute; 
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.grid-view {
  position: relative;
  width: 100%;
  height: 100%;  
  overflow: hidden; 
}

.row {
  
}

.cell {
  position: relative;
  display: inline-block;
  width: 100vw;
  height: 100vh;  
}

.cell-content {
  position: relative;
  width: calc(100% - 20px);
  height: calc(100% - 20px);
  left: 10px;
  top: 10px;
  padding: 15px;
  font-size: 18px;
  font-family: monospace;
  border: 1px solid rgba(0,0,0,0.25);
}

.viewport {
  position: absolute;  
  width: 100vw;
  height: 100vh;   
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);  
  border: 2px solid transparent;
}

.drag-proxy {
  position: absolute;
  top: 0;
  left: 0;  
}




              
            
!

JS

              
                console.clear();
var log = console.log.bind(console);

var Action = {
  Up: 0,
  Down: 1,
  Left: 2,
  Right: 3
};

var grid = Grid({
  gridView: $(".grid-view"),
  viewport: $(".viewport"),
  swipeSpeed: 300,
  pivotRow: 3,
  pivotCol: 3,
  rows: 7,
  cols: 7
});

bindEvents(grid);

//
// DRAG PROXY
// ===========================================================================
function DragProxy(grid) {
  
  var view     = grid.view;  
  var viewport = grid.viewport;
  var gridView = grid.gridView;
  
  var draggable = new Draggable(".drag-proxy", {
    trigger: ".grid",
    onDrag: onDrag,
    onDragStart: onDragStart,
    onDragEnd: onDragEnd,
    onPress: onPress,
    onRelease: onRelease
  });
        
  var pointer  = draggable.target;
  var tracker  = ThrowPropsPlugin.track(pointer, "x,y");  
  var dragAxis = null;
  var hasDrag  = false;
  var target   = null;
  var velocity = grid.swipeSpeed;
  
  var startX = 0;
  var startY = 0;
  
  var moveX = 0;
  var moveY = 0;  
      
  function onRelease() {        
    if (!hasDrag) view.reset();
  }
  
  function setPosition() {
    
    if (view.isActive()) {
      view.kill();
      dragAxis = grid.axis;
      target   = grid.xAxis ? view.currentRow : gridView;
    }      
        
    moveX  = moveY = 0;    
    startX = view.cx;
    startY = view.cy;
    
    TweenLite.set(pointer, { x: startX, y: startY });
    draggable.update();
  }
    
  function onPress() {
        
    dragAxis = null;
    target   = null;
    hasDrag  = false;
        
    setPosition();
  }
  
  function onDrag() {
            
    moveX = this.x - startX;
    moveY = this.y - startY;
    
    if (!dragAxis) setAxis(moveX, moveY);
    if (!target) return;
        
    if (grid.xAxis) {      
      TweenLite.set(target, { x: moveX });            
      if (Math.abs(moveX) > view.vw) this.endDrag(event);    
      return;
    } 
    
    if (grid.yAxis) {      
      TweenLite.set(target, { y: moveY });      
      if (Math.abs(moveY) > view.vh) this.endDrag(event);
      return;
    }    
  }
    
  function onDragStart(event) {
    hasDrag = true;
  }
  
  function setAxis(dx, dy) {
    
    dx = Math.abs(dx);
    dy = Math.abs(dy);

    if (dx > 2)  dragAxis = grid.axis = "x";
    if (dy > dx) dragAxis = grid.axis = "y";
          
    if (dragAxis) {
      target = grid.xAxis ? view.currentRow : grid.gridView;
    }
  }
      
  function onDragEnd() {
        
    var vx = tracker.getVelocity("x");
    var vy = tracker.getVelocity("y");
        
    if (grid.xAxis) {
          
      if (moveX >  view.cx || vx >  velocity) return grid.moveRight();
      if (moveX < -view.cx || vx < -velocity) return grid.moveLeft();
      
      view.reset();      
      return;
    }
    
    if (grid.yAxis) {
      
      if (moveY >  view.cy || vy >  velocity) return grid.moveDown();
      if (moveY < -view.cy || vy < -velocity) return grid.moveUp();   
      
      view.reset();      
      return;
    }
  }
      
  return draggable;
}

//
// CELL
// ===========================================================================
function Cell(config) {
  
  var id = "cell-" + config.id;
  var bg = "hsla(" + config.hue + ", 100%, 80%, 1)";
    
  var content = $("<div/>", { "class": "cell-content"})
    .html("Row: " + config.y + " <br>Col: " + config.x);
  
  var element = $("<div/>", { "class": "cell", id: id })
    .html(content)
    .appendTo(config.row); 
  
  TweenLite.set(content, { backgroundColor: bg });
  
  return element;
}

//
// VIEW
// ===========================================================================
function View(grid) {
  
  var tl = new TimelineMax();
  var viewRows = [];  
    
  var view = {
    addRow: addRow,
    init: init,
    isActive: isActive,
    layout: layout,
    timeline: tl,
    viewRows: viewRows,
    vw: 0,
    vh: 0,
    cx: 0,
    cy: 0,
    kill: kill,
    currentRow: null,
    reset: reset,
    zoom: zoom
  };
  
  var gridView = null;
  var viewport = null;
  var isZoomed = true;
  var yPercent = 0;
  var xPercent = 0;    
  
  $(window).resize(resize);
   
  function init() {
    
    viewport = grid.viewport;
    gridView = grid.gridView;
    xPercent = 100 / grid.cols;
    yPercent = 100 / grid.rows;
        
    TweenLite.set(gridView, {
      width:  grid.rows + "00vw",
      height: grid.cols + "00vh"
    });
    
    view.currentRow = $(".row").eq(grid.pivotRow);        
    grid.dragProxy  = DragProxy(grid);
    resize();
  }
  
  function kill() {
    tl.kill();
  }
  
  function reset() {
    
    tl.clear()
      .to(gridView, 0.2, { y: 0, yPercent: 0 }, 0)
      .to(viewRows, 0.2, { x: 0, xPercent: 0 }, 0);
  }
  
  function zoom() {       
    
    isZoomed = !isZoomed;
    
    if (isZoomed) {
           
      TweenLite.to(".cell",  0.3, { width: "100vw", height: "100vh", onComplete: resize });
      TweenLite.to(viewport, 0.3, { width: "100vw", height: "100vh", borderColor: "transparent" });
      TweenLite.to(gridView, 0.3, { width: grid.rows + "00vw", height: grid.cols + "00vh" });
      
    } else {
      
      TweenLite.to(".cell",  0.3, { width: xPercent + "vw", height: yPercent + "vh" });
      TweenLite.to(viewport, 0.3, { width: xPercent + "vw", height: yPercent + "vh", borderColor: "red" });
      TweenLite.to(gridView, 0.3, { width: "100vw", height: "100vh", onComplete: resize });
    }
  }
  
  function resize() {
    view.vw = grid.viewport.width();
    view.vh = grid.viewport.height();
    view.cx = view.vw / 2;
    view.cy = view.vh / 2;
  }
   
  function isActive() {
    return tl.isActive();
  }
  
  function addRow(id) {    
    var row = $("<div />", { "class": "row", id: "row-" + id }).appendTo(grid.gridView);      
    viewRows.push(row);
    return row;
  }
    
  function layout() {
        
    tl.clear();
    
    var time = 0.35;
    
    switch (grid.action) {
        
      case Action.Up:
        
        gridView.append(gridView.children().first());
        view.currentRow = $(".row").eq(grid.pivotRow);
        tl.fromTo(gridView, time, { yPercent: "+=" + yPercent }, { y: 0, yPercent: 0 });        
        break;
        
      case Action.Down:
        
        gridView.prepend(gridView.children().last());
        view.currentRow = $(".row").eq(grid.pivotRow);        
        tl.fromTo(gridView, time, { yPercent: "-=" + yPercent }, { y: 0, yPercent: 0 });
        break;
        
      case Action.Left:
        
        var row = view.currentRow = $(".row").eq(grid.pivotRow);
        row.append(row.children().first());
        tl.fromTo(row, time, { xPercent: "+=" + xPercent }, { x: 0, xPercent: 0 });        
        break;        
        
      case Action.Right:
        
        var row = view.currentRow = $(".row").eq(grid.pivotRow);
        row.prepend(row.children().last());
        tl.fromTo(row, time, { xPercent: "-=" + xPercent }, { x: 0, xPercent: 0 });
        break;
    }
  }  
  
  return view;
}

//
// GRID
// ===========================================================================
function Grid(options) {
  
  var grid = {};
  var view = View(grid);
    
  $.extend(grid, {
    action: null,
    axis: "x",
    moveUp: moveUp,
    moveDown: moveDown,
    moveLeft: moveLeft,
    moveRight: moveRight,
    view: view,
  }, options);
  
  Object.defineProperties(grid, {
    "xAxis": { get: function() { return grid.axis === "x"; }},
    "yAxis": { get: function() { return grid.axis === "y"; }}
  });
  
  init();
  
  function init() {
    
    var total = grid.rows * grid.cols;    

    for (var y = 0, count = 0, row; y < grid.rows; y++) {      
      row = view.addRow(y);
      for (var x = 0; x < grid.cols; x++, count++) {      
        
        Cell({
          x: x,
          y: y,
          id:  count,
          hue: count / total * 360,
          row: row
        });
      }
    }

    view.init();
  }
      
  function moveLeft() {
    
    if (grid.yAxis && view.isActive()) return;
    
    grid.action = Action.Left;
    grid.axis = "x";    
    view.layout();
  }
  
  function moveRight() {
    
    if (grid.yAxis && view.isActive()) return;
    
    grid.action = Action.Right;
    grid.axis = "x";    
    view.layout();
  }
  
  function moveUp() {
    
    if (grid.xAxis && view.isActive()) return;
    
    grid.action = Action.Up;
    grid.axis = "y";    
    view.layout();
  }
  
  function moveDown() {
    
    if (grid.xAxis && view.isActive()) return;
    
    grid.action = Action.Down;
    grid.axis = "y";    
    view.layout();
  }
  
  return grid;
}

//
// EVENTS
// ===========================================================================
function bindEvents(grid) {
    
  
  $("#up").click(function() { grid.moveUp(); });
  $("#down").click(function() { grid.moveDown(); });
  $("#left").click(function() { grid.moveLeft(); });
  $("#right").click(function() { grid.moveRight(); });

  $("#zoom").click(function() { grid.view.zoom(); }).focus();
  
  $(window).keydown(function(event) {

    switch (event.keyCode) {
      case 37: case 39: case 38: case 40: 
        event.preventDefault(); break;
    }  
  });

  $(window).keyup(function(event) {  

    var handled = false;

    switch (event.keyCode) {
      case 37: grid.moveLeft();  handled = true; break;
      case 39: grid.moveRight(); handled = true; break;
      case 38: grid.moveUp();    handled = true; break;
      case 40: grid.moveDown();  handled = true; break;
    }

    if (handled) event.preventDefault();
  });
}

              
            
!
999px

Console