I am using the code provided here: S/O 2-face Animated Cube
I want to make this effect fill up the entire screen of the webpage to provide a fullscreen transition from one div/iframe to the next.
I tried using percentages:
<style>
#experiment {
-webkit-perspective: 1000;
}
html,body{
margin: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
.cube {
position: relative;
height: 100%; //<----
width: 100%; //<----
-webkit-transition: -webkit-transform .5s linear;
-webkit-transform-style: preserve-3d;
}
.face {
position: absolute;
height: 100%; //<----
width: 100%; //<----
color: #fff;
}
.cube .front {
-webkit-transform: translateZ(50%); //50% should make sense, but doesn't work so I went to Javascript instead
background-color:gray;
}
.cube .side {
-webkit-transform: rotateX(-90deg) translateZ(50%);
background-color:lightgray;
}
.cube:hover{
-webkit-transform:rotateX(90deg);
}
</style>
<div id="experiment">
<div id="cube" class="cube" align="center">
<div id="front" class="face front">
front face
</div>
<div id="side" class="face side">
side face
</div>
</div>
</div>
I also tried setting the sizes with Javascript:
<script>
var w = window.innerWidth*.7, h = window.innerHeight*.7;
function Id(obj) { return document.getElementById(obj); }
Id('cube').style.width = w+'px';
Id('cube').style.height = h+'px';
Id('front').style.width = w+'px';
Id('front').style.height = h+'px';
Id('side').style.width = w+'px';
Id('side').style.height = h+'px';
Id('front').style.webkitTransform = 'translateZ('+(w/2)+'px)';
Id('side').style.webkitTransform = 'rotateX(-90deg) translateZ('+(h/2)+'px)';
</script>
But the -webkit-perspective
"zooms" in and pixelates the content and pushes parts of it off the screen. I could not find any margins that would correctly offset/center the cube on-page on varying window sizes.
How do I perfectly center this cube on the page? Once I can get it to center programatically, I should be able to easily fill it onto the entire screen.
I could not find any articles regarding perspective
that explain how to align it to any window bounds.
This is kinda what I'm looking for, in a scalable fashion:
Thanks,
Well, the calculus that you are asking is quite hard.
The good news is that you don't really need to do it.
Any object placed at 0 in the z direction won't be scaled by the perspective. So, the trick is to place the front face of the cube at z = 0px.
On the other side, to make this responsive, we will need to use viewport units. Like this :
html,body{
margin: 0;
height: 100%;
width: 100%;
}
#experiment {
perspective: 200vw;
height: 100%;
width: 100vw;
border: solid 1px blue;
}
.cube {
position: relative;
height: 100%;
width: 100vw;
transform-style: preserve-3d;
}
.face {
position: absolute;
height: 100%;
width: 100%;
color: #fff;
transition: transform 4s linear;
}
.cube .front {
transform: translateZ(-50vw) rotateY(0deg) translateZ(50vw);
transform-origin: center center;
background-color:gray;
}
.cube .side {
transform: translateZ(-50vw) rotateY(-90deg) translateZ(50vw);
background-color:lightgray;
}
.cube:hover .front {
transform: translateZ(-50vw) rotateY(90deg) translateZ(50vw);
}
.cube:hover .side {
transform: translateZ(-50vw) rotateY(0deg) translateZ(50vw);
}
<div id="experiment">
<div id="cube" class="cube" align="center">
<div id="front" class="face front">
front face
</div>
<div id="side" class="face side">
side face
</div>
</div>
</div>
The idea about the chained rotation comes from Lea Verou here
The above snippet uses viewport units, since those are the only responsive approach for z movements. But vW is buggy in iOS. A more complex solution, not using vW, is below (using percentage dimension, that can be set only in translateX. So 2 additional rotations are needed.
html,body{
margin: 0;
height: 100%;
width: 100%;
}
#experiment {
perspective: 2000px;
height: 100%;
width: 100%;
overflow: hidden;
}
.cube {
position: relative;
height: 100%;
width: 100%;
transform-style: preserve-3d;
}
.face {
position: absolute;
height: 100%;
width: 100%;
color: #fff;
transition: transform 4s linear;
}
.cube .front {
transform: rotateY(-90deg) translateX(-50%) rotateY(0deg) translateX(50%) rotateY(90deg);
transform-origin: center center;
background-color:gray;
}
.cube .side {
transform: rotateY(-90deg) translateX(-50%) rotateY(-90deg) translateX(50%) rotateY(90deg);
background-color:lightgray;
}
.cube:hover .front {
transform: rotateY(-90deg) translateX(-50%) rotateY(90deg) translateX(50%) rotateY(90deg);
}
.cube:hover .side {
transform: rotateY(-90deg) translateX(-50%) rotateY(0deg) translateX(50%) rotateY(90deg);
}
<div id="experiment">
<div id="cube" class="cube" align="center">
<div id="front" class="face front">
front face
</div>
<div id="side" class="face side">
side face
</div>
</div>
</div>
Here a small demo trying to show how the transform works. just watch it.
.demo {
height: 20px;
width: 400px;
background-color: lightblue;
position: absolute;
top: 200px;
}
.demo:after {
content: "";
position: absolute;
background-color: red;
width: 10px;
height: 10px;
left: 50%;
top: 200px;
border-radius: 50%;
}
#demo {
-webkit-animation: demo 25s infinite;
z-index: 2;
}
@-webkit-keyframes demo {
0% {transform: rotate( 0deg) translateX( 0%) rotate( 0deg) translateX( 0%) rotate( 0deg); }
20% {transform: rotate( 0deg) translateX( 0%) rotate( 0deg) translateX( 0%) rotate(90deg); }
40% {transform: rotate( 0deg) translateX( 0%) rotate( 0deg) translateX(50%) rotate(90deg); }
60% {transform: rotate( 0deg) translateX( 0%) rotate(30deg) translateX(50%) rotate(90deg); }
80% {transform: rotate( 0deg) translateX(-50%) rotate(30deg) translateX(50%) rotate(90deg); }
100% {transform: rotate(-90deg) translateX(-50%) rotate(30deg) translateX(50%) rotate(90deg); }
}
#text {
width: 400px;
}
#text:after {
content: "";
background-color: rgba(200, 0, 0, 0.2);
width: 300px;
height: 1em;
position: absolute;
top: 15px;
-webkit-animation: text 25s infinite;
}
@-webkit-keyframes text {
10% {transform: translateY( 0px)}
30% {transform: translateY( 20px)}
50% {transform: translateY( 40px)}
70% {transform: translateY( 58px)}
90% {transform: translateY( 76px)}
100% {transform: translateY( 92px)}
}
span {
margin-left: 400px;
top: 20px;
width: 200px;
border: solid 1px blue;
position: absolute;
}
<div class="demo" id="demo"></div>
<div class="demo" id="demo2"></div>
<div id="text">
<ul id="phases">
<li >ROTATE to get the x axis </li>
<li >MOVE in the X axis (but really in Z)</li>
<li >ROTATE the wanted rotation </li>
<li >MOVE back in the X axis </li>
<li >ROTATE back the x axis trick </li>
<li >DONE </li>
</ul>
</div>
<span>Do a rotation of 30deg around a point that is at a distance equal to 50% width in the z axis - Showed as a red point</span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With