Fabricjs - how do I deselect an object and select a group without releasing the mouse button

人盡茶涼 提交于 2019-12-04 20:38:42

I messed around a little and the best I got is to manually move the object and the group together until the user lets up the mouse for the first time.

I modified the mousedown listener to look more like this:

  shape.on('mousedown', function(evt) {

    var objs = canvas.getObjects();

    var group = new fabric.Group(objs, {status: 'moving'});

    // Relevant code
    var originalX = shape.left,
        originalY = shape.top,
        mouseX = evt.e.pageX,
        mouseY = evt.e.pageY;

    canvas.on('object:moving', function(evt) {
      shape.left = originalX;
      shape.top = originalY;
      group.left += evt.e.pageX - mouseX;
      group.top += evt.e.pageY - mouseY;
      originalX = shape.left;
      originalY = shape.top;
      mouseX = evt.e.pageX;
      mouseY = evt.e.pageY;
    });

    canvas.setActiveGroup(group.setCoords()).renderAll();
  });

  // clean up the listener
  shape.on('mouseup', function(evt) {
    canvas.off('object:moving');
  });

I used the mouse offset, since adding the object to the group makes it "jump" a little. Also, I continuously set the object to the same position, so that it doesn't move within the group.

http://jsfiddle.net/kqfswu4b/1/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!