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="view">
  <div class="box number" id="js-number">
  </div>
  <div class="right">
    <div class="box reserve" id="js-reverse">
    </div>
    <a class="box link" href="#" id="js-cleanup">Aufräumen</a>
    <a class="box link" href="#" id="js-reset">Neustart</a>
  </div>
</div>

<div class="box result" id="js-result">0<sub>2</sub> / 0<sub>10</sub></div>

<div class="box -noborder bottom">
  <a class="box link" target="_blank" href="https://codepen.io/btemperli/full/RwZbMjp">Binäre Kartendarstellung</a>
  <a class="box link" target="_blank" href="https://codepen.io/btemperli/full/abwexEL">5-adische Kartendarstellung</a>
  <a class="box link" target="_blank" href="https://codepen.io/btemperli/full/bGRZyxz">Dezimale Kartendartellung</a>
</div>
              
            
!

CSS

              
                body {
  margin: 0;
  color: #2c3531;
  font-family: Helvetica, Arial, sans-serif;
  font-weight: bold;
}

[draggable=true] {
  cursor: move;
}

.view {
  display: flex;
  height: 80vh;
}

.result {
  background: #2c3531;
  color: #d1e8e2;
  height: calc(20vh - 50px);
  font-size: 5vh;
  align-items: center;
  justify-content: center;
  display: flex;
}

.result sub {
  margin-top: 4vh;
  width: 30px;
  font-size: 2vh;
}

.box {
  margin: 10px;
  padding: 10px;
  border: 2px solid #2c3531;
}

.box.-noborder {
  border: none;
}

.number {
  flex: 1;
  background: #116466;
  transition: background 200ms;
}

.number.-over {
  background: #168487;
}

.bottom {
  padding: 20px 0;
}

.right {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.reserve {
  background: #d9b08c;
  flex: 1;
}

.card {
  background: #d1e8e2;
  width: 50px;
  height: 50px;
  float: left;
  align-items: center;
  justify-content: center;
  display: flex;
  border-color: #ffcb9a
}

.link {
  text-align: center;
  text-decoration: none;
  color: #2c3531;
  background: #ffcb9a;
  transition: color 200ms, background 200ms;
}

.link:hover {
  background: #2c3531;
  color: #ffcb9a;
}

@media only screen and (min-width: 900px) {
  .result {
    font-size: 10vh;
  }
  .number {
    flex: 2;
  }
}

@media only screen and (min-width: 1200px) {
  .result {
    font-size: 12vh;
  }
  
  .result sub {
    width: 70px;
    font-size: 5vh;
  }
  
  .number {
    flex: 3;
  }
}
              
            
!

JS

              
                const buttonCleanup = document.getElementById('js-cleanup');
const buttonReset = document.getElementById('js-reset');
const areaResult = document.getElementById('js-result');
const areaReserve = document.getElementById('js-reverse');
const areaNumber = document.getElementById('js-number');
const values = [256, 128, 64, 32, 16, 8, 4, 2, 1];
let draggedElement = null;
let dragToRemove = false;
let result = 0;

function getValue(element) {
  const val = element.dataset.value;
  return parseInt(val);
}

function updateResult() {
  let systemresult = result.toString(2);
  systemresult = systemresult.replace(/(\d)(?=(\d{4})+$)/g, '$1 ');
  areaResult.innerHTML = systemresult + "<sub>2</sub> / " + result + "<sub>10</sub>";
}

function addElement(element) {
  const value = getValue(element);
  result = result + value;
  updateResult();
}

function removeElement(element) {
  const value = getValue(element);
  result = result - value;
  updateResult();
}

function createElement(value) {
  const element = document.createElement('div');
  element.setAttribute('class', 'box card');
  element.setAttribute('data-value', value);
  element.setAttribute('draggable', true);
  element.innerHTML = value;
  return element;
}

function start() {
  [...values].reverse().forEach(value => {
    const element = createElement(value);
    areaReserve.appendChild(element);
  });
}

/** drop somewhere **/
document.addEventListener("drop", function (event) {
  event.preventDefault();
  document.getElementById('js-number').classList.remove("-over");

  if (dragToRemove) {
    dragToRemove = false;
    removeElement(draggedElement);
    draggedElement.remove();
    draggedElement = null;
  }
  
  else if (event.target.classList.contains("number")) {    
    if (draggedElement) {
      addElement(draggedElement);
      event.target.appendChild(draggedElement.cloneNode(true));
      draggedElement = null;
    }
  }
}, false);

document.addEventListener("dragenter", function (event) {
  if (event.target.classList.contains("number") && !dragToRemove) {
    event.target.classList.add("-over");
  }
}, false);

document.addEventListener("dragleave", function (event) {
  if (event.target.classList.contains("number")) {
    event.target.classList.remove("-over");
  }
}, false);

document.addEventListener("dragover", function( event ) {
  event.preventDefault();
}, false);

document.addEventListener("dragstart", function( event ) {
  draggedElement = event.target;
  event.dataTransfer.effectAllowed = "move";
  
  if (event.target.parentNode.classList.contains('number')) {
    dragToRemove = true;
  }
}, false);

buttonCleanup.addEventListener('click', function () {
  areaNumber.innerHTML = "";
  let cleanupResult = result;

  values.forEach(value => {
    const currentTiles = parseInt(cleanupResult / value);
    const element = createElement(value);
    
    for (let i = 0; i < currentTiles; i++) {
      areaNumber.appendChild(element.cloneNode(true));
    }
    
    cleanupResult = cleanupResult - currentTiles * value;
  });
});

buttonReset.addEventListener('click', function () {
  result = 0;
  areaNumber.innerHTML = "";
  updateResult();
});

// init
start();
              
            
!
999px

Console