I am trying to create a CSS animation that moves horizontally from left to right while appearing to get closer and further away, simulating the motion of a glass on a turntable platter viewed from the front.
I have got close but its still not looking right, looks like at the moment its travelling along a diamond shape rather than a circle.
Here is what I have tried..
.roll {
display: block;
width: 100px;
height: 100px;
background: red;
margin: 10px auto 10px auto;
animation: roll 2s cubic-bezier(0.42, 0, 0.58, 1) infinite;
}
@keyframes roll {
0%,
100% {
transform: translateX(0%) scale(1);
}
20% {
transform: translateX(50%) scale(0.8);
}
50% {
transform: translateX(0%) scale(0.6);
}
80% {
transform: translateX(-50%) scale(0.8);
}
}
<div class="roll"></div>
2
Answers
You can achieve this 3d effect by playing with the z-axis. Have a look: https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translateZ
If you want to animate the square on a horizontal plane circle and keep it facing the viewer, you can use 3d transforms on a wrapper element and resverse it on the square so it stays facing the viewer.
The point is to rotate the element on the Y axis as it would "in real life".
Here is an example :
Note that you need to use
transform-style: preserve-3d;
(more info on MDN)