I'm playing around with browsers lately, and now I'm fighting with 3D. WebGL is awesome, and I can do 3D in CSS3 too? Even better! Ok, so my vision was to create floating 3D objects using nested transformations (with preserve-3d).
HTML
<div class="scene">
<div class="box">
<div class="side_1"></div>
<div class="side_2"></div>
<div class="side_3"></div>
<div class="side_4"></div>
<div class="side_5"></div>
<div class="side_6"></div>
</div>
<div class="box">
<div class="side_1"></div>
<div class="side_2"></div>
<div class="side_3"></div>
<div class="side_4"></div>
<div class="side_5"></div>
<div class="side_6"></div>
</div>
<div class="box">
<div class="side_1"></div>
<div class="side_2"></div>
<div class="side_3"></div>
<div class="side_4"></div>
<div class="side_5"></div>
<div class="side_6"></div>
</div>
<div class="box">
<div class="side_1"></div>
<div class="side_2"></div>
<div class="side_3"></div>
<div class="side_4"></div>
<div class="side_5"></div>
<div class="side_6"></div>
</div>
</div>
CSS (I'm using prefixfree)
.scene {
perspective: 3500;
perspective-origin: 25% 100%;
animation: spin 15s infinite linear;
transform-style: preserve-3d;
width: 960px;
height: 350px;
margin: 80px auto;
}
.box {
width: 150px;
height: 180px;
position: absolute;
transform-style: preserve-3d;
top: 40px;
}
.box div {
width: 150px;
height: 180px;
opacity: 0.75;
background-color: #bada55;
position: absolute;
}
.box .side_1 {
transform: rotateY( 0deg ) translateZ( 75px );
background-color: #FF0;
}
.box .side_2 {
transform: rotateX( 180deg ) translateZ( 75px );
background-color: #F00;
}
.box .side_3 {
transform: rotateY( 90deg ) translateZ( 75px );
background-color: #CCC; }
.box .side_4 {
transform: rotateY( -90deg ) translateZ( 75px );
background-color: #000;
}
.box .side_5 {
height: 150px;
width: 150px;
background-color: #00a2ff;
transform: rotateX( 90deg ) translateZ( 75px ); }
.box .side_6 {
height: 150px;
background-color: #00a2ff;
width: 150px;
transform: rotateX( -90deg ) translateZ( 105px ); }
.box:first-child {
position: absolute;
top: 40px;
}
.box:last-child {
right: 0;
}
.box:nth-child(2) {
background: url(../img/dot.jpg) repeat-y center center transparent;
transform: translateZ( -600px );
left: 480px;
}
.box:nth-child(3) {
background: url(../img/dot.jpg) repeat-y center center transparent;
transform: translateZ( 600px );
left: 480px;
}
#keyframes spin {
0% { transform: rotateY(0); }
100% { transform: rotateY(360deg); }
}
Everything is set up, and works. But wait, what is happening? Why does the far object is bigger than close one? I don't understand. Is it webkit bug, or I'm missing something?
http://stretchbox.org/projects/Nextgen/floaters.php - here you have an example.
.scene is also rotating, so you need to apply the perspective and perspective-origin on its parent, which in your case would be body.
body {
perspective: 3500;
perspective-origin: 25% 100%;
}
You can see a demo here:
http://jsfiddle.net/dw58P/embedded/result/
Nice project by the way!
Related
I'm designing a rotating cube logo for my portfolio site. After trying all night, for some reason my 3D cube logo is no longer a cube. Two problems:
The shape of the cube is distorted. The .front div is larger than all the other divs for the cube. I can't see why this is happening.
When .container div's animation is commented out, you'll notice the viewer position is head on. I need the view position to be more 'isometric', like the viewer is looking at the edge of the cube from above. I've tried to rotate the Z- and Y-axis of the .container div to achieve this but no luck so far. Un-comment the background-color: pink; on the .container div to see this.
I have a feeling the above problems are to do with the perspective property. I'm not sure how to calculate the correct amount of perspective here, and this could be my problem.
Here's my CodePen link.
HTML:
<div class="container">
<div class="cube">
<div class="front"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="back"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="left"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="right"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="top"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="bottom"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
</div>
</div>
CSS:
html {
background: #666;
}
body {
margin: 0;
padding: 0;
height: 100vh;
}
.container {
position: relative;
top: 50%;
left: 50%;
margin-left: -200px;
margin-top: -200px;
width: 400px;
height: 400px;
/* background-color: pink; */
transform-style: preserve-3d;
perspective: 1000px;
animation: rotate 2000ms linear infinite;
}
.cube {
/* background-color: blue; */
width: 200px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -100px;
transform-style: preserve-3d;
}
.cube div {
position: absolute;
width: 200px;
height: 200px;
border: 5px solid #ccc;
box-sizing: border-box;
background-size: cover;
}
.cube img {
width: 100%;
opacity: 1;
}
.front {
transform: translateZ(100px);
}
.back {
transform: rotateY(180deg) translateZ(100px);
}
.left {
transform: rotateY(-90deg) translateZ(100px);
}
.right {
transform: rotateY(90deg) translateZ(100px);
}
.top {
transform: rotateX(90deg) translateZ(100px);
}
.bottom {
transform: rotateX(-90deg) translateZ(100px);
}
#keyframes rotate {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(360deg);
}
}
There are an issue because you rotate the container and its perspective as well.
you want a perspective on the cube, you have to set the perspective property on body element (or any kind of container of your animation that is not animated itself) and avoid to set it on the animated element. Actually, moving the element that is set by a perspective value will move this 3D element inside a 2D view – its own parent element. That causes the weird cube rendering on your exemple.
Also, if you want to control the perpective origin, you can use perspective-origin that lets you determine the position at which the viewer is looking. Associated with perspective property, you will be able to control the whole rendered scene.
So, the result will change with following code:
html { background: #666; }
body {
margin: 0;
padding: 0;
height: 100vh;
perspective: 900px;
perspective-origin: bottom;
}
.container {
position: relative;
top: 50%;
left: 50%;
margin-left: -200px;
margin-top: -200px;
width: 400px;
height: 400px;
transform-style: preserve-3d;
animation: rotate 2000ms linear infinite;
}
.cube {
width: 200px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -100px;
transform-style: preserve-3d;
}
.cube div {
position: absolute;
width: 200px;
height: 200px;
border: 5px solid #ccc;
box-sizing: border-box;
background-size: cover;
}
.cube img {
width: 100%;
}
.front { transform: translateZ(100px); }
.back { transform: rotateY(180deg) translateZ(100px); }
.left { transform: rotateY(-90deg) translateZ(100px); }
.right { transform: rotateY(90deg) translateZ(100px); }
.top { transform: rotateX(90deg) translateZ(100px); }
.bottom { transform: rotateX(-90deg) translateZ(100px); }
#keyframes rotate {
to { transform: rotateY(360deg); }
}
<body>
<div class="container">
<div class="cube">
<div class="front"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="back"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="left"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="right"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="top"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
<div class="bottom"><img src="https://s3.eu-west-2.amazonaws.com/cube-logo/logo.png" alt="logo"></div>
</div>
</div>
</body>
I am trying to make a responsive cuboid using HTML/CSS but the right face of the cuboid is not aligning with the remaining faces.
Can anyone help me out with this?
I am pasting a jsfiddle link for the same, below:
#container {
width: 100vw;
height: 100vh;
perspective: 1000px;
perspective-origin: 50% 50%;
}
#container div {
height: 100vh;
/*width: 100%;*/
position: absolute;
/*display: inline-block;*/
transform-origin: 50% 50%;
transform-style: preserve-3d;
}
#left {
width: 100vh;
background: steelblue;
transform: translateX(-50vh) translateZ(-50vh) rotateY(90deg);
}
#right {
width: 100vh;
background: teal;
transform: translateX(50vw) rotateY(-90deg);
}
#floor {
width: 100%;
background: #55DF03;
transform: translateY(50vh) translateZ(-50vh) rotateX(90deg);
}
#ceil {
width: 100%;
background: grey;
transform: translateY(-50vh) translateZ(-50vh) rotateX(90deg);
}
#back {
width: 100%;
background: #2091FE;
transform: translateZ(-100vh);
}
<div id="container">
<div id="left"></div>
<div id="floor"></div>
<div id="right"></div>
<div id="ceil"></div>
<div id="back"></div>
</div>
https://jsfiddle.net/srikanthaero/4s8wovjm/
Here is the responsive 3D Cuboid face:
#container {
width: 100vw;
height: 100vh;
perspective: 1000px;
perspective-origin: 50% 50%;
}
#container div {
height: 100vh;
/*width: 100%;*/
position: absolute;
/*display: inline-block;*/
transform-origin: 50% 50%;
transform-style: preserve-3d;
}
#left {
width: 100vh;
background: steelblue;
transform: translateX(-50vh) translateZ(-50vh) rotateY(90deg);
}
#right {
width: 100vh;
background: teal;
transform: translateX(0%) rotateY(-90deg);
right: 0px;
TRANSFORM-ORIGIN: 100% 100% !important;
}
#floor {
width: 100%;
background: #55DF03;
transform: translateY(50vh) translateZ(-50vh) rotateX(90deg);
}
#ceil {
width: 100%;
background: grey;
transform: translateY(-50vh) translateZ(-50vh) rotateX(90deg);
}
#back {
width: 100%;
background: #2091FE;
transform: translateZ(-100vh);
}
<div id="container">
<div id="left"></div>
<div id="floor"></div>
<div id="right"></div>
<div id="ceil"></div>
<div id="back"></div>
</div>
I have changed the way to move the elements, it's easier to change the transform origin that to play with translates:
body {
margin: 0px;
}
#container {
width: 100vw;
height: 100vh;
perspective: 1000px;
perspective-origin: 50% 50%;
}
#container div {
height: 100vh;
width: 100vw;
position: absolute;
transform-style: preserve-3d;
}
#container #left {
width: 100vh;
background: steelblue;
transform-origin: left center;
transform: rotateY(90deg);
}
#container #right {
width: 100vh;
background: teal;
transform-origin: right center;
transform: rotateY(-90deg);
right: 0px;
}
#floor {
width: 100%;
background: #55DF03;
transform: translateY(50vh) translateZ(-50vh) rotateX(90deg);
}
#ceil {
width: 100%;
background: grey;
transform: translateY(-50vh) translateZ(-50vh) rotateX(90deg);
}
#back {
width: 100%;
background: #2091FE;
transform: translateZ(-100vh);
opacity: 0.5;
}
<div id="container">
<div id="left"></div>
<div id="floor"></div>
<div id="right"></div>
<div id="ceil"></div>
<div id="back"></div>
</div>
On a side note, you are asking:
For left face, when I use 'translateX(-50vh)', it aligns perfectly. But I felt that it should have aligned on 'translateX(-50vw)'. How '-50vh' is sufficient?
The left side has a width of 100vh. The transform origin is center, so the rotation of 90deg is made around a point that is 50vh (the half of 100vh) to the right of the left border of the element. To make it fit, you need to translate in X minus this amount.
Also, if you want to keep your original way of work, the right style should be
#right {
width: 100vh;
background: teal;
right: 0px;
transform: translateX(50vh) translateZ(-50vh) rotateY(-90deg);
}
Notice that positioning it to the right simplifies a lot the problem.
I was making a carousel in the vertical direction, but on rotating 180deg of the X-axis, the backside of the carousel seems like its orientation is not proper in the 3D space.
I would prefer that the solution provided contains not just the code, but also reasoning why this is happening.
#container1 {
position: relative;
left: 100px;
width: 200px;
height: 600px;
transform-style: preserve-3d;
transform-origin: 0 300px 0;
perspective-origin: 100px 300px 0;
perspective: 800px;
animation-name: rotate;
animation-duration: 5s;
}
#keyframes rotate {
from {transform: rotateX(0deg);}
to {transform: rotateX(180deg);}
}
#container1 div {
position: absolute;
top: 225px;
width: 150px;
height: 150px;
}
#div1 {
transform: rotateX(0deg) translateZ(130px);
background-color: red;
}
#div2 {
transform: rotateX(60deg) translateZ(130px);
background-color: blue;
}
#div3 {
transform: rotateX(120deg) translateZ(130px);
background-color: green;
}
#div4 {
transform: rotateX(180deg) translateZ(130px);
background-color: brown;
}
#div5 {
transform: rotateX(240deg) translateZ(130px);
background-color: orange;
}
#div6 {
transform: rotateX(300deg) translateZ(130px);
background-color: pink;
}
<html>
<head>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div id="container1">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
<div id="div6"></div>
</div>
<script src="script.js"></script>
</body>
</html>
The problem is you put perspective in the #container. You should put the perspective in the "stage". Here you can the implementation: JSFiddle
I was learning to create a cube rotating effect. On hover if i replace the rotateX by rotateY the cube is rotating around Y axis centered. But when rotateX is present, the cube is not rotating about X axis centered. How do i implement proper rotation of cube?
#container {
perspective: 1000px;
perspective-origin: 0 0;
}
#cube {
position: relative;
top: 100px;
left: 100px;
width: 200px;
transform-style: preserve-3d;
transition: transform 2s;
transform-origin: 50% 50%;
}
#cube div {
position: absolute;
width: 200px;
height: 200px;
}
#front {
transform: rotateY( 0deg ) translateZ( 100px );
background-color: rgba(0,34,62,0.3);
}
#right {
transform: rotateY( 90deg ) translateZ( 100px );
background-color: rgba(110,34,162,0.3);
}
#back {
transform: rotateY( 180deg ) translateZ( 100px );
background-color: rgba(20,4,62,0.3);
}
#left {
transform: rotateY( -90deg ) translateZ( 100px );
background-color: rgba(80,134,2,0.3);
}
#top {
transform: rotateX(90deg) translateZ(100px);
}
#bottom {
transform: rotateX(-90deg) translateZ(100px);
}
#cube:hover {
transform: rotateX(360deg);
}
<html>
<body>
<div id="container">
<div id="cube">
<div id="front">
<h1>1</h1>
</div>
<div id="right">
<h1>2</h1>
</div>
<div id="back">
<h1>3</h1>
</div>
<div id="left">
<h1>4</h1>
</div>
<div id="top">
<h1>5</h1>
</div>
<div id="bottom">
<h1>6</h1>
</div>
</div>
</div>
</body>
</html>
If I understand you correctly, you just set the #cube's height to 200px
#container {
perspective: 1000px;
perspective-origin: 0 0;
}
#cube {
position: relative;
top: 100px;
left: 100px;
width: 200px;
height:200px;
transform-style: preserve-3d;
transition: transform 2s;
transform-origin: 50% 50%;
}
#cube div {
position: absolute;
width: 200px;
height: 200px;
}
#front {
transform: rotateY( 0deg ) translateZ( 100px );
background-color: rgba(0,34,62,0.3);
}
#right {
transform: rotateY( 90deg ) translateZ( 100px );
background-color: rgba(110,34,162,0.3);
}
#back {
transform: rotateY( 180deg ) translateZ( 100px );
background-color: rgba(20,4,62,0.3);
}
#left {
transform: rotateY( -90deg ) translateZ( 100px );
background-color: rgba(80,134,2,0.3);
}
#top {
transform: rotateX(90deg) translateZ(100px);
}
#bottom {
transform: rotateX(-90deg) translateZ(100px);
}
#cube:hover {
transform: rotateX(360deg);
}
<html>
<body>
<div id="container">
<div id="cube">
<div id="front">
<h1>1</h1>
</div>
<div id="right">
<h1>2</h1>
</div>
<div id="back">
<h1>3</h1>
</div>
<div id="left">
<h1>4</h1>
</div>
<div id="top">
<h1>5</h1>
</div>
<div id="bottom">
<h1>6</h1>
</div>
</div>
</div>
</body>
</html>
You need to set the transform origin according to the div size (one side of the cude). So I just changed the transform-origin: 100px 100px; for the cube like this:
#container {
perspective: 1000px;
perspective-origin: 0 0;
height: 500px;
}
#cube {
position: relative;
top: 100px;
left: 100px;
width: 200px;
transform-style: preserve-3d;
transition: transform 2s;
transform-origin: 100px 100px;
}
#cube div {
position: absolute;
width: 200px;
height: 200px;
}
#front {
transform: rotateY( 0deg ) translateZ( 100px );
background-color: rgba(0,34,62,0.3);
}
#right {
transform: rotateY( 90deg ) translateZ( 100px );
background-color: rgba(110,34,162,0.3);
}
#back {
transform: rotateY( 180deg ) translateZ( 100px );
background-color: rgba(20,4,62,0.3);
}
#left {
transform: rotateY( -90deg ) translateZ( 100px );
background-color: rgba(80,134,2,0.3);
}
#top {
transform: rotateX(90deg) translateZ(100px);
}
#bottom {
transform: rotateX(-90deg) translateZ(100px);
}
#cube:hover {
transform: rotateX(360deg);
}
<html>
<body>
<div id="container">
<div id="cube">
<div id="front">
<h1>1</h1>
</div>
<div id="right">
<h1>2</h1>
</div>
<div id="back">
<h1>3</h1>
</div>
<div id="left">
<h1>4</h1>
</div>
<div id="top">
<h1>5</h1>
</div>
<div id="bottom">
<h1>6</h1>
</div>
</div>
</div>
</body>
</html>
It did not work with percentage since the cube is not "straight" sided and the container uses perspective.
Using CSS, I am trying to create a rectangular prism with rounded edges like those in the photo below.
So far, I have specified the border radius for the top and bottom sides. The problem is that I do not know a way to get the left and right edges of the other sides to curl inwards. As a result, there should not be any holes at the corners. Is there a certain CSS property or trick I could use to do that?
Code from https://jsfiddle.net/jkantner/oqo73a2h/:
.cube {
top: 100px;
left: 100px;
position: relative;
transform-style: preserve-3d;
transform: rotateX(30deg) rotateY(-45deg);
}
.left, .right, .front, .top, .back, .bottom {
position: absolute;
}
.left, .right {
background: #06a;
width: 150px;
height: 150px;
}
.front, .back {
background: #048;
width: 300px;
height: 150px;
}
.top, .bottom {
background: #08c;
border-radius: 30px;
width: 300px;
height: 150px;
}
.front {
z-index: 2;
}
.top {
transform-origin: 0% 100%;
transform: translateY(-150px) rotateX(-90deg);
z-index: 2;
}
.left {
transform-origin: 100% 100%;
transform: translateX(-150px) rotateY(90deg);
z-index: 2;
}
.right {
transform-origin: 0% 0%;
transform: translateX(300px) rotateY(-90deg);
}
.back {
transform: translateZ(150px);
}
.bottom {
transform-origin: 0% 0%;
transform: translateY(150px) rotateX(90deg);
}
<div class='cube'>
<div class='front'></div>
<div class='top'></div>
<div class='left'></div>
<div class='right'></div>
<div class='back'></div>
<div class='bottom'></div>
</div>
If you specify the border-radius for the left and right sides and the front and back sides, just as you did for the top and bottom:
.left, .right {
background: #06a;
border-radius: 30px;
width: 150px;
height: 150px;
}
.front, .back {
background: #048;
border-radius: 30px;
width: 300px;
height: 150px;
}
You will get a rounded rectangular prism, as seen here.