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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

            
              <!-- CONTROLS -->
<button type="button" id="reset" class="upper-panel upper-panel__button">Reset</button>

<input type="radio" checked id="radio-front" name="select-face" />
<input type="radio" id="radio-left" name="select-face" />
<input type="radio" id="radio-right" name="select-face" />
<input type="radio" id="radio-top" name="select-face" />
<input type="radio" id="radio-bottom" name="select-face" />
<input type="radio" id="radio-back" name="select-face" />
<input type="checkbox" id="backface" checked/><label for="backface">backface-visibility</label>

<hr>

<div class="scene">
	<div class="cube">
		<div class="cube__face  cube__face--front"><label for="radio-front">表</label></div>
		<div class="cube__face  cube__face--back"><label for="radio-back">裏</label></div>
		<div class="cube__face  cube__face--left"><label for="radio-left">左</label></div>
		<div class="cube__face  cube__face--right"><label for="radio-right">右</label></div>
		<div class="cube__face  cube__face--top"><label for="radio-top">上</label></div>
		<div class="cube__face  cube__face--bottom"><label for="radio-bottom">下</label></div>
	</div>
</div>

<div class="dimensions-controls">
	<h3>Cube dimensions</h3>
	<div class="axis-container">
		<div class="axis axis--x">
			<input type="range" id="cube-width" max="800" value="150"><label>width</label>
		</div>
		<div class="axis axis--y">
			<input type="range" id="cube-height" max="800" value="150"><label>height</label>
		</div>
		<div class="axis axis--z">
			<input type="range" id="cube-depth" max="800" value="150"><label>depth</label>
		</div>
	</div>
	<hr>
	<output for="cube-width,cube-height,cube-depth" id="dimensions-out" />
</div>

<div class="perspective-controls">
	<h3>Viewer's perspective</h3>
	<div class="axis-container">
		<div class="axis axis--x">
			<input type="range" id="origin-x" min="-50" max="150"><label>origin-x</label>
		</div>
		<div class="axis axis--y">
			<input type="range" id="origin-y" min="-50" max="150"><label>origin-y</label>
		</div>
		<div class="axis axis--z">
			<input type="range" id="perspective" max="2000" value="600"><label>perspective</label>
		</div>
	</div>
	<hr>
	<output for="origin-x,origin-y,perspective" id="perspectives-out" />
</div>


<p class="backface-checked-show">
	Can't click a label through the back of a side with <code>backface-visibility: hidden</code>
</p>
            
          
!
            
              $size: 150px; // default cube side

$cubeWidth: $size !default;
$cubeHeight: $cubeWidth !default;
$cubeDepth: $cubeHeight !default;

// $cubeWidth: 250px;
// $cubeHeight: 150px;
// $cubeDepth: 75px;

:root {
  --cube-width: $cubeWidth;
  --cube-height: $cubeHeight;
  --cube-depth: $cubeDepth;
	--margin-correction: calc(var(--cube-depth) / -2);
}

.scene {
  display: inline-block;
  margin-top: 50px;
  width: var(--cube-width);
	height: var(--cube-height);
  
  perspective: 600px;
}

.cube {
  width: 100%;
  height: 100%;
  position: relative;
  
  transform-style: preserve-3d;
  transition: 0.9s cubic-bezier(0.175,0.885,0.320,1.275);
  transform: rotateX(-15deg) rotateY(15deg);
  
  &__face {
    position: absolute;
    background: red;
    opacity: 0.8;
    
    &--front {
      width: var(--cube-width);
      height: var(--cube-height);
      background: gray;
      transform: translate3d(0, 0, calc(var(--cube-depth) / 2));
    }
    
    &--back {
      width: var(--cube-width);
      height: var(--cube-height);
      background: orange;
      transform: rotateY(180deg) translate3d(0, 0, calc(var(--cube-depth) / 2));
    }
    
    &--left {
      width: var(--cube-depth);
      height: var(--cube-height);
      left: 50%;
      margin-left: var(--margin-correction);
      background: green;
      transform: rotateY(-90deg) translate3d(0, 0, calc(var(--cube-width) / 2));
    }
    
    &--right {
      width: var(--cube-depth);
      height: var(--cube-height);
      left: 50%;
      margin-left: var(--margin-correction);
      background: magenta;
      transform: rotateY(90deg) translate3d(0, 0, calc(var(--cube-width) / 2));
    }
    
    &--top {
      width: var(--cube-width);
      height: var(--cube-depth);
      top: 50%;
      margin-top: var(--margin-correction);
      background: blue;
      transform: rotateX(90deg) translate3d(0, 0, calc(var(--cube-height) / 2));
    }
    
    &--bottom {
      width: var(--cube-width);
      height: var(--cube-depth);
      top: 50%;
      margin-top: var(--margin-correction);
      background: red;
      transform: rotateX(-90deg) translate3d(0, 0, calc(var(--cube-height) / 2));
    }
  }
}

