nando
nando

Reputation: 377

Animate in aframe entity on hover

i am having trouble animating in AFRAME element/entities. In the following demo i have set up a box and on top of the box a text entity that needs to animate in (scale up) when i hover the mouse over the box the text element does not animate in or show up. Can anyone help?

https://jsfiddle.net/0d6ymk21/

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script>
    <script src="https://rawgit.com/mayognaise/aframe-mouse-cursor-component/master/dist/aframe-mouse-cursor-component.min.js"></script>

  </head>
  <body>
    <a-scene>
      <a-entity id="camera" camera mouse-cursor look-controls>
        <a-cursor fuse="true" color="blue"></a-cursor>
      </a-entity>
      <a-entity
        id="#fernando"
        text="color: black;value: Fernando;"
        scale=".1 .1 .1"
        position="2 1 -2"
      ></a-entity>
      <a-box box position="1 0 -2" color="red" activate-name=""></a-box>
    </a-scene>
  </body>
</html>

-- JS:

AFRAME.registerComponent("activate-name", {
  schema: {
    default: ""
  },
  init: function() {
    var data = this.data;
    var el = this.el;
    var fernando = document.querySelector("#fernando");
    el.addEventListener("mouseenter", function() {
      fernando.setAttribute("scale", "2 2 2");
    });
  }
});

Upvotes: 0

Views: 2055

Answers (1)

Piotr Adam Milewski
Piotr Adam Milewski

Reputation: 14645

Two issues here:

1) If you want to grab fernando using document.querySelector('#fernando') - the id needs to be fernando instead of #fernando.

2) The component declaration - activate-name in this case - needs to be done before the component is attached in html. You can simply throw it it a <script> tag before the scene

<script>
  AFRAME.registerComponent('foo', ...
</script>
<a-scene>
  <a-entity foo></a-entity>
</a-scene>

even better - keep it in a separate .js file and include it in the <head>. Fiddle here.


This is necessary because jsfiddle executes the code part when the window is loaded.

Upvotes: 1

Related Questions