JSFiddle - React, Tailwind, and code Playground
HTML
<div id="main">
<div id="box">
<div class="front">
<div>
<div>
<span></span>
<span></span>
<span></span>
</div>
<div>
<span></span>
<span></span>
<span></span>
</div>
<div>
<span></span>
<span></span>
<span></span>
</div>
</div>
</div>
<div class="back">
<div>
<div>
<span></span>
<span></span>
<span></span>
</div>
<div>
<span></span>
<span></span>
<span></span>
</div>
<div>
<span></span>
<span></span>
<span></span>
</div>
</div>
</div>
<div class="right">
<div>
<div>
<span></span>
<span></span>
<span></span>
</div>
<div>
<span></span>
<span></span>
<span></span>
</div>
<div>
<span></span>
<span></span>
<span></span>
</div>
</div>
</div>
<div class="left">
<div>
<div>
<span></span>
<span></span>
<span></span>
</div>
<div>
<span></span>
<span></span>
<span></span>
</div>
<div>
<span></span>
<span></span>
<span></span>
</div>
</div>
</div>
<div class="top"></div>
<div class="bottom"></div>
</div>
</div>
<div id="colors">
<span draggable="true"></span>
<span draggable="true"></span>
<span draggable="true"></span>
<span draggable="true"></span>
<span draggable="true"></span>
</div>
<span id="opacity">Opacité <input type="range" min="0" max="100" value="100"></span>
CSS
html,body {margin: 0;}
body {overflow: hidden;background: #333;}
.in-grab {cursor: -webkit-grabbing !important;cursor: grabbing !important;}
#main {position: absolute;perspective:1000px;width: 300px;height: 300px;margin: -150px;top: 50%;left: 50%;}
#box {position: absolute;width: 100%;height: 100%;transform-style: preserve-3d;cursor: -webkit-grab;cursor: grab;}
#box>div {position: absolute;width: 100%;height: 100%;background: #333;opacity: 1;}
#box>div>div {display: table;width: 100%;height: 100%;}
#box>div>div>div {display: table-row;}
#box span {display: table-cell;background: #ccc;border-radius: 12px;}
#box span.highlight {background: #666 !important;}
/* Face avant + arrière */
#box>div:nth-child(-n+2)>div>div:not(:nth-child(2)) span:nth-child(even),
#box>div:nth-child(-n+2)>div>div:nth-child(2) span:nth-child(odd) {background: #ddd;}
/* Faces latérales */
#box>div:nth-child(n+3)>div>div:not(:nth-child(2)) span:nth-child(odd),
#box>div:nth-child(n+3)>div>div:nth-child(2) span:nth-child(even) {background: #ddd;}
#colors {position: fixed;width: 102px;top: 5%;left: 0;}
#colors span {position: relative;z-index: 10;}
#colors span {float: left;width: 50px;height: 50px;margin-bottom: 2px;border-radius: 10px;cursor: -webkit-grab;cursor: grab;}
#colors span:nth-child(odd) {margin-right: 2px;}
#colors span:nth-child(1) {background: #0099CC;}
#colors span:nth-child(2) {background: #12D05F;}
#colors span:nth-child(3) {background: #CC3029;}
#colors span:nth-child(4) {background: #FFC02A;}
#colors span:nth-child(5) {background: #CC3D91;}
#opacity {position: fixed;width: 240px;margin-left: -120px;bottom: 20px;left: 50%;color: #fff;}
input {width: 160px;vertical-align: middle;}
JavaScript
// --------------------------
// ------- Functions -------
// ------------------------
function arcSin(value){
return Math.round(Math.asin(value) * 180 / Math.PI);
}
function setTranslationValues(elem, val){
document.querySelector("."+elem).style.transform = "rotate"+ val[0] +" translateZ("+ val[1] +"px)";
}
function endSwap(e){
angleX = Math.round(angleX/90) * 90;
angleY = Math.round(angleY/90) * 90;
box.className = "";
document.removeEventListener("mousemove", swapping, false);
document.removeEventListener("mouseup", endSwap, false);
}
function swapping(e){
dX = e.clientX - clickX;
angleX = curAngleX;
angleY = arcSin(dX / wWidth) + curAngleY;
box.style.transform = "translateZ(-"+ halfH +"px) rotateX("+ angleX +"deg) rotateY("+ angleY +"deg)";
}
function initSwap(e){
e.preventDefault();
clickX = e.clientX;
var trStyle = box.style.transform,
regX = /X\((-?\d+)deg\)/,
regY = /Y\((-?\d+)deg\)/;
curAngleX = parseInt(regX.exec(trStyle)[1]);
curAngleY = parseInt(regY.exec(trStyle)[1]);
box.className = "in-grab";
document.addEventListener("mousemove", swapping, false);
document.addEventListener("mouseup", endSwap, false);
}
function highlightCase(e){
e.preventDefault();
this.className = "highlight";
}
function endGrabColor(e){
e.preventDefault();
this.className = "";
this.style.backgroundColor = e.dataTransfer.getData("text/plain");
}
function grabColor(e){
e.dataTransfer.setData("text/plain", getComputedStyle(this, null).backgroundColor);
}
function setOpacity(){
var f = document.querySelectorAll("#box>div"),
i = 0,
l = f.length;
for ( ; i<l; i++) f[i].style.opacity = range.value/100;
}
// -------------------------------
// ----- Variables & Events -----
// -----------------------------
var wHeight = document.getElementById("main").offsetHeight,
wWidth = document.getElementById("main").offsetWidth,
halfH = Math.round(wHeight/2),
halfW = Math.round(wWidth/2);
var box =...