关于悬停问题的AFRAME文字动画

时间:2019-02-22 19:05:20

标签: animation text aframe

https://jsfiddle.net/ykma6r0d/1/

链接到演示^

HMTL:

<a-scene>
  <!-- CAMERA CURSOR -->
  <a-entity id="camera" camera mouse-cursor look-controls wasd-controls>
    <a-cursor fuse="true" color="red"></a-cursor>
  </a-entity>
  <a-entity id="myTarget" scale="1 .001 1" look-at="#camera" text="color: black;align: center; value: YAYA!; width: 6; wrap-count: 10" animation__1="startEvents: in;property: scale; dur: 200; from: 1 .001 7; to: 1 1 1" animation__2="startEvents: out;property: scale; dur: 200; from: 1 1 1; to: 1 .001 1"></a-entity>
  <a-circle name-on-hover="target: #myTarget" position="-2 0 -10" material="color: blue" material="opacity:.75" look-at="#camera" animation="property: position;easing: easeInSine;loop:true;dir: alternate;dur: 2000;to:-2 .2 -10">
  </a-circle>
</a-scene>

JS:

AFRAME.registerComponent("name-on-hover", {
  schema: {
    target: {
      type: "selector",
      default: ""
    }
  },
  init: function() {
    var target = this.data.target;
    var el = this.el;
    this.setupNamePos();
    el.addEventListener("mouseenter", function() {
      target.emit("in"); // animate in
    });
    el.addEventListener("mouseleave", function() {
      target.emit("out"); // animate out
    });
  },
  setupNamePos: function() {
    var name = this.data.target; // get name element
    var elPos = this.el.getAttribute("position"); // get the pos of hovered element

    name.setAttribute("position", {
      //set name position relevant to hovered element
      x: elPos.x,
      y: elPos.y + 2,
      z: elPos.z
    });
  }
});

我有两个元素,一个圆圈和一个文本元素

圆形元素:悬停时需要在其上方的文本进行动画处理。在我的JS中,我对其进行了设置,以使其抓住圆的位置,然后将该位置设置为我的text元素,但在此之前,它将Y位置增加了2个单位。

由于某种原因,当我将鼠标悬停在圆圈上时,我的动画就像疯狂一样循环。起初,我认为这可能是因为文本元素与圆圈重叠,从而失去了悬停事件。但是我也尝试通过修改X和Z将文本元素移动到其他位置。.但是没有运气。

1 个答案:

答案 0 :(得分:1)

当文本展开时,它会阻碍游标raycaster的方向-并且发射mouseleave

您可以使用objects属性来限制光标可以单击的实体。

<a-cursor objects='clickable'> </a-cursor>

提琴here