Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: When activating two or more objects, discardActiveObject or setActiveObject after setting props of active objects cause unexpected movement. #10193

Open
7 tasks done
huangbuyi opened this issue Oct 2, 2024 · 3 comments

Comments

@huangbuyi
Copy link

CheckList

  • I agree to follow this project's Code of Conduct
  • I have read and followed the Contributing Guide
  • I have read and followed the Issue Tracker Guide
  • I have searched and referenced existing issues and discussions
  • I am filing a BUG report.
  • I have managed to reproduce the bug after upgrading to the latest version
  • I have created an accurate and minimal reproduction

Version

6.0.2

In What environments are you experiencing the problem?

Chrome

Node Version (if applicable)

None

Link To Reproduction

See code below.

Steps To Reproduce

  1. Acitve two or more objects manually.
  2. Set props of active object.
  3. Run discardActiveObject or setActiveObject.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fabric Test</title>
  <!-- <script src="https://cdn.jsdelivr.net/npm/fabric@latest/dist/index.min.js"></script> -->
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js"></script>
</head>
<body>
  <canvas id="canvas"></canvas>
  <button id="btn">Reproduce Exception</button>
  <p>When activating two or more objects, discardActiveObject or setActiveObject after setting props of active objects cause unexpected movement. </p>
  <p>Click it before select all objects manually or at least two objects</p>
  <script>
    const canvas = new fabric.Canvas('canvas', {
      width: 800,
      height: 800
    });
    const rects = [[20, 20], [40, 20], [60, 20], [80, 20]].map(item => {
      return new fabric.Rect({
        left: item[0],
        top: item[1],
        fill: 'yellow',
        width: 120,
        height: 200,
        strokeWidth: 1,
        stroke: 'lightgreen',
      })
    });
    canvas.add(...rects);

    const btn = document.getElementById('btn');
    btn.addEventListener('click', () => {
      for (const rect of rects) {
        rect.set({
          left: 0,
          top: 0
        })
      }
      canvas.renderAll();
      canvas.discardActiveObject();
      // canvas.setActiveObject(rects[0]);
    });

  </script>
</body>
</html>

Expected Behavior

All objects move to right position.

image

Actual Behavior

Active objects move to wrong position.

Origin positions:

image

Unexcepted positions:

image

I found this problems in v5.3.0, then I upgraded to latest version, the problem was still existed. Is this a real problem, or just I did something wrong? To fix this, I have to discardActiveObject before set object's props then setActiveObject same objects back in my project.

Error Message & Stack Trace

No response

@zhe-he
Copy link
Contributor

zhe-he commented Oct 9, 2024

When A and B (multiple elements) are selected, an active group (C) is created internally. Setting the position of A means setting A's position relative to C. The design is such that you need to deselect A before setting its position.

@huangbuyi
Copy link
Author

I need to change objects's positions whether they are selected or not. The way, Deselecting -> Setting Position, -> Selecting back, is not good enough for me. Instead, I will check the object before setting and getting, if it is in a group, then make a conversion:

export function setObjectProps(object: fabric.Object, props: Partial<IObjectProps>) {
  const { left, top } = props;

  if (typeof left === 'number' && object.group) {
    props.left = left - object.group.left! - object.group.width! / 2;
  }

  if (typeof top === 'number' && object.group) {
    props.top = top - object.group.top! - object.group.height! / 2;
  }

  object.set(props);
}

export function getObjectProp<T extends keyof IObjectProps>(object: fabric.Object, prop: T): IObjectProps[T] {
  if (object.group) {
    if (prop === 'left') {
      return (object.left! + object.group.left! + object.group.width! / 2) as IObjectProps[T];
    }
    if (prop === 'top') { 
      return (object.top! + object.group.top! + object.group.height! / 2) as IObjectProps[T];
    }
  }

  return object.get(prop) as IObjectProps[T];
}

@zhe-he
Copy link
Contributor

zhe-he commented Oct 14, 2024

The size of the group is determined by the position of its child elements. When you modify the position of the child elements, the size of the group should update. I noticed that the code in your example does not take into account rotated groups; you might want to try the code below.

export function setObjectProps(object: FabricObject, props: AnyObject) {
  unwarp(object, () => object.set(props))
}
export function getObjectProp(object: FabricObject, prop: string) {
  return unwarp(object, () => object.get(prop));
}

function unwarp(object: FabricObject, fn = () => {}) {
  const { group, parent } = object;
  const isActiveSelection = group && "multiSelectionStacking" in group;
  // const isActiveSelection = group && group != object.parent; // Equivalent to the line above.
  if (isActiveSelection) group.remove(object);

  // const parent = object.group; // Equivalent to `the parent` mentioned above.
  parent?.remove(object);

  const v = fn();

  parent?.add(object);
  parent?.triggerLayout();
  if (isActiveSelection) {
    group.add(object);
    group.triggerLayout();
  }

  return v;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants