【问题标题】:fabric.js set the objects to new position but the event target not followedfabric.js 将对象设置到新位置但未遵循事件目标
【发布时间】:2018-06-23 01:33:42
【问题描述】:
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 方法移动对象时,对象移动了,但它的事件没有跟随,见代码。即使我重新绑定事件,事件目标也只会停留在以前的位置。所以问题是当我设置新位置时如何使事件跟随对象。

【问题讨论】:

  • 你能做一个sn-p吗?
  • 好的,这是一个jsFiddle 演示
  • 您只想移动另一个圆圈或所有对象?
  • 这不是重点。在 jsFiddle 中,我在 circle1 上添加了一个“鼠标悬停”事件,然后设置它的新位置,但它的事件目标保持在原来的位置。这不是我的想要。我需要它的事件跟随对象。也许你有更好的方法来做到这一点。希望你能理解,原谅我糟糕的英语。谢谢!

标签: fabricjs


【解决方案1】:

你应该使用

object.setCoords();

【讨论】:

  • 这为我修好了。
猜你喜欢
  • 2023-03-06
  • 2018-12-12
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多