Sync the position of an entity with respect to two animations in aframe

share link

by vsasikalabe dot icon Updated: Apr 6, 2023

technology logo
technology logo

Solution Kit Solution Kit  

In this kit, we will see how to Sync the position of an entity concerning two animations in aframe with simple steps. Components are registered with AFRAME. registerComponent() . We pass the name of the component, which will be used as the HTML attribute name in the component's representation in the DOM. Then we pass the component definition, a JavaScript object of methods and properties. Algebraically, a vector in 3 (real) dimensions is defined to be an ordered triple (x, y, z), where x, y, and z are all real numbers (x, y, z ∈ R). The set of all three-dimensional vectors is denoted R3.


Please check the code below.

Preview of the output that you will get on running this code from your IDE.

Code

<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
  AFRAME.registerComponent("animation-manager", {
    init: function() {
      // bind updateAnimation() so that we have new functions
      // doing exactly what we need
      this.rotateClockwise = this.updateAnimation.bind(this, 1);
      this.rotateCounterClockwise = this.updateAnimation.bind(this, -1);

      // bind the above functions to mouseenter/mouseleave
      this.el.addEventListener("mouseenter", this.rotateClockwise)
      this.el.addEventListener("mouseleave", this.rotateCounterClockwise)
    },
    updateAnimation: (function() {
      // this is an IIFE - there two vectors are created only once
      // even though the returned function is called multiple times
      const fromRotation = new THREE.Vector3();
      const toRotation = new THREE.Vector3();

      return function(dir) {
        // get current rotation, and move the target rotation by 360 or -360
        // depending on the direction
        fromRotation.copy(this.el.getAttribute("rotation"));
        toRotation.copy(this.el.getAttribute("rotation"));
        toRotation.y += dir * 360

        // update the animation
        this.el.setAttribute("animation", {
          "from": AFRAME.utils.coordinates.stringify(fromRotation),
          "to": AFRAME.utils.coordinates.stringify(toRotation),
          "dur": dir == -1 ? "8000" : "1000"
        })
      }
    })()
  })
</script>
<a-scene cursor="rayOrigin: mouse" raycaster="objects: a-box">
  <a-box position="0 1 -4" color="blue" animation-manager 
         animation="property: rotation; to:0 -360 0; loop:true; easing:linear; dur:8000">
    <a-sphere position="2 0 0" color="green" radius="0.25" foo></a-sphere>
  </a-box>
</a-scene>

Instructions

Follow the steps carefully to get the output easily.

  1. Download and Install VS Code on your Desktop.
  2. Open the VS Code and install Live Server from Extensions.
  3. Open the new file and save as filename.html.
  4. Copy the code using the "Copy" button above, and paste it in your html file.
  5. Click Go live button in the bottom right corner to get the output.


I hope you found this useful. I have added version information in the following sections.


I found this code snippet by searching for ' Sync the position of entity with respect to two animation in aframe' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. Live Server v5.7.9


Using this solution, we are able to Sync the position of entity with respect to two animation in aframe with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to Sync the position of entity with respect to two animation in aframe.

Dependent Library

aframeby aframevr

JavaScript doticonstar image 15445 doticonVersion:v1.4.2doticon
License: Permissive (MIT)

:a: Web framework for building virtual reality experiences.

Support
    Quality
      Security
        License
          Reuse

            aframeby aframevr

            JavaScript doticon star image 15445 doticonVersion:v1.4.2doticon License: Permissive (MIT)

            :a: Web framework for building virtual reality experiences.
            Support
              Quality
                Security
                  License
                    Reuse

                      If you do not have aframe that is required to run this code, you can install it by clicking on the above link .

                      You can search for any dependent library on kandi like aframe.

                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page

                      See similar Kits and Libraries