Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 animation function for smooth 3D revolution?

I have this pen which tries to emulate an object revolving around something. This works, but it isn't smooth. While revolving it pauses around the left and right edges.

I thought it had something to do with animation-timing-function but can't get the desired result with any of the in-built functions like ease-in-out or linear or a custom cubic-bezier function.

How can I make the animation feel smooth? If there are better ways something like this can be done, feel free to let me know.

.overlay {
  background-image: -webkit-repeating-linear-gradient(0deg, transparent, transparent 1%, rgb(255, 255, 255) 2%, rgb(255, 255, 255) 2%);
  background-image: repeating-linear-gradient(90deg, transparent, transparent 1%, rgb(255, 255, 255) 2%, rgb(255, 255, 255) 2%);
  height: 200px;
  position: relative;
  width: 40%;
  margin: auto;
}
.circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: #888;
  position: absolute;
  z-index: -1;
  left: 0;
  display: inline-block;
}
.move {
  -webkit-animation: moveAndGlow 2s infinite ease-in-out;
  animation: moveAndGlow 2s infinite ease-in-out;
}
@-webkit-keyframes moveAndGlow {
  25% {
    background: #ccc;
    -webkit-transform: scale(.5);
    transform: scale(.5);
    margin-top: 25px;
  }
  50% {
    left: 100%;
    margin-left: -100px;
    background: #888;
    -webkit-transform: scale(1);
    transform: scale(1);
    margin-top: 0;
  }
  75% {
    background: #000;
    -webkit-transform: scale(1.5);
    transform: scale(1.5);
    margin-top: 25px;
  }
}
@keyframes moveAndGlow {
  25% {
    background: #ccc;
    -webkit-transform: scale(.5);
    transform: scale(.5);
    margin-top: 25px;
  }
  50% {
    left: 100%;
    margin-left: -100px;
    background: #888;
    -webkit-transform: scale(1);
    transform: scale(1);
    margin-top: 0;
  }
  75% {
    background: #000;
    -webkit-transform: scale(1.5);
    transform: scale(1.5);
    margin-top: 25px;
  }
}
<div class="overlay">
  <span class="circle move"></span>
</div>
like image 722
Praveen Puglia Avatar asked May 27 '15 13:05

Praveen Puglia


People also ask

How do I make my animation smoother in CSS?

The animation-timing-function specifies the speed curve of an animation. The speed curve defines the TIME an animation uses to change from one set of CSS styles to another. The speed curve is used to make the changes smoothly.

Which CSS property is used to describe how an animation is to play?

The animation-timing-function CSS property sets how an animation progresses through the duration of each cycle.

Is CSS good for animation?

CSS animations make a website visually attractive and enhance the user experience. We hope you can get inspiration from these 30 top cool CSS animation examples to make a wonderful animation website.


1 Answers

If you want to move you element in a 3d environement, you can use the perspective property and actual 3d rotation.

Right now you are animating on straight lines between positions so simulating a rotation is almost imposible. I built the following example, you will need to tweak the size to fit it into your project but you should get the idea.

Also note that I put the gradient background in a pseudo element so it appear in front of the moving object :

.overlay {
  height: 200px;
  position: relative;
  width: 40%;
  margin: auto;
  perspective:500px;
  margin-top:50px;
}
.overlay:after{
  content:'';
  position:absolute;
  top:-100px; left:-10%;
  width:120%; height:100%;
  background-image: repeating-linear-gradient(90deg, transparent, transparent 1%, rgb(255, 255, 255) 2%, rgb(255, 255, 255) 2%);
}

.circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: #888;
  position: absolute;
  z-index: -1;
  left: 50%;
  margin-left:-50px;
  transform: rotateY(0deg) translateX(-100px) rotateY(0deg);
  display: inline-block;
}

.move {
  animation: moveAndGlow 2s infinite linear;
}

@keyframes moveAndGlow {
  to{  transform:rotateY(360deg) translateX(-100px) rotateY(-360deg); }
}
<div class="overlay">
  <span class="circle move"></span>
</div>
like image 99
web-tiki Avatar answered Sep 21 '22 21:09

web-tiki