fabric.js将对象设置为新位置,但未遵循事件目标

时间:2018-05-06 16:19:25

标签: fabricjs

var that = this;
var mouseEvents = this.mouseEvents();
this.timeAxis.on('mouseover', mouseEvents.timeAxisOver);
this.timeAxis.on('mouseout', mouseEvents.timeAxisOut);
this.canvas.on('mouse:over', mouseEvents.canvasOver);
this.canvas.on('mouse:out', mouseEvents.canvasOut);
this.canvas.on('mouse:down', mouseEvents.canvasDown);
this.startPoint.on('mousedown', function(e) {
  var originPos = new Array();
  that.canvas.getObjects().map(function(obj, i) {
    originPos.push({
      x: obj.left,
      y: obj.top
    });
  });
  this.originPos = originPos;
  this.pos = that.canvas.getPointer(e.e);
});
this.startPoint.on('moving', function(e) {
  var currPos = that.canvas.getPointer(e.e),
    originPos = this.originPos,
    moveX = currPos.x - this.pos.x,
    moveY = currPos.y - this.pos.y;
  that.canvas.forEachObject(function(obj, i) {
    obj.set({
      left: originPos[i].x + moveX,
      top: originPos[i].y + moveY
    })
  });
  that.canvas.renderAll();
});

这是jsFiddle

我是fabric.js的新手,现在我遇到了问题。我正在尝试找到一些解决方案,但它无法正常工作。 当我通过set方法移动对象时,对象移动了但它的事件没有跟随,请参阅代码。即使我重新绑定事件,事件目标也会保持在先前的位置。问题是当我设置新位置时,如何使事件跟随对象。

1 个答案:

答案 0 :(得分:1)

您应该使用

object.setCoords();