#radio-front:checked ~ .scene .cube {
  &__face--front {
    /* allow click-through to the back side */
    pointer-events: none;
  }
} 

#radio-back:checked ~ .scene .cube {
  transform: rotateY(180deg);
  
  &__face--back {
    pointer-events: none;
  }
} 
#radio-left:checked ~ .scene .cube {
  transform: rotateY(90deg);
  
  &__face--left {
    pointer-events: none;
  }
} 
#radio-right:checked ~ .scene .cube {
  transform: rotateY(-90deg);
  
  &__face--right {
    pointer-events: none;
  }
}
#radio-top:checked ~ .scene .cube {
  transform: rotateX(-90deg);
  
  &__face--top {
    pointer-events: none;
  }
}  
#radio-bottom:checked ~ .scene .cube {
  transform: rotateX(90deg);
  
  &__face--bottom {
    pointer-events: none;
  }
}

body {
  text-align: center;
  padding: 50px;
}

.cube {
  -webkit-user-select: none;
  -ms-user-select: none;
  -moz-user-select: none;
  user-select: none;
}

/* stretch label over the whole surface */
.cube__face > label {
    width: 100%;
    height: 100%;
    font-size: 6em;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: white;
    overflow: hidden;
		cursor: pointer;
}

.cube__face {
  backface-visibility: hidden;
}

#backface:checked ~ .scene .cube__face {
  backface-visibility: visible;
}

.backface-checked-show {
  margin-top: 4em;
  display: none;
  
  #backface:not(:checked) ~ & {
    display: block;
  }
}


// axis
$size: 300px;

.axis-container {
	position: relative;
  width: $size;
  height: $size;
  // box-shadow: 2px 2px 4px black;
  filter: drop-shadow(2px 2px 3px gray);
	
}

.axis {
	position: absolute;
	transform-origin: -10px;
  width: 50%;
  white-space: nowrap;
  left: 45%;
  top: 40%;
  
  > input[type=range] {
    width: 80%;
  }
  
  > label {
    display: inline-block;
  }
	
	&--y {
		transform: rotate(90deg);
    
    > label {
      transform: rotate(-90deg);
    }
	}
  
  &--z {
    transform: rotate(-135deg);
    
    > label {
      transform: rotate(135deg);
    }
	}
}

.dimensions-controls {
	float: left;
}
.perspective-controls {
	float: right;
	position: relative;
	
	
	> output {
		position: absolute;
    left: 0;
    width: max-content;
	}
}

.upper-panel {
	float: left;
}
            
          
!
            
              // dimensions

const dimensions = [...document.querySelectorAll(".dimensions-controls input[type=range]")];
const dimensionsOutput = document.getElementById("dimensions-out");

for (let axis of dimensions) {
	axis.oninput = setDimensionsOutput;
}
const docStyle = document.documentElement.style;
setDimensionsOutput();

dimensions[0].addEventListener("input", setWidth);
dimensions[1].addEventListener("input", setHeight);
dimensions[2].addEventListener("input", setDepth);

// --------------------------

function setWidth() {
	docStyle.setProperty("--cube-width", dimensions[0].value + "px");
}

function setHeight() {
	docStyle.setProperty("--cube-height", dimensions[1].value + "px");
}

function setDepth() {
	docStyle.setProperty("--cube-depth", dimensions[2].value + "px");
}

function setDimensionsOutput() {
	dimensionsOutput.value = `Width: ${dimensions[0].value}px, Height: ${dimensions[1].value}px, Depth: ${dimensions[2].value}px`;
}

// ===================================
// perspective
// dimensions

const perspectives = [...document.querySelectorAll(".perspective-controls input[type=range]")];
const perspectivesOutput = document.getElementById("perspectives-out");
const scene = document.querySelector(".scene");

for (let axis of perspectives) {
	axis.oninput = setPerspectivesOutput;
}
const sceneStyle = scene.style;
setPerspectivesOutput();

for (let p of perspectives.slice(0, 2)) {
	p.addEventListener("input", setPerspectiveOrigin)
}

perspectives[2].addEventListener("input", setPerspective);

// --------------------

function setPerspectiveOrigin() {
	sceneStyle.setProperty("perspective-origin", `${perspectives[0].value}% ${perspectives[1].value}%`);
}

function setPerspective() {
	sceneStyle.setProperty("perspective", this.value + "px");
}

function setPerspectivesOutput() {
	perspectivesOutput.value = `Perspective: ${perspectives[2].value}px, perspective-origin: ${perspectives[0].value}%, ${perspectives[1].value}%`;
}

// ===============================

// reset to initial
const initialValues = dimensions.concat(perspectives).map(el => el.value);

document.getElementById("reset").onclick = reset;

function reset() {
	const els = dimensions.concat(perspectives);
	for (let i = 0, len = els.length; i < len; ++i) {
		els[i].value = initialValues[i];
	}

	setWidth();
	setHeight();
	setDepth();
	setDimensionsOutput();

	setPerspectiveOrigin();
	setPerspective();
	setPerspectivesOutput();
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console