40 lines
938 B
JavaScript
40 lines
938 B
JavaScript
|
|
/** 克隆对象 */
|
||
|
|
export async function cloneObjects(objects = []) {
|
||
|
|
const arrs = []
|
||
|
|
for (const obj of objects) {
|
||
|
|
const clonedObj = await new Promise((resolve, reject) => {
|
||
|
|
obj.clone((v) => {
|
||
|
|
v.set({
|
||
|
|
left: obj.left,
|
||
|
|
top: obj.top,
|
||
|
|
width: obj.width,
|
||
|
|
height: obj.height,
|
||
|
|
scaleX: obj.scaleX,
|
||
|
|
scaleY: obj.scaleY,
|
||
|
|
})
|
||
|
|
resolve(v)
|
||
|
|
})
|
||
|
|
})
|
||
|
|
arrs.push(clonedObj)
|
||
|
|
}
|
||
|
|
return arrs
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/** 获取组合对象边界 */
|
||
|
|
export async function getObjectsBoundingBox(objects = []) {
|
||
|
|
const box1 = { x: Infinity, y: Infinity }
|
||
|
|
const box2 = { x: -Infinity, y: -Infinity }
|
||
|
|
objects.forEach(obj => {
|
||
|
|
box1.x = Math.min(box1.x, obj.left)
|
||
|
|
box1.y = Math.min(box1.y, obj.top)
|
||
|
|
box2.x = Math.max(box2.x, obj.left + obj.width * obj.scaleX)
|
||
|
|
box2.y = Math.max(box2.y, obj.top + obj.height * obj.scaleY)
|
||
|
|
})
|
||
|
|
return {
|
||
|
|
left: box1.x,
|
||
|
|
top: box1.y,
|
||
|
|
width: box2.x - box1.x,
|
||
|
|
height: box2.y - box1.y,
|
||
|
|
}
|
||
|
|
}
|