Search code examples
animationrotationaframevirtual-realitywebvr

How to loop A-Frame animation forever using duration attribute


I have a earth object that I would like to rotate using animation in A-frame. I just want the object to rotate forever and I don't know how to do it. Below is my code thanks!

    <a-sphere rotation="45 0 0" position="-44.277 50 -80.933" radius="30" src="images/earth.png" roughness="0.6">

        <a-animation attribute="rotation"
                     easing="linear"
                     dur="10000"
                     to="0 360 0"
                     repeat="indefinite">

        </a-animation>
    </a-sphere>

Solution

  • That code is correct. It will take 10,000 ms (10 seconds) to complete 1 rotation, and it will repeat indefinitely ("forever"). I just tested the code and it works. If the earth.png texture isn't appearing, it may be difficult to see the sphere rotation. If you use the same code on an <a-box>, for example, it's much more obvious.

    That said, since you have the rotation for the <a-sphere> set to 45 along the x axis and have the <a-animation> set to rotate to 360 over the y axis, it will not rotate in a linear fashion.

    This can be remedied by adding a container <a-entity> element and setting the default position and rotation on that entity, removing it from the <a-sphere>, which will now be positioned relative to its container.

    Here is demo: https://codepen.io/dansinni/pen/MVgqxd

    Note that I had to use a different texture for the Earth.