Skip to content

Instantly share code, notes, and snippets.

@CodeWhiteWeb
Created December 10, 2021 11:26
Show Gist options
  • Save CodeWhiteWeb/0902f9880239c569bde3710011299060 to your computer and use it in GitHub Desktop.
Save CodeWhiteWeb/0902f9880239c569bde3710011299060 to your computer and use it in GitHub Desktop.
3D Throwing Dice
<body>
<h1>Number : <span id="num"></span></h1>
<div id="prespective">
<div id="cube">
<div class="face faceFront">1</div>
<div class="face faceBack">2</div>
<div class="face faceRight">3</div>
<div class="face faceLeft">4</div>
<div class="face faceTop">5</div>
<div class="face faceBottom">6</div>
</div>
</div>
<p id="note">Click & Roll</p>
</body>
let tl = gsap.timeline({ease:Power2.in});
const sound = new Audio('https://www.whoisofek.in/dice.mp3')
const diceSound = ()=>{
sound.currentTime = 0
sound.play()
}
const cube = document.querySelector('#cube')
let cubeNum = [
{
y:0,
x:0,
text:'1'
},
{
y:-90,
x:0,
text:'3'
},
{
y:-180,
x:0,
text:'2'
},
{
y:90,
x:0,
text:'4'
},
{
y:0,
x:-90,
text:'5'
},
{
y:0,
x:90,
text:'6'
},
]
let _randomValue
document.addEventListener('click',()=>{
_randomValue = cubeNum[Math.floor(Math.random() * cubeNum.length)];
cube.style.transform = `rotateX(${_randomValue.x}deg) rotateY(${_randomValue.y}deg)`
tl.to('#num',{y:'100%',onComplete:()=>{
document.querySelector('#num').innerHTML = _randomValue.text
}})
tl.to('#num',{y:'0%'})
diceSound()
})
<script src="https://unpkg.co/gsap@3/dist/gsap.min.js"></script>
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@900&display=swap');
body{
font-family: 'Montserrat', sans-serif;
width:100%;
height:100%;
overflow:hidden;
margin:0;
padding:0;
}
#prespective {
padding-top:10%;
width: 200px;
height: 200px;
perspective: 600px;
margin-right:auto;
margin-left:auto;
}
#cube {
transform: translateZ(-100px)rotateX(-20deg)rotateY(20deg);
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d;
transition:.75s cubic-bezier(.22,.79,1,.31);
}
.face {
background:#000;
opacity:0.7;
text-align:center;
line-height:200px;
font-size:3em;
color:#fff;
font-weight:900;
position: absolute;
width: 200px;
height: 200px;
}
.faceFront {
transform: rotateY( 0deg) translateZ(100px);
}
.faceRight {
transform: rotateY( 90deg) translateZ(100px);
}
.faceBack {
transform: rotateY(180deg) translateZ(100px);
}
.faceLeft{
transform: rotateY(-90deg) translateZ(100px);
}
.faceTop{
transform: rotateX( 90deg) translateZ(100px);
}
.faceBottom{
transform: rotateX(-90deg) translateZ(100px);
}
h1{
overflow:hidden;
text-align:center;
font-size:3em;
color: black;
-webkit-text-fill-color: white;
-webkit-text-stroke-width: 1.5px;
-webkit-text-stroke-color: black;
}
#num{
color:#f37022;
-webkit-text-stroke-color: #f37022;
display:inline-block;
transform:translateY(100%);
}
#note{
position:fixed;
top:80%;
font-size:.95em;
text-align:center;
width:100%;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment