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

              
                <main>

  <section id="panel">
    <header>Percentages</header>
    <form>
      <label>
        Snap X
        <input id="snap-x" type="number" value="10" min="5" max="50" step="0.1" autofocus />
      </label>
      <label>
        Snap Y
        <input id="snap-y" type="number" value="10" min="5" max="50" step="0.1" />
      </label>
      <label>
        Marker Width
        <input id="marker-width" type="number" value="20" min="1" max="100" step="0.1" />
      </label>
      <label>
        Marker Height
        <input id="marker-height" type="number" value="20" min="1" max="100" step="0.1" />
      </label>
      <label>
        Board Size
        <input id="board-size" type="number" value="80" min="1" max="100" step="0.1" />
      </label>
      <input type="submit" value="Apply" />
    </form>
  </section>

  <section id="board">
    <div id="marker"></div>
  </section>
</main>
              
            
!

CSS

              
                html, body {
  height: 100%;
  margin: 0;
}

body {
  background-color: #ddd;
  overflow: hidden;
}

main {
  background: #ddd;
  position: relative;
  height: 100vh;
}

#board {
  position: relative;
  top: 50%;
  left: 50%;
  z-index: 10;
  background: #d7d7d7;
}

#marker {
  position: absolute;
  background: rgba(128, 0, 128, 0.7);
  outline: 1px solid transparent;
  top: 0;
  left: 0;
  z-index: 1000;
}

.cell {
  background: #aeaeae;
  position: absolute;
  top: 0;
  left: 0;
}

#panel {
  position: absolute;
  padding: 10px;
  z-index: 20;
  background: #ddd;
  border: 1px solid #ccc;

  header {
    font-weight: bold;
    font-size: 14px;
  }
}

input {
  display: block;
}

label {
  display: block;
  font-size: 12px;
  margin: 6px 0;
}

[type="number"] {
  width: 100px;
  margin-top: 4px;
}

[type="submit"] {
  background: #ddd;
  box-shadow: none;
  outline: none;
  font-size: 12px;
  padding: 4px 8px;
  border: 1px solid #aaa;
  margin-top: 8px;

  &:focus {
    outline: none;
  }
}
              
            
!

JS

              
                var board, marker;

var input = {
  width  : $("#marker-width"),
  height : $("#marker-height"),
  snapX  : $("#snap-x"),
  snapY  : $("#snap-y"),
  size   : $("#board-size")
};

$("form").submit(init);

// BOARD ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
board = {
  _bounds : null,
  size    : null,
  target  : $("#board"),

  get bounds() { return this._bounds || this.target[0].getBoundingClientRect(); },
  set bounds(bounds) { this._bounds = bounds; },

  get cols() { return Math.floor(100 / marker._snapX); },
  get rows() { return Math.floor(100 / marker._snapY); },

  get width() { return this.bounds.width; },
  get height() { return this.bounds.height; }
}

// MARKER :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
marker = {
  _snapX   : null,
  _snapY   : null,
  target   : $("#marker"),
  dragger  : createDraggable("#marker"),
  width    : null,
  height   : null,
  xPercent : 0,
  yPercent : 0,

  get snapX() { return this._snapX + 100 % this._snapX / board.cols; },
  set snapX(snapX) { this._snapX = snapX; },

  get snapY() { return this._snapY + 100 % this._snapY / board.rows; },
  set snapY(snapY) { this._snapY = snapY; },

  get x() { return this.dragger.x; },
  get y() { return this.dragger.y; }
};

init();

// DRAGGABLE ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function createDraggable(target) {

  return new Draggable(target, {
    type      : "top,left",
    onPress   : onPress,
    onDrag    : onDrag,
    onRelease : onRelease
  });
}

function onPress() {

  // this will cause it to recalculate
  board.bounds = null;

  setAbsolute();
  this.update();
}

function onRelease() {

  board.bounds = null;

  this.update();
  setRelative();
}

function onDrag() {

  // snap the marker when it's on the board
  if (this.hitTest(board.target)) {

    // calculate position as a percent
    var xPercent = this.x / board.width  * 100;
    var yPercent = this.y / board.height * 100;

    // calculate snap as a percent and store the value
    marker.xPercent = Math.round(xPercent / marker.snapX) * marker.snapX;
    marker.yPercent = Math.round(yPercent / marker.snapY) * marker.snapY;

    // set snap value in px
    TweenLite.set(this.target, {
      left : board.width  * marker.xPercent / 100,
      top  : board.height * marker.yPercent / 100
    });

  } else {
    marker.xPercent = 0;
    marker.yPercent = 0;
  }
}

// POSITIONING ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function setAbsolute() {

  // calculate px value
  var left = marker.xPercent * board.width  / 100;
  var top  = marker.yPercent * board.height / 100;

  TweenLite.set(board.target, { zIndex: 30 });
  TweenLite.set(marker.target, { left: left, top: top });
}

function setRelative() {

  // if the percent isn't set, calculate it
  marker.xPercent = marker.xPercent || marker.x / board.width  * 100;
  marker.yPercent = marker.yPercent || marker.y / board.height * 100;

  TweenLite.set(board.target, { zIndex: 10 });
  TweenLite.set(marker.target, {
    left : marker.xPercent + "%",
    top  : marker.yPercent + "%"
  });
}

// SIZING :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function updateSize() {

  var winWidth  = window.innerWidth;
  var winHeight = window.innerHeight;
  var landscape = (winWidth > winHeight);

  var min  = Math.min(winWidth, winHeight);
  var max  = Math.max(winWidth, winHeight);
  var size = board.size * min / 100;

  TweenLite.set(board.target, {
    xPercent   : -50,
    yPercent   : -50,
    width      : size / (landscape ? max : min) * 100 + "%",
    paddingTop : size / (landscape ? max : min) * 100 + "%"
  });

  TweenLite.set(marker.target, {
    width  : marker.width  + "%",
    height : marker.height + "%"
  });
}

// GRID :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function createGrid() {

  // remove previous cells
  $(".cell").remove();

  for (var row = 0; row < board.rows; row++) {
    for (var col = 0; col < board.cols; col++) {

      // skip every other one to create a checkerboard pattern
      if (((row % 2) && !(col % 2)) || (!(row % 2) && (col % 2))) continue;

      var target = $("<div class='cell' />").appendTo(board.target);

      TweenLite.set(target, {        
        width  : marker.snapX + "%",
        height : marker.snapY + "%",
        left   : col * marker.snapX + "%",
        top    : row * marker.snapY + "%"
      });
    }
  }
}

// INIT :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function init(event) {

  // prevent when called by submit
  event && event.preventDefault();

  // cast input values to numbers
  board.size    = +input.size.val();
  marker.snapX  = +input.snapX.val();
  marker.snapY  = +input.snapY.val();
  marker.width  = +input.width.val();
  marker.height = +input.height.val();

  createGrid();
  updateSize();

  // snap the marker
  if (Draggable.hitTest(marker.target, board.target)) {
    onDrag.call(marker.dragger);
  	onRelease.call(marker.dragger);
  }
}





              
            
!
999px

Console