, , . v2.X Fabric, . , , false containsPoint, .
js
const getSelectedObject = (target, e) => {
const point = target.canvas.getPointer(e, false);
const objects = findSubTargets(target, []);
const objectIndex = objects
.reduce((reduction, object, index) => {
reduction.push(
getBoundingPoints(
object,
index === 0 ? 0 : reduction[0].start.x,
index === 0 ? 0 : reduction[0].start.y
)
);
return reduction;
}, [])
.reverse()
.findIndex((bounds) => pointIsInBounds(point, bounds));
return objects.reverse()[objectIndex];
};
and auxiliary functions
js
const getBoundingPoints = (object, deltaX, deltaY) => {
const coords = object.get("aCoords");
const x1 = coords.tl.x + deltaX;
const y1 = coords.tl.y + deltaY;
const x2 = coords.br.x + deltaX;
const y2 = coords.br.y + deltaY;
return {
start: { x: x1, y: y1 },
finish: { x: x2, y: y2 }
};
};
const pointIsInBounds = (point, bounds) => {
const xIsInBounds = point.x >= bounds.start.x && point.x <= bounds.finish.x;
const yIsInBounds = point.y >= bounds.start.y && point.y <= bounds.finish.y;
return xIsInBounds && yIsInBounds;
};
const findSubTargets = (target, objects) => {
if (target.isType("group")) {
objects.push(target);
target.forEachObject((object) => findSubTargets(object, objects));
}
return objects;
};
I checked this with simple groups, nested groups and groups within groups within groups.
source
share