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

让人想犯罪 __ 提交于 2019-12-06 15:25:49

问题


I have 3 fabric objects on the canvas, a rect and text are grouped as 1 object, then I dynamically create a polygon, and redraw it on object:moving between the group object and another rect. These form what looks like a "speech bubble", with the group and the rect (pointer) both being movable.

What I'd like to achieve is: when the polygon is clicked with the mouse, you can move all 3 objects while holding the mouse down.

So far what I have groups the items together fine, but you can only move all the items if you select the polygon, then release the mouse button, then click the group again, hold down the mouse and move the group.

I've tried the following canvas methods, thinking they might "deselect" the polygon on click, but none of them appear to work.

canvas.deactivateAllWithDispatch();
canvas.deactivateAll();
canvas.discardActiveObject();

Jsfiddle of the prototype I have so far is here:

http://jsfiddle.net/beewayne/3y2x3vty/


回答1:


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/



来源:https://stackoverflow.com/questions/38154392/fabricjs-how-do-i-deselect-an-object-and-select-a-group-without-releasing-the

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