简体   繁体   English

Mouse:over事件在Fabric.js中触发两次

[英]Mouse:over event fires twice in Fabric.js

I'm trying to write a simple card game on canvas + Fabricjs. 我正在尝试在canvas + Fabricjs上编写一个简单的纸牌游戏。 Need to make bigger player's card when mouse is on it and return to normal size if mouse is out. 当鼠标放在上面时,需要制作更大的玩家卡片,如果鼠标不在,则需要恢复到正常大小。 Here is my js code: 这是我的js代码:

  canvas.on('mouse:over', function(e) {
  if(e.target.mytype && e.target.mytype=='card'){
    e.target.animate({
      top:352,
      left: e.target.left - (max_width - min_width)/2,
      scaleX:0.4,
      scaleY:0.4
    } , {
      duration: 500,
      onChange: function(value) { 
        canvas.renderAll.bind(canvas);
      },
      easing: fabric.util.ease.easeInOut
    }); 
    canvas.renderAll();
  }

}); });

canvas.on('mouse:out', function(e) {
    if(e.target.mytype && e.target.mytype=='card'){
      e.target.animate({
          top:385,
          left: e.target.left + (max_width - min_width)/2,
          scaleX:0.3,
          scaleY:0.3
        } , {
          duration: 500,
          onChange: function(value) { 
            canvas.renderAll.bind(canvas);
          },
          easing: fabric.util.ease.easeInOut
        });
      canvas.renderAll();
    }
  });

if player hover the card for 200ms (animation duration is 500ms) and move out, the animation freeze and the card remains at a new position. 如果玩家将卡悬停200毫秒(动画持续时间为500毫秒)并移出,动画将冻结,并且卡将保持在新位置。 Hovering again wil start animation from this new position. 再次悬停将从该新位置开始动画。 Here is a fiddle 这是一个小提琴

Just try to move mouse in/out on the object and you'll see the bug. 只需尝试将鼠标移入/移出对象,您就会看到错误。 Please, help me to fix this. 请帮我解决这个问题。

The problem is the way that your code has relative positioning changes within the animation action - in the jsFiddle, the "top" value, in your code above, in the "left" value. 问题是您的代码在动画动作中具有相对位置更改的方式-在jsFiddle中,“ top”值在上面的代码中在“ left”值中。

What each of these do is move the element +X or -X from the position the event starts at . 这些操作的唯一作用是将元素+ X或-X 从事件开始的位置移开。 If the animation finishes before the mouseout event is fired, it's fine, because the amount it moves back is equal (but opposite) to the amount it moved in the mouseover event. 如果动画在触发mouseout事件之前完成,那很好,因为它向后移动的数量等于(但相反)在mouseover事件中移动的数量。

However, if the animation for mouseout starts Before the first is finished , it takes it's current position, not the position it will be at when it finishes the animation. 但是,如果用于mouseout的动画在第一个动画完成之前开始,它将采用当前位置,而不是动画完成时所在的位置。 This leads to a situation where the element drifts away from its original position. 这导致元件偏离其原始位置的情况。 This is certainly what the issue is in the JSFiddle, I understand from your comments that this is the same issue in your own code. 当然,这就是JSFiddle中的问题,我从您的评论中了解到,这在您自己的代码中是相同的问题。

How to resolve it? 怎么解决呢? As long as you are using a fixed relative position value in the mouseout event you probably can't. 只要您在mouseout事件中使用固定的相对位置值,就可能不会。 You could try logging the initial positioning (ie, the value of "left/top" you started at) in the mouseover function and returning specifically to that point in the mouseout event. 您可以尝试在mouseover函数中记录初始位置(即,您从其开始的“左/上”的值),并在mouseout事件中专门返回到该点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM