Я получил это, чтобы работать, перебирая каждый объект холста и получая и проверяя, является ли прямоугольник ограничивающего объекта top + height больше или равен ограничивающему прямоугольнику объекта top который должен быть размещен. Вот пример кода
const presentObjs = canvas.getObjects();
let objectToBeplaced = presentObjs[3]; //random object just for example, please change according to your need.
let currentObj = objectToBePlaced;
const presentObjsCount = presentObjs.length;
const gapY = 10;//10 points gap vertically
let j;
for (j = 0; j < presentObjsCount; j++)
{
const obj = presentObjs[j];
if (obj === currentObj) {
continue;
}
const objBox = obj.getBoundingRect();
const objW = Math.ceil(Math.abs(objBox.width));
const objLeft = Math.ceil(Math.abs(objBox.left));
const objH = Math.ceil(Math.abs(objBox.height));
const objTop = Math.ceil(Math.abs(objBox.top));
const cObjBox = currentObj.getBoundingRect();
const cObjW = Math.ceil(Math.abs(cObjBox.width));
const cObjLeft = Math.ceil(Math.abs(cObjBox.left));
const cObjH = Math.ceil(Math.abs(cObjBox.height));
const cObjTop = Math.ceil(Math.abs(cObjBox.top));
const a = cObjLeft;
const b = cObjLeft + cObjW;
const x = objLeft;
const y = objLeft + objW;
if ((x >= a && x <= b) || (y >= a && y <= b) || (a >= x && b <= x) || (a >= y && b <= y)) { //Checks if both object has intersection on Y-axis
if ((cObjTop - (objTop + objH)) < gapY && (cObjTop - (objTop + objH)) >= 0) { //checks if gap is less than needed
currentObj.set({ top: objTop + objH + gapY });
currentObj.setCoords();
}
}
}
canvas.renderAll();
Не стесняйтесь задавать любые вопросы или сомнения, если у вас есть.