Introduction
In this article we will create a rotating cube using CSS3 and JavaScript. The cube can be rotated using navigation keys. Apart from this all the faces of the cubes can be used for showing various types of content. In this article I'm showing four different "iframes". So let's start.
Properties of cube
The following properties are used in implementing the rotating cube:
- All cube faces are of equal area.
- All edges of the cube face are equal in length.
- All sides are angled at 90 degrees with each other.
- The cube can be rotated by a minimum amount of 45 degrees.
HTML for cube
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
<div id="camera">
<div id="cube">
<div class="face one">
<iframe src="http://codewithlogic.wordpress.com/" width=400 height=400 ></iframe>
</div>
<div class="face two">
<iframe src="" width=400 height=400 ></iframe>
</div>
<div class="face three">
<iframe src="http://www.c-sharpcorner.com/UploadFile/4aac15/working-with-javascript-timers/" width=400 height=400 ></iframe>
</div>
<div class="face four">
<iframe src="http://www.c-sharpcorner.com/UploadFile/4aac15/windows-8-sidebar-for-website-using-jquery/" width=400 height=400 ></iframe> </div>
<div class="face five">
<iframe src="http://www.c-sharpcorner.com/authors/4aac15/arpit-jain.aspx" width=400 height=400 ></iframe> </div>
<div class="face six">
</div>
</div>
</div>
</body>
</html>
In the code above the div with id="camera" represents a perspective camera setting.
The div with id="cube" represents an entire cube. This div holds all the faces of the cube.
The div with id="face one" represents a first face of the cube and similarly for other cube faces.
The "iframe" inside the div represents the content of the cube face.
The following is the CSS for rotating the cube:
-
#camera{
-
-webkit-perspective:2000px ;
-
}
-
-
#cube{
-
height:400px;
-
width:400px;
-
position:absolute;
-
top:50px;
-
left:100px;
-
border:1px solid green;
-
-webkit-transition:all 5s linear;
-
-webkit-transform-style:preserve-3d;
-
}
-
.face{
-
position:absolute;
-
width:400px;
-
border:3px solid red;
-
height:400px;
-
background-color:rgba(50,50,50,0.7);
-
}
-
-
#cube.one {
-
-webkit-transform: rotateX(90deg) translateZ(200px);
-
}
-
-
#cube.two{
-
-webkit-transform: translateZ(200px);
-
}
-
-
#cube.three{
-
-webkit-transform: rotateY(90deg) translateZ(200px);
-
}
-
-
#cube.four{
-
-webkit-transform: rotateY(180deg) translateZ(200px);
-
}
-
-
#cube.five{
-
-webkit-transform: rotateY(-90deg) translateZ(200px);
-
}
-
-
#cube.six{
-
-webkit-transform: rotateX(-90deg) translateZ(200px) rotate(180deg);
-
}
Line number 2 sets the depth of the z plane. It is required for a good 3D experience.
Line numbers 5-14 define the various properties of the cube, like its width, height, position and so on. Line number 12 sets the time the cube takes to rotate 45 degrees. It is set to 5 seconds. Line number 14 preserves the 3D nature of the cube.
Line numbers 15-21 set the properties of the cube's face. The position of each cube face is absolute.
Line numbers 23 to 45 place the cube faces together to form a proper cube. For a better understanding of face placement look at the following sequence of images.
JavaScript for rotating cube
-
var xAngle = 0, yAngle = 0;
-
document.addEventListener('keydown',function(e)
-
{
-
switch(e.keyCode)
-
{
-
-
case37:// for left key
-
yAngle -= 45;
-
break;
-
-
case38:// for up key
-
xAngle += 45;
-
break;
-
-
case39:// for right key
-
yAngle += 45;
-
break;
-
-
case40:// for down key
-
xAngle -= 45;
-
break;
-
}
-
$('#cube').css("webkit-transform","rotateX("+xAngle+"deg) rotateY("+yAngle+"deg)");
-
},false);
In the preceding JavaScript "xAngle" and "yAngle" represent the angle of the cube with the x-axes and the y-axes respectively. Initially it is set to zero.
Line number 2 adds a key down event listener to the document so that the cube can respond to the keyboard navigation keys.
Line numbers 7 to 21 set the angle the cube is to be rotated. "e.keyCode" returns an ASCII code of the key and the case statement sets the angle accordingly.
Line number 23 sets the CSS transform property equal to the angle through which the cube is to be rotated.
Output
Summary
That's all for this article. You can enjoy the this rotating cube by visiting the following link DEMO. If you have any doubts then feel free to ask in the comments section.