Perspective live visualize

by Julien Roche

HTML

<body class="main">
  <section class="main__visualizer">
    <div class="container">
      <div class="cube">
        <div class="face front">1</div>
        <div class="face back">2</div>
        <div class="face right">3</div>
        <div class="face left">4</div>
        <div class="face top">5</div>
        <div class="face bottom">6</div>
      </div>
    </div>
  </section>
  <footer class="main__editor">
   <label>
      <span>Perspective</span>
      <input type="range" min="0" max="500" value="150" step="1" name="origin" />
    </label>
    <label>
      <span>Perspective origin X</span>
      <input type="range" min="0" max="250" value="50" step="1" name="x" />
    </label>
    <label>
      <span>Perspective origin Y</span>
      <input type="range" min="0" max="250" value="50" step="1" name="y" />
    </label>
    <br />
    <br />
    <output name="cssConfiguration"></output>
  </footer>
</body>

CSS

html,
body {
  border: none;
  height: 100%;
  margin: 0 0 0 0;
  padding: 0 0 0 0;
  width: 100%;
}

:root {
  --x: 50px;
  --y: 50px;
  --origin: 150px;
}

.main {
  background-color: white;
  display: flex;
  flex-direction: column;
}

.main__visualizer {
  align-items: center;
  display: flex;
  flex: 1 1 auto;
  justify-content: center;
  margin-bottom: 1rem;
}

.main__editor {
  border-top: 1px solid black;
  flex: 0 0 150px;
  margin-top: 1rem;
}

 .container {
  width: 200px;
  height: 200px;
  margin: 75px 0 0 75px;
  border: none;
}

.cube {
  width: 100%;
  height: 100%;
  backface-visibility: visible;
  perspective-origin: var(--x) var(--y);
  perspective: var(--origin);
  transform-style: preserve-3d;
}

.face {
  display: block;
  position: absolute;
  width: 100px;
  height: 100px;
  border: none;
  line-height: 100px;
  font-family: sans-serif;
  font-size: 60px;
  color: white;
  text-align: center;
}

/* Define each face based on direction */
.front {
  background: rgba(0, 0, 0, 0.3);
  transform: translateZ(50px);
}

.back {
  background: rgba(0, 255, 0, 1);
  color: black;
  transform: rotateY(180deg) translateZ(50px);
}

.right {
  background: rgba(196, 0, 0, 0.7);
  transform: rotateY(90deg) translateZ(50px);
}

.left {
  background: rgba(0, 0, 196, 0.7);
  transform: rotateY(-90deg) translateZ(50px);
}

.top {
  background: rgba(196, 196, 0, 0.7);
  transform: rotateX(90deg) translateZ(50px);
}

.bottom {
  background: rgba(196, 0, 196, 0.7);
  transform: rotateX(-90deg) translateZ(50px);
}

Babel + JSX

// https://developer.mozilla.org/en-US/docs/Web/CSS/initial-letter
let cssConfigurationOutputElement = document.querySelector('[name="cssConfiguration"');
let sampleElement = document.querySelector('.cube');

document.addEventListener('input', event => {
	let inputName = event.target.name;
  document.documentElement.style.setProperty(`--${inputName}`, `${event.target.value}px`);
  updateCssConfigurationOutput();
}, false);

function updateCssConfigurationOutput() {
	cssConfigurationOutputElement.value = `perspective: ${window.getComputedStyle(sampleElement)['perspective']}  perspective-origin: ${window.getComputedStyle(sampleElement)['perspective-origin']}`;
}

updateCssConfigurationOutput();