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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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#container
  div.v-pane
    div
      button#tile-clear Clear
      button#tile-add-area Add 10

  div.v-pane#grid

  div.v-pane
    div
      button#tile-create Create
      button#tile-discard Discard
    label
      input(type="radio", name="layer", value=0 checked="true")
      span Base
    label
      input(type="radio", name="layer", value=1)
      span Obstacle
    pre
      code#json
            
          
!
            
              body {
  background-color: #333
}

.v-pane {
  display: inline-block
  vertical-align: top
}

.tile {
  color: white
  display: inline-block
  background-color: blue;
  width: 23px;
  height: 23px;
  position: absolute;
}
.tile.hover {
  background-color: rgba(255, 0, 0, 0.5)
  z-index: 100
}
.tile.hover.clicked {
  display: none
}
.tile.tmpTile {
  display: none
  background-color: rgba(255, 0, 0, 0.25)
  z-index: 99
}
.tile.tmpTile.dragged {
  display: inline-block
}
.tile.drawn {
  background-color: rgba(64, 64, 255, 0.75)
}

#json {
  display: inline-block;
  height: 5em;
  background-color: #eee;
  font-family: monospace
}

#grid {
  width: 169px
  height: 241px
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.25) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.25) 1px, transparent 1px);
  background-size:24px 24px;
  background-repeat: repeat
}
.clearfix::after {
    content: "";
    clear: both;
    display: table;
}

            
          
!
            
              var tmpStart = {x: 0, y: 0};
var tmpStop = {x: 0, y: 0};
var isDown=false;
var graphHeight = 10;

var tilesize = {w: 24, h: 24}
var tiles = []

var container = $("#grid")
var containerPos = container.offset()
var hoverTile = $('<div class="tile hover"></tile>')
var tmpTile = $('<div class="tile tmpTile"></tile>')
container.append(hoverTile)
container.append(tmpTile)

var btnPick = $('#tile-pick');
var btnClear = $('#tile-clear');
var btnCreate = $('#tile-create');
var btnDiscard = $('#tile-discard');
var btnAddArea = $('#tile-add-area');

container.mousedown(handleMouseDown.bind(this));
container.mouseup(handleMouseUp.bind(this));
container.mousemove(_.throttle(handleMouseMove.bind(this), 16, {leading: true, trailing: true}));

btnCreate.click(createTile);
btnDiscard.click(hideTmpTile);
btnClear.click(clearAllTiles);
btnAddArea.click(addArea);

function handleMouseDown(ev) {
  isDown = true;
  hoverTile.addClass('clicked')
  tmpTile.addClass('dragged')
  tmpTile.css('width', 23)
  tmpTile.css('height', 23)
  tmpTile.css('left', containerPos.left + pos.t.x * tilesize.w + 1)
  tmpTile.css('top', containerPos.top + pos.t.y * tilesize.h + 1)
  pos = getPos(ev);
  setStart(pos.t.x, pos.t.y);
}
function handleMouseUp(ev) {
  isDown = false;
  hoverTile.removeClass('clicked')
  pos = getPos(ev);
  setStop(pos.t.x, pos.t.y);
}
function handleMouseMove(ev) {
  pos = getPos(ev);
  hoverTile.css('left', containerPos.left + pos.t.x * tilesize.w + 1)
  hoverTile.css('top', containerPos.top + pos.t.y * tilesize.h + 1)
  if (isDown) {
    tmpTile.css('left', containerPos.left + Math.min(pos.t.x, tmpStart.x) * tilesize.w + 1)
    tmpTile.css('top', containerPos.top + Math.min(pos.t.y, tmpStart.y) * tilesize.h + 1)
    tmpTile.css('width', 24 * (Math.abs(pos.t.x - tmpStart.x) + 1) - 1)
    tmpTile.css('height', 24 * (Math.abs(pos.t.y - tmpStart.y) + 1) - 1)
  }
}

function getPos(ev) {
  var parentOffset = container.offset();
  var pos = {x: ev.pageX - parentOffset.left, y: ev.pageY - parentOffset.top};
  pos.t = {x: Math.floor(pos.x/tilesize.w), y: Math.floor(pos.y/tilesize.h)};
  return pos;
}
function setStart(x, y) {
  tmpStart = {x: x, y: y};
}
function setStop(x, y) {
  tmpStop = {x: x, y: y};
}

function createTile() {
  var newTile = {};
  newTile.start = {x: Math.min(tmpStart.x, tmpStop.x), y: Math.min(tmpStart.y, tmpStop.y)};
  newTile.stop = {x: Math.max(tmpStart.x, tmpStop.x), y: Math.max(tmpStart.y, tmpStop.y)};
  newTile.layer = parseInt( $('input[name="layer"]:checked').val() );
  tiles.push(newTile);
  drawTile(newTile);
  hideTmpTile();
  calcJson();
}
function drawTile(tile) {
  var newTile = $('<div class="tile drawn"></div>');
  newTile.css('left', containerPos.left + tile.start.x * tilesize.w + 1)
  newTile.css('top', containerPos.top + tile.start.y * tilesize.h + 1)
  newTile.css('width', 24 * (Math.abs(tile.start.x - tile.stop.x) + 1) - 1)
  newTile.css('height', 24 * (Math.abs(tile.start.y - tile.stop.y) + 1) - 1)
  container.append(newTile)
}
function hideTmpTile() {
  tmpTile.removeClass('dragged')
}
function clearAllTiles() {
  $('.tile.drawn').remove();
  tiles = [];
  $('#json').text('');
}

function addArea() {
  graphHeight += 10;
  container.css('height', tilesize.h * graphHeight + 1)
  $('.tile.drawn').each(function(index, element) {
    var elem = $(element);
    var cTop = elem.offset().top + (tilesize.h * 10);
    elem.css('top', cTop);
    html = $('html');
    html.scrollTop( container.height() - html.scrollTop() + 1 );
  });
  for(var i in tiles) {
    tiles[i].start.y += 10;
    tiles[i].stop.y += 10;
  }
}

function calcJson() {
  var convertedTiles = [];
  for(var i in tiles) {
    t = tiles[i];
    // Invert y
    var tmp1 = graphHeight - t.start.y - 1;
    var tmp2 = graphHeight - t.stop.y - 1;
    var yStart = Math.min(tmp1, tmp2);
    var yStop = Math.max(tmp1, tmp2);
    
    tile = [
      [t.start.x, yStart],
      [t.stop.x, yStop],
      1,
      t.layer,
      0
    ];
    convertedTiles.push(tile)
  }
  var tilesJson = JSON.stringify(convertedTiles);
  console.log(tilesJson);
  $('#json').text(tilesJson);
}

function loadTiles() {
  // Todo
}

            
          
!
999px
Loading ..................

Console